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.

298 lines
11KB

  1. /* ----
  2. * ---- file : lglw.h
  3. * ---- author : bsp
  4. * ---- legal : Distributed under terms of the MIT LICENSE (MIT).
  5. * ----
  6. * ---- Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * ---- of this software and associated documentation files (the "Software"), to deal
  8. * ---- in the Software without restriction, including without limitation the rights
  9. * ---- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * ---- copies of the Software, and to permit persons to whom the Software is
  11. * ---- furnished to do so, subject to the following conditions:
  12. * ----
  13. * ---- The above copyright notice and this permission notice shall be included in
  14. * ---- all copies or substantial portions of the Software.
  15. * ----
  16. * ---- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * ---- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * ---- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * ---- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * ---- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * ---- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * ---- THE SOFTWARE.
  23. * ----
  24. * ---- info : This is part of the "lglw" package.
  25. * ----
  26. * ---- created: 04Aug2018
  27. * ---- changed: 05Aug2018, 06Aug2018, 07Aug2018, 08Aug2018, 18Aug2018, 05Sep2018
  28. * ----
  29. * ----
  30. */
  31. #ifndef __LGLW_H__
  32. #define __LGLW_H__
  33. #include "cplusplus_begin.h"
  34. #include <stdint.h>
  35. // Opaque library / instance handle
  36. typedef void *lglw_t;
  37. // Boolean type
  38. #define LGLW_TRUE (1)
  39. #define LGLW_FALSE (0)
  40. typedef int32_t lglw_bool_t;
  41. // Vector type
  42. typedef struct lglw_vec2i_s {
  43. int32_t x;
  44. int32_t y;
  45. } lglw_vec2i_t;
  46. // Mouse buttons
  47. #define LGLW_MOUSE_LBUTTON (1u << 0)
  48. #define LGLW_MOUSE_RBUTTON (1u << 1)
  49. #define LGLW_MOUSE_MBUTTON (1u << 2)
  50. #define LGLW_MOUSE_WHEELUP (1u << 3)
  51. #define LGLW_MOUSE_WHEELDOWN (1u << 4)
  52. // Mouse button utility macros
  53. #define LGLW_IS_MOUSE_BUTTON_DOWN(a) ((a) == (_buttonState & _changedButtonState))
  54. #define LGLW_IS_MOUSE_BUTTON_UP(a) ( (0u != (_changedButtonState & (a))) && (0u == (_buttonState & (a))) )
  55. #define LGLW_IS_MOUSE_LBUTTON_DOWN() LGLW_IS_MOUSE_BUTTON_DOWN(LGLW_MOUSE_LBUTTON)
  56. #define LGLW_IS_MOUSE_RBUTTON_DOWN() LGLW_IS_MOUSE_BUTTON_DOWN(LGLW_MOUSE_RBUTTON)
  57. #define LGLW_IS_MOUSE_MBUTTON_DOWN() LGLW_IS_MOUSE_BUTTON_DOWN(LGLW_MOUSE_MBUTTON)
  58. #define LGLW_IS_MOUSE_WHEELUP() LGLW_IS_MOUSE_BUTTON_DOWN(LGLW_MOUSE_WHEELUP)
  59. #define LGLW_IS_MOUSE_WHEELDOWN() LGLW_IS_MOUSE_BUTTON_DOWN(LGLW_MOUSE_WHEELDOWN)
  60. #define LGLW_IS_MOUSE_LBUTTON_UP() LGLW_IS_MOUSE_BUTTON_UP(LGLW_MOUSE_LBUTTON)
  61. #define LGLW_IS_MOUSE_RBUTTON_UP() LGLW_IS_MOUSE_BUTTON_UP(LGLW_MOUSE_RBUTTON)
  62. #define LGLW_IS_MOUSE_MBUTTON_UP() LGLW_IS_MOUSE_BUTTON_UP(LGLW_MOUSE_MBUTTON)
  63. // Mouse grab modes
  64. #define LGLW_MOUSE_GRAB_NONE (0u) // no mouse grab
  65. #define LGLW_MOUSE_GRAB_CAPTURE (1u) // report mouse positions beyond window boundaries (+ show mouse pointer)
  66. #define LGLW_MOUSE_GRAB_WARP (2u) // capture + invisible mouse pointer + warp back to original position when ungrabbed
  67. // Focus types
  68. #define LGLW_FOCUS_MOUSE (1u << 0)
  69. // Focus utility macros
  70. #define LGLW_FOCUS_MOUSE_GAINED (LGLW_FOCUS_MOUSE == (_focusState & _changedFocusState))
  71. #define LGLW_FOCUS_MOUSE_LOST ( (0u != (_changedFocusState & LGLW_FOCUS_MOUSE)) && (0u == (_focusState & LGLW_FOCUS_MOUSE)) )
  72. // Modifier keys
  73. #define LGLW_KMOD_LCTRL (1u << 0)
  74. #define LGLW_KMOD_LSHIFT (1u << 1)
  75. #define LGLW_KMOD_LALT (1u << 2)
  76. #define LGLW_KMOD_LSUPER (1u << 3) // (note) unused (on Windows)
  77. #define LGLW_KMOD_RCTRL (1u << 4)
  78. #define LGLW_KMOD_RSHIFT (1u << 5)
  79. #define LGLW_KMOD_RALT (1u << 6)
  80. #define LGLW_KMOD_RSUPER (1u << 7) // (note) right windows key
  81. #define LGLW_KMOD_CTRL (LGLW_KMOD_LCTRL | LGLW_KMOD_RCTRL)
  82. #define LGLW_KMOD_SHIFT (LGLW_KMOD_LSHIFT | LGLW_KMOD_RSHIFT)
  83. #define LGLW_KMOD_ALT (LGLW_KMOD_LALT | LGLW_KMOD_RALT)
  84. #define LGLW_KMOD_SUPER (LGLW_KMOD_LSUPER | LGLW_KMOD_RSUPER)
  85. // Special (non-unicode) keys
  86. #define LGLW_VKEY_EXT (0x10000000u)
  87. #define LGLW_VKEY_BACKSPACE (LGLW_VKEY_EXT | 0x08u)
  88. #define LGLW_VKEY_TAB (LGLW_VKEY_EXT | 0x09u)
  89. #define LGLW_VKEY_RETURN (LGLW_VKEY_EXT | 0x0Du)
  90. #define LGLW_VKEY_ESCAPE (LGLW_VKEY_EXT | 0x1Bu)
  91. #define LGLW_VKEY_PAGEUP (LGLW_VKEY_EXT | 0x21u)
  92. #define LGLW_VKEY_PAGEDOWN (LGLW_VKEY_EXT | 0x22u)
  93. #define LGLW_VKEY_END (LGLW_VKEY_EXT | 0x23u)
  94. #define LGLW_VKEY_HOME (LGLW_VKEY_EXT | 0x24u)
  95. #define LGLW_VKEY_LEFT (LGLW_VKEY_EXT | 0x25u)
  96. #define LGLW_VKEY_UP (LGLW_VKEY_EXT | 0x26u)
  97. #define LGLW_VKEY_RIGHT (LGLW_VKEY_EXT | 0x27u)
  98. #define LGLW_VKEY_DOWN (LGLW_VKEY_EXT | 0x28u)
  99. #define LGLW_VKEY_INSERT (LGLW_VKEY_EXT | 0x2Du)
  100. #define LGLW_VKEY_DELETE (LGLW_VKEY_EXT | 0x2Eu)
  101. #define LGLW_VKEY_F1 (LGLW_VKEY_EXT | 0x70u)
  102. #define LGLW_VKEY_F2 (LGLW_VKEY_EXT | 0x71u)
  103. #define LGLW_VKEY_F3 (LGLW_VKEY_EXT | 0x72u)
  104. #define LGLW_VKEY_F4 (LGLW_VKEY_EXT | 0x73u)
  105. #define LGLW_VKEY_F5 (LGLW_VKEY_EXT | 0x74u)
  106. #define LGLW_VKEY_F6 (LGLW_VKEY_EXT | 0x75u)
  107. #define LGLW_VKEY_F7 (LGLW_VKEY_EXT | 0x76u)
  108. #define LGLW_VKEY_F8 (LGLW_VKEY_EXT | 0x77u)
  109. #define LGLW_VKEY_F9 (LGLW_VKEY_EXT | 0x78u)
  110. #define LGLW_VKEY_F10 (LGLW_VKEY_EXT | 0x79u)
  111. #define LGLW_VKEY_F11 (LGLW_VKEY_EXT | 0x7Au)
  112. #define LGLW_VKEY_F12 (LGLW_VKEY_EXT | 0x7Bu)
  113. #define LGLW_VKEY_NUMLOCK (LGLW_VKEY_EXT | 0x90u)
  114. #define LGLW_VKEY_SCROLLLOCK (LGLW_VKEY_EXT | 0x91u)
  115. #define LGLW_VKEY_LSHIFT (LGLW_VKEY_EXT | 0xa0u)
  116. #define LGLW_VKEY_RSHIFT (LGLW_VKEY_EXT | 0xa1u)
  117. #define LGLW_VKEY_LCTRL (LGLW_VKEY_EXT | 0xa2u)
  118. #define LGLW_VKEY_RCTRL (LGLW_VKEY_EXT | 0xa3u)
  119. #define LGLW_VKEY_LALT (LGLW_VKEY_EXT | 0xa4u)
  120. #define LGLW_VKEY_RALT (LGLW_VKEY_EXT | 0xa5u)
  121. #define LGLW_VKEY_LSUPER (LGLW_VKEY_EXT | 0x5bu) // start menu
  122. #define LGLW_VKEY_RSUPER (LGLW_VKEY_EXT | 0x5du) // context menu
  123. // Keyboard utility macros
  124. #define LGLW_IS_CHAR_KEY(a) (0u == ((a) & LGLW_VKEY_EXT))
  125. #define LGLW_IS_SPECIAL_KEY(a) (0u != ((a) & LGLW_VKEY_EXT))
  126. // Mouse callback function type
  127. typedef void (*lglw_mouse_fxn_t) (lglw_t _lglw, int32_t _x, int32_t _y, uint32_t _buttonState, uint32_t _changedButtonState);
  128. // Focus callback function type
  129. typedef void (*lglw_focus_fxn_t) (lglw_t _lglw, uint32_t _focusState, uint32_t _changedFocusState);
  130. // Keyboard callback function type
  131. // (note) 'vkey' is either a special key (cursor, insert, ..) or a unicode-translated character
  132. // Must return LGLW_TRUE when event has been processed, LGLW_FALSE when event shall be forwarded to next window / listener
  133. typedef lglw_bool_t (*lglw_keyboard_fxn_t) (lglw_t _lglw, uint32_t _vkey, uint32_t _kmod, lglw_bool_t _bPressed);
  134. // Timer callback function type
  135. typedef void (*lglw_timer_fxn_t) (lglw_t _lglw);
  136. // File drag'n'drop callback function type
  137. typedef void (*lglw_dropfiles_fxn_t) (lglw_t _lglw, int32_t _x, int32_t _y, uint32_t _numFiles, const char**_pathNames);
  138. // Redraw function type
  139. typedef void (*lglw_redraw_fxn_t) (lglw_t _lglw);
  140. // Initialize LGLW instance
  141. // (note) (w; h) determine the hidden window size, which should match the size of the actual window that is created later on
  142. // (note) when w or h is less than 16, a default width/height is used instead
  143. lglw_t lglw_init (int32_t _w, int32_t _h);
  144. // Shutdown LGLW instance
  145. void lglw_exit (lglw_t _lglw);
  146. // Set LGLW instance userdata
  147. void lglw_userdata_set (lglw_t _lglw, void *_userData);
  148. // Get LGLW instance userdata
  149. void *lglw_userdata_get (lglw_t _lglw);
  150. // Open LGLW window
  151. // Return: 1=window open, 0=error
  152. // (note) when w or h is less than 16, the hidden window size is used instead (see lglw_init())
  153. lglw_bool_t lglw_window_open (lglw_t _lglw, void *_parentHWNDOrNull, int32_t _x, int32_t _y, int32_t _w, int32_t _h);
  154. // Resize previously opened LGLW window
  155. lglw_bool_t lglw_window_resize (lglw_t _lglw, int32_t _w, int32_t _h);
  156. // Close LGLW window
  157. void lglw_window_close (lglw_t _lglw);
  158. // Show window
  159. void lglw_window_show (lglw_t _lglw);
  160. // Hide window
  161. void lglw_window_hide (lglw_t _lglw);
  162. // Check if window is visible
  163. lglw_bool_t lglw_window_is_visible (lglw_t _lglw);
  164. // Get window size
  165. void lglw_window_size_get (lglw_t _lglw, int32_t *_retX, int32_t *_retY);
  166. // Redraw window (send message)
  167. void lglw_redraw (lglw_t _lglw);
  168. // Set redraw callback
  169. void lglw_redraw_callback_set (lglw_t _lglw, lglw_redraw_fxn_t _cbk);
  170. // Save previous GL context and bind LGLW context
  171. void lglw_glcontext_push (lglw_t _lglw);
  172. // Rebind LGLW context
  173. // (note) experimental. try push/pop first.
  174. void lglw_glcontext_rebind (lglw_t _lglw);
  175. // Unbind LGLW context and restore previous GL context
  176. void lglw_glcontext_pop (lglw_t _lglw);
  177. // Swap front- and backbuffers
  178. void lglw_swap_buffers (lglw_t _lglw);
  179. // Set swap interval (0=vsync off, 1=vsync on)
  180. void lglw_swap_interval_set (lglw_t _lglw, int32_t _ival);
  181. // Get swap interval (0=vsync off, 1=vsync on)
  182. int32_t lglw_swap_interval_get (lglw_t _lglw);
  183. // Install mouse callback
  184. void lglw_mouse_callback_set (lglw_t _lglw, lglw_mouse_fxn_t _cbk);
  185. // Install focus callback
  186. void lglw_focus_callback_set (lglw_t _lglw, lglw_focus_fxn_t _cbk);
  187. // Install keyboard callback
  188. void lglw_keyboard_callback_set (lglw_t _lglw, lglw_keyboard_fxn_t _cbk);
  189. // Get current key modifier state
  190. uint32_t lglw_keyboard_get_modifiers (lglw_t _lglw);
  191. // Clear current key modifier state (workaround for stuck-modifier-after-dialog issue)
  192. void lglw_keyboard_clear_modifiers (lglw_t _lglw);
  193. // Get current mouse button state
  194. uint32_t lglw_mouse_get_buttons (lglw_t _lglw);
  195. // Grab (lock) mouse pointer
  196. // (note) see LGLW_MOUSE_GRAB_xxx
  197. void lglw_mouse_grab (lglw_t _lglw, uint32_t _grabMode);
  198. // Ungrab (unlock) mouse pointer
  199. void lglw_mouse_ungrab (lglw_t _lglw);
  200. // Set mouse pointer position
  201. void lglw_mouse_warp (lglw_t _lglw, int32_t _x, int32_t _y);
  202. // Show / hide mouse pointer
  203. void lglw_mouse_cursor_show (lglw_t _lglw, lglw_bool_t _bShow);
  204. // Start periodic timer
  205. // (note) requires an output window (see lglw_window_open())
  206. void lglw_timer_start (lglw_t _lglw, uint32_t _millisec);
  207. // Stop periodic timer
  208. void lglw_timer_stop (lglw_t _lglw);
  209. // Set periodic timer callback
  210. void lglw_timer_callback_set (lglw_t _lglw, lglw_timer_fxn_t _cbk);
  211. // Get milliseconds since init
  212. uint32_t lglw_time_get_millisec (lglw_t _lglw);
  213. // Set file drag'n'drop callback
  214. void lglw_dropfiles_callback_set (lglw_t _lglw, lglw_dropfiles_fxn_t _cbk);
  215. // Enable / disable Windows 8+ touch API
  216. void lglw_touchinput_set (lglw_t _lglw, lglw_bool_t _bEnable);
  217. // Check if touch input is enabled
  218. lglw_bool_t lglw_touchinput_get (lglw_t _lglw);
  219. // Show / hide virtual keyboard
  220. void lglw_touchkeyboard_show (lglw_t _lglw, lglw_bool_t _bEnable);
  221. // Set clipboard string
  222. // (note) numChars==0: copy until (including) ASCIIz
  223. void lglw_clipboard_text_set (lglw_t _lglw, const uint32_t _numChars, const char *_text);
  224. // Get clipboard string
  225. void lglw_clipboard_text_get (lglw_t _lglw, uint32_t _maxChars, uint32_t *_retNumChars, char *_retText);
  226. // Process all available events and return (i.e.: don't loop, don't block)
  227. // (note) events are usually delivered to the window's event callback (WndProc on Windows)
  228. // (note) do we really need this on Linux ?
  229. void lglw_events (lglw_t _lglw);
  230. #include "cplusplus_end.h"
  231. #endif // __LGLW_H__