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: @APP_VERSION, @"Copyright (C) 2005-2008 Nathan Keynes", nil];
156 NSDictionary *options= [NSDictionary dictionaryWithObjects: values forKeys: keys];
158 [NSApp orderFrontStandardAboutPanelWithOptions: options];
160 - (void) preferences_action: (id)sender
162 LxdreamPrefsPanel *panel = [[LxdreamPrefsPanel alloc] initWithContentRect: NSMakeRect(0,0,600,400)];
163 [NSApp runModalForWindow: panel];
165 - (void) load_action: (id)sender
167 NSOpenPanel *panel = [NSOpenPanel openPanel];
168 const gchar *dir = lxdream_get_config_value(CONFIG_SAVE_PATH);
169 NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
170 NSArray *fileTypes = [NSArray arrayWithObject: @"dst"];
171 int result = [panel runModalForDirectory: path file: nil types: fileTypes];
172 if( result == NSOKButton && [[panel filenames] count] > 0 ) {
173 NSString *filename = [[panel filenames] objectAtIndex: 0];
174 dreamcast_load_state( [filename UTF8String] );
177 - (void) save_action: (id)sender
179 NSSavePanel *panel = [NSSavePanel savePanel];
180 const gchar *dir = lxdream_get_config_value(CONFIG_SAVE_PATH);
181 NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
182 [panel setRequiredFileType: @"dst"];
183 int result = [panel runModalForDirectory: path file:@""];
184 if( result == NSOKButton ) {
185 NSString *filename = [panel filename];
186 dreamcast_save_state( [filename UTF8String] );
189 - (void) load_binary_action: (id)sender
191 NSOpenPanel *panel = [NSOpenPanel openPanel];
192 const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
193 NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
194 int result = [panel runModalForDirectory: path file: nil types: nil];
195 if( result == NSOKButton && [[panel filenames] count] > 0 ) {
196 NSString *filename = [[panel filenames] objectAtIndex: 0];
197 file_load_magic( [filename UTF8String] );
200 - (void) mount_action: (id)sender
202 NSOpenPanel *panel = [NSOpenPanel openPanel];
203 const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
204 NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
205 int result = [panel runModalForDirectory: path file: nil types: nil];
206 if( result == NSOKButton && [[panel filenames] count] > 0 ) {
207 NSString *filename = [[panel filenames] objectAtIndex: 0];
208 gdrom_mount_image( [filename UTF8String] );
211 - (void) pause_action: (id)sender
216 - (void) reset_action: (id)sender
220 - (void) run_action: (id)sender
222 cocoa_gui_run_later();
224 - (void) run_immediate
228 - (void) gdrom_list_action: (id)sender
230 gdrom_list_set_selection( [sender tag] );
235 gboolean gui_parse_cmdline( int *argc, char **argv[] )
237 /* If started from the finder, the first (and only) arg will look something like
238 * -psn_0_... - we want to remove this so that lxdream doesn't try to process it
241 if( *argc == 2 && strncmp((*argv)[1], "-psn_", 5) == 0 ) {
247 gboolean gui_init( gboolean withDebug )
249 dreamcast_register_module( &cocoa_gui_module );
251 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
252 [NSApplication sharedApplication];
254 LxdreamDelegate *delegate = [[LxdreamDelegate alloc] init];
255 [NSApp setDelegate: delegate];
256 NSString *iconFile = [[NSBundle mainBundle] pathForResource:@"dcemu" ofType:@"gif"];
257 NSImage *iconImage = [[NSImage alloc] initWithContentsOfFile: iconFile];
258 [iconImage setName: @"NSApplicationIcon"];
259 [NSApp setApplicationIconImage: iconImage];
260 cocoa_gui_create_menu();
261 NSWindow *window = cocoa_gui_create_main_window();
262 [window makeKeyAndOrderFront: nil];
263 [NSApp activateIgnoringOtherApps: YES];
268 void gui_main_loop( gboolean run )
271 cocoa_gui_autorun = YES;
273 cocoa_gui_is_running = YES;
275 cocoa_gui_is_running = NO;
278 void gui_update_state(void)
283 gboolean gui_error_dialog( const char *msg, ... )
285 if( cocoa_gui_is_running ) {
286 NSString *error_string;
290 error_string = [[NSString alloc] initWithFormat: [NSString stringWithCString: msg] arguments: args];
291 NSRunAlertPanel(@"Error in lxdream", error_string, nil, nil, nil);
299 void gui_update_io_activity( io_activity_type io, gboolean active )
305 uint32_t cocoa_gui_run_slice( uint32_t nanosecs )
308 NSAutoreleasePool *pool;
310 cocoa_gui_nanos += nanosecs;
311 if( cocoa_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
312 cocoa_gui_nanos -= GUI_TICK_PERIOD;
314 uint32_t current_period = cocoa_gui_ticks * GUI_TICK_PERIOD;
316 // Run the event loop
317 pool = [NSAutoreleasePool new];
318 while( (event = [NSApp nextEventMatchingMask: NSAnyEventMask untilDate: nil
319 inMode: NSDefaultRunLoopMode dequeue: YES]) != nil ) {
320 [NSApp sendEvent: event];
325 gettimeofday(&tv,NULL);
326 uint32_t ns = ((tv.tv_sec - cocoa_gui_lasttv.tv_sec) * 1000000000) +
327 (tv.tv_usec - cocoa_gui_lasttv.tv_usec)*1000;
328 if( (ns * 1.05) < current_period ) {
329 // We've gotten ahead - sleep for a little bit
332 tv.tv_nsec = current_period - ns;
336 /* Update the display every 10 ticks (ie 10 times a second) and
337 * save the current tv value */
338 if( cocoa_gui_ticks > 10 ) {
340 cocoa_gui_ticks -= 10;
342 double speed = (float)( (double)current_period * 100.0 / ns );
343 cocoa_gui_lasttv.tv_sec = tv.tv_sec;
344 cocoa_gui_lasttv.tv_usec = tv.tv_usec;
345 snprintf( buf, 32, _("Running (%2.4f%%)"), speed );
346 [((LxdreamMainWindow *)[NSApp mainWindow]) setStatusText: buf];
353 void cocoa_gui_update( void )
358 void cocoa_gui_start( void )
360 LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
361 [win setRunning: YES];
363 gettimeofday(&cocoa_gui_lasttv,NULL);
366 void cocoa_gui_stop( void )
368 LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
369 [win setRunning: NO];
373 * Queue a dreamcast_run() to execute after the currently event(s)
375 void cocoa_gui_run_later( void )
377 [[NSRunLoop currentRunLoop] performSelector: @selector(run_immediate)
378 target: [NSApp delegate] argument: nil order: 1
379 modes: [NSArray arrayWithObject: NSDefaultRunLoopMode] ];
382 NSImage *NSImage_new_from_framebuffer( frame_buffer_t buffer )
384 NSBitmapImageRep *rep =
385 [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: &buffer->data
386 pixelsWide: buffer->width pixelsHigh: buffer->height
387 bitsPerSample: 8 samplesPerPixel: 3
388 hasAlpha: NO isPlanar: NO
389 colorSpaceName: NSDeviceRGBColorSpace bitmapFormat: 0
390 bytesPerRow: buffer->rowstride bitsPerPixel: 24];
392 NSImage *image = [[NSImage alloc] initWithSize: NSMakeSize(0.0,0.0)];
393 [image addRepresentation: rep];
.