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