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.

499 lines
12KB

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