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.

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