Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

332 lines
7.9KB

  1. /*
  2. Copyright 2012 David Robillard <http://drobilla.net>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. /**
  15. @file pugl_osx.m OSX/Cocoa Pugl Implementation.
  16. */
  17. #include <stdlib.h>
  18. #import <Cocoa/Cocoa.h>
  19. #include "pugl_internal.h"
  20. @interface PuglOpenGLView : NSOpenGLView
  21. {
  22. int colorBits;
  23. int depthBits;
  24. @public
  25. PuglView* view;
  26. }
  27. - (id) initWithFrame:(NSRect)frame
  28. colorBits:(int)numColorBits
  29. depthBits:(int)numDepthBits;
  30. - (void) reshape;
  31. - (void) drawRect:(NSRect)rect;
  32. - (void) mouseMoved:(NSEvent*)event;
  33. - (void) mouseDown:(NSEvent*)event;
  34. - (void) mouseUp:(NSEvent*)event;
  35. - (void) rightMouseDown:(NSEvent*)event;
  36. - (void) rightMouseUp:(NSEvent*)event;
  37. - (void) keyDown:(NSEvent*)event;
  38. - (void) keyUp:(NSEvent*)event;
  39. - (void) flagsChanged:(NSEvent*)event;
  40. @end
  41. @implementation PuglOpenGLView
  42. - (id) initWithFrame:(NSRect)frame
  43. colorBits:(int)numColorBits
  44. depthBits:(int)numDepthBits
  45. {
  46. colorBits = numColorBits;
  47. depthBits = numDepthBits;
  48. NSOpenGLPixelFormatAttribute pixelAttribs[16] = {
  49. NSOpenGLPFADoubleBuffer,
  50. NSOpenGLPFAAccelerated,
  51. NSOpenGLPFAColorSize,
  52. colorBits,
  53. NSOpenGLPFADepthSize,
  54. depthBits,
  55. 0
  56. };
  57. NSOpenGLPixelFormat* pixelFormat = [[NSOpenGLPixelFormat alloc]
  58. initWithAttributes:pixelAttribs];
  59. if (pixelFormat) {
  60. self = [super initWithFrame:frame pixelFormat:pixelFormat];
  61. [pixelFormat release];
  62. if (self) {
  63. [[self openGLContext] makeCurrentContext];
  64. [self reshape];
  65. }
  66. } else {
  67. self = nil;
  68. }
  69. return self;
  70. }
  71. - (void) reshape
  72. {
  73. [[self openGLContext] update];
  74. NSRect bounds = [self bounds];
  75. int width = bounds.size.width;
  76. int height = bounds.size.height;
  77. if (view->reshapeFunc) {
  78. view->reshapeFunc(view, width, height);
  79. } else {
  80. puglDefaultReshape(view, width, height);
  81. }
  82. view->width = width;
  83. view->height = height;
  84. }
  85. - (void) drawRect:(NSRect)rect
  86. {
  87. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  88. glLoadIdentity();
  89. if (self->view->displayFunc) {
  90. self->view->displayFunc(self->view);
  91. }
  92. glFlush();
  93. glSwapAPPLE();
  94. }
  95. static int
  96. getModifiers(unsigned modifierFlags)
  97. {
  98. int mods = 0;
  99. mods |= (modifierFlags & NSShiftKeyMask) ? PUGL_MOD_SHIFT : 0;
  100. mods |= (modifierFlags & NSControlKeyMask) ? PUGL_MOD_CTRL : 0;
  101. mods |= (modifierFlags & NSAlternateKeyMask) ? PUGL_MOD_ALT : 0;
  102. mods |= (modifierFlags & NSCommandKeyMask) ? PUGL_MOD_SUPER : 0;
  103. return mods;
  104. }
  105. - (void) mouseMoved:(NSEvent*)event
  106. {
  107. if (view->motionFunc) {
  108. NSPoint loc = [event locationInWindow];
  109. view->mods = getModifiers([event modifierFlags]);
  110. view->motionFunc(view, loc.x, loc.y);
  111. }
  112. }
  113. - (void) mouseDown:(NSEvent*)event
  114. {
  115. if (view->mouseFunc) {
  116. NSPoint loc = [event locationInWindow];
  117. view->mods = getModifiers([event modifierFlags]);
  118. view->mouseFunc(view, 1, true, loc.x, loc.y);
  119. }
  120. }
  121. - (void) mouseUp:(NSEvent*)event
  122. {
  123. if (view->mouseFunc) {
  124. NSPoint loc = [event locationInWindow];
  125. view->mods = getModifiers([event modifierFlags]);
  126. view->mouseFunc(view, 1, false, loc.x, loc.y);
  127. }
  128. }
  129. - (void) rightMouseDown:(NSEvent*)event
  130. {
  131. if (view->mouseFunc) {
  132. NSPoint loc = [event locationInWindow];
  133. view->mods = getModifiers([event modifierFlags]);
  134. view->mouseFunc(view, 3, true, loc.x, loc.y);
  135. }
  136. }
  137. - (void) rightMouseUp:(NSEvent*)event
  138. {
  139. if (view->mouseFunc) {
  140. NSPoint loc = [event locationInWindow];
  141. view->mods = getModifiers([event modifierFlags]);
  142. view->mouseFunc(view, 3, false, loc.x, loc.y);
  143. }
  144. }
  145. - (void) scrollWheel:(NSEvent*)event
  146. {
  147. if (view->scrollFunc) {
  148. view->mods = getModifiers([event modifierFlags]);
  149. view->scrollFunc(view, [event deltaX], [event deltaY]);
  150. }
  151. }
  152. - (void) keyDown:(NSEvent*)event
  153. {
  154. if (view->keyboardFunc && !(view->ignoreKeyRepeat && [event isARepeat])) {
  155. NSString* chars = [event characters];
  156. view->mods = getModifiers([event modifierFlags]);
  157. view->keyboardFunc(view, true, [chars characterAtIndex:0]);
  158. }
  159. }
  160. - (void) keyUp:(NSEvent*)event
  161. {
  162. if (view->keyboardFunc) {
  163. NSString* chars = [event characters];
  164. view->mods = getModifiers([event modifierFlags]);
  165. view->keyboardFunc(view, false, [chars characterAtIndex:0]);
  166. }
  167. }
  168. - (void) flagsChanged:(NSEvent*)event
  169. {
  170. if (view->specialFunc) {
  171. int mods = getModifiers([event modifierFlags]);
  172. if ((mods & PUGL_MOD_SHIFT) != (view->mods & PUGL_MOD_SHIFT)) {
  173. view->specialFunc(view, mods & PUGL_MOD_SHIFT, PUGL_KEY_SHIFT);
  174. } else if ((mods & PUGL_MOD_CTRL) != (view->mods & PUGL_MOD_CTRL)) {
  175. view->specialFunc(view, mods & PUGL_MOD_CTRL, PUGL_KEY_CTRL);
  176. } else if ((mods & PUGL_MOD_ALT) != (view->mods & PUGL_MOD_ALT)) {
  177. view->specialFunc(view, mods & PUGL_MOD_ALT, PUGL_KEY_ALT);
  178. } else if ((mods & PUGL_MOD_SUPER) != (view->mods & PUGL_MOD_SUPER)) {
  179. view->specialFunc(view, mods & PUGL_MOD_SUPER, PUGL_KEY_SUPER);
  180. }
  181. view->mods = mods;
  182. }
  183. }
  184. @end
  185. struct PuglInternalsImpl {
  186. PuglOpenGLView* view;
  187. NSModalSession session;
  188. id window;
  189. };
  190. PuglView*
  191. puglCreate(PuglNativeWindow parent,
  192. const char* title,
  193. int width,
  194. int height,
  195. bool resizable,
  196. bool addToDesktop,
  197. const char* x11Display)
  198. {
  199. PuglView* view = (PuglView*)calloc(1, sizeof(PuglView));
  200. PuglInternals* impl = (PuglInternals*)calloc(1, sizeof(PuglInternals));
  201. if (!view || !impl) {
  202. return NULL;
  203. }
  204. view->impl = impl;
  205. view->width = width;
  206. view->height = height;
  207. [NSAutoreleasePool new];
  208. [NSApplication sharedApplication];
  209. [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
  210. NSString* titleString = [[NSString alloc]
  211. initWithBytes:title
  212. length:strlen(title)
  213. encoding:NSUTF8StringEncoding];
  214. id window = [[[NSWindow alloc]
  215. initWithContentRect:NSMakeRect(0, 0, 512, 512)
  216. styleMask:NSTitledWindowMask
  217. backing:NSBackingStoreBuffered
  218. defer:NO]
  219. autorelease];
  220. [window cascadeTopLeftFromPoint:NSMakePoint(20, 20)];
  221. [window setTitle:titleString];
  222. [window setAcceptsMouseMovedEvents:YES];
  223. impl->view = [PuglOpenGLView new];
  224. impl->window = window;
  225. impl->view->view = view;
  226. [window setContentView:impl->view];
  227. [NSApp activateIgnoringOtherApps:YES];
  228. [window makeFirstResponder:impl->view];
  229. impl->session = [NSApp beginModalSessionForWindow:view->impl->window];
  230. return view;
  231. // unused
  232. (void)addToDesktop;
  233. (void)x11Display;
  234. }
  235. void
  236. puglDestroy(PuglView* view)
  237. {
  238. [NSApp endModalSession:view->impl->session];
  239. [view->impl->view release];
  240. free(view->impl);
  241. free(view);
  242. }
  243. void
  244. puglDisplay(PuglView* view)
  245. {
  246. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  247. glLoadIdentity();
  248. if (view->displayFunc) {
  249. view->displayFunc(view);
  250. }
  251. glFlush();
  252. view->redisplay = false;
  253. }
  254. PuglStatus
  255. puglProcessEvents(PuglView* view)
  256. {
  257. NSInteger response = [NSApp runModalSession:view->impl->session];
  258. if (response != NSRunContinuesResponse) {
  259. if (view->closeFunc) {
  260. view->closeFunc(view);
  261. }
  262. }
  263. if (view->redisplay) {
  264. puglDisplay(view);
  265. }
  266. return PUGL_SUCCESS;
  267. }
  268. void
  269. puglPostRedisplay(PuglView* view)
  270. {
  271. view->redisplay = true;
  272. }
  273. PuglNativeWindow
  274. puglGetNativeWindow(PuglView* view)
  275. {
  276. return (PuglNativeWindow)view->impl->view;
  277. }