ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/main_unix.cpp
Revision: 1.54
Committed: 2003-09-29T07:02:58Z (20 years, 10 months ago) by gbeauche
Branch: MAIN
Changes since 1.53: +30 -6 lines
Log Message:
New SIGSEGV API so that skip-instruction requests are more explicit. Yes,
that's api change, but that's cooler now for SheepShaver. ;-)

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 return false;
389
390 // Register dump state function when we got mad after a segfault
391 sigsegv_set_dump_state(sigsegv_dump_state);
392
393 // Read RAM size
394 RAMSize = PrefsFindInt32("ramsize") & 0xfff00000; // Round down to 1MB boundary
395 if (RAMSize < 1024*1024) {
396 WarningAlert(GetString(STR_SMALL_RAM_WARN));
397 RAMSize = 1024*1024;
398 }
399
400 #if REAL_ADDRESSING || DIRECT_ADDRESSING
401 RAMSize = RAMSize & -getpagesize(); // Round down to page boundary
402 #endif
403
404 // Initialize VM system
405 vm_init();
406
407 #if REAL_ADDRESSING
408 // Flag: RAM and ROM are contigously allocated from address 0
409 bool memory_mapped_from_zero = false;
410
411 // Under Solaris/SPARC and NetBSD/m68k, Basilisk II is known to crash
412 // when trying to map a too big chunk of memory starting at address 0
413 #if defined(OS_solaris) || defined(OS_netbsd)
414 const bool can_map_all_memory = false;
415 #else
416 const bool can_map_all_memory = true;
417 #endif
418
419 // Try to allocate all memory from 0x0000, if it is not known to crash
420 if (can_map_all_memory && (vm_acquire_fixed(0, RAMSize + 0x100000) == 0)) {
421 D(bug("Could allocate RAM and ROM from 0x0000\n"));
422 memory_mapped_from_zero = true;
423 }
424
425 // Otherwise, just create the Low Memory area (0x0000..0x2000)
426 else if (vm_acquire_fixed(0, 0x2000) == 0) {
427 D(bug("Could allocate the Low Memory globals\n"));
428 lm_area_mapped = true;
429 }
430
431 // Exit on failure
432 else {
433 sprintf(str, GetString(STR_LOW_MEM_MMAP_ERR), strerror(errno));
434 ErrorAlert(str);
435 QuitEmulator();
436 }
437 #endif
438
439 // Create areas for Mac RAM and ROM
440 #if REAL_ADDRESSING
441 if (memory_mapped_from_zero) {
442 RAMBaseHost = (uint8 *)0;
443 ROMBaseHost = RAMBaseHost + RAMSize;
444 }
445 else
446 #endif
447 {
448 RAMBaseHost = (uint8 *)vm_acquire(RAMSize);
449 ROMBaseHost = (uint8 *)vm_acquire(0x100000);
450 if (RAMBaseHost == VM_MAP_FAILED || ROMBaseHost == VM_MAP_FAILED) {
451 ErrorAlert(STR_NO_MEM_ERR);
452 QuitEmulator();
453 }
454 }
455
456 #if USE_SCRATCHMEM_SUBTERFUGE
457 // Allocate scratch memory
458 ScratchMem = (uint8 *)vm_acquire(SCRATCH_MEM_SIZE);
459 if (ScratchMem == VM_MAP_FAILED) {
460 ErrorAlert(STR_NO_MEM_ERR);
461 QuitEmulator();
462 }
463 ScratchMem += SCRATCH_MEM_SIZE/2; // ScratchMem points to middle of block
464 #endif
465
466 #if DIRECT_ADDRESSING
467 // RAMBaseMac shall always be zero
468 MEMBaseDiff = (uintptr)RAMBaseHost;
469 RAMBaseMac = 0;
470 ROMBaseMac = Host2MacAddr(ROMBaseHost);
471 #endif
472 #if REAL_ADDRESSING
473 RAMBaseMac = (uint32)RAMBaseHost;
474 ROMBaseMac = (uint32)ROMBaseHost;
475 #endif
476 D(bug("Mac RAM starts at %p (%08x)\n", RAMBaseHost, RAMBaseMac));
477 D(bug("Mac ROM starts at %p (%08x)\n", ROMBaseHost, ROMBaseMac));
478
479 // Get rom file path from preferences
480 const char *rom_path = PrefsFindString("rom");
481
482 // Load Mac ROM
483 int rom_fd = open(rom_path ? rom_path : ROM_FILE_NAME, O_RDONLY);
484 if (rom_fd < 0) {
485 ErrorAlert(STR_NO_ROM_FILE_ERR);
486 QuitEmulator();
487 }
488 printf(GetString(STR_READING_ROM_FILE));
489 ROMSize = lseek(rom_fd, 0, SEEK_END);
490 if (ROMSize != 64*1024 && ROMSize != 128*1024 && ROMSize != 256*1024 && ROMSize != 512*1024 && ROMSize != 1024*1024) {
491 ErrorAlert(STR_ROM_SIZE_ERR);
492 close(rom_fd);
493 QuitEmulator();
494 }
495 lseek(rom_fd, 0, SEEK_SET);
496 if (read(rom_fd, ROMBaseHost, ROMSize) != (ssize_t)ROMSize) {
497 ErrorAlert(STR_ROM_FILE_READ_ERR);
498 close(rom_fd);
499 QuitEmulator();
500 }
501
502 #if !EMULATED_68K
503 // Get CPU model
504 int mib[2] = {CTL_HW, HW_MODEL};
505 char *model;
506 size_t model_len;
507 sysctl(mib, 2, NULL, &model_len, NULL, 0);
508 model = (char *)malloc(model_len);
509 sysctl(mib, 2, model, &model_len, NULL, 0);
510 D(bug("Model: %s\n", model));
511
512 // Set CPU and FPU type
513 CPUIs68060 = false;
514 if (strstr(model, "020"))
515 CPUType = 2;
516 else if (strstr(model, "030"))
517 CPUType = 3;
518 else if (strstr(model, "040"))
519 CPUType = 4;
520 else if (strstr(model, "060")) {
521 CPUType = 4;
522 CPUIs68060 = true;
523 } else {
524 printf("WARNING: Cannot detect CPU type, assuming 68020\n");
525 CPUType = 2;
526 }
527 FPUType = 1; // NetBSD has an FPU emulation, so the FPU ought to be available at all times
528 TwentyFourBitAddressing = false;
529 #endif
530
531 // Initialize everything
532 if (!InitAll())
533 QuitEmulator();
534 D(bug("Initialization complete\n"));
535
536 #if !EMULATED_68K
537 // (Virtual) supervisor mode, disable interrupts
538 EmulatedSR = 0x2700;
539
540 #ifdef HAVE_PTHREADS
541 // Get handle of main thread
542 emul_thread = pthread_self();
543 #endif
544
545 // Create and install stack for signal handlers
546 sig_stack = malloc(SIG_STACK_SIZE);
547 D(bug("Signal stack at %p\n", sig_stack));
548 if (sig_stack == NULL) {
549 ErrorAlert(STR_NOT_ENOUGH_MEMORY_ERR);
550 QuitEmulator();
551 }
552 stack_t new_stack;
553 new_stack.ss_sp = sig_stack;
554 new_stack.ss_flags = 0;
555 new_stack.ss_size = SIG_STACK_SIZE;
556 if (sigaltstack(&new_stack, NULL) < 0) {
557 sprintf(str, GetString(STR_SIGALTSTACK_ERR), strerror(errno));
558 ErrorAlert(str);
559 QuitEmulator();
560 }
561
562 // Install SIGILL handler for emulating privileged instructions and
563 // executing A-Trap and EMUL_OP opcodes
564 sigemptyset(&sigill_sa.sa_mask); // Block virtual 68k interrupts during SIGILL handling
565 sigaddset(&sigill_sa.sa_mask, SIG_IRQ);
566 sigaddset(&sigill_sa.sa_mask, SIGALRM);
567 sigill_sa.sa_handler = (void (*)(int))sigill_handler;
568 sigill_sa.sa_flags = SA_ONSTACK;
569 if (sigaction(SIGILL, &sigill_sa, NULL) < 0) {
570 sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGILL", strerror(errno));
571 ErrorAlert(str);
572 QuitEmulator();
573 }
574
575 // Install virtual 68k interrupt signal handler
576 sigemptyset(&sigirq_sa.sa_mask);
577 sigaddset(&sigirq_sa.sa_mask, SIGALRM);
578 sigirq_sa.sa_handler = (void (*)(int))sigirq_handler;
579 sigirq_sa.sa_flags = SA_ONSTACK | SA_RESTART;
580 if (sigaction(SIG_IRQ, &sigirq_sa, NULL) < 0) {
581 sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIG_IRQ", strerror(errno));
582 ErrorAlert(str);
583 QuitEmulator();
584 }
585 #endif
586
587 #ifdef ENABLE_MON
588 // Setup SIGINT handler to enter mon
589 sigemptyset(&sigint_sa.sa_mask);
590 sigint_sa.sa_handler = (void (*)(int))sigint_handler;
591 sigint_sa.sa_flags = 0;
592 sigaction(SIGINT, &sigint_sa, NULL);
593 #endif
594
595 #if defined(HAVE_PTHREADS)
596
597 // POSIX threads available, start 60Hz thread
598 Set_pthread_attr(&tick_thread_attr, 0);
599 tick_thread_active = (pthread_create(&tick_thread, &tick_thread_attr, tick_func, NULL) == 0);
600 if (!tick_thread_active) {
601 sprintf(str, GetString(STR_TICK_THREAD_ERR), strerror(errno));
602 ErrorAlert(str);
603 QuitEmulator();
604 }
605 D(bug("60Hz thread started\n"));
606
607 #elif defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
608
609 // POSIX.4 timers and real-time signals available, start 60Hz timer
610 sigemptyset(&timer_sa.sa_mask);
611 timer_sa.sa_sigaction = (void (*)(int, siginfo_t *, void *))one_tick;
612 timer_sa.sa_flags = SA_SIGINFO | SA_RESTART;
613 if (sigaction(SIG_TIMER, &timer_sa, NULL) < 0) {
614 sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIG_TIMER", strerror(errno));
615 ErrorAlert(str);
616 QuitEmulator();
617 }
618 struct sigevent timer_event;
619 timer_event.sigev_notify = SIGEV_SIGNAL;
620 timer_event.sigev_signo = SIG_TIMER;
621 if (timer_create(CLOCK_REALTIME, &timer_event, &timer) < 0) {
622 sprintf(str, GetString(STR_TIMER_CREATE_ERR), strerror(errno));
623 ErrorAlert(str);
624 QuitEmulator();
625 }
626 struct itimerspec req;
627 req.it_value.tv_sec = 0;
628 req.it_value.tv_nsec = 16625000;
629 req.it_interval.tv_sec = 0;
630 req.it_interval.tv_nsec = 16625000;
631 if (timer_settime(timer, 0, &req, NULL) < 0) {
632 sprintf(str, GetString(STR_TIMER_SETTIME_ERR), strerror(errno));
633 ErrorAlert(str);
634 QuitEmulator();
635 }
636 D(bug("60Hz timer started\n"));
637
638 #else
639
640 // Start 60Hz timer
641 sigemptyset(&timer_sa.sa_mask); // Block virtual 68k interrupts during SIGARLM handling
642 #if !EMULATED_68K
643 sigaddset(&timer_sa.sa_mask, SIG_IRQ);
644 #endif
645 timer_sa.sa_handler = one_tick;
646 timer_sa.sa_flags = SA_ONSTACK | SA_RESTART;
647 if (sigaction(SIGALRM, &timer_sa, NULL) < 0) {
648 sprintf(str, GetString(STR_SIG_INSTALL_ERR), "SIGALRM", strerror(errno));
649 ErrorAlert(str);
650 QuitEmulator();
651 }
652 struct itimerval req;
653 req.it_interval.tv_sec = req.it_value.tv_sec = 0;
654 req.it_interval.tv_usec = req.it_value.tv_usec = 16625;
655 setitimer(ITIMER_REAL, &req, NULL);
656
657 #endif
658
659 #ifdef HAVE_PTHREADS
660 // Start XPRAM watchdog thread
661 memcpy(last_xpram, XPRAM, XPRAM_SIZE);
662 xpram_thread_active = (pthread_create(&xpram_thread, NULL, xpram_func, NULL) == 0);
663 D(bug("XPRAM thread started\n"));
664 #endif
665
666 // Start 68k and jump to ROM boot routine
667 D(bug("Starting emulation...\n"));
668 Start680x0();
669
670 QuitEmulator();
671 return 0;
672 }
673
674
675 /*
676 * Quit emulator
677 */
678
679 void QuitEmulator(void)
680 {
681 D(bug("QuitEmulator\n"));
682
683 #if EMULATED_68K
684 // Exit 680x0 emulation
685 Exit680x0();
686 #endif
687
688 #if defined(HAVE_PTHREADS)
689 // Stop 60Hz thread
690 if (tick_thread_active) {
691 tick_thread_cancel = true;
692 #ifdef HAVE_PTHREAD_CANCEL
693 pthread_cancel(tick_thread);
694 #endif
695 pthread_join(tick_thread, NULL);
696 }
697 #elif defined(HAVE_TIMER_CREATE) && defined(_POSIX_REALTIME_SIGNALS)
698 // Stop 60Hz timer
699 timer_delete(timer);
700 #else
701 struct itimerval req;
702 req.it_interval.tv_sec = req.it_value.tv_sec = 0;
703 req.it_interval.tv_usec = req.it_value.tv_usec = 0;
704 setitimer(ITIMER_REAL, &req, NULL);
705 #endif
706
707 #ifdef HAVE_PTHREADS
708 // Stop XPRAM watchdog thread
709 if (xpram_thread_active) {
710 xpram_thread_cancel = true;
711 #ifdef HAVE_PTHREAD_CANCEL
712 pthread_cancel(xpram_thread);
713 #endif
714 pthread_join(xpram_thread, NULL);
715 }
716 #endif
717
718 // Deinitialize everything
719 ExitAll();
720
721 // Free ROM/RAM areas
722 if (RAMBaseHost != VM_MAP_FAILED) {
723 vm_release(RAMBaseHost, RAMSize);
724 RAMBaseHost = NULL;
725 }
726 if (ROMBaseHost != VM_MAP_FAILED) {
727 vm_release(ROMBaseHost, 0x100000);
728 ROMBaseHost = NULL;
729 }
730
731 #if USE_SCRATCHMEM_SUBTERFUGE
732 // Delete scratch memory area
733 if (ScratchMem != (uint8 *)VM_MAP_FAILED) {
734 vm_release((void *)(ScratchMem - SCRATCH_MEM_SIZE/2), SCRATCH_MEM_SIZE);
735 ScratchMem = NULL;
736 }
737 #endif
738
739 #if REAL_ADDRESSING
740 // Delete Low Memory area
741 if (lm_area_mapped)
742 vm_release(0, 0x2000);
743 #endif
744
745 // Exit VM wrappers
746 vm_exit();
747
748 // Exit system routines
749 SysExit();
750
751 // Exit preferences
752 PrefsExit();
753
754 // Close X11 server connection
755 if (x_display)
756 XCloseDisplay(x_display);
757
758 exit(0);
759 }
760
761
762 /*
763 * Code was patched, flush caches if neccessary (i.e. when using a real 680x0
764 * or a dynamically recompiling emulator)
765 */
766
767 void FlushCodeCache(void *start, uint32 size)
768 {
769 #if USE_JIT
770 if (UseJIT)
771 flush_icache(-1);
772 #endif
773 #if !EMULATED_68K && defined(__NetBSD__)
774 m68k_sync_icache(start, size);
775 #endif
776 }
777
778
779 /*
780 * SIGINT handler, enters mon
781 */
782
783 #ifdef ENABLE_MON
784 static void sigint_handler(...)
785 {
786 #if EMULATED_68K
787 uaecptr nextpc;
788 extern void m68k_dumpstate(uaecptr *nextpc);
789 m68k_dumpstate(&nextpc);
790 #endif
791 VideoQuitFullScreen();
792 char *arg[4] = {"mon", "-m", "-r", NULL};
793 mon(3, arg);
794 QuitEmulator();
795 }
796 #endif
797
798
799 #ifdef HAVE_PTHREADS
800 /*
801 * Pthread configuration
802 */
803
804 void Set_pthread_attr(pthread_attr_t *attr, int priority)
805 {
806 pthread_attr_init(attr);
807 #if defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
808 // Some of these only work for superuser
809 if (geteuid() == 0) {
810 pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED);
811 pthread_attr_setschedpolicy(attr, SCHED_FIFO);
812 struct sched_param fifo_param;
813 fifo_param.sched_priority = ((sched_get_priority_min(SCHED_FIFO) +
814 sched_get_priority_max(SCHED_FIFO)) / 2 +
815 priority);
816 pthread_attr_setschedparam(attr, &fifo_param);
817 }
818 if (pthread_attr_setscope(attr, PTHREAD_SCOPE_SYSTEM) != 0) {
819 #ifdef PTHREAD_SCOPE_BOUND_NP
820 // If system scope is not available (eg. we're not running
821 // with CAP_SCHED_MGT capability on an SGI box), try bound
822 // scope. It exposes pthread scheduling to the kernel,
823 // without setting realtime priority.
824 pthread_attr_setscope(attr, PTHREAD_SCOPE_BOUND_NP);
825 #endif
826 }
827 #endif
828 }
829 #endif // HAVE_PTHREADS
830
831
832 /*
833 * Mutexes
834 */
835
836 #ifdef HAVE_PTHREADS
837
838 struct B2_mutex {
839 B2_mutex() {
840 pthread_mutexattr_t attr;
841 pthread_mutexattr_init(&attr);
842 // Initialize the mutex for priority inheritance --
843 // required for accurate timing.
844 #ifdef HAVE_PTHREAD_MUTEXATTR_SETPROTOCOL
845 pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
846 #endif
847 #if defined(HAVE_PTHREAD_MUTEXATTR_SETTYPE) && defined(PTHREAD_MUTEX_NORMAL)
848 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);
849 #endif
850 #ifdef HAVE_PTHREAD_MUTEXATTR_SETPSHARED
851 pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
852 #endif
853 pthread_mutex_init(&m, &attr);
854 pthread_mutexattr_destroy(&attr);
855 }
856 ~B2_mutex() {
857 pthread_mutex_trylock(&m); // Make sure it's locked before
858 pthread_mutex_unlock(&m); // unlocking it.
859 pthread_mutex_destroy(&m);
860 }
861 pthread_mutex_t m;
862 };
863
864 B2_mutex *B2_create_mutex(void)
865 {
866 return new B2_mutex;
867 }
868
869 void B2_lock_mutex(B2_mutex *mutex)
870 {
871 pthread_mutex_lock(&mutex->m);
872 }
873
874 void B2_unlock_mutex(B2_mutex *mutex)
875 {
876 pthread_mutex_unlock(&mutex->m);
877 }
878
879 void B2_delete_mutex(B2_mutex *mutex)
880 {
881 delete mutex;
882 }
883
884 #else
885
886 struct B2_mutex {
887 int dummy;
888 };
889
890 B2_mutex *B2_create_mutex(void)
891 {
892 return new B2_mutex;
893 }
894
895 void B2_lock_mutex(B2_mutex *mutex)
896 {
897 }
898
899 void B2_unlock_mutex(B2_mutex *mutex)
900 {
901 }
902
903 void B2_delete_mutex(B2_mutex *mutex)
904 {
905 delete mutex;
906 }
907
908 #endif
909
910
911 /*
912 * Interrupt flags (must be handled atomically!)
913 */
914
915 uint32 InterruptFlags = 0;
916
917 #if EMULATED_68K
918 void SetInterruptFlag(uint32 flag)
919 {
920 LOCK_INTFLAGS;
921 InterruptFlags |= flag;
922 UNLOCK_INTFLAGS;
923 }
924
925 void ClearInterruptFlag(uint32 flag)
926 {
927 LOCK_INTFLAGS;
928 InterruptFlags &= ~flag;
929 UNLOCK_INTFLAGS;
930 }
931 #endif
932
933 #if !EMULATED_68K
934 void TriggerInterrupt(void)
935 {
936 #if defined(HAVE_PTHREADS)
937 pthread_kill(emul_thread, SIG_IRQ);
938 #else
939 raise(SIG_IRQ);
940 #endif
941 }
942
943 void TriggerNMI(void)
944 {
945 // not yet supported
946 }
947 #endif
948
949
950 /*
951 * XPRAM watchdog thread (saves XPRAM every minute)
952 */
953
954 static void xpram_watchdog(void)
955 {
956 if (memcmp(last_xpram, XPRAM, XPRAM_SIZE)) {
957 memcpy(last_xpram, XPRAM, XPRAM_SIZE);
958 SaveXPRAM();
959 }
960 }
961
962 #ifdef HAVE_PTHREADS
963 static void *xpram_func(void *arg)
964 {
965 while (!xpram_thread_cancel) {
966 for (int i=0; i<60 && !xpram_thread_cancel; i++)
967 Delay_usec(999999); // Only wait 1 second so we quit promptly when xpram_thread_cancel becomes true
968 xpram_watchdog();
969 }
970 return NULL;
971 }
972 #endif
973
974
975 /*
976 * 60Hz thread (really 60.15Hz)
977 */
978
979 static void one_second(void)
980 {
981 // Pseudo Mac 1Hz interrupt, update local time
982 WriteMacInt32(0x20c, TimerDateTime());
983
984 SetInterruptFlag(INTFLAG_1HZ);
985 TriggerInterrupt();
986
987 #ifndef HAVE_PTHREADS
988 static int second_counter = 0;
989 if (++second_counter > 60) {
990 second_counter = 0;
991 xpram_watchdog();
992 }
993 #endif
994 }
995
996 static void one_tick(...)
997 {
998 static int tick_counter = 0;
999 if (++tick_counter > 60) {
1000 tick_counter = 0;
1001 one_second();
1002 }
1003
1004 #ifndef HAVE_PTHREADS
1005 // No threads available, perform video refresh and networking from here
1006 VideoRefresh();
1007 SetInterruptFlag(INTFLAG_ETHER);
1008 #endif
1009
1010 // Trigger 60Hz interrupt
1011 if (ROMVersion != ROM_VERSION_CLASSIC || HasMacStarted()) {
1012 SetInterruptFlag(INTFLAG_60HZ);
1013 TriggerInterrupt();
1014 }
1015 }
1016
1017 #ifdef HAVE_PTHREADS
1018 static void *tick_func(void *arg)
1019 {
1020 uint64 start = GetTicks_usec();
1021 int64 ticks = 0;
1022 uint64 next = GetTicks_usec();
1023 while (!tick_thread_cancel) {
1024 one_tick();
1025 next += 16625;
1026 int64 delay = next - GetTicks_usec();
1027 if (delay > 0)
1028 Delay_usec(delay);
1029 else if (delay < -16625)
1030 next = GetTicks_usec();
1031 ticks++;
1032 }
1033 uint64 end = GetTicks_usec();
1034 D(bug("%Ld ticks in %Ld usec = %f ticks/sec\n", ticks, end - start, ticks * 1000000.0 / (end - start)));
1035 return NULL;
1036 }
1037 #endif
1038
1039
1040 #if !EMULATED_68K
1041 /*
1042 * Virtual 68k interrupt handler
1043 */
1044
1045 static void sigirq_handler(int sig, int code, struct sigcontext *scp)
1046 {
1047 // Interrupts disabled? Then do nothing
1048 if (EmulatedSR & 0x0700)
1049 return;
1050
1051 struct sigstate *state = (struct sigstate *)scp->sc_ap;
1052 M68kRegisters *regs = (M68kRegisters *)&state->ss_frame;
1053
1054 // Set up interrupt frame on stack
1055 uint32 a7 = regs->a[7];
1056 a7 -= 2;
1057 WriteMacInt16(a7, 0x64);
1058 a7 -= 4;
1059 WriteMacInt32(a7, scp->sc_pc);
1060 a7 -= 2;
1061 WriteMacInt16(a7, scp->sc_ps | EmulatedSR);
1062 scp->sc_sp = regs->a[7] = a7;
1063
1064 // Set interrupt level
1065 EmulatedSR |= 0x2100;
1066
1067 // Jump to MacOS interrupt handler on return
1068 scp->sc_pc = ReadMacInt32(0x64);
1069 }
1070
1071
1072 /*
1073 * SIGILL handler, for emulation of privileged instructions and executing
1074 * A-Trap and EMUL_OP opcodes
1075 */
1076
1077 static void sigill_handler(int sig, int code, struct sigcontext *scp)
1078 {
1079 struct sigstate *state = (struct sigstate *)scp->sc_ap;
1080 uint16 *pc = (uint16 *)scp->sc_pc;
1081 uint16 opcode = *pc;
1082 M68kRegisters *regs = (M68kRegisters *)&state->ss_frame;
1083
1084 #define INC_PC(n) scp->sc_pc += (n)
1085
1086 #define GET_SR (scp->sc_ps | EmulatedSR)
1087
1088 #define STORE_SR(v) \
1089 scp->sc_ps = (v) & 0xff; \
1090 EmulatedSR = (v) & 0xe700; \
1091 if (((v) & 0x0700) == 0 && InterruptFlags) \
1092 TriggerInterrupt();
1093
1094 //printf("opcode %04x at %p, sr %04x, emul_sr %04x\n", opcode, pc, scp->sc_ps, EmulatedSR);
1095
1096 if ((opcode & 0xf000) == 0xa000) {
1097
1098 // A-Line instruction, set up A-Line trap frame on stack
1099 uint32 a7 = regs->a[7];
1100 a7 -= 2;
1101 WriteMacInt16(a7, 0x28);
1102 a7 -= 4;
1103 WriteMacInt32(a7, (uint32)pc);
1104 a7 -= 2;
1105 WriteMacInt16(a7, GET_SR);
1106 scp->sc_sp = regs->a[7] = a7;
1107
1108 // Jump to MacOS A-Line handler on return
1109 scp->sc_pc = ReadMacInt32(0x28);
1110
1111 } else if ((opcode & 0xff00) == 0x7100) {
1112
1113 // Extended opcode, push registers on user stack
1114 uint32 a7 = regs->a[7];
1115 a7 -= 4;
1116 WriteMacInt32(a7, (uint32)pc);
1117 a7 -= 2;
1118 WriteMacInt16(a7, scp->sc_ps);
1119 for (int i=7; i>=0; i--) {
1120 a7 -= 4;
1121 WriteMacInt32(a7, regs->a[i]);
1122 }
1123 for (int i=7; i>=0; i--) {
1124 a7 -= 4;
1125 WriteMacInt32(a7, regs->d[i]);
1126 }
1127 scp->sc_sp = regs->a[7] = a7;
1128
1129 // Jump to EmulOp trampoline code on return
1130 scp->sc_pc = (uint32)EmulOpTrampoline;
1131
1132 } else switch (opcode) { // Emulate privileged instructions
1133
1134 case 0x40e7: // move sr,-(sp)
1135 regs->a[7] -= 2;
1136 WriteMacInt16(regs->a[7], GET_SR);
1137 scp->sc_sp = regs->a[7];
1138 INC_PC(2);
1139 break;
1140
1141 case 0x46df: { // move (sp)+,sr
1142 uint16 sr = ReadMacInt16(regs->a[7]);
1143 STORE_SR(sr);
1144 regs->a[7] += 2;
1145 scp->sc_sp = regs->a[7];
1146 INC_PC(2);
1147 break;
1148 }
1149
1150 case 0x007c: { // ori #xxxx,sr
1151 uint16 sr = GET_SR | pc[1];
1152 scp->sc_ps = sr & 0xff; // oring bits into the sr can't enable interrupts, so we don't need to call STORE_SR
1153 EmulatedSR = sr & 0xe700;
1154 INC_PC(4);
1155 break;
1156 }
1157
1158 case 0x027c: { // andi #xxxx,sr
1159 uint16 sr = GET_SR & pc[1];
1160 STORE_SR(sr);
1161 INC_PC(4);
1162 break;
1163 }
1164
1165 case 0x46fc: // move #xxxx,sr
1166 STORE_SR(pc[1]);
1167 INC_PC(4);
1168 break;
1169
1170 case 0x46ef: { // move (xxxx,sp),sr
1171 uint16 sr = ReadMacInt16(regs->a[7] + (int32)(int16)pc[1]);
1172 STORE_SR(sr);
1173 INC_PC(4);
1174 break;
1175 }
1176
1177 case 0x46d8: // move (a0)+,sr
1178 case 0x46d9: { // move (a1)+,sr
1179 uint16 sr = ReadMacInt16(regs->a[opcode & 7]);
1180 STORE_SR(sr);
1181 regs->a[opcode & 7] += 2;
1182 INC_PC(2);
1183 break;
1184 }
1185
1186 case 0x40f8: // move sr,xxxx.w
1187 WriteMacInt16(pc[1], GET_SR);
1188 INC_PC(4);
1189 break;
1190
1191 case 0x40d0: // move sr,(a0)
1192 case 0x40d1: // move sr,(a1)
1193 case 0x40d2: // move sr,(a2)
1194 case 0x40d3: // move sr,(a3)
1195 case 0x40d4: // move sr,(a4)
1196 case 0x40d5: // move sr,(a5)
1197 case 0x40d6: // move sr,(a6)
1198 case 0x40d7: // move sr,(sp)
1199 WriteMacInt16(regs->a[opcode & 7], GET_SR);
1200 INC_PC(2);
1201 break;
1202
1203 case 0x40c0: // move sr,d0
1204 case 0x40c1: // move sr,d1
1205 case 0x40c2: // move sr,d2
1206 case 0x40c3: // move sr,d3
1207 case 0x40c4: // move sr,d4
1208 case 0x40c5: // move sr,d5
1209 case 0x40c6: // move sr,d6
1210 case 0x40c7: // move sr,d7
1211 regs->d[opcode & 7] = GET_SR;
1212 INC_PC(2);
1213 break;
1214
1215 case 0x46c0: // move d0,sr
1216 case 0x46c1: // move d1,sr
1217 case 0x46c2: // move d2,sr
1218 case 0x46c3: // move d3,sr
1219 case 0x46c4: // move d4,sr
1220 case 0x46c5: // move d5,sr
1221 case 0x46c6: // move d6,sr
1222 case 0x46c7: { // move d7,sr
1223 uint16 sr = regs->d[opcode & 7];
1224 STORE_SR(sr);
1225 INC_PC(2);
1226 break;
1227 }
1228
1229 case 0xf327: // fsave -(sp)
1230 regs->a[7] -= 4;
1231 WriteMacInt32(regs->a[7], 0x41000000); // Idle frame
1232 scp->sc_sp = regs->a[7];
1233 INC_PC(2);
1234 break;
1235
1236 case 0xf35f: // frestore (sp)+
1237 regs->a[7] += 4;
1238 scp->sc_sp = regs->a[7];
1239 INC_PC(2);
1240 break;
1241
1242 case 0x4e73: { // rte
1243 uint32 a7 = regs->a[7];
1244 uint16 sr = ReadMacInt16(a7);
1245 a7 += 2;
1246 scp->sc_ps = sr & 0xff;
1247 EmulatedSR = sr & 0xe700;
1248 scp->sc_pc = ReadMacInt32(a7);
1249 a7 += 4;
1250 uint16 format = ReadMacInt16(a7) >> 12;
1251 a7 += 2;
1252 static const int frame_adj[16] = {
1253 0, 0, 4, 4, 8, 0, 0, 52, 50, 12, 24, 84, 16, 0, 0, 0
1254 };
1255 scp->sc_sp = regs->a[7] = a7 + frame_adj[format];
1256 break;
1257 }
1258
1259 case 0x4e7a: // movec cr,x
1260 switch (pc[1]) {
1261 case 0x0002: // movec cacr,d0
1262 regs->d[0] = 0x3111;
1263 break;
1264 case 0x1002: // movec cacr,d1
1265 regs->d[1] = 0x3111;
1266 break;
1267 case 0x0003: // movec tc,d0
1268 case 0x0004: // movec itt0,d0
1269 case 0x0005: // movec itt1,d0
1270 case 0x0006: // movec dtt0,d0
1271 case 0x0007: // movec dtt1,d0
1272 case 0x0806: // movec urp,d0
1273 case 0x0807: // movec srp,d0
1274 regs->d[0] = 0;
1275 break;
1276 case 0x1000: // movec sfc,d1
1277 case 0x1001: // movec dfc,d1
1278 case 0x1003: // movec tc,d1
1279 case 0x1801: // movec vbr,d1
1280 regs->d[1] = 0;
1281 break;
1282 case 0x8801: // movec vbr,a0
1283 regs->a[0] = 0;
1284 break;
1285 case 0x9801: // movec vbr,a1
1286 regs->a[1] = 0;
1287 break;
1288 default:
1289 goto ill;
1290 }
1291 INC_PC(4);
1292 break;
1293
1294 case 0x4e7b: // movec x,cr
1295 switch (pc[1]) {
1296 case 0x1000: // movec d1,sfc
1297 case 0x1001: // movec d1,dfc
1298 case 0x0801: // movec d0,vbr
1299 case 0x1801: // movec d1,vbr
1300 break;
1301 case 0x0002: // movec d0,cacr
1302 case 0x1002: // movec d1,cacr
1303 FlushCodeCache(NULL, 0);
1304 break;
1305 default:
1306 goto ill;
1307 }
1308 INC_PC(4);
1309 break;
1310
1311 case 0xf478: // cpusha dc
1312 case 0xf4f8: // cpusha dc/ic
1313 FlushCodeCache(NULL, 0);
1314 INC_PC(2);
1315 break;
1316
1317 default:
1318 ill: printf("SIGILL num %d, code %d\n", sig, code);
1319 printf(" context %p:\n", scp);
1320 printf(" onstack %08x\n", scp->sc_onstack);
1321 printf(" sp %08x\n", scp->sc_sp);
1322 printf(" fp %08x\n", scp->sc_fp);
1323 printf(" pc %08x\n", scp->sc_pc);
1324 printf(" opcode %04x\n", opcode);
1325 printf(" sr %08x\n", scp->sc_ps);
1326 printf(" state %p:\n", state);
1327 printf(" flags %d\n", state->ss_flags);
1328 for (int i=0; i<8; i++)
1329 printf(" d%d %08x\n", i, state->ss_frame.f_regs[i]);
1330 for (int i=0; i<8; i++)
1331 printf(" a%d %08x\n", i, state->ss_frame.f_regs[i+8]);
1332
1333 VideoQuitFullScreen();
1334 #ifdef ENABLE_MON
1335 char *arg[4] = {"mon", "-m", "-r", NULL};
1336 mon(3, arg);
1337 #endif
1338 QuitEmulator();
1339 break;
1340 }
1341 }
1342 #endif
1343
1344
1345 /*
1346 * Display alert
1347 */
1348
1349 #ifdef ENABLE_GTK
1350 static void dl_destroyed(void)
1351 {
1352 gtk_main_quit();
1353 }
1354
1355 static void dl_quit(GtkWidget *dialog)
1356 {
1357 gtk_widget_destroy(dialog);
1358 }
1359
1360 void display_alert(int title_id, int prefix_id, int button_id, const char *text)
1361 {
1362 char str[256];
1363 sprintf(str, GetString(prefix_id), text);
1364
1365 GtkWidget *dialog = gtk_dialog_new();
1366 gtk_window_set_title(GTK_WINDOW(dialog), GetString(title_id));
1367 gtk_container_border_width(GTK_CONTAINER(dialog), 5);
1368 gtk_widget_set_uposition(GTK_WIDGET(dialog), 100, 150);
1369 gtk_signal_connect(GTK_OBJECT(dialog), "destroy", GTK_SIGNAL_FUNC(dl_destroyed), NULL);
1370
1371 GtkWidget *label = gtk_label_new(str);
1372 gtk_widget_show(label);
1373 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label, TRUE, TRUE, 0);
1374
1375 GtkWidget *button = gtk_button_new_with_label(GetString(button_id));
1376 gtk_widget_show(button);
1377 gtk_signal_connect_object(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(dl_quit), GTK_OBJECT(dialog));
1378 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area), button, FALSE, FALSE, 0);
1379 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1380 gtk_widget_grab_default(button);
1381 gtk_widget_show(dialog);
1382
1383 gtk_main();
1384 }
1385 #endif
1386
1387
1388 /*
1389 * Display error alert
1390 */
1391
1392 void ErrorAlert(const char *text)
1393 {
1394 #ifdef ENABLE_GTK
1395 if (PrefsFindBool("nogui") || x_display == NULL) {
1396 printf(GetString(STR_SHELL_ERROR_PREFIX), text);
1397 return;
1398 }
1399 VideoQuitFullScreen();
1400 display_alert(STR_ERROR_ALERT_TITLE, STR_GUI_ERROR_PREFIX, STR_QUIT_BUTTON, text);
1401 #else
1402 printf(GetString(STR_SHELL_ERROR_PREFIX), text);
1403 #endif
1404 }
1405
1406
1407 /*
1408 * Display warning alert
1409 */
1410
1411 void WarningAlert(const char *text)
1412 {
1413 #ifdef ENABLE_GTK
1414 if (PrefsFindBool("nogui") || x_display == NULL) {
1415 printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1416 return;
1417 }
1418 display_alert(STR_WARNING_ALERT_TITLE, STR_GUI_WARNING_PREFIX, STR_OK_BUTTON, text);
1419 #else
1420 printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1421 #endif
1422 }
1423
1424
1425 /*
1426 * Display choice alert
1427 */
1428
1429 bool ChoiceAlert(const char *text, const char *pos, const char *neg)
1430 {
1431 printf(GetString(STR_SHELL_WARNING_PREFIX), text);
1432 return false; //!!
1433 }