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.

1189 lines
35KB

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