filename | src/cocoaui/cocoaui.c |
changeset | 738:0cbff49086b8 |
prev | 736:a02d1475ccfd |
next | 765:4cd066048203 |
author | nkeynes |
date | Mon Jul 21 00:08:34 2008 +0000 (15 years ago) |
permissions | -rw-r--r-- |
last change | Add gettext.h and build sanely without libintl if it's not available Remove x86dasm's config.h & opintl.h (no longer needed and actually wrong) |
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 "display.h"
29 #include "gui.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,
39 cocoa_gui_update,
40 cocoa_gui_start,
41 cocoa_gui_run_slice,
42 cocoa_gui_stop,
43 NULL, NULL };
45 /**
46 * Count of running nanoseconds - used to cut back on the GUI runtime
47 */
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;
56 @end
58 static void cocoa_gui_create_menu(void)
59 {
60 NSMenu *appleMenu, *services;
61 NSMenuItem *menuItem;
62 NSString *title;
63 NSString *appName;
65 appName = @"Lxdream";
66 appleMenu = [[NSMenu alloc] initWithTitle:@""];
68 /* Add menu items */
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:@","];
75 // Services Menu
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"]];
81 // Hide AppName
82 title = [@"Hide " stringByAppendingString:appName];
83 [appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
85 // Hide Others
86 menuItem = (NSMenuItem *)[appleMenu addItemWithTitle:@"Hide Others"
87 action:@selector(hideOtherApplications:)
88 keyEquivalent:@"h"];
89 [menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
91 // Show All
92 [appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
93 [appleMenu addItem:[NSMenuItem separatorItem]];
95 // Quit AppName
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 */
130 [appleMenu release];
131 [menuItem release];
132 [menu release];
133 }
135 @interface LxdreamDelegate : NSObject
136 @end
138 @implementation LxdreamDelegate
139 - (void)windowWillClose: (NSNotification *)notice
140 {
141 dreamcast_shutdown();
142 exit(0);
143 }
144 - (void)windowDidBecomeMain: (NSNotification *)notice
145 {
146 if( cocoa_gui_autorun ) {
147 cocoa_gui_autorun = NO;
148 cocoa_gui_run_later();
149 }
150 }
151 - (void) about_action: (id)sender
152 {
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];
159 }
160 - (void) preferences_action: (id)sender
161 {
162 LxdreamPrefsPanel *panel = [[LxdreamPrefsPanel alloc] initWithContentRect: NSMakeRect(0,0,600,400)];
163 [NSApp runModalForWindow: panel];
164 }
165 - (void) load_action: (id)sender
166 {
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] );
175 }
176 }
177 - (void) save_action: (id)sender
178 {
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] );
187 }
188 }
189 - (void) load_binary_action: (id)sender
190 {
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] );
198 }
199 }
200 - (void) mount_action: (id)sender
201 {
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] );
209 }
210 }
211 - (void) pause_action: (id)sender
212 {
213 dreamcast_stop();
214 }
216 - (void) reset_action: (id)sender
217 {
218 dreamcast_reset();
219 }
220 - (void) run_action: (id)sender
221 {
222 cocoa_gui_run_later();
223 }
224 - (void) run_immediate
225 {
226 dreamcast_run();
227 }
228 - (void) gdrom_list_action: (id)sender
229 {
230 gdrom_list_set_selection( [sender tag] );
231 }
232 @end
235 gboolean gui_parse_cmdline( int *argc, char **argv[] )
236 {
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
239 * normally
240 */
241 if( *argc == 2 && strncmp((*argv)[1], "-psn_", 5) == 0 ) {
242 *argc = 1;
243 }
244 return TRUE;
245 }
247 gboolean gui_init( gboolean withDebug )
248 {
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];
265 [pool release];
266 }
268 void gui_main_loop( gboolean run )
269 {
270 if( run ) {
271 cocoa_gui_autorun = YES;
272 }
273 cocoa_gui_is_running = YES;
274 [NSApp run];
275 cocoa_gui_is_running = NO;
276 }
278 void gui_update_state(void)
279 {
280 cocoa_gui_update();
281 }
283 gboolean gui_error_dialog( const char *msg, ... )
284 {
285 if( cocoa_gui_is_running ) {
286 NSString *error_string;
288 va_list args;
289 va_start(args, msg);
290 error_string = [[NSString alloc] initWithFormat: [NSString stringWithCString: msg] arguments: args];
291 NSRunAlertPanel(@"Error in lxdream", error_string, nil, nil, nil);
292 va_end(args);
293 return TRUE;
294 } else {
295 return FALSE;
296 }
297 }
299 void gui_update_io_activity( io_activity_type io, gboolean active )
300 {
302 }
305 uint32_t cocoa_gui_run_slice( uint32_t nanosecs )
306 {
307 NSEvent *event;
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;
313 cocoa_gui_ticks ++;
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];
321 }
322 [pool release];
324 struct timeval tv;
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
330 struct timespec tv;
331 tv.tv_sec = 0;
332 tv.tv_nsec = current_period - ns;
333 nanosleep(&tv, &tv);
334 }
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 ) {
339 gchar buf[32];
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];
348 }
349 }
350 return nanosecs;
351 }
353 void cocoa_gui_update( void )
354 {
356 }
358 void cocoa_gui_start( void )
359 {
360 LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
361 [win setRunning: YES];
362 cocoa_gui_nanos = 0;
363 gettimeofday(&cocoa_gui_lasttv,NULL);
364 }
366 void cocoa_gui_stop( void )
367 {
368 LxdreamMainWindow *win = (LxdreamMainWindow *)[NSApp mainWindow];
369 [win setRunning: NO];
370 }
372 /**
373 * Queue a dreamcast_run() to execute after the currently event(s)
374 */
375 void cocoa_gui_run_later( void )
376 {
377 [[NSRunLoop currentRunLoop] performSelector: @selector(run_immediate)
378 target: [NSApp delegate] argument: nil order: 1
379 modes: [NSArray arrayWithObject: NSDefaultRunLoopMode] ];
380 }
382 NSImage *NSImage_new_from_framebuffer( frame_buffer_t buffer )
383 {
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];
394 return image;
395 }
.