DISTRHO Plugin Framework
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.

493 lines
11KB

  1. /*
  2. Copyright 2012-2014 David Robillard <http://drobilla.net>
  3. Copyright 2012-2019 Filipe Coelho <falktx@falktx.com>
  4. Permission to use, copy, modify, and/or distribute this software for any
  5. purpose with or without fee is hereby granted, provided that the above
  6. copyright notice and this permission notice appear in all copies.
  7. THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. */
  15. /**
  16. @file pugl.h API for Pugl, a minimal portable API for OpenGL.
  17. */
  18. #ifndef PUGL_H_INCLUDED
  19. #define PUGL_H_INCLUDED
  20. #include <stdint.h>
  21. /*
  22. This API is pure portable C and contains no platform specific elements, or
  23. even a GL dependency. However, unfortunately GL includes vary across
  24. platforms so they are included here to allow for pure portable programs.
  25. */
  26. #ifdef __APPLE__
  27. # include <OpenGL/gl.h>
  28. #else
  29. # ifdef _WIN32
  30. # include <winsock2.h>
  31. # include <windows.h> /* Broken Windows GL headers require this */
  32. # endif
  33. # include <GL/gl.h>
  34. #endif
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #else
  38. # include <stdbool.h>
  39. #endif
  40. /**
  41. @defgroup pugl Pugl
  42. A minimal portable API for OpenGL.
  43. @{
  44. */
  45. /**
  46. A Pugl view.
  47. */
  48. typedef struct PuglViewImpl PuglView;
  49. /**
  50. A native window handle.
  51. On X11, this is a Window.
  52. On OSX, this is an NSView*.
  53. On Windows, this is a HWND.
  54. */
  55. typedef intptr_t PuglNativeWindow;
  56. /**
  57. Return status code.
  58. */
  59. typedef enum {
  60. PUGL_SUCCESS = 0
  61. } PuglStatus;
  62. /**
  63. Convenience symbols for ASCII control characters.
  64. */
  65. typedef enum {
  66. PUGL_CHAR_BACKSPACE = 0x08,
  67. PUGL_CHAR_ESCAPE = 0x1B,
  68. PUGL_CHAR_DELETE = 0x7F
  69. } PuglChar;
  70. /**
  71. Special (non-Unicode) keyboard keys.
  72. */
  73. typedef enum {
  74. PUGL_KEY_F1 = 1,
  75. PUGL_KEY_F2,
  76. PUGL_KEY_F3,
  77. PUGL_KEY_F4,
  78. PUGL_KEY_F5,
  79. PUGL_KEY_F6,
  80. PUGL_KEY_F7,
  81. PUGL_KEY_F8,
  82. PUGL_KEY_F9,
  83. PUGL_KEY_F10,
  84. PUGL_KEY_F11,
  85. PUGL_KEY_F12,
  86. PUGL_KEY_LEFT,
  87. PUGL_KEY_UP,
  88. PUGL_KEY_RIGHT,
  89. PUGL_KEY_DOWN,
  90. PUGL_KEY_PAGE_UP,
  91. PUGL_KEY_PAGE_DOWN,
  92. PUGL_KEY_HOME,
  93. PUGL_KEY_END,
  94. PUGL_KEY_INSERT,
  95. PUGL_KEY_SHIFT,
  96. PUGL_KEY_CTRL,
  97. PUGL_KEY_ALT,
  98. PUGL_KEY_SUPER
  99. } PuglKey;
  100. /**
  101. Keyboard modifier flags.
  102. */
  103. typedef enum {
  104. PUGL_MOD_SHIFT = 1 << 0, /**< Shift key */
  105. PUGL_MOD_CTRL = 1 << 1, /**< Control key */
  106. PUGL_MOD_ALT = 1 << 2, /**< Alt/Option key */
  107. PUGL_MOD_SUPER = 1 << 3 /**< Mod4/Command/Windows key */
  108. } PuglMod;
  109. /**
  110. Handle for opaque user data.
  111. */
  112. typedef void* PuglHandle;
  113. /**
  114. A function called when the window is closed.
  115. */
  116. typedef void (*PuglCloseFunc)(PuglView* view);
  117. /**
  118. A function called to draw the view contents with OpenGL.
  119. */
  120. typedef void (*PuglDisplayFunc)(PuglView* view);
  121. /**
  122. A function called when a key is pressed or released.
  123. @param view The view the event occured in.
  124. @param press True if the key was pressed, false if released.
  125. @param key Unicode point of the key pressed.
  126. @return 0 if event was handled, otherwise send event to parent window.
  127. */
  128. typedef int (*PuglKeyboardFunc)(PuglView* view, bool press, uint32_t key);
  129. /**
  130. A function called when the pointer moves.
  131. @param view The view the event occured in.
  132. @param x The window-relative x coordinate of the pointer.
  133. @param y The window-relative y coordinate of the pointer.
  134. */
  135. typedef void (*PuglMotionFunc)(PuglView* view, int x, int y);
  136. /**
  137. A function called when a mouse button is pressed or released.
  138. @param view The view the event occured in.
  139. @param button The button number (1 = left, 2 = middle, 3 = right).
  140. @param press True if the key was pressed, false if released.
  141. @param x The window-relative x coordinate of the pointer.
  142. @param y The window-relative y coordinate of the pointer.
  143. */
  144. typedef void (*PuglMouseFunc)(
  145. PuglView* view, int button, bool press, int x, int y);
  146. /**
  147. A function called when the view is resized.
  148. @param view The view being resized.
  149. @param width The new view width.
  150. @param height The new view height.
  151. */
  152. typedef void (*PuglReshapeFunc)(PuglView* view, int width, int height);
  153. /**
  154. A function called outside of gl-context when the plugin schedules a resize via puglPostResize.
  155. @param view The view being resized.
  156. @param width The new width to resize to (variable is initialized to current size)
  157. @param height The new height to resize to (variable is initialized to current size)
  158. @param set_hints If not null, set window-hints
  159. */
  160. typedef void (*PuglResizeFunc)(PuglView* view, int *width, int *height, int *set_hints);
  161. /**
  162. A function called on scrolling (e.g. mouse wheel or track pad).
  163. The distances used here are in "lines", a single tick of a clicking mouse
  164. wheel. For example, @p dy = 1.0 scrolls 1 line up. Some systems and
  165. devices support finer resolution and/or higher values for fast scrolls,
  166. so programs should handle any value gracefully.
  167. @param view The view being scrolled.
  168. @param x The window-relative x coordinate of the pointer.
  169. @param y The window-relative y coordinate of the pointer.
  170. @param dx The scroll x distance.
  171. @param dx The scroll y distance.
  172. */
  173. typedef void (*PuglScrollFunc)(PuglView* view, int x, int y, float dx, float dy);
  174. /**
  175. A function called when a special key is pressed or released.
  176. This callback allows the use of keys that do not have unicode points.
  177. Note that some are non-printable keys.
  178. @param view The view the event occured in.
  179. @param press True if the key was pressed, false if released.
  180. @param key The key pressed.
  181. @return 0 if event was handled, otherwise send event to parent window.
  182. */
  183. typedef int (*PuglSpecialFunc)(PuglView* view, bool press, PuglKey key);
  184. /**
  185. A function called when a filename is selected via file-browser.
  186. @param view The view the event occured in.
  187. @param filename The selected file name or NULL if the dialog was canceled.
  188. */
  189. typedef void (*PuglFileSelectedFunc)(PuglView* view, const char* filename);
  190. /**
  191. @name Initialization
  192. Configuration functions which must be called before creating a window.
  193. @{
  194. */
  195. /**
  196. Create a Pugl context.
  197. To create a window, call the various puglInit* functions as necessary, then
  198. call puglCreateWindow().
  199. */
  200. PuglView*
  201. puglInit(void);
  202. /**
  203. Set the parent window before creating a window (for embedding).
  204. */
  205. void
  206. puglInitWindowParent(PuglView* view, PuglNativeWindow parent);
  207. /**
  208. Set the window size before creating a window.
  209. */
  210. void
  211. puglInitWindowSize(PuglView* view, int width, int height);
  212. /**
  213. Set the minimum window size before creating a window.
  214. */
  215. void
  216. puglInitWindowMinSize(PuglView* view, int width, int height);
  217. /**
  218. Enable or disable resizing before creating a window.
  219. */
  220. void
  221. puglInitUserResizable(PuglView* view, bool resizable);
  222. /**
  223. Set transient parent before creating a window.
  224. On X11, parent_id must be a Window.
  225. On OSX, parent_id must be an NSView*.
  226. */
  227. void
  228. puglInitTransientFor(PuglView* view, uintptr_t parent);
  229. /**
  230. @}
  231. */
  232. /**
  233. @name Windows
  234. Window management functions.
  235. @{
  236. */
  237. /**
  238. Create a window with the settings given by the various puglInit functions.
  239. @return 1 (pugl does not currently support multiple windows).
  240. */
  241. int
  242. puglCreateWindow(PuglView* view, const char* title);
  243. /**
  244. Create a new GL window.
  245. @param parent Parent window, or 0 for top level.
  246. @param title Window title, or NULL.
  247. @param width Window width in pixels.
  248. @param height Window height in pixels.
  249. @param resizable Whether window should be user resizable.
  250. */
  251. PuglView*
  252. puglCreate(PuglNativeWindow parent,
  253. const char* title,
  254. int min_width,
  255. int min_height,
  256. int width,
  257. int height,
  258. bool resizable,
  259. unsigned long transientId);
  260. /**
  261. Show Window (external ui)
  262. */
  263. void
  264. puglShowWindow(PuglView* view);
  265. /**
  266. Hide Window (external ui)
  267. */
  268. void
  269. puglHideWindow(PuglView* view);
  270. /**
  271. Return the native window handle.
  272. */
  273. PuglNativeWindow
  274. puglGetNativeWindow(PuglView* view);
  275. /**
  276. @}
  277. */
  278. /**
  279. Set the handle to be passed to all callbacks.
  280. This is generally a pointer to a struct which contains all necessary state.
  281. Everything needed in callbacks should be here, not in static variables.
  282. Note the lack of this facility makes GLUT unsuitable for plugins or
  283. non-trivial programs; this mistake is largely why Pugl exists.
  284. */
  285. void
  286. puglSetHandle(PuglView* view, PuglHandle handle);
  287. /**
  288. Get the handle to be passed to all callbacks.
  289. */
  290. PuglHandle
  291. puglGetHandle(PuglView* view);
  292. /**
  293. Get the drawing context.
  294. For Cairo contexts, this returns a pointer to a cairo_t.
  295. For everything else, this is unused and returns NULL.
  296. */
  297. void*
  298. puglGetContext(PuglView* view);
  299. /**
  300. Return the timestamp (if any) of the currently-processing event.
  301. */
  302. uint32_t
  303. puglGetEventTimestamp(PuglView* view);
  304. /**
  305. Get the currently active modifiers (PuglMod flags).
  306. This should only be called from an event handler.
  307. */
  308. int
  309. puglGetModifiers(PuglView* view);
  310. /**
  311. Ignore synthetic repeated key events.
  312. */
  313. void
  314. puglIgnoreKeyRepeat(PuglView* view, bool ignore);
  315. /**
  316. @name Event Callbacks
  317. Functions to set event callbacks for handling user input.
  318. @{
  319. */
  320. /**
  321. Set the function to call when the window is closed.
  322. */
  323. void
  324. puglSetCloseFunc(PuglView* view, PuglCloseFunc closeFunc);
  325. /**
  326. Set the display function which should draw the UI using GL.
  327. */
  328. void
  329. puglSetDisplayFunc(PuglView* view, PuglDisplayFunc displayFunc);
  330. /**
  331. Set the function to call on keyboard events.
  332. */
  333. void
  334. puglSetKeyboardFunc(PuglView* view, PuglKeyboardFunc keyboardFunc);
  335. /**
  336. Set the function to call on mouse motion.
  337. */
  338. void
  339. puglSetMotionFunc(PuglView* view, PuglMotionFunc motionFunc);
  340. /**
  341. Set the function to call on mouse button events.
  342. */
  343. void
  344. puglSetMouseFunc(PuglView* view, PuglMouseFunc mouseFunc);
  345. /**
  346. Set the function to call on scroll events.
  347. */
  348. void
  349. puglSetScrollFunc(PuglView* view, PuglScrollFunc scrollFunc);
  350. /**
  351. Set the function to call on special events.
  352. */
  353. void
  354. puglSetSpecialFunc(PuglView* view, PuglSpecialFunc specialFunc);
  355. /**
  356. Set the function to call when the window size changes.
  357. */
  358. void
  359. puglSetReshapeFunc(PuglView* view, PuglReshapeFunc reshapeFunc);
  360. /**
  361. Set callback function to change window size.
  362. */
  363. void
  364. puglSetResizeFunc(PuglView* view, PuglResizeFunc resizeFunc);
  365. /**
  366. Set the function to call on file-browser selections.
  367. */
  368. void
  369. puglSetFileSelectedFunc(PuglView* view, PuglFileSelectedFunc fileSelectedFunc);
  370. /**
  371. @}
  372. */
  373. /**
  374. TODO document this.
  375. */
  376. int
  377. puglUpdateGeometryConstraints(PuglView* view, int min_width, int min_height, bool aspect);
  378. /**
  379. Grab the input focus.
  380. */
  381. void
  382. puglGrabFocus(PuglView* view);
  383. /**
  384. Process all pending window events.
  385. This handles input events as well as rendering, so it should be called
  386. regularly and rapidly enough to keep the UI responsive.
  387. */
  388. PuglStatus
  389. puglProcessEvents(PuglView* view);
  390. /**
  391. Request a redisplay on the next call to puglProcessEvents().
  392. */
  393. void
  394. puglPostRedisplay(PuglView* view);
  395. /**
  396. Request a resize on the next call to puglProcessEvents().
  397. */
  398. void
  399. puglPostResize(PuglView* view);
  400. /**
  401. Destroy a GL window.
  402. */
  403. void
  404. puglDestroy(PuglView* view);
  405. /**
  406. @}
  407. */
  408. #ifdef __cplusplus
  409. } /* extern "C" */
  410. #endif
  411. #endif /* PUGL_H_INCLUDED */