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 770:429ff505c450
prev736:a02d1475ccfd
next780:4e4ea322cb84
author nkeynes
date Mon Jul 28 03:41:25 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Implement key-binding configuration pane for Cocoa UI
Minor tweaks for consistency and static-correctness
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 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);
    38 static gchar *video_osx_keycode_to_keysym(uint16_t keycode);
    40 struct display_driver display_osx_driver = { 
    41         "osx",
    42         N_("OS X Cocoa GUI-based OpenGL driver"),
    43         video_osx_init, video_osx_shutdown,
    44         video_osx_resolve_keysym,
    45         video_osx_keycode_to_dckeysym,
    46         video_osx_keycode_to_keysym,
    47         NULL, NULL, NULL, NULL, NULL, 
    48         video_osx_display_blank, NULL };
    51 static NSView *video_view = NULL;
    52 int video_width = 640;
    53 int video_height = 480;
    55 #define MAX_MASK_KEYCODE 128
    57 @interface LxdreamVideoView : NSView
    58 {
    59     BOOL isGrabbed;
    60     int buttonMask;
    61     int flagsMask[MAX_MASK_KEYCODE];
    62 }
    63 - (BOOL)isOpaque;
    64 - (BOOL)isFlipped;
    65 - (void)drawRect: (NSRect) rect;
    66 @end
    68 @implementation LxdreamVideoView
    69 //--------------------------------------------------------------------
    70 - (id)initWithFrame: (NSRect)contentRect
    71 {
    72     if( [super initWithFrame: contentRect] != nil ) {
    73         int i;
    74         isGrabbed = NO;
    75         buttonMask = 0;
    76         for( i=0; i<MAX_MASK_KEYCODE; i++ ) {
    77             flagsMask[i] = 0;
    78         }
    79         return self;
    80     }
    81     return nil;
    82 }
    83 - (BOOL)isOpaque
    84 {
    85     return YES;
    86 }
    87 - (BOOL)acceptsFirstResponder
    88 {
    89     return YES;
    90 }
    91 - (BOOL)isFlipped
    92 {
    93     return YES;
    94 }
    95 //--------------------------------------------------------------------
    96 - (void)drawRect: (NSRect) rect
    97 {
    98     NSSize size = [self frame].size;
    99     if( video_width != size.width || video_height != size.height ) {
   100         video_width = size.width;
   101         video_height = size.height;
   102         video_nsgl_update();
   103     }
   104     pvr2_redraw_display();
   105 }
   106 - (void)keyDown: (NSEvent *) event
   107 {
   108     if( ![event isARepeat] ) {
   109         input_event_keydown( NULL, [event keyCode]+1, 1 );
   110     }
   111 }
   112 - (void)keyUp: (NSEvent *) event
   113 {
   114     input_event_keyup( NULL, [event keyCode]+1, 1 );
   115 }
   116 - (void)flagsChanged: (NSEvent *) event
   117 {
   118     int keycode = [event keyCode];
   119     if ( isGrabbed && ([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask) ) {
   120         // Release the display grab
   121         isGrabbed = NO;
   122         [NSCursor unhide];
   123         CGAssociateMouseAndMouseCursorPosition(YES);
   124         [((LxdreamMainWindow *)[self window]) setIsGrabbed: NO];
   125     }
   127     if( flagsMask[keycode] == 0 ) {
   128         input_event_keydown( NULL, keycode+1, 1 );
   129         flagsMask[keycode] = 1;
   130     } else {
   131         input_event_keyup( NULL, keycode+1, 1 );
   132         flagsMask[keycode] = 0;
   133     }
   134 }
   135 - (void)mouseDown: (NSEvent *) event
   136 {
   137     if( isGrabbed ) { 
   138         buttonMask |= 1;
   139         input_event_mouse( buttonMask, 0, 0 );
   140     } else { // take display grab
   141         isGrabbed = YES;
   142         [NSCursor hide];
   143         CGAssociateMouseAndMouseCursorPosition(NO);
   144         [((LxdreamMainWindow *)[self window]) setIsGrabbed: YES];
   145     }
   146 }
   147 - (void)mouseUp: (NSEvent *)event
   148 {
   149     buttonMask &= ~1;
   150     input_event_mouse( buttonMask, 0, 0 );
   151 }
   153 - (void)rightMouseDown: (NSEvent *) event
   154 {
   155     buttonMask |= 2;
   156     input_event_mouse( buttonMask, 0, 0 );
   157 }
   158 - (void)rightMouseUp: (NSEvent *)event
   159 {
   160     buttonMask &= ~2;
   161     input_event_mouse( buttonMask, 0, 0 );
   162 }
   163 - (void)otherMouseDown: (NSEvent *) event
   164 {
   165     buttonMask |= (1<< [event buttonNumber] );
   166     input_event_mouse( buttonMask, 0, 0 );
   167 }
   168 - (void)otherMouseUp: (NSEvent *) event
   169 {
   170     buttonMask &= ~(1<< [event buttonNumber] );
   171     input_event_mouse( buttonMask, 0, 0 );
   172 }
   173 - (void)mouseMoved: (NSEvent *) event
   174 {
   175     if( isGrabbed ) {
   176         input_event_mouse( buttonMask, [event deltaX], [event deltaY] );
   177     }
   178 }
   179 - (void)mouseDragged: (NSEvent *) event
   180 {
   181     if( isGrabbed ) {
   182         input_event_mouse( buttonMask, [event deltaX], [event deltaY] );
   183     }
   184 }
   185 - (void)rightMouseDragged: (NSEvent *) event
   186 {
   187     if( isGrabbed ) {
   188         input_event_mouse( buttonMask, [event deltaX], [event deltaY] );
   189     }
   190 }
   191 - (void)otherMouseDragged: (NSEvent *) event
   192 {
   193     if( isGrabbed ) {
   194         input_event_mouse( buttonMask, [event deltaX], [event deltaY] );
   195     }
   196 }
   198 @end
   200 NSView *video_osx_create_drawable()
   201 {
   202     NSRect contentRect = {{0,0},{640,480}};
   203     video_view = [[LxdreamVideoView alloc] initWithFrame: contentRect];
   204     [video_view setAutoresizingMask: (NSViewWidthSizable|NSViewHeightSizable)];
   205     return video_view;
   206 }
   208 static gboolean video_osx_init()
   209 {
   210     if( video_view == NULL ) {
   211         return FALSE;
   212     }
   213     if( !video_nsgl_init_driver(video_view, &display_osx_driver) ) {
   214         return FALSE;
   215     }
   216     pvr2_setup_gl_context();
   217     return TRUE;
   218 }
   220 static void video_osx_shutdown()
   221 {
   222 }
   224 static void video_osx_display_blank( uint32_t colour )
   225 {
   226 }
   228 static int mac_keymap_cmp(const void *a, const void *b)
   229 {
   230     const gchar *key = a;
   231     const struct mac_keymap_struct *kb = b;
   232     return strcasecmp(key, kb->name);
   233 }
   235 static uint16_t video_osx_resolve_keysym( const gchar *keysym )
   236 {
   237     struct mac_keymap_struct *result = bsearch( keysym, mac_keysyms, mac_keysym_count, sizeof(struct mac_keymap_struct), mac_keymap_cmp );
   238     if( result == NULL ) {
   239         return 0;
   240     } else {
   241         return result->keycode + 1;
   242     }
   243 }
   245 static uint16_t video_osx_keycode_to_dckeysym(uint16_t keycode)
   246 {
   247     if( keycode < 1 || keycode > 128 ) {
   248         return DCKB_NONE;
   249     } else {
   250         return mac_keycode_to_dckeysym[keycode-1];
   251     }
   252 }
   254 static gchar *video_osx_keycode_to_keysym(uint16_t keycode)
   255 {
   256     if( keycode < 1 || keycode > 128 ) {
   257         return NULL;
   258     } else {
   259         return g_strdup(mac_keysyms_by_keycode[keycode-1]);
   260     }
.