ext/say_osx_window.h in ray-0.1.1 vs ext/say_osx_window.h in ray-0.2.0

- old
+ new

@@ -1,11 +1,11 @@ /* -*- mode: objc -*- */ void say_osx_flip_pool() { static say_thread_variable *var = NULL; if (!var) { - var = say_thread_variable_create(NULL); + var = say_thread_variable_create(); } NSAutoreleasePool *pool = say_thread_variable_get(var); [pool drain]; @@ -57,23 +57,18 @@ - (id)init { if (!(self = [super init])) return nil; - events = say_array_create(sizeof(say_event), NULL, NULL); + mo_array_init(&events, sizeof(say_event)); return self; } - (BOOL)openWithTitle:(const char*)title width:(size_t)w height:(size_t)h style:(uint8_t)style { - if ([NSThread currentThread] != [NSThread mainThread]) { - say_error_set("can't create window outside main thread"); - return NO; - } - say_osx_setup_process(); NSRect rect = NSMakeRect(0, 0, w, h); unsigned int cocoa_style = NSBorderlessWindowMask; @@ -172,15 +167,13 @@ colorSpaceName:NSCalibratedRGBColorSpace bytesPerRow:0 bitsPerPixel:0]; - - say_color *buf = say_image_get_buffer(img); for (size_t y = 0; y < h; y++) { for (size_t x = 0; x < w; x++) { - say_color col = buf[x + y * w]; + say_color col = say_image_get(img, x, y); NSUInteger pixel[] = {col.r, col.g, col.b, col.a}; [rep setPixel:pixel atX:x y:y]; } } @@ -224,11 +217,11 @@ [self updateContext:[view openGLContext]]; say_event ev; ev.type = SAY_EVENT_RESIZE; ev.ev.resize.size = say_make_vector2(w, h); - say_array_push(events, &ev); + mo_array_push(&events, &ev); } else { NSRect frame = view.frame; frame.size = NSMakeSize(w, h); @@ -241,23 +234,25 @@ } } - (void)close { if (window) { - allow_close = YES; + allow_close = YES; [window close]; [window release]; window = nil; + + mo_array_resize(&events, 0); } } - (void)dealloc { say_osx_flip_pool(); [self close]; [view release]; - say_array_free(events); + mo_array_release(&events); [super dealloc]; say_osx_flip_pool(); } @@ -317,13 +312,13 @@ if ((nsev = say_osx_get_event(false))) { [self handleEvent:nsev]; } [pool drain]; - if (say_array_get_size(events) != 0) { - *ev = *(say_event*)say_array_get(events, 0); - say_array_delete(events, 0); + if (events.size != 0) { + *ev = mo_array_get_as(&events, 0, say_event); + mo_array_delete(&events, 0); return YES; } else ev->type = SAY_EVENT_NONE; @@ -334,18 +329,18 @@ - (void)waitEvent:(say_event*)ev { NSEvent *nsev = nil; NSAutoreleasePool *pool = [NSAutoreleasePool new]; - while (say_array_get_size(events) == 0 && (nsev = say_osx_get_event(true))) { + while (events.size == 0 && (nsev = say_osx_get_event(true))) { [NSApp sendEvent:nsev]; } [pool drain]; - *ev = *(say_event*)say_array_get(events, 0); - say_array_delete(events, 0); + *ev = mo_array_get_as(&events, 0, say_event); + mo_array_delete(&events, 0); } static say_key say_osx_convert_key(NSEvent *ev) { NSString *str = [ev charactersIgnoringModifiers]; @@ -478,22 +473,22 @@ ev.ev.key.code = say_osx_convert_key(nsev); ev.ev.key.mod = say_osx_convert_mod(nsev); ev.ev.key.native_code = nsev.keyCode; - say_array_push(events, &ev); + mo_array_push(&events, &ev); } NSText *text = [window fieldEditor:YES forObject:self]; [text interpretKeyEvents:[NSArray arrayWithObject:nsev]]; if (text.string.length != 0) { say_event ev; ev.type = SAY_EVENT_TEXT_ENTERED; ev.ev.text.text = [text.string characterAtIndex:0]; - say_array_push(events, &ev); + mo_array_push(&events, &ev); text.string = @""; } } @@ -503,11 +498,11 @@ ev.ev.key.code = say_osx_convert_key(nsev); ev.ev.key.mod = say_osx_convert_mod(nsev); ev.ev.key.native_code = nsev.keyCode; - say_array_push(events, &ev); + mo_array_push(&events, &ev); } - (void)submitFlagChange:(uint8_t)mod forKey:(say_key)key press:(BOOL)press { say_event ev; @@ -515,11 +510,11 @@ ev.ev.key.code = key; ev.ev.key.mod = mod; ev.ev.key.native_code = 0; - say_array_push(events, &ev); + mo_array_push(&events, &ev); } - (void)flagsChanged:(NSEvent*)nsev { uint8_t new_mod = say_osx_convert_mod(nsev); if (new_mod == modifiers) @@ -576,92 +571,92 @@ - (void)ownMouseDown:(NSEvent*)nsev { say_event ev; ev.type = SAY_EVENT_BUTTON_PRESS; ev.ev.button.pos = [self convertPoint:nsev.locationInWindow]; ev.ev.button.button = SAY_BUTTON_LEFT; - say_array_push(events, &ev); + mo_array_push(&events, &ev); } - (void)ownMouseUp:(NSEvent*)nsev { say_event ev; ev.type = SAY_EVENT_BUTTON_RELEASE; ev.ev.button.pos = [self convertPoint:nsev.locationInWindow]; ev.ev.button.button = SAY_BUTTON_LEFT; - say_array_push(events, &ev); + mo_array_push(&events, &ev); } - (void)ownRightMouseDown:(NSEvent*)nsev { say_event ev; ev.type = SAY_EVENT_BUTTON_PRESS; ev.ev.button.pos = [self convertPoint:nsev.locationInWindow]; ev.ev.button.button = SAY_BUTTON_RIGHT; - say_array_push(events, &ev); + mo_array_push(&events, &ev); } - (void)ownRightMouseUp:(NSEvent*)nsev { say_event ev; ev.type = SAY_EVENT_BUTTON_RELEASE; ev.ev.button.pos = [self convertPoint:nsev.locationInWindow]; ev.ev.button.button = SAY_BUTTON_RIGHT; - say_array_push(events, &ev); + mo_array_push(&events, &ev); } /* Assume other mouse button will be middle. */ - (void)ownOtherMouseDown:(NSEvent*)nsev { say_event ev; ev.type = SAY_EVENT_BUTTON_PRESS; ev.ev.button.pos = [self convertPoint:nsev.locationInWindow]; ev.ev.button.button = SAY_BUTTON_MIDDLE; - say_array_push(events, &ev); + mo_array_push(&events, &ev); } - (void)ownOtherMouseUp:(NSEvent*)nsev { say_event ev; ev.type = SAY_EVENT_BUTTON_RELEASE; ev.ev.button.pos = [self convertPoint:nsev.locationInWindow]; ev.ev.button.button = SAY_BUTTON_MIDDLE; - say_array_push(events, &ev); + mo_array_push(&events, &ev); } - (void)mouseMoved:(NSEvent*)nsev { say_event ev; ev.type = SAY_EVENT_MOUSE_MOTION; ev.ev.motion.pos = [self convertPoint:nsev.locationInWindow]; - say_array_push(events, &ev); + mo_array_push(&events, &ev); } - (void)mouseEntered:(NSEvent*)nsev { say_event ev; ev.type = SAY_EVENT_MOUSE_ENTERED; - say_array_push(events, &ev); + mo_array_push(&events, &ev); } - (void)mouseExited:(NSEvent*)nsev { say_event ev; ev.type = SAY_EVENT_MOUSE_LEFT; - say_array_push(events, &ev); + mo_array_push(&events, &ev); } - (void)ownScrollWheel:(NSEvent*)nsev { if (nsev.deltaY == 0) return; say_event ev; ev.type = SAY_EVENT_WHEEL_MOTION; ev.ev.wheel.pos = [self convertPoint:nsev.locationInWindow]; ev.ev.wheel.delta = nsev.deltaY; - say_array_push(events, &ev); + mo_array_push(&events, &ev); } /* * Window delegate */ - (BOOL)windowShouldClose:(id)sender { say_event ev; ev.type = SAY_EVENT_QUIT; - say_array_push(events, &ev); + mo_array_push(&events, &ev); if (allow_close) { allow_close = NO; return YES; } @@ -670,27 +665,27 @@ } - (void)windowDidBecomeKey:(NSNotification*)not { say_event ev; ev.type = SAY_EVENT_FOCUS_GAIN; - say_array_push(events, &ev); + mo_array_push(&events, &ev); } - (void)windowDidResignKey:(NSNotification*)not { say_event ev; ev.type = SAY_EVENT_FOCUS_LOSS; - say_array_push(events, &ev); + mo_array_push(&events, &ev); } - (void)windowDidResize:(NSNotification*)not { NSSize size = view.frame.size; /* Ignore resize events in fullscreen windows */ if (real_w == 0 && real_h == 0) { say_event ev; ev.type = SAY_EVENT_RESIZE; ev.ev.resize.size = say_make_vector2(size.width, size.height); - say_array_push(events, &ev); + mo_array_push(&events, &ev); } [view removeTrackingRect:track]; track = [view addTrackingRect:view.frame owner:self