4 * Core Cocoa-based user interface
6 * Copyright (c) 2005 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(orderFrontStandardAboutPanel:) keyEquivalent:@""];
71 [appleMenu addItem:[NSMenuItem separatorItem]];
74 services = [[[NSMenu alloc] init] autorelease];
75 [appleMenu addItemWithTitle:@"Services" action:nil keyEquivalent:@""];
76 [appleMenu setSubmenu: services forItem: [appleMenu itemWithTitle: @"Services"]];
79 title = [@"Hide " stringByAppendingString:appName];
80 [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
83 menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others"
84 action:@selector(hideOtherApplications:)
86 [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
89 [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
90 [appleMenu addItem:[NSMenuItem separatorItem]];
93 title = [@"Quit " stringByAppendingString:appName];
94 [appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
96 /* Put menu into the menubar */
97 menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
98 [menuItem setSubmenu: appleMenu];
99 NSMenu *menu = [NSMenu new];
100 [menu addItem: menuItem];
102 NSMenu *gdromMenu = cocoa_gdrom_menu_new();
104 NSMenu *fileMenu = [[NSMenu alloc] initWithTitle: NS_("File")];
105 [fileMenu addItemWithTitle: NS_("Load Binary") action: @selector(load_binary_action:) keyEquivalent: @"b"];
106 [[fileMenu addItemWithTitle: NS_("GD-Rom") action: nil keyEquivalent: @""]
107 setSubmenu: gdromMenu];
108 [fileMenu addItem: [NSMenuItem separatorItem]];
109 [[fileMenu addItemWithTitle: NS_("Reset") action: @selector(reset_action:) keyEquivalent: @"r"]
110 setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
111 [fileMenu addItemWithTitle: NS_("Pause") action: @selector(pause_action:) keyEquivalent: @"p"];
112 [fileMenu addItemWithTitle: NS_("Resume") action: @selector(run_action:) keyEquivalent: @"r"];
113 [fileMenu addItem: [NSMenuItem separatorItem]];
114 [fileMenu addItemWithTitle: NS_("Load State") action: @selector(load_action:) keyEquivalent: @"o"];
115 [fileMenu addItemWithTitle: NS_("Save State") action: @selector(save_action:) keyEquivalent: @"s"];
117 menuItem = [[NSMenuItem alloc] initWithTitle:NS_("File") action: nil keyEquivalent: @""];
118 [menuItem setSubmenu: fileMenu];
119 [menu addItem: menuItem];
121 /* Tell the application object that this is now the application menu */
122 [NSApp setMainMenu: menu];
123 [NSApp setAppleMenu: appleMenu];
124 [NSApp setServicesMenu: services];
126 /* Finally give up our references to the objects */
132 @interface LxdreamDelegate : NSObject
135 @implementation LxdreamDelegate
136 - (void)windowWillClose: (NSNotification *)notice
138 dreamcast_shutdown();
141 - (void)windowDidBecomeMain: (NSNotification *)notice
143 if( cocoa_gui_autorun ) {
144 cocoa_gui_autorun = NO;
145 cocoa_gui_run_later();
148 - (void) load_action: (id)sender
150 NSOpenPanel *panel = [NSOpenPanel openPanel];
151 const gchar *dir = lxdream_get_config_value(CONFIG_SAVE_PATH);
152 NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
153 NSArray *fileTypes = [NSArray arrayWithObject: @"dst"];
154 int result = [panel runModalForDirectory: path file: nil types: fileTypes];
155 if( result == NSOKButton && [[panel filenames] count] > 0 ) {
156 NSString *filename = [[panel filenames] objectAtIndex: 0];
157 dreamcast_load_state( [filename UTF8String] );
160 - (void) save_action: (id)sender
162 NSSavePanel *panel = [NSSavePanel savePanel];
163 const gchar *dir = lxdream_get_config_value(CONFIG_SAVE_PATH);
164 NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
165 [panel setRequiredFileType: @"dst"];
166 int result = [panel runModalForDirectory: path file:@""];
167 if( result == NSOKButton ) {
168 NSString *filename = [panel filename];
169 dreamcast_save_state( [filename UTF8String] );
172 - (void) load_binary_action: (id)sender
174 NSOpenPanel *panel = [NSOpenPanel openPanel];
175 const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
176 NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
177 int result = [panel runModalForDirectory: path file: nil types: nil];
178 if( result == NSOKButton && [[panel filenames] count] > 0 ) {
179 NSString *filename = [[panel filenames] objectAtIndex: 0];
180 file_load_magic( [filename UTF8String] );
183 - (void) mount_action: (id)sender
185 NSOpenPanel *panel = [NSOpenPanel openPanel];
186 const gchar *dir = lxdream_get_config_value(CONFIG_DEFAULT_PATH);
187 NSString *path = (dir == NULL ? NSHomeDirectory() : [NSString stringWithCString: dir]);
188 int result = [panel runModalForDirectory: path file: nil types: nil];
189 if( result == NSOKButton && [[panel filenames] count] > 0 ) {
190 NSString *filename = [[panel filenames] objectAtIndex: 0];
191 gdrom_mount_image( [filename UTF8String] );
194 - (void) pause_action: (id)sender
199 - (void) reset_action: (id)sender
203 - (void) run_action: (id)sender
205 cocoa_gui_run_later();
207 - (void) run_immediate
211 - (void) gdrom_list_action: (id)sender
213 gdrom_list_set_selection( [sender tag] );
218 gboolean gui_parse_cmdline( int *argc, char **argv[] )
220 /* If started from the finder, the first (and only) arg will look something like
221 * -psn_0_... - we want to remove this so that lxdream doesn't try to process it
224 if( *argc == 2 && strncmp((*argv)[1], "-psn_", 5) == 0 ) {
230 gboolean gui_init( gboolean withDebug )
232 dreamcast_register_module( &cocoa_gui_module );
234 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
235 [NSApplication sharedApplication];
237 LxdreamDelegate *delegate = [[LxdreamDelegate alloc] init];
238 [NSApp setDelegate: delegate];
239 NSString *iconFile = [[NSBundle mainBundle] pathForResource:@"dcemu" ofType:@"gif"];
240 NSImage *iconImage = [[NSImage alloc] initWithContentsOfFile: iconFile];
241 [NSApp setApplicationIconImage: iconImage];
242 cocoa_gui_create_menu();
243 NSWindow *window = cocoa_gui_create_main_window();
244 [window makeKeyAndOrderFront: nil];
245 [NSApp activateIgnoringOtherApps: YES];
250 void gui_main_loop( gboolean run )
253 cocoa_gui_autorun = YES;
255 cocoa_gui_is_running = YES;
257 cocoa_gui_is_running = NO;
260 void gui_update_state(void)
265 gboolean gui_error_dialog( const char *msg, ... )
267 if( cocoa_gui_is_running ) {
268 NSString *error_string;
272 error_string = [[NSString alloc] initWithFormat: [NSString stringWithCString: msg] arguments: args];
273 NSRunAlertPanel(@"Error in lxdream", error_string, nil, nil, nil);
281 void gui_update_io_activity( io_activity_type io, gboolean active )
287 uint32_t cocoa_gui_run_slice( uint32_t nanosecs )
290 NSAutoreleasePool *pool;
292 cocoa_gui_nanos += nanosecs;
293 if( cocoa_gui_nanos > GUI_TICK_PERIOD ) { /* 10 ms */
294 cocoa_gui_nanos -= GUI_TICK_PERIOD;
296 uint32_t current_period = cocoa_gui_ticks * GUI_TICK_PERIOD;
298 // Run the event loop
299 pool = [NSAutoreleasePool new];
300 while( (event = [NSApp nextEventMatchingMask: NSAnyEventMask untilDate: nil
301 inMode: NSDefaultRunLoopMode dequeue: YES]) != nil ) {
302 [NSApp sendEvent: event];
307 gettimeofday(&tv,NULL);
308 uint32_t ns = ((tv.tv_sec - cocoa_gui_lasttv.tv_sec) * 1000000000) +
309 (tv.tv_usec - cocoa_gui_lasttv.tv_usec)*1000;
310 if( (ns * 1.05) < current_period ) {
311 // We've gotten ahead - sleep for a little bit
314 tv.tv_nsec = current_period - ns;
318 /* Update the display every 10 ticks (ie 10 times a second) and
319 * save the current tv value */
320 if( cocoa_gui_ticks > 10 ) {
322 cocoa_gui_ticks -= 10;
324 double speed = (float)( (double)current_period * 100.0 / ns );
325 cocoa_gui_lasttv.tv_sec = tv.tv_sec;
326 cocoa_gui_lasttv.tv_usec = tv.tv_usec;
327 snprintf( buf, 32, _("Running (%2.4f%%)"), speed );
328 [((LxdreamMainWindow *)[NSApp mainWindow]) setStatusText: buf];
335 void cocoa_gui_update( void )
340 void cocoa_gui_start( void )
342 LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
343 [win setRunning: YES];
345 gettimeofday(&cocoa_gui_lasttv,NULL);
348 void cocoa_gui_stop( void )
350 LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
351 [win setRunning: NO];
355 * Queue a dreamcast_run() to execute after the currently event(s)
357 void cocoa_gui_run_later( void )
359 [[NSRunLoop currentRunLoop] performSelector: @selector(run_immediate)
360 target: [NSApp delegate] argument: nil order: 1
361 modes: [NSArray arrayWithObject: NSDefaultRunLoopMode] ];
364 NSImage *NSImage_new_from_framebuffer( frame_buffer_t buffer )
366 NSBitmapImageRep *rep =
367 [[NSBitmapImageRep alloc] initWithBitmapDataPlanes: &buffer->data
368 pixelsWide: buffer->width pixelsHigh: buffer->height
369 bitsPerSample: 8 samplesPerPixel: 3
370 hasAlpha: NO isPlanar: NO
371 colorSpaceName: NSDeviceRGBColorSpace bitmapFormat: 0
372 bytesPerRow: buffer->rowstride bitsPerPixel: 24];
374 NSImage *image = [[NSImage alloc] initWithSize: NSMakeSize(0.0,0.0)];
375 [image addRepresentation: rep];
.