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 781:88d48559380a
prev780:4e4ea322cb84
next805:b355f7b3ff2e
author nkeynes
date Tue Jul 29 08:51:10 2008 +0000 (15 years ago)
permissions -rw-r--r--
last change Fix minor warnings
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         video_osx_display_blank, NULL };
    54 static NSView *video_view = NULL;
    55 int video_width = 640;
    56 int video_height = 480;
    58 #define MAX_MASK_KEYCODE 128
    60 @interface LxdreamOSXView : LxdreamVideoView
    61 {
    62     int buttonMask;
    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         buttonMask = 0;
    90         for( i=0; i<MAX_MASK_KEYCODE; i++ ) {
    91             flagsMask[i] = 0;
    92         }
    93         return self;
    94     }
    95     return nil;
    96 }
    97 - (void)requestGrab
    98 {
    99     if( delegate && [delegate respondsToSelector: @selector(viewRequestedGrab:)] )
   100         [delegate performSelector: @selector(viewRequestedGrab:) withObject: self];
   101 }
   102 - (void)requestUngrab
   103 {
   104     if( delegate && [delegate respondsToSelector: @selector(viewRequestedUngrab:)] )
   105         [delegate performSelector: @selector(viewRequestedUngrab:) withObject: self];
   106 }
   107 - (BOOL)isOpaque
   108 {
   109     return YES;
   110 }
   111 - (BOOL)acceptsFirstResponder
   112 {
   113     return YES;
   114 }
   115 - (BOOL)isFlipped
   116 {
   117     return YES;
   118 }
   119 //--------------------------------------------------------------------
   120 - (void)drawRect: (NSRect) rect
   121 {
   122     NSSize size = [self frame].size;
   123     if( video_width != size.width || video_height != size.height ) {
   124         video_width = size.width;
   125         video_height = size.height;
   126         video_nsgl_update();
   127     }
   128     pvr2_redraw_display();
   129 }
   130 - (void)keyDown: (NSEvent *) event
   131 {
   132     if( ![event isARepeat] ) {
   133         input_event_keydown( NULL, [event keyCode]+1, 1 );
   134     }
   135 }
   136 - (void)keyUp: (NSEvent *) event
   137 {
   138     input_event_keyup( NULL, [event keyCode]+1, 1 );
   139 }
   140 - (void)flagsChanged: (NSEvent *) event
   141 {
   142     int keycode = [event keyCode];
   143     if( ([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask) ) {
   144         [self requestUngrab];
   145     }
   147     if( flagsMask[keycode] == 0 ) {
   148         input_event_keydown( NULL, keycode+1, 1 );
   149         flagsMask[keycode] = 1;
   150     } else {
   151         input_event_keyup( NULL, keycode+1, 1 );
   152         flagsMask[keycode] = 0;
   153     }
   154 }
   155 - (void)mouseDown: (NSEvent *) event
   156 {
   157     if( isGrabbed ) { 
   158         buttonMask |= 1;
   159         input_event_mouse( buttonMask, 0, 0 );
   160     } else {
   161         [self requestGrab];
   162     }
   163 }
   164 - (void)mouseUp: (NSEvent *)event
   165 {
   166     buttonMask &= ~1;
   167     input_event_mouse( buttonMask, 0, 0 );
   168 }
   170 - (void)rightMouseDown: (NSEvent *) event
   171 {
   172     buttonMask |= 2;
   173     input_event_mouse( buttonMask, 0, 0 );
   174 }
   175 - (void)rightMouseUp: (NSEvent *)event
   176 {
   177     buttonMask &= ~2;
   178     input_event_mouse( buttonMask, 0, 0 );
   179 }
   180 - (void)otherMouseDown: (NSEvent *) event
   181 {
   182     buttonMask |= (1<< [event buttonNumber] );
   183     input_event_mouse( buttonMask, 0, 0 );
   184 }
   185 - (void)otherMouseUp: (NSEvent *) event
   186 {
   187     buttonMask &= ~(1<< [event buttonNumber] );
   188     input_event_mouse( buttonMask, 0, 0 );
   189 }
   190 - (void)mouseMoved: (NSEvent *) event
   191 {
   192     if( isGrabbed ) {
   193         input_event_mouse( buttonMask, [event deltaX] * MOUSE_X_SCALE, [event deltaY] * MOUSE_Y_SCALE );
   194     }
   195 }
   196 - (void)mouseDragged: (NSEvent *) event
   197 {
   198     if( isGrabbed ) {
   199         input_event_mouse( buttonMask, [event deltaX] * MOUSE_X_SCALE, [event deltaY] * MOUSE_Y_SCALE );
   200     }
   201 }
   202 - (void)rightMouseDragged: (NSEvent *) event
   203 {
   204     if( isGrabbed ) {
   205         input_event_mouse( buttonMask, [event deltaX] * MOUSE_X_SCALE, [event deltaY] * MOUSE_Y_SCALE );
   206     }
   207 }
   208 - (void)otherMouseDragged: (NSEvent *) event
   209 {
   210     if( isGrabbed ) {
   211         input_event_mouse( buttonMask, [event deltaX] * MOUSE_X_SCALE, [event deltaY] * MOUSE_Y_SCALE );
   212     }
   213 }
   215 @end
   217 NSView *video_osx_create_drawable()
   218 {
   219     NSRect contentRect = {{0,0},{640,480}};
   220     video_view = [[LxdreamOSXView alloc] initWithFrame: contentRect];
   221     [video_view setAutoresizingMask: (NSViewWidthSizable|NSViewHeightSizable)];
   222     return video_view;
   223 }
   225 static gboolean video_osx_init()
   226 {
   227     if( video_view == NULL ) {
   228         return FALSE;
   229     }
   230     if( !video_nsgl_init_driver(video_view, &display_osx_driver) ) {
   231         return FALSE;
   232     }
   233     pvr2_setup_gl_context();
   234     return TRUE;
   235 }
   237 static void video_osx_shutdown()
   238 {
   239 }
   241 static void video_osx_display_blank( uint32_t colour )
   242 {
   243 }
   245 static int mac_keymap_cmp(const void *a, const void *b)
   246 {
   247     const gchar *key = a;
   248     const struct mac_keymap_struct *kb = b;
   249     return strcasecmp(key, kb->name);
   250 }
   252 static uint16_t video_osx_resolve_keysym( const gchar *keysym )
   253 {
   254     struct mac_keymap_struct *result = bsearch( keysym, mac_keysyms, mac_keysym_count, sizeof(struct mac_keymap_struct), mac_keymap_cmp );
   255     if( result == NULL ) {
   256         return 0;
   257     } else {
   258         return result->keycode + 1;
   259     }
   260 }
   262 static uint16_t video_osx_keycode_to_dckeysym(uint16_t keycode)
   263 {
   264     if( keycode < 1 || keycode > 128 ) {
   265         return DCKB_NONE;
   266     } else {
   267         return mac_keycode_to_dckeysym[keycode-1];
   268     }
   269 }
   271 static gchar *video_osx_keycode_to_keysym(uint16_t keycode)
   272 {
   273     if( keycode < 1 || keycode > 128 ) {
   274         return NULL;
   275     } else {
   276         return g_strdup(mac_keysyms_by_keycode[keycode-1]);
   277     }
.