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 681:1755a126b109
next700:4650d0c7f6f9
author nkeynes
date Sat Jun 14 11:54:15 2008 +0000 (15 years ago)
permissions -rw-r--r--
last change Change colour params to float
Convert background processing over to scene structure (fixes some depth issues as well)
Add color unclamp when supported
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 = { "osx", video_osx_init, video_osx_shutdown,
    40                     video_osx_resolve_keysym,
    41                     video_osx_keycode_to_dckeysym,
    42                     NULL,
    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
    54 {
    55     BOOL isGrabbed;
    56     int buttonMask;
    57     int flagsMask[MAX_MASK_KEYCODE];
    58 }
    59 - (BOOL)isOpaque;
    60 - (BOOL)isFlipped;
    61 - (void)drawRect: (NSRect) rect;
    62 @end
    64 @implementation LxdreamVideoView
    65 //--------------------------------------------------------------------
    66 - (id)initWithFrame: (NSRect)contentRect
    67 {
    68     if( [super initWithFrame: contentRect] != nil ) {
    69         int i;
    70         isGrabbed = NO;
    71         buttonMask = 0;
    72         for( i=0; i<MAX_MASK_KEYCODE; i++ ) {
    73             flagsMask[i] = 0;
    74         }
    75         return self;
    76     }
    77     return nil;
    78 }
    79 - (BOOL)isOpaque
    80 {
    81    return YES;
    82 }
    83 - (BOOL)acceptsFirstResponder
    84 {
    85    return YES;
    86 }
    87 - (BOOL)isFlipped
    88 {
    89    return YES;
    90 }
    91 //--------------------------------------------------------------------
    92 - (void)drawRect: (NSRect) rect
    93 {
    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;
    98         video_nsgl_update();
    99     }
   100     pvr2_redraw_display();
   101 }
   102 - (void)keyDown: (NSEvent *) event
   103 {
   104     if( ![event isARepeat] ) {
   105         input_event_keydown( NULL, [event keyCode]+1, 1 );
   106     }
   107 }
   108 - (void)keyUp: (NSEvent *) event
   109 {
   110     input_event_keyup( NULL, [event keyCode]+1, 1 );
   111 }
   112 - (void)flagsChanged: (NSEvent *) event
   113 {
   114     int keycode = [event keyCode];
   115     if ( isGrabbed && ([event modifierFlags] & NSControlKeyMask) && ([event modifierFlags] & NSAlternateKeyMask) ) {
   116         // Release the display grab
   117         isGrabbed = NO;
   118         [NSCursor unhide];
   119         CGAssociateMouseAndMouseCursorPosition(YES);
   120         [((LxdreamMainWindow *)[self window]) setIsGrabbed: NO];
   121     }
   123     if( flagsMask[keycode] == 0 ) {
   124         input_event_keydown( NULL, keycode+1, 1 );
   125         flagsMask[keycode] = 1;
   126     } else {
   127         input_event_keyup( NULL, keycode+1, 1 );
   128         flagsMask[keycode] = 0;
   129     }
   130 }
   131 - (void)mouseDown: (NSEvent *) event
   132 {
   133     if( isGrabbed ) { 
   134         buttonMask |= 1;
   135         input_event_mouse( buttonMask, 0, 0 );
   136     } else { // take display grab
   137         isGrabbed = YES;
   138         [NSCursor hide];
   139         CGAssociateMouseAndMouseCursorPosition(NO);
   140         [((LxdreamMainWindow *)[self window]) setIsGrabbed: YES];
   141     }
   142 }
   143 - (void)mouseUp: (NSEvent *)event
   144 {
   145     buttonMask &= ~1;
   146     input_event_mouse( buttonMask, 0, 0 );
   147 }
   149 - (void)rightMouseDown: (NSEvent *) event
   150 {
   151     buttonMask |= 2;
   152     input_event_mouse( buttonMask, 0, 0 );
   153 }
   154 - (void)rightMouseUp: (NSEvent *)event
   155 {
   156     buttonMask &= ~2;
   157     input_event_mouse( buttonMask, 0, 0 );
   158 }
   159 - (void)otherMouseDown: (NSEvent *) event
   160 {
   161     buttonMask |= (1<< [event buttonNumber] );
   162     input_event_mouse( buttonMask, 0, 0 );
   163 }
   164 - (void)otherMouseUp: (NSEvent *) event
   165 {
   166     buttonMask &= ~(1<< [event buttonNumber] );
   167     input_event_mouse( buttonMask, 0, 0 );
   168 }
   169 - (void)mouseMoved: (NSEvent *) event
   170 {
   171     if( isGrabbed ) {
   172         input_event_mouse( buttonMask, [event deltaX], [event deltaY] );
   173     }
   174 }
   175 - (void)mouseDragged: (NSEvent *) event
   176 {
   177     if( isGrabbed ) {
   178         input_event_mouse( buttonMask, [event deltaX], [event deltaY] );
   179     }
   180 }
   181 - (void)rightMouseDragged: (NSEvent *) event
   182 {
   183     if( isGrabbed ) {
   184         input_event_mouse( buttonMask, [event deltaX], [event deltaY] );
   185     }
   186 }
   187 - (void)otherMouseDragged: (NSEvent *) event
   188 {
   189     if( isGrabbed ) {
   190         input_event_mouse( buttonMask, [event deltaX], [event deltaY] );
   191     }
   192 }
   194 @end
   196 NSView *video_osx_create_drawable()
   197 {
   198     NSRect contentRect = {{0,0},{640,480}};
   199     video_view = [[LxdreamVideoView alloc] initWithFrame: contentRect];
   200     [video_view setAutoresizingMask: (NSViewWidthSizable|NSViewHeightSizable)];
   201     return video_view;
   202 }
   204 static gboolean video_osx_init()
   205 {
   206    if( video_view == NULL ) {
   207       return FALSE;
   208    }
   209    if( !video_nsgl_init_driver(video_view, &display_osx_driver) ) {
   210       return FALSE;
   211    }
   212    pvr2_setup_gl_context();
   213    return TRUE;
   214 }
   216 static void video_osx_shutdown()
   217 {
   218 }
   220 static void video_osx_display_blank( uint32_t colour )
   221 {
   222 }
   224 static int mac_keymap_cmp(const void *a, const void *b)
   225 {
   226     const gchar *key = a;
   227     const struct mac_keymap_struct *kb = b;
   228     return strcasecmp(key, kb->name);
   229 }
   231 static uint16_t video_osx_resolve_keysym( const gchar *keysym )
   232 {
   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 ) {
   235         return 0;
   236     } else {
   237         return result->keycode + 1;
   238     }
   239 }
   241 static uint16_t video_osx_keycode_to_dckeysym(uint16_t keycode)
   242 {
   243     if( keycode < 1 || keycode > 128 ) {
   244         return DCKB_NONE;
   245     } else {
   246         return mac_keycode_to_dckeysym[keycode-1];
   247     }
   248 }
.