# HG changeset patch # User John Tsiombikas # Date 1309835971 -10800 # Node ID 46e90f9c1e0f4850d2d01ef5e3afba9e06e4be6d # Parent fc2dba4c5a5f134bd461ead650874a3360cd2d5b added most of the missing events in the cocoa module diff -r fc2dba4c5a5f -r 46e90f9c1e0f src/wsys_cocoa.m --- a/src/wsys_cocoa.m Sun Jul 03 05:23:30 2011 +0300 +++ b/src/wsys_cocoa.m Tue Jul 05 06:19:31 2011 +0300 @@ -10,8 +10,6 @@ #include "wsys.h" #include "log.h" -#define APPLE_SUCKS() sgl_log("%s called\n", __func__) - @interface OpenGLView : NSOpenGLView { int foo; @@ -21,7 +19,7 @@ -(void) drawRect: (NSRect) rect; -(void) reshape; -/*-(void) keyDown: (NSEvent*) ev; +-(void) keyDown: (NSEvent*) ev; -(void) keyUp: (NSEvent*) ev; -(void) mouseDown: (NSEvent*) ev; -(void) mouseUp: (NSEvent*) ev; @@ -31,7 +29,7 @@ -(void) otherMouseUp: (NSEvent*) ev; -(void) mouseDragged: (NSEvent*) ev; -(void) rightMouseDragged: (NSEvent*) ev; --(void) otherMouseDragged: (NSEvent*) ev;*/ +-(void) otherMouseDragged: (NSEvent*) ev; -(BOOL) acceptsFirstResponder; @end @@ -39,6 +37,7 @@ @interface AppDelegate : NSObject { + int foo; } -(void) applicationWillFinishLaunching: (NSNotification*) notification; @@ -53,6 +52,7 @@ @interface WinDelegate : NSObject { + @public struct window *win; } -(id) init; @@ -101,6 +101,11 @@ static void set_event(int idx, int enable); static int process_events(void); +static void select_event_window(NSEvent *ev); +static void handle_key(NSEvent *ev, int state); +static void handle_mouse(NSEvent *ev, int state); +static void handle_motion(NSEvent *ev); + static void fill_attr(NSOpenGLPixelFormatAttribute *attr, unsigned int flags); @@ -137,14 +142,12 @@ -(id) initWithFrame: (NSRect) frame pixelFormat: (NSOpenGLPixelFormat*) pf { - APPLE_SUCKS(); self = [super initWithFrame: frame pixelFormat: pf]; return self; } -(void) drawRect: (NSRect) rect { - APPLE_SUCKS(); sgl_display_callback_t func = sgl_get_callback(SGL_DISPLAY); if(func) { func(); @@ -153,7 +156,6 @@ -(void) reshape { - APPLE_SUCKS(); NSSize sz; sgl_reshape_callback_t func; @@ -167,9 +169,64 @@ } } +-(void) keyDown: (NSEvent*) ev +{ + handle_key(ev, 1); +} + +-(void) keyUp: (NSEvent*) ev +{ + handle_key(ev, 0); +} + +-(void) mouseDown: (NSEvent*) ev +{ + handle_mouse(ev, 1); +} + +-(void) mouseUp: (NSEvent*) ev +{ + handle_mouse(ev, 0); +} + +-(void) rightMouseDown: (NSEvent*) ev +{ + handle_mouse(ev, 1); +} + +-(void) rightMouseUp: (NSEvent*) ev +{ + handle_mouse(ev, 0); +} + +-(void) otherMouseDown: (NSEvent*) ev +{ + handle_mouse(ev, 1); +} + +-(void) otherMouseUp: (NSEvent*) ev +{ + handle_mouse(ev, 0); +} + +-(void) mouseDragged: (NSEvent*) ev +{ + handle_motion(ev); +} + +-(void) rightMouseDragged: (NSEvent*) ev +{ + handle_motion(ev); +} + +-(void) otherMouseDragged: (NSEvent*) ev +{ + handle_motion(ev); +} + + -(BOOL) acceptsFirstResponder { - APPLE_SUCKS(); return YES; } @end @@ -177,29 +234,24 @@ @implementation AppDelegate -(void) applicationWillFinishLaunching: (NSNotification*) notification { - APPLE_SUCKS(); } -(void) applicationDidFinishLaunching: (NSNotification*) notification { - APPLE_SUCKS(); } -(BOOL) applicationShouldTerminate: (NSApplication*) app { - APPLE_SUCKS(); return NSTerminateNow; } -(BOOL) applicationShouldTerminateAfterLastWindowClosed: (NSApplication*) app { - APPLE_SUCKS(); return YES; } -(void) applicationWillTerminate: (NSNotification*) notification { - APPLE_SUCKS(); /*[NSApp setDelegate: nil]; [global_pool drain];*/ } @@ -208,30 +260,25 @@ @implementation WinDelegate -(id) init { - APPLE_SUCKS(); self = [super init]; return self; } -(void) dealloc { - APPLE_SUCKS(); [super dealloc]; } -(void) windowDidExpose: (NSNotification*) notification { - APPLE_SUCKS(); } -(void) windowDidResize: (NSNotification*) notification { - APPLE_SUCKS(); } -(BOOL) windowShouldClose: (id) win { - APPLE_SUCKS(); assert(self->win); close_window(self->win->wid); return YES; @@ -239,14 +286,12 @@ -(void) windowWillClose: (NSNotification*) notification { - APPLE_SUCKS(); /*[NSApp terminate: nil];*/ } @end static int init(void) { - APPLE_SUCKS(); AppDelegate *delegate; global_pool = [[NSAutoreleasePool alloc] init]; @@ -260,7 +305,6 @@ static void shutdown(void) { - APPLE_SUCKS(); while(winlist) { close_window(winlist->wid); } @@ -285,7 +329,6 @@ /* create/destroy windows */ static int create_window(int xsz, int ysz, unsigned int flags) { - APPLE_SUCKS(); NSAutoreleasePool *pool; WinDelegate *delegate; NSWindow *nswin; @@ -349,7 +392,6 @@ static void close_window(int wid) { - APPLE_SUCKS(); struct window *win, *prev, dummy; sgl_close_callback_t close_func; @@ -439,7 +481,19 @@ static int get_modifiers(void) { - return 0; /* TODO */ + unsigned int nsmod = [NSEvent modifierFlags]; + unsigned int mod = 0; + + if(nsmod & NSShiftKeyMask) { + mod |= SGL_MOD_SHIFT; + } + if(nsmod & NSControlKeyMask) { + mod |= SGL_MOD_CONTROL; + } + if(nsmod & NSAlternateKeyMask) { + mod |= SGL_MOD_ALT; + } + return mod; } @@ -500,6 +554,71 @@ return quit_main_loop ? -1 : 0; } +static void select_event_window(NSEvent *ev) +{ + NSWindow *nswin; + WinDelegate *del; + struct window *win; + + if(!ev || !(nswin = [ev window])) { + sgl_log("%s failed\n", __func__); + return; + } + del = [nswin delegate]; + win = del->win; + + activate_window(win); +} + +static void handle_key(NSEvent *ev, int state) +{ + NSString *str; + sgl_keyboard_callback_t func = sgl_get_callback(SGL_KEYBOARD); + + if(func) { + str = [ev characters]; + if([str length]) { + unichar c = [str characterAtIndex: 0]; + + select_event_window(ev); + func(c, state); + } + } +} + +static void handle_mouse(NSEvent *ev, int state) +{ + int bn; + NSPoint pt; + sgl_mouse_callback_t func = sgl_get_callback(SGL_MOUSE); + + if(func) { + bn = [ev buttonNumber]; + if(bn == 2) { + bn = 1; + } else if(bn == 1) { + bn = 2; + } + pt = [ev locationInWindow]; + + select_event_window(ev); + func(0, bn, state, pt.x, pt.y - 1); + } +} + +static void handle_motion(NSEvent *ev) +{ + NSPoint pt; + sgl_motion_callback_t func = sgl_get_callback(SGL_MOTION); + + if(func) { + pt = [ev locationInWindow]; + + select_event_window(ev); + func(0, pt.x, pt.y - 1); + } +} + static void fill_attr(NSOpenGLPixelFormatAttribute *attr, unsigned int flags) { int i = 0;