filename | src/cocoaui/cocoaui.m |
changeset | 1109:700c5ab26a63 |
prev | 1098:4f2750753a6c |
next | 1298:d0eb2307b847 |
author | nkeynes |
date | Sat Aug 04 08:46:28 2012 +1000 (11 years ago) |
permissions | -rw-r--r-- |
last change | Handle corner case in pvr2_run_slice when we've previously slightly overrun the end of the time slice |
view | annotate | diff | log | raw |
1 /**
2 * $Id$
3 *
4 * Core Cocoa-based user interface
5 *
6 * Copyright (c) 2008 Nathan Keynes.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 */
19 #include <AppKit/AppKit.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/time.h>
24 #include "lxdream.h"
25 #include "dream.h"
26 #include "dreamcast.h"
27 #include "config.h"
28 #include "lxpaths.h"
29 #include "display.h"
30 #include "gui.h"
31 #include "gdrom/gdrom.h"
32 #include "gdlist.h"
33 #include "loader.h"
34 #include "cocoaui/cocoaui.h"
36 void cocoa_gui_update( void );
37 void cocoa_gui_start( void );
38 void cocoa_gui_stop( void );
39 uint32_t cocoa_gui_run_slice( uint32_t nanosecs );
41 struct dreamcast_module cocoa_gui_module = { "gui", NULL,
42 cocoa_gui_update,
43 cocoa_gui_start,
44 cocoa_gui_run_slice,
45 cocoa_gui_stop,
46 NULL, NULL };
48 /**
49 * Count of running nanoseconds - used to cut back on the GUI runtime
50 */
51 static uint32_t cocoa_gui_nanos = 0;
52 static uint32_t cocoa_gui_ticks = 0;
53 static struct timeval cocoa_gui_lasttv;
54 static BOOL cocoa_gui_autorun = NO;
55 static BOOL cocoa_gui_is_running = NO;
56 static LxdreamMainWindow *mainWindow = NULL;
58 @interface NSApplication (PrivateAdditions)
59 - (void) setAppleMenu:(NSMenu *)aMenu;
60 @end
62 gboolean cocoa_gui_disc_changed( cdrom_disc_t disc, const gchar *disc_name, void *user_data )
63 {
64 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
65 LxdreamMainWindow *window = (LxdreamMainWindow *)user_data;
66 [window updateTitle];
67 [pool release];
68 return TRUE;
69 }
71 /**
72 * Produces the menu title by looking the text up in gettext, removing any
73 * underscores, and returning the result as an NSString.
74 */
75 static NSString *NSMENU_( const char *text )
76 {
77 const char *s = gettext(text);
78 char buf[strlen(s)+1];
79 char *d = buf;
81 while( *s != '\0' ) {
82 if( *s != '_' ) {
83 *d++ = *s;
84 }
85 s++;
86 }
87 *d = '\0';
89 return [NSString stringWithUTF8String: buf];
90 }
92 static void cocoa_gui_create_menu(void)
93 {
94 int i;
95 NSMenu *appleMenu, *services;
96 NSMenuItem *menuItem;
97 NSString *title;
98 NSString *appName;
100 appName = @"Lxdream";
101 appleMenu = [[NSMenu alloc] initWithTitle:@""];
103 /* Add menu items */
104 title = [@"About " stringByAppendingString:appName];
105 [appleMenu addItemWithTitle:title action:@selector(about_action:) keyEquivalent:@""];
107 [appleMenu addItem:[NSMenuItem separatorItem]];
108 [appleMenu addItemWithTitle: NSMENU_("_Preferences...") action:@selector(preferences_action:) keyEquivalent:@","];
110 // Services Menu
111 [appleMenu addItem:[NSMenuItem separatorItem]];
112 services = [[[NSMenu alloc] init] autorelease];
113 [appleMenu addItemWithTitle: NS_("Services") action:nil keyEquivalent:@""];
114 [appleMenu setSubmenu: services forItem: [appleMenu itemWithTitle: @"Services"]];
116 // Hide AppName
117 title = [@"Hide " stringByAppendingString:appName];
118 [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
120 // Hide Others
121 menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others"
122 action:@selector(hideOtherApplications:)
123 keyEquivalent:@"h"];
124 [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
126 // Show All
127 [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
128 [appleMenu addItem:[NSMenuItem separatorItem]];
130 // Quit AppName
131 title = [@"Quit " stringByAppendingString:appName];
132 [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
134 /* Put menu into the menubar */
135 menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
136 [menuItem setSubmenu: appleMenu];
137 NSMenu *menu = [NSMenu new];
138 [menu addItem: menuItem];
140 NSMenu *gdromMenu = cocoa_gdrom_menu_new();
142 NSMenu *quickStateMenu = [[NSMenu alloc] initWithTitle:NSMENU_("_Quick State")];
143 int quickState = dreamcast_get_quick_state();
144 for( i=0; i<=MAX_QUICK_STATE; i++ ) {
145 NSString *label = [NSString stringWithFormat: NSMENU_("State _%d"), i];
146 NSString *keyEquiv = [NSString stringWithFormat: @"%d", i];
147 menuItem = [[NSMenuItem alloc] initWithTitle: label action: @selector(quick_state_action:) keyEquivalent: keyEquiv];
148 [menuItem setTag: i];
149 if( i == quickState ) {
150 [menuItem setState:NSOnState];
151 }
152 [quickStateMenu addItem: menuItem];
153 }
155 NSMenu *fileMenu = [[NSMenu alloc] initWithTitle: NSMENU_("_File")];
156 [fileMenu addItemWithTitle: NSMENU_("Load _Binary...") action: @selector(load_binary_action:) keyEquivalent: @"b"];
157 [[fileMenu addItemWithTitle: NSMENU_("_GD-Rom") action: nil keyEquivalent: @""]
158 setSubmenu: gdromMenu];
159 [fileMenu addItem: [NSMenuItem separatorItem]];
160 [[fileMenu addItemWithTitle: NSMENU_("_Reset") action: @selector(reset_action:) keyEquivalent: @"r"]
161 setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
162 [fileMenu addItemWithTitle: NSMENU_("_Pause") action: @selector(pause_action:) keyEquivalent: @"p"];
163 [fileMenu addItemWithTitle: NS_("Resume") action: @selector(run_action:) keyEquivalent: @"r"];
164 [fileMenu addItem: [NSMenuItem separatorItem]];
165 [fileMenu addItemWithTitle: NSMENU_("L_oad State...") action: @selector(load_action:) keyEquivalent: @"o"];
166 [fileMenu addItemWithTitle: NSMENU_("S_ave State...") action: @selector(save_action:) keyEquivalent: @"a"];
167 menuItem = [[NSMenuItem alloc] initWithTitle:NSMENU_("Select _Quick State") action: nil keyEquivalent: @""];
168 [fileMenu addItem: [NSMenuItem separatorItem]];
169 [fileMenu addItemWithTitle: NSMENU_("_Load Quick State") action: @selector(quick_load_action:) keyEquivalent: @"l"];
170 [fileMenu addItemWithTitle: NSMENU_("_Save Quick State") action: @selector(quick_save_action:) keyEquivalent: @"s"];
171 [menuItem setSubmenu: quickStateMenu];
172 [fileMenu addItem: menuItem];
173 [fileMenu addItem: [NSMenuItem separatorItem]];
174 [fileMenu addItemWithTitle: NSMENU_("_Full Screen...") action: @selector(fullscreen_action:) keyEquivalent: @"\r"];
176 menuItem = [[NSMenuItem alloc] initWithTitle:NSMENU_("_File") action: nil keyEquivalent: @""];
177 [menuItem setSubmenu: fileMenu];
178 [menu addItem: menuItem];
180 /* Tell the application object that this is now the application menu */
181 [NSApp setMainMenu: menu];
182 [NSApp setAppleMenu: appleMenu];
183 [NSApp setServicesMenu: services];
185 /* Finally give up our references to the objects */
186 [appleMenu release];
187 [menuItem release];
188 [menu release];
189 }
191 @interface LxdreamDelegate : NSObject
192 @end
194 @implementation LxdreamDelegate
195 - (void)windowWillClose: (NSNotification *)notice
196 {
197 dreamcast_shutdown();
198 exit(0);
199 }
200 - (void)windowDidBecomeMain: (NSNotification *)notice
201 {
202 if( cocoa_gui_autorun ) {
203 cocoa_gui_autorun = NO;
204 gui_do_later(dreamcast_run);
205 }
206 }
207 - (void)windowDidBecomeKey: (NSNotification *)notice
208 {
209 display_set_focused( TRUE );
210 }
211 - (void)windowDidResignKey: (NSNotification *)notice
212 {
213 display_set_focused( FALSE );
214 [mainWindow setIsGrabbed: NO];
215 }
216 - (BOOL)application: (NSApplication *)app openFile: (NSString *)filename
217 {
218 const gchar *cname = [filename UTF8String];
219 ERROR err;
220 if( file_load_magic(cname, FALSE, &err) != FILE_ERROR ) {
221 // Queue up a run event
222 gui_do_later(dreamcast_run);
223 return YES;
224 } else {
225 return NO;
226 }
228 }
229 - (void) about_action: (id)sender
230 {
231 NSArray *keys = [NSArray arrayWithObjects: @"Version", @"Copyright", nil];
232 NSArray *values = [NSArray arrayWithObjects: NS_(lxdream_full_version), NS_(lxdream_copyright), nil];
234 NSDictionary *options= [NSDictionary dictionaryWithObjects: values forKeys: keys];
236 [NSApp orderFrontStandardAboutPanelWithOptions: options];
237 }
238 - (void) preferences_action: (id)sender
239 {
240 cocoa_gui_show_preferences();
241 }
242 - (void) load_action: (id)sender
243 {
244 NSOpenPanel *panel = [NSOpenPanel openPanel];
245 NSString *path = [NSString stringWithCString: get_gui_path(CONFIG_SAVE_PATH)];
246 NSArray *fileTypes = [NSArray arrayWithObject: @"dst"];
247 int result = [panel runModalForDirectory: path file: nil types: fileTypes];
248 if( result == NSOKButton && [[panel filenames] count] > 0 ) {
249 NSString *filename = [[panel filenames] objectAtIndex: 0];
250 dreamcast_load_state( [filename UTF8String] );
251 set_gui_path(CONFIG_SAVE_PATH, [[panel directory] UTF8String]);
252 }
253 }
254 - (void) save_action: (id)sender
255 {
256 NSSavePanel *panel = [NSSavePanel savePanel];
257 NSString *path = [NSString stringWithCString: get_gui_path(CONFIG_SAVE_PATH)];
258 [panel setRequiredFileType: @"dst"];
259 int result = [panel runModalForDirectory: path file:@""];
260 if( result == NSOKButton ) {
261 NSString *filename = [panel filename];
262 dreamcast_save_state( [filename UTF8String] );
263 set_gui_path(CONFIG_SAVE_PATH, [[panel directory] UTF8String]);
264 }
265 }
266 - (void) load_binary_action: (id)sender
267 {
268 NSOpenPanel *panel = [NSOpenPanel openPanel];
269 NSString *path = [NSString stringWithCString: get_gui_path(CONFIG_DEFAULT_PATH)];
270 int result = [panel runModalForDirectory: path file: nil types: nil];
271 if( result == NSOKButton && [[panel filenames] count] > 0 ) {
272 NSString *filename = [[panel filenames] objectAtIndex: 0];
273 ERROR err;
274 gboolean ok = file_load_exec( [filename UTF8String], &err );
275 set_gui_path(CONFIG_DEFAULT_PATH, [[panel directory] UTF8String]);
276 if( !ok ) {
277 ERROR( err.msg );
278 }
279 }
280 }
281 - (void) mount_action: (id)sender
282 {
283 NSOpenPanel *panel = [NSOpenPanel openPanel];
284 NSString *path = [NSString stringWithCString: get_gui_path(CONFIG_DEFAULT_PATH)];
285 int result = [panel runModalForDirectory: path file: nil types: nil];
286 if( result == NSOKButton && [[panel filenames] count] > 0 ) {
287 ERROR err;
288 NSString *filename = [[panel filenames] objectAtIndex: 0];
289 gboolean ok = gdrom_mount_image( [filename UTF8String], &err );
290 if( !ok ) {
291 ERROR(err.msg);
292 }
293 set_gui_path(CONFIG_DEFAULT_PATH, [[panel directory] UTF8String]);
294 }
295 }
296 - (void) pause_action: (id)sender
297 {
298 dreamcast_stop();
299 }
301 - (void) reset_action: (id)sender
302 {
303 dreamcast_reset();
304 }
305 - (void) run_action: (id)sender
306 {
307 if( !dreamcast_is_running() ) {
308 gui_do_later(dreamcast_run);
309 }
310 }
311 - (void) gdrom_list_action: (id)sender
312 {
313 ERROR err;
314 gboolean ok = gdrom_list_set_selection( [sender tag], &err );
315 if( !ok ) {
316 ERROR( err.msg );
317 }
318 }
319 - (void) fullscreen_action: (id)sender
320 {
321 [mainWindow setFullscreen: ![mainWindow isFullscreen]];
322 }
323 - (void) quick_state_action: (id)sender
324 {
325 [[[sender menu] itemWithTag: dreamcast_get_quick_state()] setState: NSOffState ];
326 [sender setState: NSOnState ];
327 dreamcast_set_quick_state( [sender tag] );
328 }
329 - (void) quick_save_action: (id)sender
330 {
331 dreamcast_quick_save();
332 }
333 - (void) quick_load_action: (id)sender
334 {
335 dreamcast_quick_load();
336 }
337 @end
340 gboolean gui_parse_cmdline( int *argc, char **argv[] )
341 {
342 /* If started from the finder, the first (and only) arg will look something like
343 * -psn_0_... - we want to remove this so that lxdream doesn't try to process it
344 * normally
345 */
346 if( *argc == 2 && strncmp((*argv)[1], "-psn_", 5) == 0 ) {
347 *argc = 1;
348 }
349 return TRUE;
350 }
352 gboolean gui_init( gboolean withDebug, gboolean withFullscreen )
353 {
354 dreamcast_register_module( &cocoa_gui_module );
356 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
357 [NSApplication sharedApplication];
359 LxdreamDelegate *delegate = [[LxdreamDelegate alloc] init];
360 [NSApp setDelegate: delegate];
361 NSString *iconFile = [[NSBundle mainBundle] pathForResource:@"lxdream" ofType:@"png"];
362 NSImage *iconImage = [[NSImage alloc] initWithContentsOfFile: iconFile];
363 [iconImage setName: @"NSApplicationIcon"];
364 [NSApp setApplicationIconImage: iconImage];
365 cocoa_gui_create_menu();
366 mainWindow = cocoa_gui_create_main_window();
367 [mainWindow makeKeyAndOrderFront: nil];
368 [NSApp activateIgnoringOtherApps: YES];
370 register_gdrom_disc_change_hook( cocoa_gui_disc_changed, mainWindow );
371 if( withFullscreen ) {
372 [mainWindow setFullscreen: YES];
373 }
374 [pool release];
375 return TRUE;
376 }
378 void gui_main_loop( gboolean run )
379 {
380 if( run ) {
381 cocoa_gui_autorun = YES;
382 }
383 cocoa_gui_is_running = YES;
384 [NSApp run];
385 cocoa_gui_is_running = NO;
386 }
388 void gui_update_state(void)
389 {
390 cocoa_gui_update();
391 }
393 void gui_set_use_grab( gboolean grab )
394 {
395 [mainWindow setUseGrab: (grab ? YES : NO)];
396 }
398 gboolean gui_error_dialog( const char *msg, ... )
399 {
400 if( cocoa_gui_is_running ) {
401 NSString *error_string;
403 va_list args;
404 va_start(args, msg);
405 error_string = [[NSString alloc] initWithFormat: [NSString stringWithCString: msg] arguments: args];
406 NSRunAlertPanel(NS_("Error in Lxdream"), error_string, nil, nil, nil);
407 va_end(args);
408 return TRUE;
409 } else {
410 return FALSE;
411 }
412 }
414 void gui_update_io_activity( io_activity_type io, gboolean active )
415 {
417 }
420 uint32_t cocoa_gui_run_slice( uint32_t nanosecs )
421 {
422 NSEvent *event;
423 NSAutoreleasePool *pool;
425 cocoa_gui_nanos += nanosecs;
426 if( cocoa_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
427 cocoa_gui_nanos -= GUI_TICK_PERIOD;
428 cocoa_gui_ticks ++;
429 uint32_t current_period = cocoa_gui_ticks * GUI_TICK_PERIOD;
431 // Run the event loop
432 pool = [NSAutoreleasePool new];
433 while( (event = [NSApp nextEventMatchingMask: NSAnyEventMask untilDate: nil
434 inMode: NSDefaultRunLoopMode dequeue: YES]) != nil ) {
435 [NSApp sendEvent: event];
436 }
437 [pool release];
439 struct timeval tv;
440 gettimeofday(&tv,NULL);
441 uint32_t ns = ((tv.tv_sec - cocoa_gui_lasttv.tv_sec) * 1000000000) +
442 (tv.tv_usec - cocoa_gui_lasttv.tv_usec)*1000;
443 if( (ns * 1.05) < current_period ) {
444 // We've gotten ahead - sleep for a little bit
445 struct timespec tv;
446 tv.tv_sec = 0;
447 tv.tv_nsec = current_period - ns;
448 nanosleep(&tv, &tv);
449 }
451 /* Update the display every 10 ticks (ie 10 times a second) and
452 * save the current tv value */
453 if( cocoa_gui_ticks > 10 ) {
454 gchar buf[32];
455 cocoa_gui_ticks -= 10;
457 double speed = (float)( (double)current_period * 100.0 / ns );
458 cocoa_gui_lasttv.tv_sec = tv.tv_sec;
459 cocoa_gui_lasttv.tv_usec = tv.tv_usec;
460 snprintf( buf, 32, _("Running (%2.4f%%)"), speed );
461 [mainWindow setStatusText: buf];
463 }
464 }
465 return nanosecs;
466 }
468 void cocoa_gui_update( void )
469 {
471 }
473 void cocoa_gui_start( void )
474 {
475 [mainWindow setRunning: YES];
476 cocoa_gui_nanos = 0;
477 gettimeofday(&cocoa_gui_lasttv,NULL);
478 }
480 void cocoa_gui_stop( void )
481 {
482 [mainWindow setRunning: NO];
483 }
485 @interface DoLaterStub : NSObject
486 {
487 do_later_callback_t func;
488 }
489 @end
491 @implementation DoLaterStub
492 - (id) init: (do_later_callback_t)f
493 {
494 [super init];
495 func = f;
496 return self;
497 }
498 - (void) do
499 {
500 func();
501 }
502 @end
504 /**
505 * Queue a dreamcast_run() to execute after the currently event(s)
506 */
507 void gui_do_later( do_later_callback_t func )
508 {
509 DoLaterStub *stub = [[[DoLaterStub alloc] init: func] autorelease];
510 [[NSRunLoop currentRunLoop] performSelector: @selector(do)
511 target: stub argument: nil order: 1
512 modes: [NSArray arrayWithObject: NSDefaultRunLoopMode] ];
513 }
515 /*************************** Convenience methods ***************************/
517 NSImage *NSImage_new_from_framebuffer( frame_buffer_t buffer )
518 {
519 NSBitmapImageRep *rep =
520 [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: &buffer->data
521 pixelsWide: buffer->width pixelsHigh: buffer->height
522 bitsPerSample: 8 samplesPerPixel: 3
523 hasAlpha: NO isPlanar: NO
524 colorSpaceName: NSDeviceRGBColorSpace bitmapFormat: 0
525 bytesPerRow: buffer->rowstride bitsPerPixel: 24];
527 NSImage *image = [[NSImage alloc] initWithSize: NSMakeSize(0.0,0.0)];
528 [image addRepresentation: rep];
529 return image;
530 }
533 NSTextField *cocoa_gui_add_label( NSView *parent, NSString *text, NSRect frame )
534 {
535 NSTextField *label = [[NSTextField alloc] initWithFrame: frame];
536 [label setStringValue: text];
537 [label setBordered: NO];
538 [label setDrawsBackground: NO];
539 [label setEditable: NO];
540 [label setAutoresizingMask: (NSViewMinYMargin|NSViewMaxXMargin)];
541 if( parent != NULL ) {
542 [parent addSubview: label];
543 }
544 return label;
545 }
.