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.

1203 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. DefaultRootWindow(lglw->xdsp)/*parent. see Cameron's comment below.*/,
  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 1
  389. // (note) [cameronleger] In Ardour's code-base, the only time it looks for the _XEventProc is during a ReparentNotify event
  390. if (0 != _parentHWNDOrNull)
  391. {
  392. lglw_log("lglw:lglw_window_open: 7\n");
  393. XReparentWindow(lglw->xdsp, lglw->win.xwnd, lglw->parent_xwnd, 0, 0);
  394. }
  395. #endif
  396. lglw->win.b_owner = LGLW_TRUE;
  397. #else
  398. lglw->win.xwnd = (Window)_parentHWNDOrNull;
  399. lglw->win.b_owner = LGLW_FALSE;
  400. #endif
  401. // Setup the event proc now, on the parent window as well just for the debug host
  402. // It was simpler to do this than check in the debug host for the reparent event
  403. lglw_log("lglw:lglw_window_open: 8\n");
  404. loc_setEventProc(lglw->xdsp, lglw->win.xwnd);
  405. loc_setEventProc(lglw->xdsp, lglw->parent_xwnd);
  406. lglw_log("lglw:lglw_window_open: 9\n");
  407. if(lglw->win.b_owner)
  408. XMapRaised(lglw->xdsp, lglw->win.xwnd);
  409. #if 0
  410. XSelectInput(lglw->xdsp, lglw->win.xwnd,
  411. ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask | ButtonMotionMask | FocusChangeMask
  412. );
  413. XGrabKeyboard(lglw->xdsp, lglw->win.xwnd,
  414. False/*owner_events*/,
  415. GrabModeAsync/*pointer_mode*/,
  416. GrabModeAsync/*keyboard_mode*/,
  417. CurrentTime/*time*/
  418. );
  419. #endif
  420. XSync(lglw->xdsp, False);
  421. lglw->win.mapped = LGLW_TRUE;
  422. lglw_log("lglw:lglw_window_open: 10\n");
  423. lglw->win.size.x = _w;
  424. lglw->win.size.y = _h;
  425. lglw_log("lglw:lglw_window_open: 11\n");
  426. loc_enable_dropfiles(lglw, (NULL != lglw->dropfiles.cbk));
  427. lglw_log("lglw:lglw_window_open: EXIT\n");
  428. }
  429. return r;
  430. }
  431. // ---------------------------------------------------------------------------- lglw_window_resize
  432. lglw_bool_t lglw_window_resize (lglw_t _lglw, int32_t _w, int32_t _h) {
  433. lglw_bool_t r = LGLW_FALSE;
  434. LGLW(_lglw);
  435. if(NULL != lglw)
  436. {
  437. if(0 != lglw->win.xwnd)
  438. {
  439. lglw_log("lglw:lglw_window_resize: 1\n");
  440. r = LGLW_TRUE;
  441. lglw_log("lglw:lglw_window_resize: 2\n");
  442. XResizeWindow(lglw->xdsp, lglw->win.xwnd, _w, _h);
  443. XRaiseWindow(lglw->xdsp, lglw->win.xwnd);
  444. lglw_log("lglw:lglw_window_resize: 3\n");
  445. int deltaW = _w - lglw->win.size.x;
  446. int deltaH = _h - lglw->win.size.y;
  447. lglw_log("lglw:lglw_window_resize: 4\n");
  448. lglw->win.size.x = _w;
  449. lglw->win.size.y = _h;
  450. lglw_log("lglw:lglw_window_resize: 5\n");
  451. Window root, parent, *children = NULL;
  452. unsigned int num_children;
  453. lglw_log("lglw:lglw_window_resize: 6\n");
  454. if(!XQueryTree(lglw->xdsp, lglw->win.xwnd, &root, &parent, &children, &num_children))
  455. return r;
  456. lglw_log("lglw:lglw_window_resize: 7\n");
  457. if(children)
  458. XFree((char *)children);
  459. lglw_log("lglw:lglw_window_resize: 8\n");
  460. // Resize parent window (if any)
  461. if(0 != parent)
  462. {
  463. lglw_log("lglw:lglw_window_resize: 8.1\n");
  464. int x, y;
  465. unsigned int width, height;
  466. unsigned int border_width;
  467. unsigned int depth;
  468. lglw_log("lglw:lglw_window_resize: 8.2\n");
  469. if(!XGetGeometry(lglw->xdsp, lglw->win.xwnd, &root, &x, &y, &width, &height, &border_width, &depth))
  470. return r;
  471. lglw_log("lglw:lglw_window_resize: 8.3\n");
  472. XResizeWindow(lglw->xdsp, parent, width + deltaW, height + deltaH);
  473. }
  474. lglw_log("lglw:lglw_window_resize: EXIT\n");
  475. }
  476. }
  477. return r;
  478. }
  479. // ---------------------------------------------------------------------------- lglw_window_close
  480. void lglw_window_close (lglw_t _lglw) {
  481. LGLW(_lglw);
  482. if(NULL != lglw)
  483. {
  484. if(0 != lglw->win.xwnd)
  485. {
  486. lglw_log("lglw:lglw_window_close: 1\n");
  487. lglw_timer_stop(_lglw);
  488. lglw_log("lglw:lglw_window_close: 2\n");
  489. loc_key_unhook(lglw);
  490. lglw_log("lglw:lglw_window_close: 3\n");
  491. glXMakeCurrent(lglw->xdsp, None, NULL);
  492. lglw_log("lglw:lglw_window_close: 4\n");
  493. if(lglw->win.b_owner)
  494. {
  495. XDestroyWindow(lglw->xdsp, lglw->win.xwnd);
  496. lglw->win.b_owner = LGLW_FALSE;
  497. }
  498. XSync(lglw->xdsp, False);
  499. lglw->win.xwnd = 0;
  500. lglw->win.mapped = LGLW_FALSE;
  501. }
  502. }
  503. lglw_log("lglw:lglw_window_close: EXIT\n");
  504. }
  505. // ---------------------------------------------------------------------------- lglw_window_show
  506. void lglw_window_show(lglw_t _lglw) {
  507. LGLW(_lglw);
  508. if(NULL != lglw)
  509. {
  510. lglw_log("lglw:lglw_window_show: 1\n");
  511. XMapRaised(lglw->xdsp, lglw->win.xwnd);
  512. lglw->win.mapped = LGLW_TRUE;
  513. }
  514. lglw_log("lglw:lglw_window_show: EXIT\n");
  515. }
  516. // ---------------------------------------------------------------------------- lglw_window_hide
  517. void lglw_window_hide(lglw_t _lglw) {
  518. LGLW(_lglw);
  519. if(NULL != lglw)
  520. {
  521. lglw_log("lglw:lglw_window_hide: 1\n");
  522. XUnmapWindow(lglw->xdsp, lglw->win.xwnd);
  523. lglw->win.mapped = LGLW_FALSE;
  524. }
  525. lglw_log("lglw:lglw_window_hide: EXIT\n");
  526. }
  527. // ---------------------------------------------------------------------------- lglw_window_is_visible
  528. lglw_bool_t lglw_window_is_visible(lglw_t _lglw) {
  529. lglw_bool_t r = LGLW_FALSE;
  530. LGLW(_lglw);
  531. // lglw_log("lglw:lglw_window_is_visible: 1\n");
  532. if(NULL != lglw && 0 != lglw->win.xwnd)
  533. {
  534. // lglw_log("lglw:lglw_window_is_visible: 2\n");
  535. r = lglw->win.mapped;
  536. }
  537. // lglw_log("lglw:lglw_window_is_visible: EXIT\n");
  538. return r;
  539. }
  540. // ---------------------------------------------------------------------------- lglw_window_size_get
  541. void lglw_window_size_get(lglw_t _lglw, int32_t *_retX, int32_t *_retY) {
  542. LGLW(_lglw);
  543. lglw_log("lglw:lglw_window_size_get: 1\n");
  544. if(NULL != lglw)
  545. {
  546. if(0 != lglw->win.xwnd)
  547. {
  548. if(NULL != _retX)
  549. *_retX = lglw->win.size.x;
  550. if(NULL != _retY)
  551. *_retY = lglw->win.size.y;
  552. }
  553. }
  554. lglw_log("lglw:lglw_window_size_get: EXIT\n");
  555. }
  556. // ---------------------------------------------------------------------------- lglw_redraw
  557. void lglw_redraw(lglw_t _lglw) {
  558. LGLW(_lglw);
  559. // (todo) implement me
  560. if(NULL != lglw)
  561. {
  562. if(0 != lglw->win.xwnd)
  563. {
  564. // TODO Event Loop
  565. lglw_log("lglw:lglw_redraw: 1\n");
  566. XClearArea(lglw->xdsp, lglw->win.xwnd, 0, 0, 1, 1, True); // clear tiny area for exposing
  567. XFlush(lglw->xdsp);
  568. }
  569. }
  570. }
  571. // ---------------------------------------------------------------------------- lglw_redraw_callback_set
  572. void lglw_redraw_callback_set(lglw_t _lglw, lglw_redraw_fxn_t _cbk) {
  573. LGLW(_lglw);
  574. if(NULL != lglw)
  575. {
  576. lglw->redraw.cbk = _cbk;
  577. }
  578. }
  579. // ---------------------------------------------------------------------------- lglw_glcontext_push
  580. void lglw_glcontext_push(lglw_t _lglw) {
  581. LGLW(_lglw);
  582. if(NULL != lglw)
  583. {
  584. lglw->prev.drw = glXGetCurrentDrawable();
  585. lglw->prev.ctx = glXGetCurrentContext();
  586. // lglw_log("lglw:lglw_glcontext_push: win.xwnd=%p hidden.xwnd=%p ctx=%p\n",
  587. // lglw->win.xwnd, lglw->hidden.xwnd, lglw->ctx);
  588. if(!glXMakeCurrent(lglw->xdsp, (0 == lglw->win.xwnd) ? lglw->hidden.xwnd : lglw->win.xwnd, lglw->ctx))
  589. {
  590. 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());
  591. }
  592. }
  593. }
  594. // ---------------------------------------------------------------------------- lglw_glcontext_pop
  595. void lglw_glcontext_pop(lglw_t _lglw) {
  596. LGLW(_lglw);
  597. if(NULL != lglw)
  598. {
  599. // lglw_log("lglw:lglw_glcontext_pop: prev.drw=%p prev.ctx=%p\n",
  600. // lglw->prev.drw, lglw->prev.ctx);
  601. if(!glXMakeCurrent(lglw->xdsp, lglw->prev.drw, lglw->prev.ctx))
  602. {
  603. lglw_log("[---] lglw_glcontext_pop: glXMakeCurrent() failed. prev.drw=%p ctx=%p glGetError()=%d\n", lglw->prev.drw, lglw->prev.ctx, glGetError());
  604. }
  605. }
  606. }
  607. // ---------------------------------------------------------------------------- lglw_swap_buffers
  608. void lglw_swap_buffers(lglw_t _lglw) {
  609. LGLW(_lglw);
  610. if(NULL != lglw)
  611. {
  612. if(0 != lglw->win.xwnd)
  613. {
  614. // lglw_log("lglw:lglw_swap_buffers: 1\n");
  615. glXSwapBuffers(lglw->xdsp, lglw->win.xwnd);
  616. }
  617. }
  618. }
  619. // ---------------------------------------------------------------------------- lglw_swap_interval_set
  620. typedef void (APIENTRY *PFNWGLEXTSWAPINTERVALPROC) (int);
  621. void lglw_swap_interval_set(lglw_t _lglw, int32_t _ival) {
  622. LGLW(_lglw);
  623. if(NULL != lglw)
  624. {
  625. lglw_log("lglw:lglw_swap_interval_set: 1\n");
  626. PFNWGLEXTSWAPINTERVALPROC glXSwapIntervalEXT;
  627. glXSwapIntervalEXT = (PFNWGLEXTSWAPINTERVALPROC) glXGetProcAddress("glXSwapIntervalEXT");
  628. if(NULL != glXSwapIntervalEXT)
  629. {
  630. lglw_log("lglw:lglw_swap_interval_set: 2\n");
  631. glXSwapIntervalEXT(_ival);
  632. lglw->win.swap_interval = _ival;
  633. }
  634. }
  635. }
  636. // ---------------------------------------------------------------------------- lglw_swap_interval_get
  637. int32_t lglw_swap_interval_get(lglw_t _lglw) {
  638. LGLW(_lglw);
  639. int32_t r = 0;
  640. if(NULL != lglw)
  641. {
  642. r = lglw->win.swap_interval;
  643. }
  644. return r;
  645. }
  646. // ---------------------------------------------------------------------------- loc_key_hook
  647. static void loc_key_hook(lglw_int_t *lglw) {
  648. loc_key_unhook(lglw);
  649. // (todo) implement me
  650. khook_lglw = lglw;
  651. }
  652. // ---------------------------------------------------------------------------- loc_key_unhook
  653. static void loc_key_unhook(lglw_int_t *lglw) {
  654. // (todo) implement me
  655. if(khook_lglw == lglw)
  656. khook_lglw = NULL;
  657. }
  658. // ---------------------------------------------------------------------------- loc_handle_mouseleave
  659. static void loc_handle_mouseleave(lglw_int_t *lglw) {
  660. loc_key_unhook(lglw);
  661. lglw->focus.state &= ~LGLW_FOCUS_MOUSE;
  662. if(NULL != lglw->focus.cbk)
  663. {
  664. lglw->focus.cbk(lglw, lglw->focus.state, LGLW_FOCUS_MOUSE);
  665. }
  666. lglw_log("xxx lglw:loc_handle_mouseleave: LEAVE\n");
  667. }
  668. // ---------------------------------------------------------------------------- loc_handle_mouseenter
  669. static void loc_handle_mouseenter(lglw_int_t *lglw) {
  670. loc_key_hook(lglw);
  671. lglw->focus.state |= LGLW_FOCUS_MOUSE;
  672. if(NULL != lglw->focus.cbk)
  673. {
  674. lglw->focus.cbk(lglw, lglw->focus.state, LGLW_FOCUS_MOUSE);
  675. }
  676. lglw_log("xxx lglw:loc_handle_mouseenter: LEAVE\n");
  677. }
  678. // ---------------------------------------------------------------------------- loc_handle_mousebutton
  679. static void loc_handle_mousebutton(lglw_int_t *lglw, lglw_bool_t _bPressed, uint32_t _button) {
  680. if(_bPressed)
  681. lglw->mouse.button_state |= _button;
  682. else
  683. lglw->mouse.button_state &= ~_button;
  684. if(NULL != lglw->mouse.cbk)
  685. {
  686. lglw->mouse.cbk(lglw, lglw->mouse.p.x, lglw->mouse.p.y, lglw->mouse.button_state, _button);
  687. }
  688. }
  689. // ---------------------------------------------------------------------------- loc_handle_mousemotion
  690. static void loc_handle_mousemotion(lglw_int_t *lglw) {
  691. if(NULL != lglw->mouse.cbk)
  692. {
  693. lglw->mouse.cbk(lglw, lglw->mouse.p.x, lglw->mouse.p.y, lglw->mouse.button_state, 0u/*changedbuttonstate*/);
  694. }
  695. }
  696. // ---------------------------------------------------------------------------- lglw_mouse_callback_set
  697. void lglw_mouse_callback_set(lglw_t _lglw, lglw_mouse_fxn_t _cbk) {
  698. LGLW(_lglw);
  699. if(NULL != lglw)
  700. {
  701. lglw->mouse.cbk = _cbk;
  702. }
  703. }
  704. // ---------------------------------------------------------------------------- lglw_mouse_callback_set
  705. void lglw_focus_callback_set(lglw_t _lglw, lglw_focus_fxn_t _cbk) {
  706. LGLW(_lglw);
  707. if(NULL != lglw)
  708. {
  709. lglw->focus.cbk = _cbk;
  710. }
  711. }
  712. // ---------------------------------------------------------------------------- loc_handle_key
  713. static lglw_bool_t loc_handle_key(lglw_int_t *lglw, lglw_bool_t _bPressed, uint32_t _vkey) {
  714. lglw_bool_t r = LGLW_FALSE;
  715. if(NULL != lglw->keyboard.cbk)
  716. {
  717. r = lglw->keyboard.cbk(lglw, _vkey, lglw->keyboard.kmod_state, _bPressed);
  718. }
  719. return r;
  720. }
  721. // ---------------------------------------------------------------------------- lglw_keyboard_callback_set
  722. void lglw_keyboard_callback_set(lglw_t _lglw, lglw_keyboard_fxn_t _cbk) {
  723. LGLW(_lglw);
  724. if(NULL != lglw)
  725. {
  726. lglw->keyboard.cbk = _cbk;
  727. }
  728. }
  729. // ---------------------------------------------------------------------------- lglw_keyboard_get_modifiers
  730. uint32_t lglw_keyboard_get_modifiers(lglw_t _lglw) {
  731. uint32_t r = 0u;
  732. LGLW(_lglw);
  733. if(NULL != lglw)
  734. {
  735. r = lglw->keyboard.kmod_state;
  736. }
  737. return r;
  738. }
  739. // ---------------------------------------------------------------------------- lglw_touchkeyboard_show
  740. void lglw_touchkeyboard_show(lglw_t _lglw, lglw_bool_t _bEnable) {
  741. LGLW(_lglw);
  742. // (todo) implement me
  743. if(NULL != lglw)
  744. {
  745. }
  746. }
  747. // ---------------------------------------------------------------------------- lglw_mouse_get_buttons
  748. uint32_t lglw_mouse_get_buttons(lglw_t _lglw) {
  749. uint32_t r = 0u;
  750. LGLW(_lglw);
  751. if(NULL != lglw)
  752. {
  753. r = lglw->mouse.button_state;
  754. }
  755. return r;
  756. }
  757. // ---------------------------------------------------------------------------- lglw_mouse_grab
  758. void lglw_mouse_grab(lglw_t _lglw, uint32_t _grabMode) {
  759. LGLW(_lglw);
  760. if(NULL != lglw)
  761. {
  762. // (todo) implement me
  763. // if(NULL != lglw->win.hwnd)
  764. {
  765. if(!lglw->mouse.touch.b_enable)
  766. {
  767. if(lglw->mouse.grab.mode != _grabMode)
  768. {
  769. lglw_mouse_ungrab(_lglw);
  770. }
  771. switch(_grabMode)
  772. {
  773. default:
  774. case LGLW_MOUSE_GRAB_NONE:
  775. break;
  776. case LGLW_MOUSE_GRAB_CAPTURE:
  777. // (todo) implement me
  778. // (void)SetCapture(lglw->win.hwnd);
  779. lglw->mouse.grab.mode = _grabMode;
  780. break;
  781. case LGLW_MOUSE_GRAB_WARP:
  782. // (todo) implement me
  783. // (void)SetCapture(lglw->win.hwnd);
  784. lglw_mouse_cursor_show(_lglw, LGLW_FALSE);
  785. lglw->mouse.grab.p = lglw->mouse.p;
  786. lglw->mouse.grab.last_p = lglw->mouse.p;
  787. lglw->mouse.grab.mode = _grabMode;
  788. break;
  789. }
  790. }
  791. }
  792. }
  793. }
  794. // ---------------------------------------------------------------------------- lglw_mouse_ungrab
  795. void lglw_mouse_ungrab(lglw_t _lglw) {
  796. LGLW(_lglw);
  797. if(NULL != lglw)
  798. {
  799. // (todo) implement me
  800. // if(NULL != lglw->win.hwnd)
  801. {
  802. if(!lglw->mouse.touch.b_enable)
  803. {
  804. switch(lglw->mouse.grab.mode)
  805. {
  806. default:
  807. case LGLW_MOUSE_GRAB_NONE:
  808. break;
  809. case LGLW_MOUSE_GRAB_CAPTURE:
  810. // (todo) implement me
  811. // (void)ReleaseCapture();
  812. lglw->mouse.grab.mode = LGLW_MOUSE_GRAB_NONE;
  813. break;
  814. case LGLW_MOUSE_GRAB_WARP:
  815. // (todo) implement me
  816. // (void)ReleaseCapture();
  817. lglw->mouse.grab.mode = LGLW_MOUSE_GRAB_NONE;
  818. lglw->mouse.grab.b_queue_warp = LGLW_TRUE;
  819. lglw_mouse_cursor_show(_lglw, LGLW_TRUE);
  820. break;
  821. }
  822. }
  823. }
  824. }
  825. }
  826. // ---------------------------------------------------------------------------- lglw_mouse_warp
  827. void lglw_mouse_warp(lglw_t _lglw, int32_t _x, int32_t _y) {
  828. LGLW(_lglw);
  829. // (todo) implement me
  830. if(NULL != lglw)
  831. {
  832. }
  833. }
  834. // ---------------------------------------------------------------------------- loc_handle_queued_mouse_warp
  835. static void loc_handle_queued_mouse_warp(lglw_int_t *lglw) {
  836. if(lglw->mouse.grab.b_queue_warp)
  837. {
  838. lglw->mouse.grab.b_queue_warp = LGLW_FALSE;
  839. lglw_mouse_warp(lglw, lglw->mouse.grab.p.x, lglw->mouse.grab.p.y);
  840. lglw->mouse.grab.last_p = lglw->mouse.grab.p;
  841. }
  842. }
  843. // ---------------------------------------------------------------------------- lglw_mouse_cursor_show
  844. void lglw_mouse_cursor_show (lglw_t _lglw, lglw_bool_t _bShow) {
  845. LGLW(_lglw);
  846. // (todo) implement me
  847. if(NULL != lglw)
  848. {
  849. }
  850. }
  851. // ---------------------------------------------------------------------------- lglw_timer_start
  852. void lglw_timer_start(lglw_t _lglw, uint32_t _millisec) {
  853. LGLW(_lglw);
  854. // (todo) implement me
  855. if(NULL != lglw)
  856. {
  857. }
  858. }
  859. // ---------------------------------------------------------------------------- lglw_timer_stop
  860. void lglw_timer_stop(lglw_t _lglw) {
  861. LGLW(_lglw);
  862. // (todo) implement me
  863. if(NULL != lglw)
  864. {
  865. }
  866. }
  867. // ---------------------------------------------------------------------------- lglw_timer_callback_set
  868. void lglw_timer_callback_set(lglw_t _lglw, lglw_timer_fxn_t _cbk) {
  869. LGLW(_lglw);
  870. if(NULL != lglw)
  871. {
  872. lglw->timer.cbk = _cbk;
  873. }
  874. }
  875. // ---------------------------------------------------------------------------- loc_enable_dropfiles
  876. static void loc_enable_dropfiles(lglw_int_t *lglw, lglw_bool_t _bEnable) {
  877. // (todo) implement me
  878. }
  879. // ---------------------------------------------------------------------------- lglw_dropfiles_callback_set
  880. void lglw_dropfiles_callback_set(lglw_t _lglw, lglw_dropfiles_fxn_t _cbk) {
  881. LGLW(_lglw);
  882. if(NULL != _lglw)
  883. {
  884. lglw->dropfiles.cbk = _cbk;
  885. loc_enable_dropfiles(lglw, (NULL != _cbk));
  886. }
  887. }
  888. // ---------------------------------------------------------------------------- loc_touchinput_update
  889. static void loc_touchinput_update(lglw_int_t *lglw) {
  890. // (todo) implement me
  891. }
  892. // ---------------------------------------------------------------------------- lglw_touchinput_set
  893. void lglw_touchinput_set(lglw_t _lglw, lglw_bool_t _bEnable) {
  894. LGLW(_lglw);
  895. if(NULL != _lglw)
  896. {
  897. lglw->mouse.touch.b_enable = _bEnable;
  898. lglw->mouse.touch.b_update_queued = LGLW_TRUE;
  899. }
  900. }
  901. // ---------------------------------------------------------------------------- lglw_touchinput_get
  902. lglw_bool_t lglw_touchinput_get(lglw_t _lglw) {
  903. lglw_bool_t r = LGLW_FALSE;
  904. LGLW(_lglw);
  905. if(NULL != _lglw)
  906. {
  907. r = lglw->mouse.touch.b_enable;
  908. }
  909. return r;
  910. }
  911. // ---------------------------------------------------------------------------- lglw_clipboard_text_set
  912. void lglw_clipboard_text_set(lglw_t _lglw, const uint32_t _numChars, const char *_text) {
  913. LGLW(_lglw);
  914. (void)_numChars;
  915. // (todo) implement me
  916. if(NULL != _text)
  917. {
  918. }
  919. }
  920. // ---------------------------------------------------------------------------- lglw_clipboard_text_get
  921. void lglw_clipboard_text_get(lglw_t _lglw, uint32_t _maxChars, uint32_t *_retNumChars, char *_retText) {
  922. LGLW(_lglw);
  923. if(NULL != _retNumChars)
  924. *_retNumChars = 0u;
  925. if(NULL != _retText)
  926. *_retText = 0;
  927. if(_maxChars > 0u)
  928. {
  929. if(NULL != _lglw)
  930. {
  931. // (todo) implement me
  932. }
  933. }
  934. }