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 "cocoaui/cocoaui.h"
32 void cocoa_gui_update( void );
33 void cocoa_gui_start( void );
34 void cocoa_gui_stop( void );
35 void cocoa_gui_run_later( void );
36 uint32_t cocoa_gui_run_slice( uint32_t nanosecs );
38 struct dreamcast_module cocoa_gui_module = { "gui", NULL,
46 * Count of running nanoseconds - used to cut back on the GUI runtime
48 static uint32_t cocoa_gui_nanos = 0;
49 static uint32_t cocoa_gui_ticks = 0;
50 static struct timeval cocoa_gui_lasttv;
51 static BOOL cocoa_gui_autorun = NO;
52 static BOOL cocoa_gui_is_running = NO;
54 @interface NSApplication (PrivateAdditions)
55 - (void) setAppleMenu:(NSMenu *)aMenu;
58 static void cocoa_gui_create_menu(void)
60 NSMenu *appleMenu, *services;
66 appleMenu = [[NSMenu alloc] initWithTitle:@""];
69 title = [@"About " stringByAppendingString:appName];
70 [appleMenu addItemWithTitle:title action:@selector(about_action:) keyEquivalent:@""];
72 [appleMenu addItem:[NSMenuItem separatorItem]];
73 [appleMenu addItemWithTitle: NS_("Preferences...") action:@selector(preferences_action:) keyEquivalent:@","];
76 [appleMenu addItem:[NSMenuItem separatorItem]];
77 services = [[[NSMenu alloc] init] autorelease];
78 [appleMenu addItemWithTitle:@"Services" action:nil keyEquivalent:@""];
79 [appleMenu setSubmenu: services forItem: [appleMenu itemWithTitle: @"Services"]];
82 title = [@"Hide " stringByAppendingString:appName];
83 [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
86 menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others"
87 action:@selector(hideOtherApplications:)
89 [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
92 [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
93 [appleMenu addItem:[NSMenuItem separatorItem]];
96 title = [@"Quit " stringByAppendingString:appName];
97 [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
99 /* Put menu into the menubar */
100 menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
101 [menuItem setSubmenu: appleMenu];
102 NSMenu *menu = [NSMenu new];
103 [menu addItem: menuItem];
105 NSMenu *gdromMenu = cocoa_gdrom_menu_new();
107 NSMenu *fileMenu = [[NSMenu alloc] initWithTitle: NS_("File")];
108 [fileMenu addItemWithTitle: NS_("Load Binary") action: @selector(load_binary_action:) keyEquivalent: @"b"];
109 [[fileMenu addItemWithTitle: NS_("GD-Rom") action: nil keyEquivalent: @""]
110 setSubmenu: gdromMenu];
111 [fileMenu addItem: [NSMenuItem separatorItem]];
112 [[fileMenu addItemWithTitle: NS_("Reset") action: @selector(reset_action:) keyEquivalent: @"r"]
113 setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
114 [fileMenu addItemWithTitle: NS_("Pause") action: @selector(pause_action:) keyEquivalent: @"p"];
115 [fileMenu addItemWithTitle: NS_("Resume") action: @selector(run_action:) keyEquivalent: @"r"];
116 [fileMenu addItem: [NSMenuItem separatorItem]];
117 [fileMenu addItemWithTitle: NS_("Load State") action: @selector(load_action:) keyEquivalent: @"o"];
118 [fileMenu addItemWithTitle: NS_("Save State") action: @selector(save_action:) keyEquivalent: @"s"];
120 menuItem = [[NSMenuItem alloc] initWithTitle:NS_("File") action: nil keyEquivalent: @""];
121 [menuItem setSubmenu: fileMenu];
122 [menu addItem: menuItem];
124 /* Tell the application object that this is now the application menu */
125 [NSApp setMainMenu: menu];
126 [NSApp setAppleMenu: appleMenu];
127 [NSApp setServicesMenu: services];
129 /* Finally give up our references to the objects */
135 @interface LxdreamDelegate : NSObject
138 @implementation LxdreamDelegate
139 - (void)windowWillClose: (NSNotification *)notice
141 dreamcast_shutdown();
144 - (void)windowDidBecomeMain: (NSNotification *)notice
146 if( cocoa_gui_autorun ) {
147 cocoa_gui_autorun = NO;
148 cocoa_gui_run_later();
151 - (void) about_action: (id)sender
153 NSArray *keys = [NSArray arrayWithObjects: @"Version", @"Copyright", nil];
154 NSArray *values = [NSArray arrayWithObjects: NS_(lxdream_full_version), NS_(lxdream_copyright), nil];
156 NSDictionary *options= [NSDictionary dictionaryWithObjects: values forKeys: keys];
158 [NSApp orderFrontStandardAboutPanelWithOptions: options];
160 - (void) preferences_action: (id)sender
162 cocoa_gui_show_preferences();
164 - (void) load_action: (id)sender
166 NSOpenPanel *panel = [NSOpenPanel openPanel];
167 const gchar *dir = lxdream_get_config_value(CONFIG_SAVE_PATH);
168 NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
169 NSArray *fileTypes = [NSArray arrayWithObject: @"dst"];
170 int result = [panel runModalForDirectory: path file: nil types: fileTypes];
171 if( result == NSOKButton && [[panel filenames] count] > 0 ) {
172 NSString *filename = [[panel filenames] objectAtIndex: 0];
173 dreamcast_load_state( [filename UTF8String] );
176 - (void) save_action: (id)sender
178 NSSavePanel *panel = [NSSavePanel savePanel];
179 const gchar *dir = lxdream_get_config_value(CONFIG_SAVE_PATH);
180 NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
181 [panel setRequiredFileType: @"dst"];
182 int result = [panel runModalForDirectory: path file:@""];
183 if( result == NSOKButton ) {
184 NSString *filename = [panel filename];
185 dreamcast_save_state( [filename UTF8String] );
188 - (void) load_binary_action: (id)sender
190 NSOpenPanel *panel = [NSOpenPanel openPanel];
191 const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
192 NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
193 int result = [panel runModalForDirectory: path file: nil types: nil];
194 if( result == NSOKButton && [[panel filenames] count] > 0 ) {
195 NSString *filename = [[panel filenames] objectAtIndex: 0];
196 file_load_magic( [filename UTF8String] );
199 - (void) mount_action: (id)sender
201 NSOpenPanel *panel = [NSOpenPanel openPanel];
202 const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
203 NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
204 int result = [panel runModalForDirectory: path file: nil types: nil];
205 if( result == NSOKButton && [[panel filenames] count] > 0 ) {
206 NSString *filename = [[panel filenames] objectAtIndex: 0];
207 gdrom_mount_image( [filename UTF8String] );
210 - (void) pause_action: (id)sender
215 - (void) reset_action: (id)sender
219 - (void) run_action: (id)sender
221 cocoa_gui_run_later();
223 - (void) run_immediate
227 - (void) gdrom_list_action: (id)sender
229 gdrom_list_set_selection( [sender tag] );
234 gboolean gui_parse_cmdline( int *argc, char **argv[] )
236 /* If started from the finder, the first (and only) arg will look something like
237 * -psn_0_... - we want to remove this so that lxdream doesn't try to process it
240 if( *argc == 2 && strncmp((*argv)[1], "-psn_", 5) == 0 ) {
246 gboolean gui_init( gboolean withDebug )
248 dreamcast_register_module( &cocoa_gui_module );
250 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
251 [NSApplication sharedApplication];
253 LxdreamDelegate *delegate = [[LxdreamDelegate alloc] init];
254 [NSApp setDelegate: delegate];
255 NSString *iconFile = [[NSBundle mainBundle] pathForResource:@"dcemu" ofType:@"gif"];
256 NSImage *iconImage = [[NSImage alloc] initWithContentsOfFile: iconFile];
257 [iconImage setName: @"NSApplicationIcon"];
258 [NSApp setApplicationIconImage: iconImage];
259 cocoa_gui_create_menu();
260 NSWindow *window = cocoa_gui_create_main_window();
261 [window makeKeyAndOrderFront: nil];
262 [NSApp activateIgnoringOtherApps: YES];
267 void gui_main_loop( gboolean run )
270 cocoa_gui_autorun = YES;
272 cocoa_gui_is_running = YES;
274 cocoa_gui_is_running = NO;
277 void gui_update_state(void)
282 gboolean gui_error_dialog( const char *msg, ... )
284 if( cocoa_gui_is_running ) {
285 NSString *error_string;
289 error_string = [[NSString alloc] initWithFormat: [NSString stringWithCString: msg] arguments: args];
290 NSRunAlertPanel(@"Error in lxdream", error_string, nil, nil, nil);
298 void gui_update_io_activity( io_activity_type io, gboolean active )
304 uint32_t cocoa_gui_run_slice( uint32_t nanosecs )
307 NSAutoreleasePool *pool;
309 cocoa_gui_nanos += nanosecs;
310 if( cocoa_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
311 cocoa_gui_nanos -= GUI_TICK_PERIOD;
313 uint32_t current_period = cocoa_gui_ticks * GUI_TICK_PERIOD;
315 // Run the event loop
316 pool = [NSAutoreleasePool new];
317 while( (event = [NSApp nextEventMatchingMask: NSAnyEventMask untilDate: nil
318 inMode: NSDefaultRunLoopMode dequeue: YES]) != nil ) {
319 [NSApp sendEvent: event];
324 gettimeofday(&tv,NULL);
325 uint32_t ns = ((tv.tv_sec - cocoa_gui_lasttv.tv_sec) * 1000000000) +
326 (tv.tv_usec - cocoa_gui_lasttv.tv_usec)*1000;
327 if( (ns * 1.05) < current_period ) {
328 // We've gotten ahead - sleep for a little bit
331 tv.tv_nsec = current_period - ns;
335 /* Update the display every 10 ticks (ie 10 times a second) and
336 * save the current tv value */
337 if( cocoa_gui_ticks > 10 ) {
339 cocoa_gui_ticks -= 10;
341 double speed = (float)( (double)current_period * 100.0 / ns );
342 cocoa_gui_lasttv.tv_sec = tv.tv_sec;
343 cocoa_gui_lasttv.tv_usec = tv.tv_usec;
344 snprintf( buf, 32, _("Running (%2.4f%%)"), speed );
345 [((LxdreamMainWindow *)[NSApp mainWindow]) setStatusText: buf];
352 void cocoa_gui_update( void )
357 void cocoa_gui_start( void )
359 LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
360 [win setRunning: YES];
362 gettimeofday(&cocoa_gui_lasttv,NULL);
365 void cocoa_gui_stop( void )
367 LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
368 [win setRunning: NO];
372 * Queue a dreamcast_run() to execute after the currently event(s)
374 void cocoa_gui_run_later( void )
376 [[NSRunLoop currentRunLoop] performSelector: @selector(run_immediate)
377 target: [NSApp delegate] argument: nil order: 1
378 modes: [NSArray arrayWithObject: NSDefaultRunLoopMode] ];
381 NSImage *NSImage_new_from_framebuffer( frame_buffer_t buffer )
383 NSBitmapImageRep *rep =
384 [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: &buffer->data
385 pixelsWide: buffer->width pixelsHigh: buffer->height
386 bitsPerSample: 8 samplesPerPixel: 3
387 hasAlpha: NO isPlanar: NO
388 colorSpaceName: NSDeviceRGBColorSpace bitmapFormat: 0
389 bytesPerRow: buffer->rowstride bitsPerPixel: 24];
391 NSImage *image = [[NSImage alloc] initWithSize: NSMakeSize(0.0,0.0)];
392 [image addRepresentation: rep];
.