4 * The OS/X side of the video support (responsible for actually displaying /
7 * Copyright (c) 2008 Nathan Keynes.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
24 #include "dckeysyms.h"
25 #include "cocoaui/cocoaui.h"
26 #include "drivers/video_nsgl.h"
27 #include "drivers/video_gl.h"
28 #include "pvr2/pvr2.h"
29 #import <AppKit/AppKit.h>
31 #include "mac_keymap.h"
33 #define MOUSE_X_SCALE 5
34 #define MOUSE_Y_SCALE 5
36 static gboolean video_osx_init();
37 static void video_osx_shutdown();
38 static void video_osx_display_blank( uint32_t colour );
39 static uint16_t video_osx_resolve_keysym( const gchar *keysym );
40 static uint16_t video_osx_keycode_to_dckeysym(uint16_t keycode);
41 static gchar *video_osx_keycode_to_keysym(uint16_t keycode);
43 struct display_driver display_osx_driver = {
45 N_("OS X Cocoa GUI-based OpenGL driver"),
46 video_osx_init, video_osx_shutdown,
47 video_osx_resolve_keysym,
48 video_osx_keycode_to_dckeysym,
49 video_osx_keycode_to_keysym,
50 NULL, NULL, NULL, NULL, NULL,
52 video_osx_display_blank, NULL };
55 static NSView *video_view = NULL;
56 int video_width = 640;
57 int video_height = 480;
59 #define MAX_MASK_KEYCODE 128
61 @interface LxdreamOSXView : LxdreamVideoView
63 int flagsMask[MAX_MASK_KEYCODE];
67 @implementation LxdreamVideoView
68 - (void)setIsGrabbed: (BOOL)grabbed
72 - (void) setDelegate: (id)other
82 @implementation LxdreamOSXView
83 //--------------------------------------------------------------------
84 - (id)initWithFrame: (NSRect)contentRect
86 if( [super initWithFrame: contentRect] != nil ) {
89 for( i=0; i<MAX_MASK_KEYCODE; i++ ) {
98 if( delegate && [delegate respondsToSelector: @selector(viewRequestedGrab:)] )
99 return [delegate performSelector: @selector(viewRequestedGrab:) withObject: self] != nil;
102 - (BOOL)requestUngrab
104 if( delegate && [delegate respondsToSelector: @selector(viewRequestedUngrab:)] )
105 return [delegate performSelector: @selector(viewRequestedUngrab:) withObject: self] != nil;
112 - (BOOL)acceptsFirstResponder
120 //--------------------------------------------------------------------
121 - (void)drawRect: (NSRect) rect
123 NSSize size = [self frame].size;
124 if( video_width != size.width || video_height != size.height ) {
125 video_width = size.width;
126 video_height = size.height;
129 pvr2_redraw_display();
131 - (void)keyDown: (NSEvent *) event
133 if( ![event isARepeat] ) {
134 input_event_keydown( NULL, [event keyCode]+1, 1 );
137 - (void)keyUp: (NSEvent *) event
139 input_event_keyup( NULL, [event keyCode]+1, 1 );
141 - (void)flagsChanged: (NSEvent *) event
143 int keycode = [event keyCode];
144 if( ([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask) ) {
145 [self requestUngrab];
148 if( flagsMask[keycode] == 0 ) {
149 input_event_keydown( NULL, keycode+1, 1 );
150 flagsMask[keycode] = 1;
152 input_event_keyup( NULL, keycode+1, 1 );
153 flagsMask[keycode] = 0;
156 - (void)emitMouseDownEvent: (NSEvent *)event button: (int)button
159 input_event_mousedown( button, 0, 0, FALSE );
161 NSPoint pt = [event locationInWindow];
163 int y = video_height - (int)pt.y;
164 gl_window_to_system_coords(&x,&y);
165 input_event_mousedown( button, x, y, TRUE );
168 - (void)emitMouseUpEvent: (NSEvent *)event button: (int)button
171 input_event_mouseup( button, 0, 0, FALSE );
173 NSPoint pt = [event locationInWindow];
175 int y = video_height - (int)pt.y;
176 gl_window_to_system_coords(&x,&y);
177 input_event_mouseup( button, x, y, TRUE );
180 - (void)emitMouseMoveEvent: (NSEvent *)event
183 input_event_mousemove( [event deltaX] * MOUSE_X_SCALE, [event deltaY] * MOUSE_Y_SCALE, FALSE );
185 NSPoint pt = [event locationInWindow];
187 int y = video_height - (int)pt.y;
188 gl_window_to_system_coords(&x,&y);
189 input_event_mousemove( x, y, TRUE );
192 - (void)mouseExited: (NSEvent *)event
195 input_event_mousemove( -1, -1, TRUE );
199 - (void)mouseDown: (NSEvent *) event
201 // If using grab but not grabbed yet, the first click should be consumed
202 // by the grabber. In all other circumstances we process normally.
203 if( isGrabbed || ![self requestGrab] ) {
204 [self emitMouseDownEvent: event button: 0];
207 - (void)mouseUp: (NSEvent *)event
209 [self emitMouseUpEvent: event button: 0];
212 - (void)rightMouseDown: (NSEvent *) event
214 [self emitMouseDownEvent: event button: 1];
216 - (void)rightMouseUp: (NSEvent *)event
218 [self emitMouseUpEvent: event button: 1];
220 - (void)otherMouseDown: (NSEvent *) event
222 [self emitMouseDownEvent: event button: [event buttonNumber]];
224 - (void)otherMouseUp: (NSEvent *) event
226 [self emitMouseUpEvent: event button: [event buttonNumber]];
228 - (void)mouseMoved: (NSEvent *) event
230 [self emitMouseMoveEvent: event];
232 - (void)mouseDragged: (NSEvent *) event
234 [self emitMouseMoveEvent: event];
236 - (void)rightMouseDragged: (NSEvent *) event
238 [self emitMouseMoveEvent: event];
240 - (void)otherMouseDragged: (NSEvent *) event
242 [self emitMouseMoveEvent: event];
247 NSView *video_osx_create_drawable()
249 NSRect contentRect = {{0,0},{640,480}};
250 video_view = [[LxdreamOSXView alloc] initWithFrame: contentRect];
251 [video_view setAutoresizingMask: (NSViewWidthSizable|NSViewHeightSizable)];
255 static gboolean video_osx_init()
257 if( video_view == NULL ) {
260 if( !video_nsgl_init_driver(video_view, &display_osx_driver) ) {
263 pvr2_setup_gl_context();
267 static void video_osx_shutdown()
271 static void video_osx_display_blank( uint32_t colour )
275 static int mac_keymap_cmp(const void *a, const void *b)
277 const gchar *key = a;
278 const struct mac_keymap_struct *kb = b;
279 return strcasecmp(key, kb->name);
282 static uint16_t video_osx_resolve_keysym( const gchar *keysym )
284 struct mac_keymap_struct *result = bsearch( keysym, mac_keysyms, mac_keysym_count, sizeof(struct mac_keymap_struct), mac_keymap_cmp );
285 if( result == NULL ) {
288 return result->keycode + 1;
292 static uint16_t video_osx_keycode_to_dckeysym(uint16_t keycode)
294 if( keycode < 1 || keycode > 128 ) {
297 return mac_keycode_to_dckeysym[keycode-1];
301 static gchar *video_osx_keycode_to_keysym(uint16_t keycode)
303 if( keycode < 1 || keycode > 128 ) {
306 return g_strdup(mac_keysyms_by_keycode[keycode-1]);
.