aboutsummaryrefslogtreecommitdiff
path: root/pugl/pugl_osx.m
diff options
context:
space:
mode:
Diffstat (limited to 'pugl/pugl_osx.m')
-rw-r--r--pugl/pugl_osx.m138
1 files changed, 121 insertions, 17 deletions
diff --git a/pugl/pugl_osx.m b/pugl/pugl_osx.m
index d2681cb6..5f57798c 100644
--- a/pugl/pugl_osx.m
+++ b/pugl/pugl_osx.m
@@ -33,6 +33,7 @@ struct PuglInternalsImpl {
PuglOpenGLView* glview;
id window;
NSEvent* nextEvent;
+ unsigned mods;
#ifdef PUGL_HAVE_CAIRO
cairo_surface_t* surface;
cairo_t* cr;
@@ -150,6 +151,9 @@ struct PuglInternalsImpl {
NSOpenGLPFAAccelerated,
NSOpenGLPFAColorSize, 32,
NSOpenGLPFADepthSize, 32,
+ NSOpenGLPFAMultisample,
+ NSOpenGLPFASampleBuffers, 1,
+ NSOpenGLPFASamples, 4,
0
};
@@ -166,6 +170,7 @@ struct PuglInternalsImpl {
if (self) {
[[self openGLContext] makeCurrentContext];
[self reshape];
+ [NSOpenGLContext clearCurrentContext];
}
return self;
}
@@ -245,6 +250,39 @@ getModifiers(PuglView* view, NSEvent* ev)
return mods;
}
+static PuglKey
+keySymToSpecial(PuglView* view, NSEvent* ev)
+{
+ NSString* chars = [ev charactersIgnoringModifiers];
+ if([chars length] == 1) {
+ switch ([chars characterAtIndex:0]) {
+ case NSF1FunctionKey: return PUGL_KEY_F1;
+ case NSF2FunctionKey: return PUGL_KEY_F2;
+ case NSF3FunctionKey: return PUGL_KEY_F3;
+ case NSF4FunctionKey: return PUGL_KEY_F4;
+ case NSF5FunctionKey: return PUGL_KEY_F5;
+ case NSF6FunctionKey: return PUGL_KEY_F6;
+ case NSF7FunctionKey: return PUGL_KEY_F7;
+ case NSF8FunctionKey: return PUGL_KEY_F8;
+ case NSF9FunctionKey: return PUGL_KEY_F9;
+ case NSF10FunctionKey: return PUGL_KEY_F10;
+ case NSF11FunctionKey: return PUGL_KEY_F11;
+ case NSF12FunctionKey: return PUGL_KEY_F12;
+ case NSLeftArrowFunctionKey: return PUGL_KEY_LEFT;
+ case NSUpArrowFunctionKey: return PUGL_KEY_UP;
+ case NSRightArrowFunctionKey: return PUGL_KEY_RIGHT;
+ case NSDownArrowFunctionKey: return PUGL_KEY_DOWN;
+ case NSPageUpFunctionKey: return PUGL_KEY_PAGE_UP;
+ case NSPageDownFunctionKey: return PUGL_KEY_PAGE_DOWN;
+ case NSHomeFunctionKey: return PUGL_KEY_HOME;
+ case NSEndFunctionKey: return PUGL_KEY_END;
+ case NSInsertFunctionKey: return PUGL_KEY_INSERT;
+ /* special keys (SHIFT, CTRL, ALT, SUPER) are handled in [flagsChanged] */
+ }
+ }
+ return (PuglKey)0;
+}
+
-(void)updateTrackingAreas
{
if (trackingArea != nil) {
@@ -415,7 +453,7 @@ getModifiers(PuglView* view, NSEvent* ev)
getModifiers(puglview, event),
[event keyCode],
puglDecodeUTF8((const uint8_t*)str),
- 0, // TODO: Special keys?
+ keySymToSpecial(puglview, event),
{ 0, 0, 0, 0, 0, 0, 0, 0 },
false
};
@@ -441,7 +479,7 @@ getModifiers(PuglView* view, NSEvent* ev)
getModifiers(puglview, event),
[event keyCode],
puglDecodeUTF8((const uint8_t*)str),
- 0, // TODO: Special keys?
+ keySymToSpecial(puglview, event),
{ 0, 0, 0, 0, 0, 0, 0, 0 },
false,
};
@@ -451,21 +489,47 @@ getModifiers(PuglView* view, NSEvent* ev)
- (void) flagsChanged:(NSEvent*)event
{
- // TODO: Is this a sensible way to handle special keys?
- /*
- const unsigned mods = getModifiers(puglview, event);
- if ((mods & PUGL_MOD_SHIFT) != (puglview->mods & PUGL_MOD_SHIFT)) {
- puglview->specialFunc(puglview, mods & PUGL_MOD_SHIFT, PUGL_KEY_SHIFT);
- } else if ((mods & PUGL_MOD_CTRL) != (puglview->mods & PUGL_MOD_CTRL)) {
- puglview->specialFunc(puglview, mods & PUGL_MOD_CTRL, PUGL_KEY_CTRL);
- } else if ((mods & PUGL_MOD_ALT) != (puglview->mods & PUGL_MOD_ALT)) {
- puglview->specialFunc(puglview, mods & PUGL_MOD_ALT, PUGL_KEY_ALT);
- } else if ((mods & PUGL_MOD_SUPER) != (puglview->mods & PUGL_MOD_SUPER)) {
- puglview->specialFunc(puglview, mods & PUGL_MOD_SUPER, PUGL_KEY_SUPER);
- }
- puglview->mods = mods;
+ const unsigned mods = getModifiers(puglview, event);
+ PuglEventType type = PUGL_NOTHING;
+ PuglKey special = 0;
+
+ if ((mods & PUGL_MOD_SHIFT) != (puglview->impl->mods & PUGL_MOD_SHIFT)) {
+ type = mods & PUGL_MOD_SHIFT ? PUGL_KEY_PRESS : PUGL_KEY_RELEASE;
+ special = PUGL_KEY_SHIFT;
+ } else if ((mods & PUGL_MOD_CTRL) != (puglview->impl->mods & PUGL_MOD_CTRL)) {
+ type = mods & PUGL_MOD_CTRL ? PUGL_KEY_PRESS : PUGL_KEY_RELEASE;
+ special = PUGL_KEY_CTRL;
+ } else if ((mods & PUGL_MOD_ALT) != (puglview->impl->mods & PUGL_MOD_ALT)) {
+ type = mods & PUGL_MOD_ALT ? PUGL_KEY_PRESS : PUGL_KEY_RELEASE;
+ special = PUGL_KEY_ALT;
+ } else if ((mods & PUGL_MOD_SUPER) != (puglview->impl->mods & PUGL_MOD_SUPER)) {
+ type = mods & PUGL_MOD_SUPER ? PUGL_KEY_PRESS : PUGL_KEY_RELEASE;
+ special = PUGL_KEY_SUPER;
}
- */
+
+ if (special != 0) {
+ const NSPoint wloc = [self eventLocation:event];
+ const NSPoint rloc = [NSEvent mouseLocation];
+ PuglEventKey ev = {
+ type,
+ puglview,
+ 0,
+ [event timestamp],
+ wloc.x,
+ puglview->height - wloc.y,
+ rloc.x,
+ [[NSScreen mainScreen] frame].size.height - rloc.y,
+ mods,
+ [event keyCode],
+ 0,
+ special,
+ { 0, 0, 0, 0, 0, 0, 0, 0 },
+ false
+ };
+ puglDispatchEvent(puglview, (PuglEvent*)&ev);
+ }
+
+ puglview->impl->mods = mods;
}
@end
@@ -498,8 +562,11 @@ puglLeaveContext(PuglView* view, bool flush)
#endif
if (flush) {
- [[view->impl->glview openGLContext] flushBuffer];
+ //[[view->impl->glview openGLContext] flushBuffer];
+ glFlush();
+ glSwapAPPLE();
}
+ [NSOpenGLContext clearCurrentContext];
}
int
@@ -575,12 +642,49 @@ puglDestroy(PuglView* view)
if (view->impl->window) {
[view->impl->window release];
}
+ puglClearSelection(view);
free(view->windowClass);
free(view->impl);
free(view);
}
void
+puglCopyToClipboard(PuglView* view, const char* selection, size_t len)
+{
+ PuglInternals* const impl = view->impl;
+
+ puglSetSelection(view, selection, len);
+
+ NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
+ if(pasteboard) {
+ [pasteboard
+ declareTypes:[NSArray arrayWithObjects:NSStringPboardType, nil]
+ owner:nil];
+ [pasteboard
+ setString:[NSString stringWithUTF8String:puglGetSelection(view, NULL)]
+ forType:NSStringPboardType];
+ }
+}
+
+const char*
+puglPasteFromClipboard(PuglView* view, size_t* len)
+{
+ PuglInternals* const impl = view->impl;
+
+ NSPasteboard* pasteboard = [NSPasteboard generalPasteboard];
+ if(pasteboard && [ [pasteboard types] containsObject:NSStringPboardType] ) {
+ const NSString* mem = [pasteboard stringForType:NSStringPboardType];
+ if(mem) {
+ const char *utf8 = [mem UTF8String];
+ if(utf8)
+ puglSetSelection(view, utf8, strlen(utf8));
+ }
+ }
+
+ return puglGetSelection(view, len);
+}
+
+void
puglGrabFocus(PuglView* view)
{
// TODO