ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/main_unix.cpp
Revision: 1.43
Committed: 2002-01-22T17:15:09Z (22 years, 6 months ago) by cebix
Branch: MAIN
Changes since 1.42: +10 -0 lines
Log Message:
GNOME-style about box and GNOME file entries are used if libgnomeui is present

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * main_unix.cpp - Startup code for Unix
3     *
4 cebix 1.42 * Basilisk II (C) 1997-2002 Christian Bauer
5 cebix 1.1 *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     */
20    
21     #include "sysdeps.h"
22    
23     #include <stdio.h>
24     #include <stdlib.h>
25     #include <signal.h>
26 cebix 1.12 #include <errno.h>
27     #include <X11/Xlib.h>
28    
29     #ifdef HAVE_PTHREADS
30     # include <pthread.h>
31     #endif
32    
33 cebix 1.27 #if REAL_ADDRESSING || DIRECT_ADDRESSING
34 cebix 1.12 # include <sys/mman.h>
35     #endif
36    
37     #if !EMULATED_68K && defined(__NetBSD__)
38     # include <m68k/sync_icache.h>
39     # include <m68k/frame.h>
40     # include <sys/param.h>
41     # include <sys/sysctl.h>
42     struct sigstate {
43     int ss_flags;
44     struct frame ss_frame;
45     struct fpframe ss_fpstate;
46     };
47     # define SS_FPSTATE 0x02
48     # define SS_USERREGS 0x04
49     #endif
50    
51     #ifdef ENABLE_GTK
52     # include <gtk/gtk.h>
53 cebix 1.28 # include <gdk/gdk.h>
54 cebix 1.43 # ifdef HAVE_GNOMEUI
55     # include <gnome.h>
56     # endif
57 cebix 1.12 #endif
58    
59     #ifdef ENABLE_XF86_DGA
60     # include <X11/Xutil.h>
61     # include <X11/extensions/xf86dga.h>
62     #endif
63 cebix 1.1
64     #include "cpu_emulation.h"
65     #include "sys.h"
66 cebix 1.3 #include "rom_patches.h"
67 cebix 1.1 #include "xpram.h"
68     #include "timer.h"
69     #include "video.h"
70 cebix 1.12 #include "emul_op.h"
71 cebix 1.1 #include "prefs.h"
72     #include "prefs_editor.h"
73     #include "macos_util.h"
74     #include "user_strings.h"
75     #include "version.h"
76     #include "main.h"
77 gbeauche 1.33 #include "vm_alloc.h"
78 cebix 1.1
79 cebix 1.12 #ifdef ENABLE_MON
80     # include "mon.h"
81     #endif
82    
83 cebix 1.13 #define DEBUG 0
84 cebix 1.1 #include "debug.h"
85    
86    
87 cebix 1.12 // Constants
88     const char ROM_FILE_NAME[] = "ROM";
89     const int SIG_STACK_SIZE = SIGSTKSZ; // Size of signal stack
90     const int SCRATCH_MEM_SIZE = 0x10000; // Size of scratch memory area
91 cebix 1.1
92 cebix 1.4
93 cebix 1.12 #if !EMULATED_68K
94     // RAM and ROM pointers
95     uint32 RAMBaseMac; // RAM base (Mac address space)
96     uint8 *RAMBaseHost; // RAM base (host address space)
97     uint32 RAMSize; // Size of RAM
98     uint32 ROMBaseMac; // ROM base (Mac address space)
99     uint8 *ROMBaseHost; // ROM base (host address space)
100     uint32 ROMSize; // Size of ROM
101 cebix 1.9 #endif
102    
103 cebix 1.1
104     // CPU and FPU type, addressing mode
105     int CPUType;
106     bool CPUIs68060;
107     int FPUType;
108     bool TwentyFourBitAddressing;
109    
110    
111     // Global variables
112 cebix 1.13 char *x_display_name = NULL; // X11 display name
113 cebix 1.1 Display *x_display = NULL; // X11 display handle
114    
115 cebix 1.41 static uint8 last_xpram[XPRAM_SIZE]; // Buffer for monitoring XPRAM changes
116 cebix 1.12
117     #ifdef HAVE_PTHREADS
118     static pthread_t emul_thread; // Handle of MacOS emulation thread (main thread)
119    
120 cebix 1.1 static bool xpram_thread_active = false; // Flag: XPRAM watchdog installed
121     static volatile bool xpram_thread_cancel = false; // Flag: Cancel XPRAM thread
122     static pthread_t xpram_thread; // XPRAM watchdog
123    
124     static bool tick_thread_active = false; // Flag: 60Hz thread installed
125     static volatile bool tick_thread_cancel = false; // Flag: Cancel 60Hz thread
126     static pthread_t tick_thread; // 60Hz thread
127     static pthread_attr_t tick_thread_attr; // 60Hz thread attributes
128    
129     static pthread_mutex_t intflag_lock = PTHREAD_MUTEX_INITIALIZER; // Mutex to protect InterruptFlags
130 cebix 1.37 #define LOCK_INTFLAGS pthread_mutex_lock(&intflag_lock)
131     #define UNLOCK_INTFLAGS pthread_mutex_unlock(&intflag_lock)
132    
133     #else
134    
135     #define LOCK_INTFLAGS
136     #define UNLOCK_INTFLAGS
137    
138 cebix 1.12 #endif
139    
140     #if !EMULATED_68K
141     #define SIG_IRQ SIGUSR1
142     static struct sigaction sigirq_sa; // Virtual 68k interrupt signal
143     static struct sigaction sigill_sa; // Illegal instruction
144     static void *sig_stack = NULL; // Stack for signal handlers
145     uint16 EmulatedSR; // Emulated bits of SR (supervisor bit and interrupt mask)
146 gbeauche 1.20 #endif
147    
148     #if USE_SCRATCHMEM_SUBTERFUGE
149 cebix 1.22 uint8 *ScratchMem = NULL; // Scratch memory for Mac ROM writes
150 cebix 1.12 #endif
151    
152     static struct sigaction timer_sa; // sigaction used for timer
153 cebix 1.1
154     #if defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
155     #define SIG_TIMER SIGRTMIN
156 cebix 1.12 static timer_t timer; // 60Hz timer
157 cebix 1.1 #endif
158    
159 cebix 1.12 #ifdef ENABLE_MON
160     static struct sigaction sigint_sa; // sigaction for SIGINT handler
161 cebix 1.4 static void sigint_handler(...);
162 cebix 1.15 #endif
163    
164     #if REAL_ADDRESSING
165     static bool lm_area_mapped = false; // Flag: Low Memory area mmap()ped
166 cebix 1.22 #endif
167    
168 cebix 1.1
169     // Prototypes
170     static void *xpram_func(void *arg);
171     static void *tick_func(void *arg);
172     static void one_tick(...);
173 cebix 1.12 #if !EMULATED_68K
174     static void sigirq_handler(int sig, int code, struct sigcontext *scp);
175     static void sigill_handler(int sig, int code, struct sigcontext *scp);
176     extern "C" void EmulOpTrampoline(void);
177     #endif
178 cebix 1.1
179    
180     /*
181     * Ersatz functions
182     */
183    
184     extern "C" {
185    
186     #ifndef HAVE_STRDUP
187     char *strdup(const char *s)
188     {
189     char *n = (char *)malloc(strlen(s) + 1);
190     strcpy(n, s);
191     return n;
192     }
193     #endif
194    
195     }
196    
197    
198     /*
199     * Main program
200     */
201    
202 cebix 1.32 static void usage(const char *prg_name)
203     {
204     printf("Usage: %s [OPTION...]\n", prg_name);
205     printf("\nUnix options:\n");
206     printf(" --display STRING\n X display to use\n");
207     printf(" --break ADDRESS\n set ROM breakpoint\n");
208     printf(" --rominfo\n dump ROM information\n");
209     PrefsPrintUsage();
210     exit(0);
211     }
212    
213 cebix 1.1 int main(int argc, char **argv)
214     {
215 cebix 1.12 char str[256];
216    
217 cebix 1.1 // Initialize variables
218     RAMBaseHost = NULL;
219     ROMBaseHost = NULL;
220     srand(time(NULL));
221     tzset();
222    
223     // Print some info
224     printf(GetString(STR_ABOUT_TEXT1), VERSION_MAJOR, VERSION_MINOR);
225     printf(" %s\n", GetString(STR_ABOUT_TEXT2));
226    
227 cebix 1.28 #ifdef ENABLE_GTK
228 cebix 1.43 #ifdef HAVE_GNOMEUI
229     // Init GNOME/GTK
230     char version[16];
231     sprintf(version, "%d.%d", VERSION_MAJOR, VERSION_MINOR);
232     gnome_init("Basilisk II", version, argc, argv);
233     #else
234 cebix 1.28 // Init GTK
235     gtk_set_locale();
236     gtk_init(&argc, &argv);
237 cebix 1.43 #endif
238 cebix 1.28 x_display_name = gdk_get_display(); // gtk_init() handles and removes the "--display" argument
239     #endif
240    
241 cebix 1.32 // Read preferences
242     PrefsInit(argc, argv);
243    
244     // Parse command line arguments
245 cebix 1.1 for (int i=1; i<argc; i++) {
246 cebix 1.32 if (strcmp(argv[i], "--help") == 0) {
247     usage(argv[0]);
248     } else if (strcmp(argv[i], "--display") == 0) {
249     i++;
250     if (i < argc)
251 cebix 1.28 x_display_name = strdup(argv[i]);
252     } else if (strcmp(argv[i], "--break") == 0) {
253 cebix 1.32 i++;
254     if (i < argc)
255 cebix 1.28 ROMBreakpoint = strtol(argv[i], NULL, 0);
256     } else if (strcmp(argv[i], "--rominfo") == 0) {
257 cebix 1.6 PrintROMInfo = true;
258 cebix 1.32 } else if (argv[i][0] == '-') {
259     fprintf(stderr, "Unrecognized option '%s'\n", argv[i]);
260     usage(argv[0]);
261 cebix 1.28 }
262 cebix 1.1 }
263    
264     // Open display
265     x_display = XOpenDisplay(x_display_name);
266     if (x_display == NULL) {
267     char str[256];
268     sprintf(str, GetString(STR_NO_XSERVER_ERR), XDisplayName(x_display_name));
269     ErrorAlert(str);
270     QuitEmulator();
271     }
272    
273 cebix 1.12 #if defined(ENABLE_XF86_DGA) && !defined(ENABLE_MON)
274 cebix 1.1 // Fork out, so we can return from fullscreen mode when things get ugly
275 cebix 1.2 XF86DGAForkApp(DefaultScreen(x_display));
276 cebix 1.1 #endif
277    
278     // Init system routines
279     SysInit();
280    
281     // Show preferences editor
282     if (!PrefsFindBool("nogui"))
283     if (!PrefsEditor())
284     QuitEmulator();
285    
286 cebix 1.9 // Read RAM size
287 cebix 1.1 RAMSize = PrefsFindInt32("ramsize") & 0xfff00000; // Round down to 1MB boundary
288     if (RAMSize < 1024*1024) {
289     WarningAlert(GetString(STR_SMALL_RAM_WARN));
290     RAMSize = 1024*1024;
291     }
292 cebix 1.9
293 gbeauche 1.20 #if REAL_ADDRESSING || DIRECT_ADDRESSING
294 gbeauche 1.33 RAMSize = RAMSize & -getpagesize(); // Round down to page boundary
295 gbeauche 1.20 #endif
296 gbeauche 1.33
297     // Initialize VM system
298     vm_init();
299 gbeauche 1.20
300 cebix 1.12 #if REAL_ADDRESSING
301 gbeauche 1.33 // Flag: RAM and ROM are contigously allocated from address 0
302     bool memory_mapped_from_zero = false;
303    
304     // Under Solaris/SPARC and NetBSD/m68k, Basilisk II is known to crash
305     // when trying to map a too big chunk of memory starting at address 0
306 cebix 1.22 #if defined(OS_solaris) || defined(OS_netbsd)
307 gbeauche 1.33 const bool can_map_all_memory = false;
308 gbeauche 1.20 #else
309 gbeauche 1.33 const bool can_map_all_memory = true;
310 gbeauche 1.20 #endif
311 gbeauche 1.33
312     // Try to allocate all memory from 0x0000, if it is not known to crash
313     if (can_map_all_memory && (vm_acquire_fixed(0, RAMSize + 0x100000) == 0)) {
314 gbeauche 1.20 D(bug("Could allocate RAM and ROM from 0x0000\n"));
315     memory_mapped_from_zero = true;
316     }
317 gbeauche 1.33
318     // Otherwise, just create the Low Memory area (0x0000..0x2000)
319     else if (vm_acquire_fixed(0, 0x2000) == 0) {
320 gbeauche 1.20 D(bug("Could allocate the Low Memory globals\n"));
321     lm_area_mapped = true;
322     }
323 gbeauche 1.33
324     // Exit on failure
325 gbeauche 1.20 else {
326 cebix 1.12 sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno));
327     ErrorAlert(str);
328     QuitEmulator();
329     }
330     #endif
331    
332 cebix 1.9 // Create areas for Mac RAM and ROM
333 gbeauche 1.20 #if REAL_ADDRESSING
334     if (memory_mapped_from_zero) {
335     RAMBaseHost = (uint8 *)0;
336 gbeauche 1.33 ROMBaseHost = RAMBaseHost + RAMSize;
337 gbeauche 1.20 }
338     else
339     #endif
340     {
341 gbeauche 1.33 RAMBaseHost = (uint8 *)vm_acquire(RAMSize);
342     ROMBaseHost = (uint8 *)vm_acquire(0x100000);
343     if (RAMBaseHost == VM_MAP_FAILED || ROMBaseHost == VM_MAP_FAILED) {
344 cebix 1.36 ErrorAlert(STR_NO_MEM_ERR);
345 gbeauche 1.20 QuitEmulator();
346     }
347     }
348 gbeauche 1.38
349     #if USE_SCRATCHMEM_SUBTERFUGE
350     // Allocate scratch memory
351     ScratchMem = (uint8 *)vm_acquire(SCRATCH_MEM_SIZE);
352     if (ScratchMem == VM_MAP_FAILED) {
353     ErrorAlert(STR_NO_MEM_ERR);
354     QuitEmulator();
355     }
356     ScratchMem += SCRATCH_MEM_SIZE/2; // ScratchMem points to middle of block
357     #endif
358 cebix 1.22
359 gbeauche 1.20 #if DIRECT_ADDRESSING
360 gbeauche 1.33 // RAMBaseMac shall always be zero
361     MEMBaseDiff = (uintptr)RAMBaseHost;
362 gbeauche 1.20 RAMBaseMac = 0;
363 gbeauche 1.33 ROMBaseMac = Host2MacAddr(ROMBaseHost);
364 gbeauche 1.20 #endif
365 gbeauche 1.33 #if REAL_ADDRESSING
366 cebix 1.12 RAMBaseMac = (uint32)RAMBaseHost;
367     ROMBaseMac = (uint32)ROMBaseHost;
368     #endif
369     D(bug("Mac RAM starts at %p (%08x)\n", RAMBaseHost, RAMBaseMac));
370     D(bug("Mac ROM starts at %p (%08x)\n", ROMBaseHost, ROMBaseMac));
371 gbeauche 1.20
372 cebix 1.1 // Get rom file path from preferences
373     const char *rom_path = PrefsFindString("rom");
374    
375     // Load Mac ROM
376     int rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME, O_RDONLY);
377     if (rom_fd < 0) {
378 cebix 1.36 ErrorAlert(STR_NO_ROM_FILE_ERR);
379 cebix 1.1 QuitEmulator();
380     }
381     printf(GetString(STR_READING_ROM_FILE));
382     ROMSize = lseek(rom_fd, 0, SEEK_END);
383     if (ROMSize != 64*1024 && ROMSize != 128*1024 && ROMSize != 256*1024 && ROMSize != 512*1024 && ROMSize != 1024*1024) {
384 cebix 1.36 ErrorAlert(STR_ROM_SIZE_ERR);
385 cebix 1.1 close(rom_fd);
386     QuitEmulator();
387     }
388     lseek(rom_fd, 0, SEEK_SET);
389     if (read(rom_fd, ROMBaseHost, ROMSize) != (ssize_t)ROMSize) {
390 cebix 1.36 ErrorAlert(STR_ROM_FILE_READ_ERR);
391 cebix 1.1 close(rom_fd);
392     QuitEmulator();
393     }
394    
395 cebix 1.12 #if !EMULATED_68K
396     // Get CPU model
397     int mib[2] = {CTL_HW, HW_MODEL};
398     char *model;
399     size_t model_len;
400     sysctl(mib, 2, NULL, &model_len, NULL, 0);
401     model = (char *)malloc(model_len);
402     sysctl(mib, 2, model, &model_len, NULL, 0);
403     D(bug("Model: %s\n", model));
404    
405     // Set CPU and FPU type
406     CPUIs68060 = false;
407     if (strstr(model, "020"))
408     CPUType = 2;
409     else if (strstr(model, "030"))
410     CPUType = 3;
411     else if (strstr(model, "040"))
412     CPUType = 4;
413     else if (strstr(model, "060")) {
414     CPUType = 4;
415     CPUIs68060 = true;
416     } else {
417     printf("WARNING: Cannot detect CPU type, assuming 68020\n");
418     CPUType = 2;
419     }
420 cebix 1.24 FPUType = 1; // NetBSD has an FPU emulation, so the FPU ought to be available at all times
421 cebix 1.12 TwentyFourBitAddressing = false;
422     #endif
423    
424 cebix 1.3 // Initialize everything
425     if (!InitAll())
426 cebix 1.1 QuitEmulator();
427 cebix 1.12 D(bug("Initialization complete\n"));
428    
429     #ifdef HAVE_PTHREADS
430     // Get handle of main thread
431     emul_thread = pthread_self();
432     #endif
433    
434     #if !EMULATED_68K
435     // (Virtual) supervisor mode, disable interrupts
436     EmulatedSR = 0x2700;
437    
438     // Create and install stack for signal handlers
439     sig_stack = malloc(SIG_STACK_SIZE);
440     D(bug("Signal stack at %p\n", sig_stack));
441     if (sig_stack == NULL) {
442 cebix 1.36 ErrorAlert(STR_NOT_ENOUGH_MEMORY_ERR);
443 cebix 1.12 QuitEmulator();
444     }
445     stack_t new_stack;
446     new_stack.ss_sp = sig_stack;
447     new_stack.ss_flags = 0;
448     new_stack.ss_size = SIG_STACK_SIZE;
449     if (sigaltstack(&new_stack, NULL) < 0) {
450     sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno));
451     ErrorAlert(str);
452     QuitEmulator();
453     }
454    
455     // Install SIGILL handler for emulating privileged instructions and
456     // executing A-Trap and EMUL_OP opcodes
457     sigemptyset(&sigill_sa.sa_mask); // Block virtual 68k interrupts during SIGILL handling
458     sigaddset(&sigill_sa.sa_mask, SIG_IRQ);
459     sigaddset(&sigill_sa.sa_mask, SIGALRM);
460     sigill_sa.sa_handler = (void (*)(int))sigill_handler;
461     sigill_sa.sa_flags = SA_ONSTACK;
462     if (sigaction(SIGILL, &sigill_sa, NULL) < 0) {
463     sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGILL", strerror(errno));
464     ErrorAlert(str);
465     QuitEmulator();
466     }
467    
468     // Install virtual 68k interrupt signal handler
469     sigemptyset(&sigirq_sa.sa_mask);
470     sigaddset(&sigirq_sa.sa_mask, SIGALRM);
471     sigirq_sa.sa_handler = (void (*)(int))sigirq_handler;
472     sigirq_sa.sa_flags = SA_ONSTACK | SA_RESTART;
473     if (sigaction(SIG_IRQ, &sigirq_sa, NULL) < 0) {
474     sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIG_IRQ", strerror(errno));
475     ErrorAlert(str);
476     QuitEmulator();
477     }
478     #endif
479 cebix 1.1
480 cebix 1.12 #ifdef ENABLE_MON
481     // Setup SIGINT handler to enter mon
482     sigemptyset(&sigint_sa.sa_mask);
483 cebix 1.21 sigint_sa.sa_handler = (void (*)(int))sigint_handler;
484 cebix 1.12 sigint_sa.sa_flags = 0;
485     sigaction(SIGINT, &sigint_sa, NULL);
486     #endif
487 cebix 1.1
488 cebix 1.39 #if defined(HAVE_PTHREADS)
489    
490     // POSIX threads available, start 60Hz thread
491     pthread_attr_init(&tick_thread_attr);
492     #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
493     if (geteuid() == 0) {
494     pthread_attr_setinheritsched(&tick_thread_attr, PTHREAD_EXPLICIT_SCHED);
495     pthread_attr_setschedpolicy(&tick_thread_attr, SCHED_FIFO);
496     struct sched_param fifo_param;
497     fifo_param.sched_priority = (sched_get_priority_min(SCHED_FIFO) + sched_get_priority_max(SCHED_FIFO)) / 2;
498     pthread_attr_setschedparam(&tick_thread_attr, &fifo_param);
499     }
500     #endif
501     tick_thread_active = (pthread_create(&tick_thread, &tick_thread_attr, tick_func, NULL) == 0);
502     if (!tick_thread_active) {
503     sprintf(str, GetString(STR_TICK_THREAD_ERR), strerror(errno));
504     ErrorAlert(str);
505     QuitEmulator();
506     }
507     D(bug("60Hz thread started\n"));
508    
509     #elif defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
510 cebix 1.12
511     // POSIX.4 timers and real-time signals available, start 60Hz timer
512 cebix 1.1 sigemptyset(&timer_sa.sa_mask);
513 cebix 1.19 timer_sa.sa_sigaction = (void (*)(int, siginfo_t *, void *))one_tick;
514 cebix 1.1 timer_sa.sa_flags = SA_SIGINFO | SA_RESTART;
515     if (sigaction(SIG_TIMER, &timer_sa, NULL) < 0) {
516 cebix 1.12 sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIG_TIMER", strerror(errno));
517     ErrorAlert(str);
518 cebix 1.1 QuitEmulator();
519     }
520     struct sigevent timer_event;
521     timer_event.sigev_notify = SIGEV_SIGNAL;
522     timer_event.sigev_signo = SIG_TIMER;
523     if (timer_create(CLOCK_REALTIME, &timer_event, &timer) < 0) {
524 cebix 1.12 sprintf(str, GetString(STR_TIMER_CREATE_ERR), strerror(errno));
525     ErrorAlert(str);
526 cebix 1.1 QuitEmulator();
527     }
528     struct itimerspec req;
529     req.it_value.tv_sec = 0;
530     req.it_value.tv_nsec = 16625000;
531     req.it_interval.tv_sec = 0;
532     req.it_interval.tv_nsec = 16625000;
533 cebix 1.10 if (timer_settime(timer, 0, &req, NULL) < 0) {
534 cebix 1.12 sprintf(str, GetString(STR_TIMER_SETTIME_ERR), strerror(errno));
535     ErrorAlert(str);
536 cebix 1.1 QuitEmulator();
537     }
538 cebix 1.12 D(bug("60Hz timer started\n"));
539 cebix 1.1
540 cebix 1.12 #else
541    
542     // Start 60Hz timer
543     sigemptyset(&timer_sa.sa_mask); // Block virtual 68k interrupts during SIGARLM handling
544     sigaddset(&timer_sa.sa_mask, SIG_IRQ);
545     timer_sa.sa_handler = one_tick;
546     timer_sa.sa_flags = SA_ONSTACK | SA_RESTART;
547     if (sigaction(SIGALRM, &timer_sa, NULL) < 0) {
548     sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGALRM", strerror(errno));
549     ErrorAlert(str);
550 cebix 1.1 QuitEmulator();
551     }
552 cebix 1.12 struct itimerval req;
553     req.it_interval.tv_sec = req.it_value.tv_sec = 0;
554     req.it_interval.tv_usec = req.it_value.tv_usec = 16625;
555     setitimer(ITIMER_REAL, &req, NULL);
556    
557 cebix 1.1 #endif
558    
559 cebix 1.12 #ifdef HAVE_PTHREADS
560     // Start XPRAM watchdog thread
561 cebix 1.41 memcpy(last_xpram, XPRAM, XPRAM_SIZE);
562 cebix 1.12 xpram_thread_active = (pthread_create(&xpram_thread, NULL, xpram_func, NULL) == 0);
563     D(bug("XPRAM thread started\n"));
564 cebix 1.4 #endif
565    
566 cebix 1.1 // Start 68k and jump to ROM boot routine
567 cebix 1.12 D(bug("Starting emulation...\n"));
568 cebix 1.1 Start680x0();
569    
570     QuitEmulator();
571     return 0;
572     }
573    
574    
575     /*
576     * Quit emulator
577     */
578    
579     void QuitEmulator(void)
580     {
581 cebix 1.12 D(bug("QuitEmulator\n"));
582    
583     #if EMULATED_68K
584 cebix 1.1 // Exit 680x0 emulation
585     Exit680x0();
586 cebix 1.12 #endif
587 cebix 1.1
588 cebix 1.39 #if defined(HAVE_PTHREADS)
589 cebix 1.1 // Stop 60Hz thread
590     if (tick_thread_active) {
591     tick_thread_cancel = true;
592     #ifdef HAVE_PTHREAD_CANCEL
593     pthread_cancel(tick_thread);
594     #endif
595     pthread_join(tick_thread, NULL);
596     }
597 cebix 1.39 #elif defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
598     // Stop 60Hz timer
599     timer_delete(timer);
600 cebix 1.12 #else
601     struct itimerval req;
602     req.it_interval.tv_sec = req.it_value.tv_sec = 0;
603     req.it_interval.tv_usec = req.it_value.tv_usec = 0;
604     setitimer(ITIMER_REAL, &req, NULL);
605 cebix 1.1 #endif
606    
607 cebix 1.12 #ifdef HAVE_PTHREADS
608 cebix 1.1 // Stop XPRAM watchdog thread
609     if (xpram_thread_active) {
610     xpram_thread_cancel = true;
611     #ifdef HAVE_PTHREAD_CANCEL
612     pthread_cancel(xpram_thread);
613     #endif
614     pthread_join(xpram_thread, NULL);
615     }
616 cebix 1.12 #endif
617 cebix 1.1
618 cebix 1.3 // Deinitialize everything
619     ExitAll();
620 cebix 1.1
621 cebix 1.22 // Free ROM/RAM areas
622 gbeauche 1.33 if (RAMBaseHost != VM_MAP_FAILED) {
623     vm_release(RAMBaseHost, RAMSize);
624 cebix 1.22 RAMBaseHost = NULL;
625 gbeauche 1.20 }
626 gbeauche 1.33 if (ROMBaseHost != VM_MAP_FAILED) {
627     vm_release(ROMBaseHost, 0x100000);
628 cebix 1.17 ROMBaseHost = NULL;
629     }
630 cebix 1.1
631 cebix 1.22 #if USE_SCRATCHMEM_SUBTERFUGE
632 cebix 1.12 // Delete scratch memory area
633 gbeauche 1.33 if (ScratchMem != (uint8 *)VM_MAP_FAILED) {
634     vm_release((void *)(ScratchMem - SCRATCH_MEM_SIZE/2), SCRATCH_MEM_SIZE);
635 cebix 1.17 ScratchMem = NULL;
636     }
637 cebix 1.12 #endif
638    
639     #if REAL_ADDRESSING
640     // Delete Low Memory area
641     if (lm_area_mapped)
642 gbeauche 1.33 vm_release(0, 0x2000);
643 cebix 1.12 #endif
644 gbeauche 1.33
645     // Exit VM wrappers
646     vm_exit();
647 cebix 1.12
648 cebix 1.1 // Exit system routines
649     SysExit();
650    
651     // Exit preferences
652     PrefsExit();
653    
654     // Close X11 server connection
655     if (x_display)
656     XCloseDisplay(x_display);
657    
658     exit(0);
659     }
660    
661    
662     /*
663     * Code was patched, flush caches if neccessary (i.e. when using a real 680x0
664     * or a dynamically recompiling emulator)
665     */
666    
667     void FlushCodeCache(void *start, uint32 size)
668     {
669 cebix 1.12 #if !EMULATED_68K && defined(__NetBSD__)
670     m68k_sync_icache(start, size);
671     #endif
672 cebix 1.4 }
673    
674    
675     /*
676     * SIGINT handler, enters mon
677     */
678    
679 cebix 1.12 #ifdef ENABLE_MON
680 cebix 1.4 static void sigint_handler(...)
681     {
682 cebix 1.12 #if EMULATED_68K
683 cebix 1.8 uaecptr nextpc;
684 cebix 1.12 extern void m68k_dumpstate(uaecptr *nextpc);
685 cebix 1.8 m68k_dumpstate(&nextpc);
686 cebix 1.34 #endif
687 cebix 1.37 VideoQuitFullScreen();
688 cebix 1.21 char *arg[4] = {"mon", "-m", "-r", NULL};
689     mon(3, arg);
690 cebix 1.4 QuitEmulator();
691 cebix 1.1 }
692     #endif
693    
694    
695     /*
696 cebix 1.37 * Mutexes
697     */
698    
699     #ifdef HAVE_PTHREADS
700    
701     struct B2_mutex {
702     B2_mutex() { pthread_mutex_init(&m, NULL); }
703     ~B2_mutex() { pthread_mutex_unlock(&m); pthread_mutex_destroy(&m); }
704     pthread_mutex_t m;
705     };
706    
707     B2_mutex *B2_create_mutex(void)
708     {
709     return new B2_mutex;
710     }
711    
712     void B2_lock_mutex(B2_mutex *mutex)
713     {
714     pthread_mutex_lock(&mutex->m);
715     }
716    
717     void B2_unlock_mutex(B2_mutex *mutex)
718     {
719     pthread_mutex_unlock(&mutex->m);
720     }
721    
722     void B2_delete_mutex(B2_mutex *mutex)
723     {
724     delete mutex;
725     }
726    
727     #else
728    
729     struct B2_mutex {
730     int dummy;
731     };
732    
733     B2_mutex *B2_create_mutex(void)
734     {
735     return new B2_mutex;
736     }
737    
738     void B2_lock_mutex(B2_mutex *mutex)
739     {
740     }
741    
742     void B2_unlock_mutex(B2_mutex *mutex)
743     {
744     }
745    
746     void B2_delete_mutex(B2_mutex *mutex)
747     {
748     delete mutex;
749     }
750    
751     #endif
752    
753    
754     /*
755 cebix 1.1 * Interrupt flags (must be handled atomically!)
756     */
757    
758     uint32 InterruptFlags = 0;
759    
760 cebix 1.12 #if EMULATED_68K
761 cebix 1.1 void SetInterruptFlag(uint32 flag)
762     {
763 cebix 1.37 LOCK_INTFLAGS;
764 cebix 1.1 InterruptFlags |= flag;
765 cebix 1.37 UNLOCK_INTFLAGS;
766 cebix 1.1 }
767    
768     void ClearInterruptFlag(uint32 flag)
769     {
770 cebix 1.37 LOCK_INTFLAGS;
771 cebix 1.1 InterruptFlags &= ~flag;
772 cebix 1.37 UNLOCK_INTFLAGS;
773 cebix 1.12 }
774     #endif
775    
776     #if !EMULATED_68K
777     void TriggerInterrupt(void)
778     {
779     #if defined(HAVE_PTHREADS)
780     pthread_kill(emul_thread, SIG_IRQ);
781     #else
782     raise(SIG_IRQ);
783     #endif
784 cebix 1.22 }
785    
786     void TriggerNMI(void)
787     {
788     // not yet supported
789 cebix 1.12 }
790     #endif
791    
792    
793     /*
794     * XPRAM watchdog thread (saves XPRAM every minute)
795     */
796    
797     static void xpram_watchdog(void)
798     {
799 cebix 1.41 if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) {
800     memcpy(last_xpram, XPRAM, XPRAM_SIZE);
801 cebix 1.12 SaveXPRAM();
802     }
803     }
804    
805     #ifdef HAVE_PTHREADS
806     static void *xpram_func(void *arg)
807     {
808     while (!xpram_thread_cancel) {
809 cebix 1.16 for (int i=0; i<60 && !xpram_thread_cancel; i++)
810 cebix 1.29 Delay_usec(999999); // Only wait 1 second so we quit promptly when xpram_thread_cancel becomes true
811 cebix 1.12 xpram_watchdog();
812     }
813     return NULL;
814 cebix 1.1 }
815 cebix 1.12 #endif
816 cebix 1.1
817    
818     /*
819     * 60Hz thread (really 60.15Hz)
820     */
821    
822 cebix 1.12 static void one_second(void)
823     {
824     // Pseudo Mac 1Hz interrupt, update local time
825     WriteMacInt32(0x20c, TimerDateTime());
826    
827 cebix 1.18 SetInterruptFlag(INTFLAG_1HZ);
828 cebix 1.14 TriggerInterrupt();
829    
830 cebix 1.12 #ifndef HAVE_PTHREADS
831     static int second_counter = 0;
832     if (++second_counter > 60) {
833     second_counter = 0;
834     xpram_watchdog();
835     }
836     #endif
837     }
838    
839 cebix 1.1 static void one_tick(...)
840     {
841     static int tick_counter = 0;
842     if (++tick_counter > 60) {
843     tick_counter = 0;
844 cebix 1.12 one_second();
845 cebix 1.1 }
846    
847 cebix 1.12 #ifndef HAVE_PTHREADS
848 cebix 1.40 // No threads available, perform video refresh and networking from here
849 cebix 1.12 VideoRefresh();
850 cebix 1.40 SetInterruptFlag(INTFLAG_ETHER);
851 cebix 1.12 #endif
852    
853 cebix 1.1 // Trigger 60Hz interrupt
854     if (ROMVersion != ROM_VERSION_CLASSIC || HasMacStarted()) {
855     SetInterruptFlag(INTFLAG_60HZ);
856     TriggerInterrupt();
857     }
858     }
859    
860 cebix 1.12 #ifdef HAVE_PTHREADS
861 cebix 1.1 static void *tick_func(void *arg)
862     {
863 cebix 1.39 uint64 start = GetTicks_usec();
864     int64 ticks = 0;
865 cebix 1.16 uint64 next = GetTicks_usec();
866 cebix 1.1 while (!tick_thread_cancel) {
867 cebix 1.16 one_tick();
868     next += 16625;
869     int64 delay = next - GetTicks_usec();
870     if (delay > 0)
871     Delay_usec(delay);
872     else if (delay < -16625)
873     next = GetTicks_usec();
874 cebix 1.39 ticks++;
875 cebix 1.16 }
876 cebix 1.39 uint64 end = GetTicks_usec();
877     D(bug("%Ld ticks in %Ld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
878 cebix 1.16 return NULL;
879     }
880     #endif
881 cebix 1.12
882    
883     #if !EMULATED_68K
884     /*
885     * Virtual 68k interrupt handler
886     */
887    
888     static void sigirq_handler(int sig, int code, struct sigcontext *scp)
889     {
890     // Interrupts disabled? Then do nothing
891     if (EmulatedSR & 0x0700)
892     return;
893    
894     struct sigstate *state = (struct sigstate *)scp->sc_ap;
895     M68kRegisters *regs = (M68kRegisters *)&state->ss_frame;
896    
897     // Set up interrupt frame on stack
898     uint32 a7 = regs->a[7];
899     a7 -= 2;
900     WriteMacInt16(a7, 0x64);
901     a7 -= 4;
902     WriteMacInt32(a7, scp->sc_pc);
903     a7 -= 2;
904     WriteMacInt16(a7, scp->sc_ps | EmulatedSR);
905     scp->sc_sp = regs->a[7] = a7;
906    
907     // Set interrupt level
908     EmulatedSR |= 0x2100;
909    
910     // Jump to MacOS interrupt handler on return
911     scp->sc_pc = ReadMacInt32(0x64);
912     }
913 cebix 1.1
914    
915     /*
916 cebix 1.12 * SIGILL handler, for emulation of privileged instructions and executing
917     * A-Trap and EMUL_OP opcodes
918 cebix 1.1 */
919    
920 cebix 1.12 static void sigill_handler(int sig, int code, struct sigcontext *scp)
921 cebix 1.1 {
922 cebix 1.12 struct sigstate *state = (struct sigstate *)scp->sc_ap;
923     uint16 *pc = (uint16 *)scp->sc_pc;
924     uint16 opcode = *pc;
925     M68kRegisters *regs = (M68kRegisters *)&state->ss_frame;
926    
927     #define INC_PC(n) scp->sc_pc += (n)
928    
929     #define GET_SR (scp->sc_ps | EmulatedSR)
930    
931     #define STORE_SR(v) \
932     scp->sc_ps = (v) & 0xff; \
933 cebix 1.24 EmulatedSR = (v) & 0xe700; \
934 cebix 1.12 if (((v) & 0x0700) == 0 && InterruptFlags) \
935     TriggerInterrupt();
936    
937     //printf("opcode %04x at %p, sr %04x, emul_sr %04x\n", opcode, pc, scp->sc_ps, EmulatedSR);
938    
939     if ((opcode & 0xf000) == 0xa000) {
940    
941     // A-Line instruction, set up A-Line trap frame on stack
942     uint32 a7 = regs->a[7];
943     a7 -= 2;
944     WriteMacInt16(a7, 0x28);
945     a7 -= 4;
946     WriteMacInt32(a7, (uint32)pc);
947     a7 -= 2;
948     WriteMacInt16(a7, GET_SR);
949     scp->sc_sp = regs->a[7] = a7;
950    
951     // Jump to MacOS A-Line handler on return
952     scp->sc_pc = ReadMacInt32(0x28);
953    
954     } else if ((opcode & 0xff00) == 0x7100) {
955    
956     // Extended opcode, push registers on user stack
957     uint32 a7 = regs->a[7];
958     a7 -= 4;
959     WriteMacInt32(a7, (uint32)pc);
960     a7 -= 2;
961     WriteMacInt16(a7, scp->sc_ps);
962     for (int i=7; i>=0; i--) {
963     a7 -= 4;
964     WriteMacInt32(a7, regs->a[i]);
965     }
966     for (int i=7; i>=0; i--) {
967     a7 -= 4;
968     WriteMacInt32(a7, regs->d[i]);
969     }
970     scp->sc_sp = regs->a[7] = a7;
971    
972     // Jump to EmulOp trampoline code on return
973     scp->sc_pc = (uint32)EmulOpTrampoline;
974    
975     } else switch (opcode) { // Emulate privileged instructions
976    
977     case 0x40e7: // move sr,-(sp)
978     regs->a[7] -= 2;
979     WriteMacInt16(regs->a[7], GET_SR);
980     scp->sc_sp = regs->a[7];
981     INC_PC(2);
982     break;
983    
984     case 0x46df: { // move (sp)+,sr
985     uint16 sr = ReadMacInt16(regs->a[7]);
986     STORE_SR(sr);
987     regs->a[7] += 2;
988     scp->sc_sp = regs->a[7];
989     INC_PC(2);
990     break;
991     }
992    
993     case 0x007c: { // ori #xxxx,sr
994     uint16 sr = GET_SR | pc[1];
995     scp->sc_ps = sr & 0xff; // oring bits into the sr can't enable interrupts, so we don't need to call STORE_SR
996 cebix 1.24 EmulatedSR = sr & 0xe700;
997 cebix 1.12 INC_PC(4);
998     break;
999     }
1000    
1001     case 0x027c: { // andi #xxxx,sr
1002     uint16 sr = GET_SR & pc[1];
1003     STORE_SR(sr);
1004     INC_PC(4);
1005     break;
1006     }
1007    
1008     case 0x46fc: // move #xxxx,sr
1009     STORE_SR(pc[1]);
1010     INC_PC(4);
1011     break;
1012    
1013     case 0x46ef: { // move (xxxx,sp),sr
1014     uint16 sr = ReadMacInt16(regs->a[7] + (int32)(int16)pc[1]);
1015     STORE_SR(sr);
1016     INC_PC(4);
1017     break;
1018     }
1019    
1020     case 0x46d8: // move (a0)+,sr
1021     case 0x46d9: { // move (a1)+,sr
1022     uint16 sr = ReadMacInt16(regs->a[opcode & 7]);
1023     STORE_SR(sr);
1024     regs->a[opcode & 7] += 2;
1025     INC_PC(2);
1026     break;
1027     }
1028 cebix 1.1
1029 cebix 1.12 case 0x40f8: // move sr,xxxx.w
1030     WriteMacInt16(pc[1], GET_SR);
1031     INC_PC(4);
1032     break;
1033    
1034     case 0x40d0: // move sr,(a0)
1035     case 0x40d1: // move sr,(a1)
1036     case 0x40d2: // move sr,(a2)
1037     case 0x40d3: // move sr,(a3)
1038     case 0x40d4: // move sr,(a4)
1039     case 0x40d5: // move sr,(a5)
1040     case 0x40d6: // move sr,(a6)
1041     case 0x40d7: // move sr,(sp)
1042     WriteMacInt16(regs->a[opcode & 7], GET_SR);
1043     INC_PC(2);
1044     break;
1045    
1046     case 0x40c0: // move sr,d0
1047     case 0x40c1: // move sr,d1
1048     case 0x40c2: // move sr,d2
1049     case 0x40c3: // move sr,d3
1050     case 0x40c4: // move sr,d4
1051     case 0x40c5: // move sr,d5
1052     case 0x40c6: // move sr,d6
1053     case 0x40c7: // move sr,d7
1054     regs->d[opcode & 7] = GET_SR;
1055     INC_PC(2);
1056     break;
1057    
1058     case 0x46c0: // move d0,sr
1059     case 0x46c1: // move d1,sr
1060     case 0x46c2: // move d2,sr
1061     case 0x46c3: // move d3,sr
1062     case 0x46c4: // move d4,sr
1063     case 0x46c5: // move d5,sr
1064     case 0x46c6: // move d6,sr
1065     case 0x46c7: { // move d7,sr
1066     uint16 sr = regs->d[opcode & 7];
1067     STORE_SR(sr);
1068     INC_PC(2);
1069     break;
1070 cebix 1.1 }
1071 cebix 1.12
1072     case 0xf327: // fsave -(sp)
1073 cebix 1.35 regs->a[7] -= 4;
1074     WriteMacInt32(regs->a[7], 0x41000000); // Idle frame
1075 cebix 1.24 scp->sc_sp = regs->a[7];
1076     INC_PC(2);
1077     break;
1078 cebix 1.12
1079     case 0xf35f: // frestore (sp)+
1080 cebix 1.35 regs->a[7] += 4;
1081 cebix 1.24 scp->sc_sp = regs->a[7];
1082     INC_PC(2);
1083     break;
1084 cebix 1.12
1085 cebix 1.24 case 0x4e73: { // rte
1086 cebix 1.12 uint32 a7 = regs->a[7];
1087     uint16 sr = ReadMacInt16(a7);
1088     a7 += 2;
1089     scp->sc_ps = sr & 0xff;
1090 cebix 1.24 EmulatedSR = sr & 0xe700;
1091 cebix 1.12 scp->sc_pc = ReadMacInt32(a7);
1092 cebix 1.24 a7 += 4;
1093     uint16 format = ReadMacInt16(a7) >> 12;
1094     a7 += 2;
1095     static const int frame_adj[16] = {
1096     0, 0, 4, 4, 8, 0, 0, 52, 50, 12, 24, 84, 16, 0, 0, 0
1097     };
1098     scp->sc_sp = regs->a[7] = a7 + frame_adj[format];
1099 cebix 1.12 break;
1100 cebix 1.1 }
1101 cebix 1.12
1102     case 0x4e7a: // movec cr,x
1103     switch (pc[1]) {
1104     case 0x0002: // movec cacr,d0
1105     regs->d[0] = 0x3111;
1106     break;
1107     case 0x1002: // movec cacr,d1
1108     regs->d[1] = 0x3111;
1109     break;
1110     case 0x0003: // movec tc,d0
1111 cebix 1.24 case 0x0004: // movec itt0,d0
1112     case 0x0005: // movec itt1,d0
1113     case 0x0006: // movec dtt0,d0
1114     case 0x0007: // movec dtt1,d0
1115     case 0x0806: // movec urp,d0
1116     case 0x0807: // movec srp,d0
1117 cebix 1.12 regs->d[0] = 0;
1118     break;
1119 cebix 1.24 case 0x1000: // movec sfc,d1
1120     case 0x1001: // movec dfc,d1
1121 cebix 1.12 case 0x1003: // movec tc,d1
1122 cebix 1.24 case 0x1801: // movec vbr,d1
1123 cebix 1.12 regs->d[1] = 0;
1124     break;
1125 cebix 1.24 case 0x8801: // movec vbr,a0
1126     regs->a[0] = 0;
1127     break;
1128     case 0x9801: // movec vbr,a1
1129     regs->a[1] = 0;
1130     break;
1131 cebix 1.12 default:
1132     goto ill;
1133     }
1134     INC_PC(4);
1135     break;
1136    
1137     case 0x4e7b: // movec x,cr
1138     switch (pc[1]) {
1139 cebix 1.24 case 0x1000: // movec d1,sfc
1140     case 0x1001: // movec d1,dfc
1141 cebix 1.12 case 0x0801: // movec d0,vbr
1142 cebix 1.24 case 0x1801: // movec d1,vbr
1143 cebix 1.12 break;
1144     case 0x0002: // movec d0,cacr
1145     case 0x1002: // movec d1,cacr
1146     FlushCodeCache(NULL, 0);
1147     break;
1148     default:
1149     goto ill;
1150     }
1151     INC_PC(4);
1152     break;
1153    
1154     case 0xf478: // cpusha dc
1155     case 0xf4f8: // cpusha dc/ic
1156     FlushCodeCache(NULL, 0);
1157     INC_PC(2);
1158     break;
1159    
1160     default:
1161     ill: printf("SIGILL num %d, code %d\n", sig, code);
1162     printf(" context %p:\n", scp);
1163     printf(" onstack %08x\n", scp->sc_onstack);
1164     printf(" sp %08x\n", scp->sc_sp);
1165     printf(" fp %08x\n", scp->sc_fp);
1166     printf(" pc %08x\n", scp->sc_pc);
1167     printf(" opcode %04x\n", opcode);
1168     printf(" sr %08x\n", scp->sc_ps);
1169     printf(" state %p:\n", state);
1170     printf(" flags %d\n", state->ss_flags);
1171     for (int i=0; i<8; i++)
1172     printf(" d%d %08x\n", i, state->ss_frame.f_regs[i]);
1173     for (int i=0; i<8; i++)
1174     printf(" a%d %08x\n", i, state->ss_frame.f_regs[i+8]);
1175    
1176 cebix 1.37 VideoQuitFullScreen();
1177 cebix 1.12 #ifdef ENABLE_MON
1178 cebix 1.21 char *arg[4] = {"mon", "-m", "-r", NULL};
1179     mon(3, arg);
1180 cebix 1.12 #endif
1181     QuitEmulator();
1182     break;
1183 cebix 1.1 }
1184     }
1185 cebix 1.12 #endif
1186 cebix 1.1
1187    
1188     /*
1189     * Display alert
1190     */
1191    
1192 cebix 1.12 #ifdef ENABLE_GTK
1193 cebix 1.1 static void dl_destroyed(void)
1194     {
1195     gtk_main_quit();
1196     }
1197    
1198     static void dl_quit(GtkWidget *dialog)
1199     {
1200     gtk_widget_destroy(dialog);
1201     }
1202    
1203     void display_alert(int title_id, int prefix_id, int button_id, const char *text)
1204     {
1205     char str[256];
1206     sprintf(str, GetString(prefix_id), text);
1207    
1208     GtkWidget *dialog = gtk_dialog_new();
1209     gtk_window_set_title(GTK_WINDOW(dialog), GetString(title_id));
1210     gtk_container_border_width(GTK_CONTAINER(dialog), 5);
1211     gtk_widget_set_uposition(GTK_WIDGET(dialog), 100, 150);
1212     gtk_signal_connect(GTK_OBJECT(dialog), "destroy", GTK_SIGNAL_FUNC(dl_destroyed), NULL);
1213    
1214     GtkWidget *label = gtk_label_new(str);
1215     gtk_widget_show(label);
1216     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label, TRUE, TRUE, 0);
1217    
1218     GtkWidget *button = gtk_button_new_with_label(GetString(button_id));
1219     gtk_widget_show(button);
1220     gtk_signal_connect_object(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(dl_quit), GTK_OBJECT(dialog));
1221     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area), button, FALSE, FALSE, 0);
1222     GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1223     gtk_widget_grab_default(button);
1224     gtk_widget_show(dialog);
1225    
1226     gtk_main();
1227     }
1228     #endif
1229    
1230    
1231     /*
1232     * Display error alert
1233     */
1234    
1235     void ErrorAlert(const char *text)
1236     {
1237 cebix 1.12 #ifdef ENABLE_GTK
1238 cebix 1.1 if (PrefsFindBool("nogui") || x_display == NULL) {
1239     printf(GetString(STR_SHELL_ERROR_PREFIX), text);
1240     return;
1241     }
1242     VideoQuitFullScreen();
1243     display_alert(STR_ERROR_ALERT_TITLE, STR_GUI_ERROR_PREFIX, STR_QUIT_BUTTON, text);
1244     #else
1245     printf(GetString(STR_SHELL_ERROR_PREFIX), text);
1246     #endif
1247     }
1248    
1249    
1250     /*
1251     * Display warning alert
1252     */
1253    
1254     void WarningAlert(const char *text)
1255     {
1256 cebix 1.12 #ifdef ENABLE_GTK
1257 cebix 1.1 if (PrefsFindBool("nogui") || x_display == NULL) {
1258     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1259     return;
1260     }
1261     display_alert(STR_WARNING_ALERT_TITLE, STR_GUI_WARNING_PREFIX, STR_OK_BUTTON, text);
1262     #else
1263     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1264     #endif
1265     }
1266    
1267    
1268     /*
1269     * Display choice alert
1270     */
1271    
1272     bool ChoiceAlert(const char *text, const char *pos, const char *neg)
1273     {
1274     printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1275     return false; //!!
1276     }