ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/main_unix.cpp
Revision: 1.55
Committed: 2003-10-12T21:21:35Z (20 years, 9 months ago) by gbeauche
Branch: MAIN
Changes since 1.54: +5 -2 lines
Log Message:
Report failure to install the SIGSEGV handler correctly

File Contents

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