Search
lxdream.org :: lxdream/src/drivers/video_osx.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/video_osx.c
changeset 850:28782ebbd01d
prev839:51f1c4195790
next854:130928a3cdcb
author nkeynes
date Mon Sep 08 07:56:33 2008 +0000 (15 years ago)
permissions -rw-r--r--
last change Add lightgun support
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * The OS/X side of the video support (responsible for actually displaying / 
     5  * rendering frames)
     6  *
     7  * Copyright (c) 2008 Nathan Keynes.
     8  *
     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.
    13  *
    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.
    18  */
    20 #include <stdlib.h>
    21 #include <string.h>
    22 #include "lxdream.h"
    23 #include "display.h"
    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 = { 
    44         "osx",
    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,
    51         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
    62 {
    63     int flagsMask[MAX_MASK_KEYCODE];
    64 }
    65 @end
    67 @implementation LxdreamVideoView
    68 - (void)setIsGrabbed: (BOOL)grabbed
    69 {
    70     isGrabbed = grabbed;
    71 }
    72 - (void) setDelegate: (id)other
    73 {
    74     delegate = other;
    75 }
    76 - (id)delegate 
    77 {
    78     return delegate;
    79 }
    80 @end
    82 @implementation LxdreamOSXView
    83 //--------------------------------------------------------------------
    84 - (id)initWithFrame: (NSRect)contentRect
    85 {
    86     if( [super initWithFrame: contentRect] != nil ) {
    87         int i;
    88         isGrabbed = NO;
    89         for( i=0; i<MAX_MASK_KEYCODE; i++ ) {
    90             flagsMask[i] = 0;
    91         }
    92         return self;
    93     }
    94     return nil;
    95 }
    96 - (BOOL)requestGrab
    97 {
    98     if( delegate && [delegate respondsToSelector: @selector(viewRequestedGrab:)] )
    99         return [delegate performSelector: @selector(viewRequestedGrab:) withObject: self] != nil;
   100     return NO;
   101 }
   102 - (BOOL)requestUngrab
   103 {
   104     if( delegate && [delegate respondsToSelector: @selector(viewRequestedUngrab:)] )
   105         return [delegate performSelector: @selector(viewRequestedUngrab:) withObject: self] != nil;
   106     return NO;
   107 }
   108 - (BOOL)isOpaque
   109 {
   110     return YES;
   111 }
   112 - (BOOL)acceptsFirstResponder
   113 {
   114     return YES;
   115 }
   116 - (BOOL)isFlipped
   117 {
   118     return YES;
   119 }
   120 //--------------------------------------------------------------------
   121 - (void)drawRect: (NSRect) rect
   122 {
   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;
   127         video_nsgl_update();
   128     }
   129     pvr2_redraw_display();
   130 }
   131 - (void)keyDown: (NSEvent *) event
   132 {
   133     if( ![event isARepeat] ) {
   134         input_event_keydown( NULL, [event keyCode]+1, 1 );
   135     }
   136 }
   137 - (void)keyUp: (NSEvent *) event
   138 {
   139     input_event_keyup( NULL, [event keyCode]+1, 1 );
   140 }
   141 - (void)flagsChanged: (NSEvent *) event
   142 {
   143     int keycode = [event keyCode];
   144     if( ([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask) ) {
   145         [self requestUngrab];
   146     }
   148     if( flagsMask[keycode] == 0 ) {
   149         input_event_keydown( NULL, keycode+1, 1 );
   150         flagsMask[keycode] = 1;
   151     } else {
   152         input_event_keyup( NULL, keycode+1, 1 );
   153         flagsMask[keycode] = 0;
   154     }
   155 }
   156 - (void)emitMouseDownEvent: (NSEvent *)event button: (int)button
   157 {
   158     if( isGrabbed ) {
   159         input_event_mousedown( button, 0, 0, FALSE );
   160     } else {
   161         NSPoint pt = [event locationInWindow];
   162         input_event_mousedown( button, (int)(pt.x * DISPLAY_WIDTH) / video_width, 
   163                                DISPLAY_HEIGHT - ((int)(pt.y * DISPLAY_HEIGHT) / video_height), TRUE );
   164     }
   165 }
   166 - (void)emitMouseUpEvent: (NSEvent *)event button: (int)button
   167 {
   168     if( isGrabbed ) {
   169         input_event_mouseup( button, 0, 0, FALSE );
   170     } else {
   171         NSPoint pt = [event locationInWindow];
   172         input_event_mouseup( button, (int)(pt.x * DISPLAY_WIDTH) / video_width, 
   173                              DISPLAY_HEIGHT - ((int)(pt.y * DISPLAY_HEIGHT) / video_height), TRUE );
   174     }
   175 }
   176 - (void)emitMouseMoveEvent: (NSEvent *)event
   177 {
   178     if( isGrabbed ) {
   179         input_event_mousemove( [event deltaX] * MOUSE_X_SCALE, [event deltaY] * MOUSE_Y_SCALE, FALSE );
   180     } else {
   181         NSPoint pt = [event locationInWindow];
   182         input_event_mousemove( (int)(pt.x * DISPLAY_WIDTH) / video_width, 
   183                                DISPLAY_HEIGHT - ((int)(pt.y * DISPLAY_HEIGHT) / video_height), TRUE );
   184     }    
   185 }
   186 - (void)mouseExited: (NSEvent *)event
   187 {
   188     if( !isGrabbed ) {
   189         input_event_mousemove( -1, -1, TRUE );
   190     }
   191 }
   193 - (void)mouseDown: (NSEvent *) event
   194 {
   195     // If using grab but not grabbed yet, the first click should be consumed
   196     // by the grabber. In all other circumstances we process normally.
   197     if( isGrabbed || ![self requestGrab] ) {
   198         [self emitMouseDownEvent: event button: 0];
   199     }
   200 }
   201 - (void)mouseUp: (NSEvent *)event
   202 {
   203     [self emitMouseUpEvent: event button: 0];
   204 }
   206 - (void)rightMouseDown: (NSEvent *) event
   207 {
   208     [self emitMouseDownEvent: event button: 1];
   209 }
   210 - (void)rightMouseUp: (NSEvent *)event
   211 {
   212     [self emitMouseUpEvent: event button: 1];
   213 }
   214 - (void)otherMouseDown: (NSEvent *) event
   215 {
   216     [self emitMouseDownEvent: event button: [event buttonNumber]];
   217 }
   218 - (void)otherMouseUp: (NSEvent *) event
   219 {
   220     [self emitMouseUpEvent: event button: [event buttonNumber]];
   221 }
   222 - (void)mouseMoved: (NSEvent *) event
   223 {
   224     [self emitMouseMoveEvent: event];
   225 }
   226 - (void)mouseDragged: (NSEvent *) event
   227 {
   228     [self emitMouseMoveEvent: event];
   229 }
   230 - (void)rightMouseDragged: (NSEvent *) event
   231 {
   232     [self emitMouseMoveEvent: event];
   233 }
   234 - (void)otherMouseDragged: (NSEvent *) event
   235 {
   236     [self emitMouseMoveEvent: event];
   237 }
   239 @end
   241 NSView *video_osx_create_drawable()
   242 {
   243     NSRect contentRect = {{0,0},{640,480}};
   244     video_view = [[LxdreamOSXView alloc] initWithFrame: contentRect];
   245     [video_view setAutoresizingMask: (NSViewWidthSizable|NSViewHeightSizable)];
   246     return video_view;
   247 }
   249 static gboolean video_osx_init()
   250 {
   251     if( video_view == NULL ) {
   252         return FALSE;
   253     }
   254     if( !video_nsgl_init_driver(video_view, &display_osx_driver) ) {
   255         return FALSE;
   256     }
   257     pvr2_setup_gl_context();
   258     return TRUE;
   259 }
   261 static void video_osx_shutdown()
   262 {
   263 }
   265 static void video_osx_display_blank( uint32_t colour )
   266 {
   267 }
   269 static int mac_keymap_cmp(const void *a, const void *b)
   270 {
   271     const gchar *key = a;
   272     const struct mac_keymap_struct *kb = b;
   273     return strcasecmp(key, kb->name);
   274 }
   276 static uint16_t video_osx_resolve_keysym( const gchar *keysym )
   277 {
   278     struct mac_keymap_struct *result = bsearch( keysym, mac_keysyms, mac_keysym_count, sizeof(struct mac_keymap_struct), mac_keymap_cmp );
   279     if( result == NULL ) {
   280         return 0;
   281     } else {
   282         return result->keycode + 1;
   283     }
   284 }
   286 static uint16_t video_osx_keycode_to_dckeysym(uint16_t keycode)
   287 {
   288     if( keycode < 1 || keycode > 128 ) {
   289         return DCKB_NONE;
   290     } else {
   291         return mac_keycode_to_dckeysym[keycode-1];
   292     }
   293 }
   295 static gchar *video_osx_keycode_to_keysym(uint16_t keycode)
   296 {
   297     if( keycode < 1 || keycode > 128 ) {
   298         return NULL;
   299     } else {
   300         return g_strdup(mac_keysyms_by_keycode[keycode-1]);
   301     }
.