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.

1158 lines
34KB

  1. /* ----
  2. * ---- file : lglw_linux.c (**stub**)
  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, 09Aug2018, 18Aug2018, 10Oct2018, 16Oct2018
  28. * ----
  29. * ----
  30. */
  31. #include "lglw.h"
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <X11/Xlib.h>
  36. #include <X11/Xutil.h>
  37. #include <X11/Xos.h>
  38. #include <GL/gl.h>
  39. #include <GL/glx.h>
  40. #define Dprintf if(0);else printf
  41. // #define Dprintf if(1);else printf
  42. // ---------------------------------------------------------------------------- macros and defines
  43. #define LGLW(a) lglw_int_t *lglw = ((lglw_int_t*)(a))
  44. #define LGLW_DEFAULT_HIDDEN_W (800)
  45. #define LGLW_DEFAULT_HIDDEN_H (600)
  46. #define LGLW_MOUSE_TOUCH_LMB_TIMEOUT (250u)
  47. #define LGLW_MOUSE_TOUCH_RMB_TIMEOUT (500u)
  48. #define LGLW_MOUSE_TOUCH_RMB_STATE_IDLE (0u)
  49. #define LGLW_MOUSE_TOUCH_RMB_STATE_LMB (1u)
  50. #define LGLW_MOUSE_TOUCH_RMB_STATE_WAIT (2u)
  51. #define LGLW_MOUSE_TOUCH_RMB_STATE_RMB (3u)
  52. #define LGLW_MOUSE_TOUCH_RMB_MOVE_THRESHOLD (7u)
  53. #define sABS(x) (((x)>0)?(x):-(x))
  54. // ---------------------------------------------------------------------------- structs and typedefs
  55. typedef struct lglw_int_s {
  56. void *user_data; // arbitrary user data
  57. Display *xdsp;
  58. XVisualInfo *vi;
  59. Colormap cmap;
  60. Window parent_xwnd; // created by host
  61. struct {
  62. lglw_vec2i_t size;
  63. Window xwnd;
  64. } hidden;
  65. struct {
  66. lglw_vec2i_t size;
  67. Window xwnd;
  68. lglw_bool_t mapped;
  69. int32_t swap_interval;
  70. } win;
  71. GLXContext ctx;
  72. struct {
  73. GLXContext ctx;
  74. GLXDrawable drw;
  75. } prev;
  76. struct {
  77. uint32_t kmod_state; // See LGLW_KMOD_xxx
  78. lglw_keyboard_fxn_t cbk;
  79. } keyboard;
  80. struct {
  81. lglw_vec2i_t p; // last seen mouse position
  82. uint32_t button_state;
  83. lglw_mouse_fxn_t cbk;
  84. struct {
  85. uint32_t mode;
  86. lglw_vec2i_t p; // grab-start mouse position
  87. lglw_bool_t b_queue_warp;
  88. lglw_vec2i_t last_p;
  89. } grab;
  90. struct {
  91. lglw_bool_t b_enable;
  92. lglw_bool_t b_update_queued;
  93. lglw_bool_t b_syn_rmb;
  94. uint32_t syn_rmb_hold_state; // see LGLW_MOUSE_TOUCH_RMB_STATE_xxx
  95. uint32_t hold_start_ms;
  96. lglw_vec2i_t hold_start_p;
  97. } touch;
  98. } mouse;
  99. struct {
  100. uint32_t state;
  101. lglw_focus_fxn_t cbk;
  102. } focus;
  103. struct {
  104. lglw_bool_t b_running;
  105. lglw_timer_fxn_t cbk;
  106. } timer;
  107. struct {
  108. lglw_dropfiles_fxn_t cbk;
  109. } dropfiles;
  110. struct {
  111. lglw_redraw_fxn_t cbk;
  112. } redraw;
  113. } lglw_int_t;
  114. // ---------------------------------------------------------------------------- module fxn fwd decls
  115. static lglw_bool_t loc_create_hidden_window (lglw_int_t *lglw, int32_t _w, int32_t _h);
  116. static void loc_destroy_hidden_window(lglw_int_t *lglw);
  117. static void loc_key_hook(lglw_int_t *lglw);
  118. static void loc_key_unhook(lglw_int_t *lglw);
  119. static lglw_bool_t loc_handle_key (lglw_int_t *lglw, lglw_bool_t _bPressed, uint32_t _vkey);
  120. // static lglw_bool_t loc_touchkeyboard_get_rect (RECT *rect);
  121. // static lglw_bool_t loc_touchkeyboard_is_visible (void);
  122. extern lglw_bool_t lglw_int_touchkeyboard_toggle (void);
  123. static void loc_handle_mouseleave (lglw_int_t *lglw);
  124. static void loc_handle_mouseenter (lglw_int_t *lglw);
  125. static void loc_handle_mousebutton (lglw_int_t *lglw, lglw_bool_t _bPressed, uint32_t _button);
  126. static void loc_handle_mousemotion (lglw_int_t *lglw);
  127. static void loc_handle_queued_mouse_warp (lglw_int_t *lglw);
  128. static void loc_touchinput_update (lglw_int_t *lglw);
  129. static void loc_enable_dropfiles (lglw_int_t *lglw, lglw_bool_t _bEnable);
  130. // ---------------------------------------------------------------------------- module vars
  131. static lglw_int_t *khook_lglw = NULL; // currently key-hooked lglw instance (one at a time)
  132. // TODO: remove and/or improve debug logging for a debug build
  133. // ---------------------------------------------------------------------------- lglw_log
  134. static FILE *logfile;
  135. void lglw_log(const char *logData, ...) {
  136. fprintf(logfile, logData);
  137. fflush(logfile);
  138. }
  139. // TODO: remove, or maybe not in some specific use cases
  140. // ---------------------------------------------------------------------------- xerror_log
  141. static int xerror_handler(Display *display, XErrorEvent *error) {
  142. char error_text[1024];
  143. XGetErrorText(display, error->error_code, error_text, 1024);
  144. lglw_log("XERROR (%d): %s, %d, %d\n", error->error_code, error_text, error->request_code, error->minor_code);
  145. return 0;
  146. }
  147. // ---------------------------------------------------------------------------- lglw_init
  148. lglw_t lglw_init(int32_t _w, int32_t _h) {
  149. lglw_int_t *lglw = malloc(sizeof(lglw_int_t));
  150. // TODO: remove/improve
  151. logfile = fopen("/home/cameron/src/VeeSeeVSTRack/other/log.txt", "w");
  152. XSetErrorHandler(xerror_handler);
  153. if(NULL != lglw)
  154. {
  155. memset(lglw, 0, sizeof(lglw_int_t));
  156. lglw_log("lglw:lglw_init: 1\n");
  157. if(_w <= 16)
  158. _w = LGLW_DEFAULT_HIDDEN_W;
  159. if(_h <= 16)
  160. _h = LGLW_DEFAULT_HIDDEN_H;
  161. lglw_log("lglw:lglw_init: 2\n");
  162. if(!loc_create_hidden_window(lglw, _w, _h))
  163. {
  164. free(lglw);
  165. lglw = NULL;
  166. }
  167. lglw_log("lglw:lglw_init: 3\n");
  168. }
  169. lglw_log("lglw:lglw_init: EXIT\n");
  170. return lglw;
  171. }
  172. // ---------------------------------------------------------------------------- lglw_exit
  173. void lglw_exit(lglw_t _lglw) {
  174. LGLW(_lglw);
  175. if(NULL != lglw)
  176. {
  177. lglw_log("lglw:lglw_exit: 1\n");
  178. loc_destroy_hidden_window(lglw);
  179. lglw_log("lglw:lglw_exit: 2\n");
  180. fclose(logfile);
  181. free(lglw);
  182. }
  183. }
  184. // ---------------------------------------------------------------------------- lglw_userdata_set
  185. void lglw_userdata_set(lglw_t _lglw, void *_userData) {
  186. LGLW(_lglw);
  187. if(NULL != lglw)
  188. {
  189. lglw_log("lglw:lglw_userdata_set: 1\n");
  190. lglw->user_data = _userData;
  191. }
  192. }
  193. // ---------------------------------------------------------------------------- lglw_userdata_get
  194. void *lglw_userdata_get(lglw_t _lglw) {
  195. LGLW(_lglw);
  196. if(NULL != lglw)
  197. {
  198. lglw_log("lglw:lglw_userdata_get: 1\n");
  199. return lglw->user_data;
  200. }
  201. return NULL;
  202. }
  203. // ---------------------------------------------------------------------------- loc_create_hidden_window
  204. static lglw_bool_t loc_create_hidden_window(lglw_int_t *lglw, int32_t _w, int32_t _h) {
  205. // TODO: compare to 'WindowClass' from Windows implementation
  206. lglw_log("lglw:loc_create_hidden_window: 1\n");
  207. XSetWindowAttributes swa;
  208. int attrib[] = { GLX_RGBA, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE, 24, None };
  209. int screen;
  210. lglw_log("lglw:loc_create_hidden_window: 2\n");
  211. lglw->xdsp = XOpenDisplay(NULL);
  212. screen = DefaultScreen(lglw->xdsp);
  213. lglw_log("lglw:loc_create_hidden_window: 3\n");
  214. lglw->vi = glXChooseVisual(lglw->xdsp, screen, attrib);
  215. lglw_log("lglw:loc_create_hidden_window: 4\n");
  216. if(NULL == lglw->vi)
  217. {
  218. lglw_log("[---] lglw: failed to find GLX Visual for hidden window\n");
  219. return LGLW_FALSE;
  220. }
  221. lglw_log("lglw:loc_create_hidden_window: 5\n");
  222. lglw->ctx = glXCreateContext(lglw->xdsp, lglw->vi, None, True);
  223. lglw_log("lglw:loc_create_hidden_window: 6\n");
  224. if(NULL == lglw->ctx)
  225. {
  226. lglw_log("[---] lglw: failed to create GLX Context for hidden window\n");
  227. return LGLW_FALSE;
  228. }
  229. lglw_log("lglw:loc_create_hidden_window: 7\n");
  230. lglw->cmap = XCreateColormap(lglw->xdsp, RootWindow(lglw->xdsp, lglw->vi->screen),
  231. lglw->vi->visual, AllocNone);
  232. lglw_log("lglw:loc_create_hidden_window: 8\n");
  233. swa.border_pixel = 0;
  234. swa.colormap = lglw->cmap;
  235. lglw->hidden.xwnd = XCreateWindow(lglw->xdsp, DefaultRootWindow(lglw->xdsp),
  236. 0, 0, LGLW_DEFAULT_HIDDEN_W, LGLW_DEFAULT_HIDDEN_H, 0, CopyFromParent, InputOutput,
  237. lglw->vi->visual, CWBorderPixel | CWColormap, &swa);
  238. lglw_log("lglw:loc_create_hidden_window: 9\n");
  239. XSetStandardProperties(lglw->xdsp, lglw->hidden.xwnd, "LGLW_hidden", "LGLW_hidden", None, NULL, 0, NULL);
  240. XSync(lglw->xdsp, False);
  241. lglw_log("lglw:loc_create_hidden_window: EXIT\n");
  242. lglw->hidden.size.x = _w;
  243. lglw->hidden.size.y = _h;
  244. return LGLW_TRUE;
  245. }
  246. // ---------------------------------------------------------------------------- loc_destroy_hidden_window
  247. static void loc_destroy_hidden_window(lglw_int_t *lglw) {
  248. lglw_log("lglw:loc_destroy_hidden_window: 1\n");
  249. if(NULL != lglw->xdsp && NULL != lglw->ctx)
  250. {
  251. glXMakeCurrent(lglw->xdsp, None, NULL);
  252. glXDestroyContext(lglw->xdsp, lglw->ctx);
  253. }
  254. lglw_log("lglw:loc_destroy_hidden_window: 2\n");
  255. if(NULL != lglw->xdsp && 0 != lglw->hidden.xwnd) XDestroyWindow(lglw->xdsp, lglw->hidden.xwnd);
  256. lglw_log("lglw:loc_destroy_hidden_window: 3\n");
  257. if(NULL != lglw->xdsp && 0 != lglw->cmap) XFreeColormap(lglw->xdsp, lglw->cmap);
  258. lglw_log("lglw:loc_destroy_hidden_window: 4\n");
  259. if(NULL != lglw->vi) XFree(lglw->vi);
  260. lglw_log("lglw:loc_destroy_hidden_window: 5\n");
  261. XSync(lglw->xdsp, False);
  262. if(NULL != lglw->xdsp) XCloseDisplay(lglw->xdsp);
  263. }
  264. // ---------------------------------------------------------------------------- loc_setEventProc
  265. // https://www.kvraudio.com/forum/viewtopic.php?t=387924
  266. // https://github.com/Ardour/ardour/blob/master/gtk2_ardour/linux_vst_gui_support.cc
  267. // https://discourse.ardour.org/t/overtonedsp-plugins/90115/22
  268. // https://github.com/amsynth/amsynth/blob/4a87798e650c6d71d70274a961c9b8d98fc6da7e/src/amsynth_vst.cpp
  269. // https://github.com/rsenn/eXT2/blob/7f00a09561ded8175ffed2f4912dad74e466a1c7/vstplugins/vstgui/vstgui.cpp
  270. // https://github.com/COx2/DistortionFilter/blob/c6a34fb56b503a6e95bf0975e00f438bbf4ff52a/juce/modules/juce_audio_processors/format_types/juce_VSTPluginFormat.cpp
  271. // Very simple function to test _XEventProc is properly called
  272. void loc_eventProc(void *xevent) {
  273. // lglw_log("XEventProc");
  274. printf("XEventProc\n");
  275. }
  276. // Pulled from the Renoise 64-bit callback example
  277. // Unsure what data was supposed to be, but swapping it to a function name did not work
  278. // This does nothing, no event proc found
  279. // TODO: 32-bit support
  280. // void loc_setEventProc (Display *display, Window window) {
  281. // size_t data = (size_t)loc_eventProc;
  282. // long temp[2];
  283. // // Split the 64 bit pointer into a little-endian long array
  284. // temp[0] = (long)(data & 0xffffffffUL);
  285. // temp[1] = (long)(data >> 32L);
  286. // Atom atom = XInternAtom(display, "_XEventProc", False);
  287. // XChangeProperty(display, window, atom, atom, 32,
  288. // PropModeReplace, (unsigned char*)temp, 2);
  289. // }
  290. // Pulled from the eXT2 example
  291. // TODO: 32-bit support
  292. void loc_setEventProc (Display *display, Window window) {
  293. void* data = (void*)&loc_eventProc; // swapped the function name here
  294. Atom atom = XInternAtom(display, "_XEventProc", False);
  295. XChangeProperty(display, window, atom, atom, 32,
  296. PropModeReplace, (unsigned char*)&data, 1); // 1 instead of 2 will crash Ardour, 2 will not do anything
  297. }
  298. // Pulled from the amsynth example
  299. // Simply swapped out the function names, crashes Ardour in the same was as the others
  300. // void loc_setEventProc (Display *display, Window window) {
  301. // #ifdef BUILD_64
  302. // //
  303. // // JUCE calls XGetWindowProperty with long_length = 1 which means it only fetches the lower 32 bits of the address.
  304. // // Therefore we need to ensure we return an address in the lower 32-bits of address space.
  305. // //
  306. // // based on mach_override
  307. // static const unsigned char kJumpInstructions[] = {
  308. // 0xFF, 0x25, 0x00, 0x00, 0x00, 0x00,
  309. // 0x00, 0x00, 0x00, 0x00,
  310. // 0x00, 0x00, 0x00, 0x00
  311. // };
  312. // static const int kJumpAddress = 6;
  313. // static char *ptr;
  314. // if (!ptr) {
  315. // ptr = (char *)mmap(0,
  316. // PAGE_SIZE,
  317. // PROT_READ | PROT_WRITE | PROT_EXEC,
  318. // MAP_ANONYMOUS | MAP_PRIVATE | MAP_32BIT,
  319. // 0, 0);
  320. // if (ptr == MAP_FAILED) {
  321. // perror("mmap");
  322. // ptr = 0;
  323. // return;
  324. // } else {
  325. // memcpy(ptr, kJumpInstructions, sizeof(kJumpInstructions));
  326. // *((uint64_t *)(ptr + kJumpAddress)) = (uint64_t)(&loc_eventProc);
  327. // msync(ptr, sizeof(kJumpInstructions), MS_INVALIDATE);
  328. // }
  329. // }
  330. // long temp[2] = {(long)ptr, 0};
  331. // Atom atom = XInternAtom(display, "_XEventProc", False);
  332. // XChangeProperty(display, window, atom, atom, 32, PropModeReplace, (unsigned char *)temp, 2);
  333. // #else
  334. // long temp[1] = {(long)(void *)(&loc_eventProc)};
  335. // Atom atom = XInternAtom(display, "_XEventProc", False);
  336. // XChangeProperty(display, window, atom, atom, 32, PropModeReplace, (unsigned char *)temp, 1);
  337. // #endif
  338. // }
  339. // ---------------------------------------------------------------------------- lglw_window_open
  340. lglw_bool_t lglw_window_open (lglw_t _lglw, void *_parentHWNDOrNull, int32_t _x, int32_t _y, int32_t _w, int32_t _h) {
  341. lglw_bool_t r = LGLW_FALSE;
  342. LGLW(_lglw);
  343. if(NULL != lglw)
  344. {
  345. lglw_log("lglw:lglw_window_open: 1, %p, %i\n", (Window)_parentHWNDOrNull, (Window)_parentHWNDOrNull);
  346. lglw->parent_xwnd = (0 == _parentHWNDOrNull) ? DefaultRootWindow(lglw->xdsp) : (Window)_parentHWNDOrNull;
  347. lglw_log("lglw:lglw_window_open: 2\n");
  348. if(_w <= 16)
  349. _w = lglw->hidden.size.x;
  350. lglw_log("lglw:lglw_window_open: 3\n");
  351. if(_h <= 16)
  352. _h = lglw->hidden.size.y;
  353. // TODO: compare to 'WindowClass' from Windows implementation
  354. lglw_log("lglw:lglw_window_open: 4\n");
  355. XSetWindowAttributes swa;
  356. XEvent event;
  357. XSync(lglw->xdsp, False);
  358. lglw_log("lglw:lglw_window_open: 5\n");
  359. swa.border_pixel = 0;
  360. swa.colormap = lglw->cmap;
  361. swa.event_mask = EnterWindowMask | LeaveWindowMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask;
  362. lglw->win.xwnd = XCreateWindow(lglw->xdsp, DefaultRootWindow(lglw->xdsp),
  363. 0, 0, _w, _h, 0, CopyFromParent, InputOutput,
  364. lglw->vi->visual, CWBorderPixel | CWColormap | CWEventMask, &swa);
  365. lglw_log("lglw:lglw_window_open: 6\n");
  366. XSetStandardProperties(lglw->xdsp, lglw->win.xwnd, "LGLW", "LGLW", None, NULL, 0, NULL);
  367. // Setup the event proc now, on the parent window as well just for the debug host
  368. // It was simpler to do this than check in the debug host for the reparent event
  369. lglw_log("lglw:lglw_window_open: 7\n");
  370. loc_setEventProc(lglw->xdsp, lglw->win.xwnd);
  371. loc_setEventProc(lglw->xdsp, lglw->parent_xwnd);
  372. // Some hosts only check and store the callback when the Window is reparented
  373. // Since creating the Window with a Parent may or may not do that, but the callback is not set,
  374. // ... it's created as a root window, the callback is set, and then it's reparented
  375. if (0 != _parentHWNDOrNull)
  376. {
  377. lglw_log("lglw:lglw_window_open: 8\n");
  378. XReparentWindow(lglw->xdsp, lglw->win.xwnd, lglw->parent_xwnd, 0, 0);
  379. }
  380. lglw_log("lglw:lglw_window_open: 9\n");
  381. XMapRaised(lglw->xdsp, lglw->win.xwnd);
  382. XSync(lglw->xdsp, False);
  383. lglw->win.mapped = LGLW_TRUE;
  384. lglw_log("lglw:lglw_window_open: 10\n");
  385. lglw->win.size.x = _w;
  386. lglw->win.size.y = _h;
  387. lglw_log("lglw:lglw_window_open: 11\n");
  388. loc_enable_dropfiles(lglw, (NULL != lglw->dropfiles.cbk));
  389. lglw_log("lglw:lglw_window_open: EXIT\n");
  390. }
  391. return r;
  392. }
  393. // ---------------------------------------------------------------------------- lglw_window_resize
  394. lglw_bool_t lglw_window_resize (lglw_t _lglw, int32_t _w, int32_t _h) {
  395. lglw_bool_t r = LGLW_FALSE;
  396. LGLW(_lglw);
  397. if(NULL != lglw)
  398. {
  399. if(0 != lglw->win.xwnd)
  400. {
  401. lglw_log("lglw:lglw_window_resize: 1\n");
  402. r = LGLW_TRUE;
  403. lglw_log("lglw:lglw_window_resize: 2\n");
  404. XResizeWindow(lglw->xdsp, lglw->win.xwnd, _w, _h);
  405. XRaiseWindow(lglw->xdsp, lglw->win.xwnd);
  406. lglw_log("lglw:lglw_window_resize: 3\n");
  407. int deltaW = _w - lglw->win.size.x;
  408. int deltaH = _h - lglw->win.size.y;
  409. lglw_log("lglw:lglw_window_resize: 4\n");
  410. lglw->win.size.x = _w;
  411. lglw->win.size.y = _h;
  412. lglw_log("lglw:lglw_window_resize: 5\n");
  413. Window root, parent, *children = NULL;
  414. unsigned int num_children;
  415. lglw_log("lglw:lglw_window_resize: 6\n");
  416. if(!XQueryTree(lglw->xdsp, lglw->win.xwnd, &root, &parent, &children, &num_children))
  417. return r;
  418. lglw_log("lglw:lglw_window_resize: 7\n");
  419. if(children)
  420. XFree((char *)children);
  421. lglw_log("lglw:lglw_window_resize: 8\n");
  422. // Resize parent window (if any)
  423. if(0 != parent)
  424. {
  425. lglw_log("lglw:lglw_window_resize: 8.1\n");
  426. int x, y;
  427. unsigned int width, height;
  428. unsigned int border_width;
  429. unsigned int depth;
  430. lglw_log("lglw:lglw_window_resize: 8.2\n");
  431. if(!XGetGeometry(lglw->xdsp, lglw->win.xwnd, &root, &x, &y, &width, &height, &border_width, &depth))
  432. return r;
  433. lglw_log("lglw:lglw_window_resize: 8.3\n");
  434. XResizeWindow(lglw->xdsp, parent, width + deltaW, height + deltaH);
  435. }
  436. lglw_log("lglw:lglw_window_resize: EXIT\n");
  437. }
  438. }
  439. return r;
  440. }
  441. // ---------------------------------------------------------------------------- lglw_window_close
  442. void lglw_window_close (lglw_t _lglw) {
  443. LGLW(_lglw);
  444. if(NULL != lglw)
  445. {
  446. if(0 != lglw->win.xwnd)
  447. {
  448. lglw_log("lglw:lglw_window_close: 1\n");
  449. lglw_timer_stop(_lglw);
  450. lglw_log("lglw:lglw_window_close: 2\n");
  451. loc_key_unhook(lglw);
  452. lglw_log("lglw:lglw_window_close: 3\n");
  453. glXMakeCurrent(lglw->xdsp, None, NULL);
  454. lglw_log("lglw:lglw_window_close: 4\n");
  455. XDestroyWindow(lglw->xdsp, lglw->win.xwnd);
  456. XSync(lglw->xdsp, False);
  457. lglw->win.xwnd = 0;
  458. lglw->win.mapped = LGLW_FALSE;
  459. }
  460. }
  461. lglw_log("lglw:lglw_window_close: EXIT\n");
  462. }
  463. // ---------------------------------------------------------------------------- lglw_window_show
  464. void lglw_window_show(lglw_t _lglw) {
  465. LGLW(_lglw);
  466. if(NULL != lglw)
  467. {
  468. lglw_log("lglw:lglw_window_show: 1\n");
  469. XMapRaised(lglw->xdsp, lglw->win.xwnd);
  470. lglw->win.mapped = LGLW_TRUE;
  471. }
  472. lglw_log("lglw:lglw_window_show: EXIT\n");
  473. }
  474. // ---------------------------------------------------------------------------- lglw_window_hide
  475. void lglw_window_hide(lglw_t _lglw) {
  476. LGLW(_lglw);
  477. if(NULL != lglw)
  478. {
  479. lglw_log("lglw:lglw_window_hide: 1\n");
  480. XUnmapWindow(lglw->xdsp, lglw->win.xwnd);
  481. lglw->win.mapped = LGLW_FALSE;
  482. }
  483. lglw_log("lglw:lglw_window_hide: EXIT\n");
  484. }
  485. // ---------------------------------------------------------------------------- lglw_window_is_visible
  486. lglw_bool_t lglw_window_is_visible(lglw_t _lglw) {
  487. lglw_bool_t r = LGLW_FALSE;
  488. LGLW(_lglw);
  489. // lglw_log("lglw:lglw_window_is_visible: 1\n");
  490. if(NULL != lglw && 0 != lglw->win.xwnd)
  491. {
  492. // lglw_log("lglw:lglw_window_is_visible: 2\n");
  493. r = lglw->win.mapped;
  494. }
  495. // lglw_log("lglw:lglw_window_is_visible: EXIT\n");
  496. return r;
  497. }
  498. // ---------------------------------------------------------------------------- lglw_window_size_get
  499. void lglw_window_size_get(lglw_t _lglw, int32_t *_retX, int32_t *_retY) {
  500. LGLW(_lglw);
  501. lglw_log("lglw:lglw_window_size_get: 1\n");
  502. if(NULL != lglw)
  503. {
  504. if(0 != lglw->win.xwnd)
  505. {
  506. if(NULL != _retX)
  507. *_retX = lglw->win.size.x;
  508. if(NULL != _retY)
  509. *_retY = lglw->win.size.y;
  510. }
  511. }
  512. lglw_log("lglw:lglw_window_size_get: EXIT\n");
  513. }
  514. // ---------------------------------------------------------------------------- lglw_redraw
  515. void lglw_redraw(lglw_t _lglw) {
  516. LGLW(_lglw);
  517. // (todo) implement me
  518. if(NULL != lglw)
  519. {
  520. if(0 != lglw->win.xwnd)
  521. {
  522. // TODO Event Loop
  523. lglw_log("lglw:lglw_redraw: 1\n");
  524. XClearArea(lglw->xdsp, lglw->win.xwnd, 0, 0, 1, 1, True); // clear tiny area for exposing
  525. XFlush(lglw->xdsp);
  526. }
  527. }
  528. }
  529. // ---------------------------------------------------------------------------- lglw_redraw_callback_set
  530. void lglw_redraw_callback_set(lglw_t _lglw, lglw_redraw_fxn_t _cbk) {
  531. LGLW(_lglw);
  532. if(NULL != lglw)
  533. {
  534. lglw->redraw.cbk = _cbk;
  535. }
  536. }
  537. // ---------------------------------------------------------------------------- lglw_glcontext_push
  538. void lglw_glcontext_push(lglw_t _lglw) {
  539. LGLW(_lglw);
  540. if(NULL != lglw)
  541. {
  542. lglw->prev.drw = glXGetCurrentDrawable();
  543. lglw->prev.ctx = glXGetCurrentContext();
  544. // lglw_log("lglw:lglw_glcontext_push: win.xwnd=%p hidden.xwnd=%p ctx=%p\n",
  545. // lglw->win.xwnd, lglw->hidden.xwnd, lglw->ctx);
  546. if(!glXMakeCurrent(lglw->xdsp, (0 == lglw->win.xwnd) ? lglw->hidden.xwnd : lglw->win.xwnd, lglw->ctx))
  547. {
  548. lglw_log("[---] lglw_glcontext_push: glXMakeCurrent() failed. win.xwnd=%p hidden.xwnd=%p ctx=%p glGetError()=%d\n", lglw->win.xwnd, lglw->hidden.xwnd, lglw->ctx, glGetError());
  549. }
  550. }
  551. }
  552. // ---------------------------------------------------------------------------- lglw_glcontext_pop
  553. void lglw_glcontext_pop(lglw_t _lglw) {
  554. LGLW(_lglw);
  555. if(NULL != lglw)
  556. {
  557. // lglw_log("lglw:lglw_glcontext_pop: prev.drw=%p prev.ctx=%p\n",
  558. // lglw->prev.drw, lglw->prev.ctx);
  559. if(!glXMakeCurrent(lglw->xdsp, lglw->prev.drw, lglw->prev.ctx))
  560. {
  561. lglw_log("[---] lglw_glcontext_pop: glXMakeCurrent() failed. prev.drw=%p ctx=%p glGetError()=%d\n", lglw->prev.drw, lglw->prev.ctx, glGetError());
  562. }
  563. }
  564. }
  565. // ---------------------------------------------------------------------------- lglw_swap_buffers
  566. void lglw_swap_buffers(lglw_t _lglw) {
  567. LGLW(_lglw);
  568. if(NULL != lglw)
  569. {
  570. if(0 != lglw->win.xwnd)
  571. {
  572. // lglw_log("lglw:lglw_swap_buffers: 1\n");
  573. glXSwapBuffers(lglw->xdsp, lglw->win.xwnd);
  574. }
  575. }
  576. }
  577. // ---------------------------------------------------------------------------- lglw_swap_interval_set
  578. typedef void (APIENTRY *PFNWGLEXTSWAPINTERVALPROC) (int);
  579. void lglw_swap_interval_set(lglw_t _lglw, int32_t _ival) {
  580. LGLW(_lglw);
  581. if(NULL != lglw)
  582. {
  583. lglw_log("lglw:lglw_swap_interval_set: 1\n");
  584. PFNWGLEXTSWAPINTERVALPROC glXSwapIntervalEXT;
  585. glXSwapIntervalEXT = (PFNWGLEXTSWAPINTERVALPROC) glXGetProcAddress("glXSwapIntervalEXT");
  586. if(NULL != glXSwapIntervalEXT)
  587. {
  588. lglw_log("lglw:lglw_swap_interval_set: 2\n");
  589. glXSwapIntervalEXT(_ival);
  590. lglw->win.swap_interval = _ival;
  591. }
  592. }
  593. }
  594. // ---------------------------------------------------------------------------- lglw_swap_interval_get
  595. int32_t lglw_swap_interval_get(lglw_t _lglw) {
  596. LGLW(_lglw);
  597. int32_t r = 0;
  598. if(NULL != lglw)
  599. {
  600. r = lglw->win.swap_interval;
  601. }
  602. return r;
  603. }
  604. // ---------------------------------------------------------------------------- loc_key_hook
  605. static void loc_key_hook(lglw_int_t *lglw) {
  606. loc_key_unhook(lglw);
  607. // (todo) implement me
  608. khook_lglw = lglw;
  609. }
  610. // ---------------------------------------------------------------------------- loc_key_unhook
  611. static void loc_key_unhook(lglw_int_t *lglw) {
  612. // (todo) implement me
  613. if(khook_lglw == lglw)
  614. khook_lglw = NULL;
  615. }
  616. // ---------------------------------------------------------------------------- loc_handle_mouseleave
  617. static void loc_handle_mouseleave(lglw_int_t *lglw) {
  618. loc_key_unhook(lglw);
  619. lglw->focus.state &= ~LGLW_FOCUS_MOUSE;
  620. if(NULL != lglw->focus.cbk)
  621. {
  622. lglw->focus.cbk(lglw, lglw->focus.state, LGLW_FOCUS_MOUSE);
  623. }
  624. lglw_log("xxx lglw:loc_handle_mouseleave: LEAVE\n");
  625. }
  626. // ---------------------------------------------------------------------------- loc_handle_mouseenter
  627. static void loc_handle_mouseenter(lglw_int_t *lglw) {
  628. loc_key_hook(lglw);
  629. lglw->focus.state |= LGLW_FOCUS_MOUSE;
  630. if(NULL != lglw->focus.cbk)
  631. {
  632. lglw->focus.cbk(lglw, lglw->focus.state, LGLW_FOCUS_MOUSE);
  633. }
  634. lglw_log("xxx lglw:loc_handle_mouseenter: LEAVE\n");
  635. }
  636. // ---------------------------------------------------------------------------- loc_handle_mousebutton
  637. static void loc_handle_mousebutton(lglw_int_t *lglw, lglw_bool_t _bPressed, uint32_t _button) {
  638. if(_bPressed)
  639. lglw->mouse.button_state |= _button;
  640. else
  641. lglw->mouse.button_state &= ~_button;
  642. if(NULL != lglw->mouse.cbk)
  643. {
  644. lglw->mouse.cbk(lglw, lglw->mouse.p.x, lglw->mouse.p.y, lglw->mouse.button_state, _button);
  645. }
  646. }
  647. // ---------------------------------------------------------------------------- loc_handle_mousemotion
  648. static void loc_handle_mousemotion(lglw_int_t *lglw) {
  649. if(NULL != lglw->mouse.cbk)
  650. {
  651. lglw->mouse.cbk(lglw, lglw->mouse.p.x, lglw->mouse.p.y, lglw->mouse.button_state, 0u/*changedbuttonstate*/);
  652. }
  653. }
  654. // ---------------------------------------------------------------------------- lglw_mouse_callback_set
  655. void lglw_mouse_callback_set(lglw_t _lglw, lglw_mouse_fxn_t _cbk) {
  656. LGLW(_lglw);
  657. if(NULL != lglw)
  658. {
  659. lglw->mouse.cbk = _cbk;
  660. }
  661. }
  662. // ---------------------------------------------------------------------------- lglw_mouse_callback_set
  663. void lglw_focus_callback_set(lglw_t _lglw, lglw_focus_fxn_t _cbk) {
  664. LGLW(_lglw);
  665. if(NULL != lglw)
  666. {
  667. lglw->focus.cbk = _cbk;
  668. }
  669. }
  670. // ---------------------------------------------------------------------------- loc_handle_key
  671. static lglw_bool_t loc_handle_key(lglw_int_t *lglw, lglw_bool_t _bPressed, uint32_t _vkey) {
  672. lglw_bool_t r = LGLW_FALSE;
  673. if(NULL != lglw->keyboard.cbk)
  674. {
  675. r = lglw->keyboard.cbk(lglw, _vkey, lglw->keyboard.kmod_state, _bPressed);
  676. }
  677. return r;
  678. }
  679. // ---------------------------------------------------------------------------- lglw_keyboard_callback_set
  680. void lglw_keyboard_callback_set(lglw_t _lglw, lglw_keyboard_fxn_t _cbk) {
  681. LGLW(_lglw);
  682. if(NULL != lglw)
  683. {
  684. lglw->keyboard.cbk = _cbk;
  685. }
  686. }
  687. // ---------------------------------------------------------------------------- lglw_keyboard_get_modifiers
  688. uint32_t lglw_keyboard_get_modifiers(lglw_t _lglw) {
  689. uint32_t r = 0u;
  690. LGLW(_lglw);
  691. if(NULL != lglw)
  692. {
  693. r = lglw->keyboard.kmod_state;
  694. }
  695. return r;
  696. }
  697. // ---------------------------------------------------------------------------- lglw_touchkeyboard_show
  698. void lglw_touchkeyboard_show(lglw_t _lglw, lglw_bool_t _bEnable) {
  699. LGLW(_lglw);
  700. // (todo) implement me
  701. if(NULL != lglw)
  702. {
  703. }
  704. }
  705. // ---------------------------------------------------------------------------- lglw_mouse_get_buttons
  706. uint32_t lglw_mouse_get_buttons(lglw_t _lglw) {
  707. uint32_t r = 0u;
  708. LGLW(_lglw);
  709. if(NULL != lglw)
  710. {
  711. r = lglw->mouse.button_state;
  712. }
  713. return r;
  714. }
  715. // ---------------------------------------------------------------------------- lglw_mouse_grab
  716. void lglw_mouse_grab(lglw_t _lglw, uint32_t _grabMode) {
  717. LGLW(_lglw);
  718. if(NULL != lglw)
  719. {
  720. // (todo) implement me
  721. // if(NULL != lglw->win.hwnd)
  722. {
  723. if(!lglw->mouse.touch.b_enable)
  724. {
  725. if(lglw->mouse.grab.mode != _grabMode)
  726. {
  727. lglw_mouse_ungrab(_lglw);
  728. }
  729. switch(_grabMode)
  730. {
  731. default:
  732. case LGLW_MOUSE_GRAB_NONE:
  733. break;
  734. case LGLW_MOUSE_GRAB_CAPTURE:
  735. // (todo) implement me
  736. // (void)SetCapture(lglw->win.hwnd);
  737. lglw->mouse.grab.mode = _grabMode;
  738. break;
  739. case LGLW_MOUSE_GRAB_WARP:
  740. // (todo) implement me
  741. // (void)SetCapture(lglw->win.hwnd);
  742. lglw_mouse_cursor_show(_lglw, LGLW_FALSE);
  743. lglw->mouse.grab.p = lglw->mouse.p;
  744. lglw->mouse.grab.last_p = lglw->mouse.p;
  745. lglw->mouse.grab.mode = _grabMode;
  746. break;
  747. }
  748. }
  749. }
  750. }
  751. }
  752. // ---------------------------------------------------------------------------- lglw_mouse_ungrab
  753. void lglw_mouse_ungrab(lglw_t _lglw) {
  754. LGLW(_lglw);
  755. if(NULL != lglw)
  756. {
  757. // (todo) implement me
  758. // if(NULL != lglw->win.hwnd)
  759. {
  760. if(!lglw->mouse.touch.b_enable)
  761. {
  762. switch(lglw->mouse.grab.mode)
  763. {
  764. default:
  765. case LGLW_MOUSE_GRAB_NONE:
  766. break;
  767. case LGLW_MOUSE_GRAB_CAPTURE:
  768. // (todo) implement me
  769. // (void)ReleaseCapture();
  770. lglw->mouse.grab.mode = LGLW_MOUSE_GRAB_NONE;
  771. break;
  772. case LGLW_MOUSE_GRAB_WARP:
  773. // (todo) implement me
  774. // (void)ReleaseCapture();
  775. lglw->mouse.grab.mode = LGLW_MOUSE_GRAB_NONE;
  776. lglw->mouse.grab.b_queue_warp = LGLW_TRUE;
  777. lglw_mouse_cursor_show(_lglw, LGLW_TRUE);
  778. break;
  779. }
  780. }
  781. }
  782. }
  783. }
  784. // ---------------------------------------------------------------------------- lglw_mouse_warp
  785. void lglw_mouse_warp(lglw_t _lglw, int32_t _x, int32_t _y) {
  786. LGLW(_lglw);
  787. // (todo) implement me
  788. if(NULL != lglw)
  789. {
  790. }
  791. }
  792. // ---------------------------------------------------------------------------- loc_handle_queued_mouse_warp
  793. static void loc_handle_queued_mouse_warp(lglw_int_t *lglw) {
  794. if(lglw->mouse.grab.b_queue_warp)
  795. {
  796. lglw->mouse.grab.b_queue_warp = LGLW_FALSE;
  797. lglw_mouse_warp(lglw, lglw->mouse.grab.p.x, lglw->mouse.grab.p.y);
  798. lglw->mouse.grab.last_p = lglw->mouse.grab.p;
  799. }
  800. }
  801. // ---------------------------------------------------------------------------- lglw_mouse_cursor_show
  802. void lglw_mouse_cursor_show (lglw_t _lglw, lglw_bool_t _bShow) {
  803. LGLW(_lglw);
  804. // (todo) implement me
  805. if(NULL != lglw)
  806. {
  807. }
  808. }
  809. // ---------------------------------------------------------------------------- lglw_timer_start
  810. void lglw_timer_start(lglw_t _lglw, uint32_t _millisec) {
  811. LGLW(_lglw);
  812. // (todo) implement me
  813. if(NULL != lglw)
  814. {
  815. }
  816. }
  817. // ---------------------------------------------------------------------------- lglw_timer_stop
  818. void lglw_timer_stop(lglw_t _lglw) {
  819. LGLW(_lglw);
  820. // (todo) implement me
  821. if(NULL != lglw)
  822. {
  823. }
  824. }
  825. // ---------------------------------------------------------------------------- lglw_timer_callback_set
  826. void lglw_timer_callback_set(lglw_t _lglw, lglw_timer_fxn_t _cbk) {
  827. LGLW(_lglw);
  828. if(NULL != lglw)
  829. {
  830. lglw->timer.cbk = _cbk;
  831. }
  832. }
  833. // ---------------------------------------------------------------------------- loc_enable_dropfiles
  834. static void loc_enable_dropfiles(lglw_int_t *lglw, lglw_bool_t _bEnable) {
  835. // (todo) implement me
  836. }
  837. // ---------------------------------------------------------------------------- lglw_dropfiles_callback_set
  838. void lglw_dropfiles_callback_set(lglw_t _lglw, lglw_dropfiles_fxn_t _cbk) {
  839. LGLW(_lglw);
  840. if(NULL != _lglw)
  841. {
  842. lglw->dropfiles.cbk = _cbk;
  843. loc_enable_dropfiles(lglw, (NULL != _cbk));
  844. }
  845. }
  846. // ---------------------------------------------------------------------------- loc_touchinput_update
  847. static void loc_touchinput_update(lglw_int_t *lglw) {
  848. // (todo) implement me
  849. }
  850. // ---------------------------------------------------------------------------- lglw_touchinput_set
  851. void lglw_touchinput_set(lglw_t _lglw, lglw_bool_t _bEnable) {
  852. LGLW(_lglw);
  853. if(NULL != _lglw)
  854. {
  855. lglw->mouse.touch.b_enable = _bEnable;
  856. lglw->mouse.touch.b_update_queued = LGLW_TRUE;
  857. }
  858. }
  859. // ---------------------------------------------------------------------------- lglw_touchinput_get
  860. lglw_bool_t lglw_touchinput_get(lglw_t _lglw) {
  861. lglw_bool_t r = LGLW_FALSE;
  862. LGLW(_lglw);
  863. if(NULL != _lglw)
  864. {
  865. r = lglw->mouse.touch.b_enable;
  866. }
  867. return r;
  868. }
  869. // ---------------------------------------------------------------------------- lglw_clipboard_text_set
  870. void lglw_clipboard_text_set(lglw_t _lglw, const uint32_t _numChars, const char *_text) {
  871. LGLW(_lglw);
  872. (void)_numChars;
  873. // (todo) implement me
  874. if(NULL != _text)
  875. {
  876. }
  877. }
  878. // ---------------------------------------------------------------------------- lglw_clipboard_text_get
  879. void lglw_clipboard_text_get(lglw_t _lglw, uint32_t _maxChars, uint32_t *_retNumChars, char *_retText) {
  880. LGLW(_lglw);
  881. if(NULL != _retNumChars)
  882. *_retNumChars = 0u;
  883. if(NULL != _retText)
  884. *_retText = 0;
  885. if(_maxChars > 0u)
  886. {
  887. if(NULL != _lglw)
  888. {
  889. // (todo) implement me
  890. }
  891. }
  892. }