sgl
diff src/wsys_cocoa.m @ 33:46e90f9c1e0f
added most of the missing events in the cocoa module
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 05 Jul 2011 06:19:31 +0300 |
parents | fc2dba4c5a5f |
children |
line diff
1.1 --- a/src/wsys_cocoa.m Sun Jul 03 05:23:30 2011 +0300 1.2 +++ b/src/wsys_cocoa.m Tue Jul 05 06:19:31 2011 +0300 1.3 @@ -10,8 +10,6 @@ 1.4 #include "wsys.h" 1.5 #include "log.h" 1.6 1.7 -#define APPLE_SUCKS() sgl_log("%s called\n", __func__) 1.8 - 1.9 @interface OpenGLView : NSOpenGLView 1.10 { 1.11 int foo; 1.12 @@ -21,7 +19,7 @@ 1.13 1.14 -(void) drawRect: (NSRect) rect; 1.15 -(void) reshape; 1.16 -/*-(void) keyDown: (NSEvent*) ev; 1.17 +-(void) keyDown: (NSEvent*) ev; 1.18 -(void) keyUp: (NSEvent*) ev; 1.19 -(void) mouseDown: (NSEvent*) ev; 1.20 -(void) mouseUp: (NSEvent*) ev; 1.21 @@ -31,7 +29,7 @@ 1.22 -(void) otherMouseUp: (NSEvent*) ev; 1.23 -(void) mouseDragged: (NSEvent*) ev; 1.24 -(void) rightMouseDragged: (NSEvent*) ev; 1.25 --(void) otherMouseDragged: (NSEvent*) ev;*/ 1.26 +-(void) otherMouseDragged: (NSEvent*) ev; 1.27 1.28 -(BOOL) acceptsFirstResponder; 1.29 @end 1.30 @@ -39,6 +37,7 @@ 1.31 1.32 @interface AppDelegate : NSObject 1.33 { 1.34 + int foo; 1.35 } 1.36 1.37 -(void) applicationWillFinishLaunching: (NSNotification*) notification; 1.38 @@ -53,6 +52,7 @@ 1.39 1.40 @interface WinDelegate : NSObject <NSWindowDelegate> 1.41 { 1.42 + @public 1.43 struct window *win; 1.44 } 1.45 -(id) init; 1.46 @@ -101,6 +101,11 @@ 1.47 static void set_event(int idx, int enable); 1.48 static int process_events(void); 1.49 1.50 +static void select_event_window(NSEvent *ev); 1.51 +static void handle_key(NSEvent *ev, int state); 1.52 +static void handle_mouse(NSEvent *ev, int state); 1.53 +static void handle_motion(NSEvent *ev); 1.54 + 1.55 static void fill_attr(NSOpenGLPixelFormatAttribute *attr, unsigned int flags); 1.56 1.57 1.58 @@ -137,14 +142,12 @@ 1.59 1.60 -(id) initWithFrame: (NSRect) frame pixelFormat: (NSOpenGLPixelFormat*) pf 1.61 { 1.62 - APPLE_SUCKS(); 1.63 self = [super initWithFrame: frame pixelFormat: pf]; 1.64 return self; 1.65 } 1.66 1.67 -(void) drawRect: (NSRect) rect 1.68 { 1.69 - APPLE_SUCKS(); 1.70 sgl_display_callback_t func = sgl_get_callback(SGL_DISPLAY); 1.71 if(func) { 1.72 func(); 1.73 @@ -153,7 +156,6 @@ 1.74 1.75 -(void) reshape 1.76 { 1.77 - APPLE_SUCKS(); 1.78 NSSize sz; 1.79 sgl_reshape_callback_t func; 1.80 1.81 @@ -167,9 +169,64 @@ 1.82 } 1.83 } 1.84 1.85 +-(void) keyDown: (NSEvent*) ev 1.86 +{ 1.87 + handle_key(ev, 1); 1.88 +} 1.89 + 1.90 +-(void) keyUp: (NSEvent*) ev 1.91 +{ 1.92 + handle_key(ev, 0); 1.93 +} 1.94 + 1.95 +-(void) mouseDown: (NSEvent*) ev 1.96 +{ 1.97 + handle_mouse(ev, 1); 1.98 +} 1.99 + 1.100 +-(void) mouseUp: (NSEvent*) ev 1.101 +{ 1.102 + handle_mouse(ev, 0); 1.103 +} 1.104 + 1.105 +-(void) rightMouseDown: (NSEvent*) ev 1.106 +{ 1.107 + handle_mouse(ev, 1); 1.108 +} 1.109 + 1.110 +-(void) rightMouseUp: (NSEvent*) ev 1.111 +{ 1.112 + handle_mouse(ev, 0); 1.113 +} 1.114 + 1.115 +-(void) otherMouseDown: (NSEvent*) ev 1.116 +{ 1.117 + handle_mouse(ev, 1); 1.118 +} 1.119 + 1.120 +-(void) otherMouseUp: (NSEvent*) ev 1.121 +{ 1.122 + handle_mouse(ev, 0); 1.123 +} 1.124 + 1.125 +-(void) mouseDragged: (NSEvent*) ev 1.126 +{ 1.127 + handle_motion(ev); 1.128 +} 1.129 + 1.130 +-(void) rightMouseDragged: (NSEvent*) ev 1.131 +{ 1.132 + handle_motion(ev); 1.133 +} 1.134 + 1.135 +-(void) otherMouseDragged: (NSEvent*) ev 1.136 +{ 1.137 + handle_motion(ev); 1.138 +} 1.139 + 1.140 + 1.141 -(BOOL) acceptsFirstResponder 1.142 { 1.143 - APPLE_SUCKS(); 1.144 return YES; 1.145 } 1.146 @end 1.147 @@ -177,29 +234,24 @@ 1.148 @implementation AppDelegate 1.149 -(void) applicationWillFinishLaunching: (NSNotification*) notification 1.150 { 1.151 - APPLE_SUCKS(); 1.152 } 1.153 1.154 -(void) applicationDidFinishLaunching: (NSNotification*) notification 1.155 { 1.156 - APPLE_SUCKS(); 1.157 } 1.158 1.159 -(BOOL) applicationShouldTerminate: (NSApplication*) app 1.160 { 1.161 - APPLE_SUCKS(); 1.162 return NSTerminateNow; 1.163 } 1.164 1.165 -(BOOL) applicationShouldTerminateAfterLastWindowClosed: (NSApplication*) app 1.166 { 1.167 - APPLE_SUCKS(); 1.168 return YES; 1.169 } 1.170 1.171 -(void) applicationWillTerminate: (NSNotification*) notification 1.172 { 1.173 - APPLE_SUCKS(); 1.174 /*[NSApp setDelegate: nil]; 1.175 [global_pool drain];*/ 1.176 } 1.177 @@ -208,30 +260,25 @@ 1.178 @implementation WinDelegate 1.179 -(id) init 1.180 { 1.181 - APPLE_SUCKS(); 1.182 self = [super init]; 1.183 return self; 1.184 } 1.185 1.186 -(void) dealloc 1.187 { 1.188 - APPLE_SUCKS(); 1.189 [super dealloc]; 1.190 } 1.191 1.192 -(void) windowDidExpose: (NSNotification*) notification 1.193 { 1.194 - APPLE_SUCKS(); 1.195 } 1.196 1.197 -(void) windowDidResize: (NSNotification*) notification 1.198 { 1.199 - APPLE_SUCKS(); 1.200 } 1.201 1.202 -(BOOL) windowShouldClose: (id) win 1.203 { 1.204 - APPLE_SUCKS(); 1.205 assert(self->win); 1.206 close_window(self->win->wid); 1.207 return YES; 1.208 @@ -239,14 +286,12 @@ 1.209 1.210 -(void) windowWillClose: (NSNotification*) notification 1.211 { 1.212 - APPLE_SUCKS(); 1.213 /*[NSApp terminate: nil];*/ 1.214 } 1.215 @end 1.216 1.217 static int init(void) 1.218 { 1.219 - APPLE_SUCKS(); 1.220 AppDelegate *delegate; 1.221 1.222 global_pool = [[NSAutoreleasePool alloc] init]; 1.223 @@ -260,7 +305,6 @@ 1.224 1.225 static void shutdown(void) 1.226 { 1.227 - APPLE_SUCKS(); 1.228 while(winlist) { 1.229 close_window(winlist->wid); 1.230 } 1.231 @@ -285,7 +329,6 @@ 1.232 /* create/destroy windows */ 1.233 static int create_window(int xsz, int ysz, unsigned int flags) 1.234 { 1.235 - APPLE_SUCKS(); 1.236 NSAutoreleasePool *pool; 1.237 WinDelegate *delegate; 1.238 NSWindow *nswin; 1.239 @@ -349,7 +392,6 @@ 1.240 1.241 static void close_window(int wid) 1.242 { 1.243 - APPLE_SUCKS(); 1.244 struct window *win, *prev, dummy; 1.245 sgl_close_callback_t close_func; 1.246 1.247 @@ -439,7 +481,19 @@ 1.248 1.249 static int get_modifiers(void) 1.250 { 1.251 - return 0; /* TODO */ 1.252 + unsigned int nsmod = [NSEvent modifierFlags]; 1.253 + unsigned int mod = 0; 1.254 + 1.255 + if(nsmod & NSShiftKeyMask) { 1.256 + mod |= SGL_MOD_SHIFT; 1.257 + } 1.258 + if(nsmod & NSControlKeyMask) { 1.259 + mod |= SGL_MOD_CONTROL; 1.260 + } 1.261 + if(nsmod & NSAlternateKeyMask) { 1.262 + mod |= SGL_MOD_ALT; 1.263 + } 1.264 + return mod; 1.265 } 1.266 1.267 1.268 @@ -500,6 +554,71 @@ 1.269 return quit_main_loop ? -1 : 0; 1.270 } 1.271 1.272 +static void select_event_window(NSEvent *ev) 1.273 +{ 1.274 + NSWindow *nswin; 1.275 + WinDelegate *del; 1.276 + struct window *win; 1.277 + 1.278 + if(!ev || !(nswin = [ev window])) { 1.279 + sgl_log("%s failed\n", __func__); 1.280 + return; 1.281 + } 1.282 + del = [nswin delegate]; 1.283 + win = del->win; 1.284 + 1.285 + activate_window(win); 1.286 +} 1.287 + 1.288 +static void handle_key(NSEvent *ev, int state) 1.289 +{ 1.290 + NSString *str; 1.291 + sgl_keyboard_callback_t func = sgl_get_callback(SGL_KEYBOARD); 1.292 + 1.293 + if(func) { 1.294 + str = [ev characters]; 1.295 + if([str length]) { 1.296 + unichar c = [str characterAtIndex: 0]; 1.297 + 1.298 + select_event_window(ev); 1.299 + func(c, state); 1.300 + } 1.301 + } 1.302 +} 1.303 + 1.304 +static void handle_mouse(NSEvent *ev, int state) 1.305 +{ 1.306 + int bn; 1.307 + NSPoint pt; 1.308 + sgl_mouse_callback_t func = sgl_get_callback(SGL_MOUSE); 1.309 + 1.310 + if(func) { 1.311 + bn = [ev buttonNumber]; 1.312 + if(bn == 2) { 1.313 + bn = 1; 1.314 + } else if(bn == 1) { 1.315 + bn = 2; 1.316 + } 1.317 + pt = [ev locationInWindow]; 1.318 + 1.319 + select_event_window(ev); 1.320 + func(0, bn, state, pt.x, pt.y - 1); 1.321 + } 1.322 +} 1.323 + 1.324 +static void handle_motion(NSEvent *ev) 1.325 +{ 1.326 + NSPoint pt; 1.327 + sgl_motion_callback_t func = sgl_get_callback(SGL_MOTION); 1.328 + 1.329 + if(func) { 1.330 + pt = [ev locationInWindow]; 1.331 + 1.332 + select_event_window(ev); 1.333 + func(0, pt.x, pt.y - 1); 1.334 + } 1.335 +} 1.336 + 1.337 static void fill_attr(NSOpenGLPixelFormatAttribute *attr, unsigned int flags) 1.338 { 1.339 int i = 0;