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.

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