4 * Core Cocoa-based user interface
6 * Copyright (c) 2008 Nathan Keynes.
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.
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.
19 #include <AppKit/AppKit.h>
26 #include "dreamcast.h"
30 #include "gdrom/gdrom.h"
33 #include "cocoaui/cocoaui.h"
35 void cocoa_gui_update( void );
36 void cocoa_gui_start( void );
37 void cocoa_gui_stop( void );
38 uint32_t cocoa_gui_run_slice( uint32_t nanosecs );
40 struct dreamcast_module cocoa_gui_module = { "gui", NULL,
48 * Count of running nanoseconds - used to cut back on the GUI runtime
50 static uint32_t cocoa_gui_nanos = 0;
51 static uint32_t cocoa_gui_ticks = 0;
52 static struct timeval cocoa_gui_lasttv;
53 static BOOL cocoa_gui_autorun = NO;
54 static BOOL cocoa_gui_is_running = NO;
55 static LxdreamMainWindow *mainWindow = NULL;
57 @interface NSApplication (PrivateAdditions)
58 - (void) setAppleMenu:(NSMenu *)aMenu;
61 gboolean cocoa_gui_disc_changed( gdrom_disc_t disc, const gchar *disc_name, void *user_data )
63 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
64 LxdreamMainWindow *window = (LxdreamMainWindow *)user_data;
71 * Produces the menu title by looking the text up in gettext, removing any
72 * underscores, and returning the result as an NSString.
74 static NSString *NSMENU_( const char *text )
76 const char *s = gettext(text);
77 char buf[strlen(s)+1];
88 return [NSString stringWithUTF8String: buf];
91 static void cocoa_gui_create_menu(void)
93 NSMenu *appleMenu, *services;
99 appleMenu = [[NSMenu alloc] initWithTitle:@""];
102 title = [@"About " stringByAppendingString:appName];
103 [appleMenu addItemWithTitle:title action:@selector(about_action:) keyEquivalent:@""];
105 [appleMenu addItem:[NSMenuItem separatorItem]];
106 [appleMenu addItemWithTitle: NSMENU_("_Preferences...") action:@selector(preferences_action:) keyEquivalent:@","];
109 [appleMenu addItem:[NSMenuItem separatorItem]];
110 services = [[[NSMenu alloc] init] autorelease];
111 [appleMenu addItemWithTitle: NS_("Services") action:nil keyEquivalent:@""];
112 [appleMenu setSubmenu: services forItem: [appleMenu itemWithTitle: @"Services"]];
115 title = [@"Hide " stringByAppendingString:appName];
116 [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
119 menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others"
120 action:@selector(hideOtherApplications:)
122 [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
125 [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
126 [appleMenu addItem:[NSMenuItem separatorItem]];
129 title = [@"Quit " stringByAppendingString:appName];
130 [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
132 /* Put menu into the menubar */
133 menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
134 [menuItem setSubmenu: appleMenu];
135 NSMenu *menu = [NSMenu new];
136 [menu addItem: menuItem];
138 NSMenu *gdromMenu = cocoa_gdrom_menu_new();
140 NSMenu *fileMenu = [[NSMenu alloc] initWithTitle: NSMENU_("_File")];
141 [fileMenu addItemWithTitle: NSMENU_("Load _Binary...") action: @selector(load_binary_action:) keyEquivalent: @"b"];
142 [[fileMenu addItemWithTitle: NSMENU_("_GD-Rom") action: nil keyEquivalent: @""]
143 setSubmenu: gdromMenu];
144 [fileMenu addItem: [NSMenuItem separatorItem]];
145 [[fileMenu addItemWithTitle: NSMENU_("_Reset") action: @selector(reset_action:) keyEquivalent: @"r"]
146 setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
147 [fileMenu addItemWithTitle: NSMENU_("_Pause") action: @selector(pause_action:) keyEquivalent: @"p"];
148 [fileMenu addItemWithTitle: NS_("Resume") action: @selector(run_action:) keyEquivalent: @"r"];
149 [fileMenu addItem: [NSMenuItem separatorItem]];
150 [fileMenu addItemWithTitle: NSMENU_("_Load State...") action: @selector(load_action:) keyEquivalent: @"o"];
151 [fileMenu addItemWithTitle: NSMENU_("_Save State...") action: @selector(save_action:) keyEquivalent: @"s"];
152 [fileMenu addItem: [NSMenuItem separatorItem]];
153 [fileMenu addItemWithTitle: NSMENU_("_Full Screen...") action: @selector(fullscreen_action:) keyEquivalent: @"\r"];
155 menuItem = [[NSMenuItem alloc] initWithTitle:NSMENU_("_File") action: nil keyEquivalent: @""];
156 [menuItem setSubmenu: fileMenu];
157 [menu addItem: menuItem];
159 /* Tell the application object that this is now the application menu */
160 [NSApp setMainMenu: menu];
161 [NSApp setAppleMenu: appleMenu];
162 [NSApp setServicesMenu: services];
164 /* Finally give up our references to the objects */
170 @interface LxdreamDelegate : NSObject
173 @implementation LxdreamDelegate
174 - (void)windowWillClose: (NSNotification *)notice
176 dreamcast_shutdown();
179 - (void)windowDidBecomeMain: (NSNotification *)notice
181 if( cocoa_gui_autorun ) {
182 cocoa_gui_autorun = NO;
183 gui_do_later(dreamcast_run);
186 - (void)windowDidBecomeKey: (NSNotification *)notice
188 display_set_focused( TRUE );
190 - (void)windowDidResignKey: (NSNotification *)notice
192 display_set_focused( FALSE );
193 [mainWindow setIsGrabbed: NO];
195 - (BOOL)application: (NSApplication *)app openFile: (NSString *)filename
197 const gchar *cname = [filename UTF8String];
198 if( file_load_magic(cname) ) {
199 // Queue up a run event
200 gui_do_later(dreamcast_run);
207 - (void) about_action: (id)sender
209 NSArray *keys = [NSArray arrayWithObjects: @"Version", @"Copyright", nil];
210 NSArray *values = [NSArray arrayWithObjects: NS_(lxdream_full_version), NS_(lxdream_copyright), nil];
212 NSDictionary *options= [NSDictionary dictionaryWithObjects: values forKeys: keys];
214 [NSApp orderFrontStandardAboutPanelWithOptions: options];
216 - (void) preferences_action: (id)sender
218 cocoa_gui_show_preferences();
220 - (void) load_action: (id)sender
222 NSOpenPanel *panel = [NSOpenPanel openPanel];
223 const gchar *dir = lxdream_get_config_value(CONFIG_SAVE_PATH);
224 NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
225 NSArray *fileTypes = [NSArray arrayWithObject: @"dst"];
226 int result = [panel runModalForDirectory: path file: nil types: fileTypes];
227 if( result == NSOKButton && [[panel filenames] count] > 0 ) {
228 NSString *filename = [[panel filenames] objectAtIndex: 0];
229 dreamcast_load_state( [filename UTF8String] );
232 - (void) save_action: (id)sender
234 NSSavePanel *panel = [NSSavePanel savePanel];
235 const gchar *dir = lxdream_get_config_value(CONFIG_SAVE_PATH);
236 NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
237 [panel setRequiredFileType: @"dst"];
238 int result = [panel runModalForDirectory: path file:@""];
239 if( result == NSOKButton ) {
240 NSString *filename = [panel filename];
241 dreamcast_save_state( [filename UTF8String] );
244 - (void) load_binary_action: (id)sender
246 NSOpenPanel *panel = [NSOpenPanel openPanel];
247 const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
248 NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
249 int result = [panel runModalForDirectory: path file: nil types: nil];
250 if( result == NSOKButton && [[panel filenames] count] > 0 ) {
251 NSString *filename = [[panel filenames] objectAtIndex: 0];
252 file_load_magic( [filename UTF8String] );
255 - (void) mount_action: (id)sender
257 NSOpenPanel *panel = [NSOpenPanel openPanel];
258 const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
259 NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
260 int result = [panel runModalForDirectory: path file: nil types: nil];
261 if( result == NSOKButton && [[panel filenames] count] > 0 ) {
262 NSString *filename = [[panel filenames] objectAtIndex: 0];
263 gdrom_mount_image( [filename UTF8String] );
266 - (void) pause_action: (id)sender
271 - (void) reset_action: (id)sender
275 - (void) run_action: (id)sender
277 if( !dreamcast_is_running() ) {
278 gui_do_later(dreamcast_run);
281 - (void) gdrom_list_action: (id)sender
283 gdrom_list_set_selection( [sender tag] );
285 - (void) fullscreen_action: (id)sender
287 [mainWindow setFullscreen: ![mainWindow isFullscreen]];
292 gboolean gui_parse_cmdline( int *argc, char **argv[] )
294 /* If started from the finder, the first (and only) arg will look something like
295 * -psn_0_... - we want to remove this so that lxdream doesn't try to process it
298 if( *argc == 2 && strncmp((*argv)[1], "-psn_", 5) == 0 ) {
304 gboolean gui_init( gboolean withDebug, gboolean withFullscreen )
306 dreamcast_register_module( &cocoa_gui_module );
308 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
309 [NSApplication sharedApplication];
311 LxdreamDelegate *delegate = [[LxdreamDelegate alloc] init];
312 [NSApp setDelegate: delegate];
313 NSString *iconFile = [[NSBundle mainBundle] pathForResource:@"lxdream" ofType:@"png"];
314 NSImage *iconImage = [[NSImage alloc] initWithContentsOfFile: iconFile];
315 [iconImage setName: @"NSApplicationIcon"];
316 [NSApp setApplicationIconImage: iconImage];
317 cocoa_gui_create_menu();
318 mainWindow = cocoa_gui_create_main_window();
319 [mainWindow makeKeyAndOrderFront: nil];
320 [NSApp activateIgnoringOtherApps: YES];
322 register_gdrom_disc_change_hook( cocoa_gui_disc_changed, mainWindow );
323 if( withFullscreen ) {
324 [mainWindow setFullscreen: YES];
330 void gui_main_loop( gboolean run )
333 cocoa_gui_autorun = YES;
335 cocoa_gui_is_running = YES;
337 cocoa_gui_is_running = NO;
340 void gui_update_state(void)
345 void gui_set_use_grab( gboolean grab )
347 [mainWindow setUseGrab: (grab ? YES : NO)];
350 gboolean gui_error_dialog( const char *msg, ... )
352 if( cocoa_gui_is_running ) {
353 NSString *error_string;
357 error_string = [[NSString alloc] initWithFormat: [NSString stringWithCString: msg] arguments: args];
358 NSRunAlertPanel(NS_("Error in Lxdream"), error_string, nil, nil, nil);
366 void gui_update_io_activity( io_activity_type io, gboolean active )
372 uint32_t cocoa_gui_run_slice( uint32_t nanosecs )
375 NSAutoreleasePool *pool;
377 cocoa_gui_nanos += nanosecs;
378 if( cocoa_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
379 cocoa_gui_nanos -= GUI_TICK_PERIOD;
381 uint32_t current_period = cocoa_gui_ticks * GUI_TICK_PERIOD;
383 // Run the event loop
384 pool = [NSAutoreleasePool new];
385 while( (event = [NSApp nextEventMatchingMask: NSAnyEventMask untilDate: nil
386 inMode: NSDefaultRunLoopMode dequeue: YES]) != nil ) {
387 [NSApp sendEvent: event];
392 gettimeofday(&tv,NULL);
393 uint32_t ns = ((tv.tv_sec - cocoa_gui_lasttv.tv_sec) * 1000000000) +
394 (tv.tv_usec - cocoa_gui_lasttv.tv_usec)*1000;
395 if( (ns * 1.05) < current_period ) {
396 // We've gotten ahead - sleep for a little bit
399 tv.tv_nsec = current_period - ns;
403 /* Update the display every 10 ticks (ie 10 times a second) and
404 * save the current tv value */
405 if( cocoa_gui_ticks > 10 ) {
407 cocoa_gui_ticks -= 10;
409 double speed = (float)( (double)current_period * 100.0 / ns );
410 cocoa_gui_lasttv.tv_sec = tv.tv_sec;
411 cocoa_gui_lasttv.tv_usec = tv.tv_usec;
412 snprintf( buf, 32, _("Running (%2.4f%%)"), speed );
413 [mainWindow setStatusText: buf];
420 void cocoa_gui_update( void )
425 void cocoa_gui_start( void )
427 [mainWindow setRunning: YES];
429 gettimeofday(&cocoa_gui_lasttv,NULL);
432 void cocoa_gui_stop( void )
434 [mainWindow setRunning: NO];
437 @interface DoLaterStub : NSObject
439 do_later_callback_t func;
443 @implementation DoLaterStub
444 - (id) init: (do_later_callback_t)f
457 * Queue a dreamcast_run() to execute after the currently event(s)
459 void gui_do_later( do_later_callback_t func )
461 DoLaterStub *stub = [[[DoLaterStub alloc] init: func] autorelease];
462 [[NSRunLoop currentRunLoop] performSelector: @selector(do)
463 target: stub argument: nil order: 1
464 modes: [NSArray arrayWithObject: NSDefaultRunLoopMode] ];
467 /*************************** Convenience methods ***************************/
469 NSImage *NSImage_new_from_framebuffer( frame_buffer_t buffer )
471 NSBitmapImageRep *rep =
472 [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: &buffer->data
473 pixelsWide: buffer->width pixelsHigh: buffer->height
474 bitsPerSample: 8 samplesPerPixel: 3
475 hasAlpha: NO isPlanar: NO
476 colorSpaceName: NSDeviceRGBColorSpace bitmapFormat: 0
477 bytesPerRow: buffer->rowstride bitsPerPixel: 24];
479 NSImage *image = [[NSImage alloc] initWithSize: NSMakeSize(0.0,0.0)];
480 [image addRepresentation: rep];
485 NSTextField *cocoa_gui_add_label( NSView *parent, NSString *text, NSRect frame )
487 NSTextField *label = [[NSTextField alloc] initWithFrame: frame];
488 [label setStringValue: text];
489 [label setBordered: NO];
490 [label setDrawsBackground: NO];
491 [label setEditable: NO];
492 [label setAutoresizingMask: (NSViewMinYMargin|NSViewMaxXMargin)];
493 if( parent != NULL ) {
494 [parent addSubview: label];
.