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 static gboolean video_osx_init();
34 static void video_osx_shutdown();
35 static void video_osx_display_blank( uint32_t colour );
36 static uint16_t video_osx_resolve_keysym( const gchar *keysym );
37 static uint16_t video_osx_keycode_to_dckeysym(uint16_t keycode);
39 struct display_driver display_osx_driver = { "osx", video_osx_init, video_osx_shutdown,
40 video_osx_resolve_keysym,
41 video_osx_keycode_to_dckeysym,
43 NULL, NULL, NULL, NULL, NULL,
44 video_osx_display_blank, NULL };
47 static NSView *video_view = NULL;
48 int video_width = 640;
49 int video_height = 480;
51 #define MAX_MASK_KEYCODE 128
53 @interface LxdreamVideoView : NSView
57 int flagsMask[MAX_MASK_KEYCODE];
61 - (void)drawRect: (NSRect) rect;
64 @implementation LxdreamVideoView
65 //--------------------------------------------------------------------
66 - (id)initWithFrame: (NSRect)contentRect
68 if( [super initWithFrame: contentRect] != nil ) {
72 for( i=0; i<MAX_MASK_KEYCODE; i++ ) {
83 - (BOOL)acceptsFirstResponder
91 //--------------------------------------------------------------------
92 - (void)drawRect: (NSRect) rect
94 NSSize size = [self frame].size;
95 if( video_width != size.width || video_height != size.height ) {
96 video_width = size.width;
97 video_height = size.height;
100 pvr2_redraw_display();
102 - (void)keyDown: (NSEvent *) event
104 if( ![event isARepeat] ) {
105 input_event_keydown( NULL, [event keyCode]+1, 1 );
108 - (void)keyUp: (NSEvent *) event
110 input_event_keyup( NULL, [event keyCode]+1, 1 );
112 - (void)flagsChanged: (NSEvent *) event
114 int keycode = [event keyCode];
115 if ( isGrabbed && ([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask) ) {
116 // Release the display grab
119 CGAssociateMouseAndMouseCursorPosition(YES);
120 [((LxdreamMainWindow *)[self window]) setIsGrabbed: NO];
123 if( flagsMask[keycode] == 0 ) {
124 input_event_keydown( NULL, keycode+1, 1 );
125 flagsMask[keycode] = 1;
127 input_event_keyup( NULL, keycode+1, 1 );
128 flagsMask[keycode] = 0;
131 - (void)mouseDown: (NSEvent *) event
135 input_event_mouse( buttonMask, 0, 0 );
136 } else { // take display grab
139 CGAssociateMouseAndMouseCursorPosition(NO);
140 [((LxdreamMainWindow *)[self window]) setIsGrabbed: YES];
143 - (void)mouseUp: (NSEvent *)event
146 input_event_mouse( buttonMask, 0, 0 );
149 - (void)rightMouseDown: (NSEvent *) event
152 input_event_mouse( buttonMask, 0, 0 );
154 - (void)rightMouseUp: (NSEvent *)event
157 input_event_mouse( buttonMask, 0, 0 );
159 - (void)otherMouseDown: (NSEvent *) event
161 buttonMask |= (1<< [event buttonNumber] );
162 input_event_mouse( buttonMask, 0, 0 );
164 - (void)otherMouseUp: (NSEvent *) event
166 buttonMask &= ~(1<< [event buttonNumber] );
167 input_event_mouse( buttonMask, 0, 0 );
169 - (void)mouseMoved: (NSEvent *) event
172 input_event_mouse( buttonMask, [event deltaX], [event deltaY] );
175 - (void)mouseDragged: (NSEvent *) event
178 input_event_mouse( buttonMask, [event deltaX], [event deltaY] );
181 - (void)rightMouseDragged: (NSEvent *) event
184 input_event_mouse( buttonMask, [event deltaX], [event deltaY] );
187 - (void)otherMouseDragged: (NSEvent *) event
190 input_event_mouse( buttonMask, [event deltaX], [event deltaY] );
196 NSView *video_osx_create_drawable()
198 NSRect contentRect = {{0,0},{640,480}};
199 video_view = [[LxdreamVideoView alloc] initWithFrame: contentRect];
200 [video_view setAutoresizingMask: (NSViewWidthSizable|NSViewHeightSizable)];
204 static gboolean video_osx_init()
206 if( video_view == NULL ) {
209 if( !video_nsgl_init_driver(video_view, &display_osx_driver) ) {
212 pvr2_setup_gl_context();
216 static void video_osx_shutdown()
220 static void video_osx_display_blank( uint32_t colour )
224 static int mac_keymap_cmp(const void *a, const void *b)
226 const gchar *key = a;
227 const struct mac_keymap_struct *kb = b;
228 return strcasecmp(key, kb->name);
231 static uint16_t video_osx_resolve_keysym( const gchar *keysym )
233 struct mac_keymap_struct *result = bsearch( keysym, mac_keysyms, mac_keysym_count, sizeof(struct mac_keymap_struct), mac_keymap_cmp );
234 if( result == NULL ) {
237 return result->keycode + 1;
241 static uint16_t video_osx_keycode_to_dckeysym(uint16_t keycode)
243 if( keycode < 1 || keycode > 128 ) {
246 return mac_keycode_to_dckeysym[keycode-1];
.