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.

1977 lines
65KB

  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. uint32_t numChars;
  114. char *data;
  115. } clipboard;
  116. struct {
  117. lglw_dropfiles_fxn_t cbk;
  118. } dropfiles;
  119. struct {
  120. lglw_redraw_fxn_t cbk;
  121. } redraw;
  122. } lglw_int_t;
  123. // ---------------------------------------------------------------------------- module fxn fwd decls
  124. static lglw_bool_t loc_create_hidden_window (lglw_int_t *lglw, int32_t _w, int32_t _h);
  125. static void loc_destroy_hidden_window(lglw_int_t *lglw);
  126. static lglw_bool_t loc_handle_key (lglw_int_t *lglw, lglw_bool_t _bPressed, uint32_t _vkey);
  127. // static lglw_bool_t loc_touchkeyboard_get_rect (RECT *rect);
  128. // static lglw_bool_t loc_touchkeyboard_is_visible (void);
  129. extern lglw_bool_t lglw_int_touchkeyboard_toggle (void);
  130. static void loc_handle_mouseleave (lglw_int_t *lglw);
  131. static void loc_handle_mouseenter (lglw_int_t *lglw);
  132. static void loc_handle_mousebutton (lglw_int_t *lglw, lglw_bool_t _bPressed, uint32_t _button);
  133. static void loc_handle_mousemotion (lglw_int_t *lglw);
  134. static void loc_handle_queued_mouse_warp (lglw_int_t *lglw);
  135. static void loc_touchinput_update (lglw_int_t *lglw);
  136. static void loc_enable_dropfiles (lglw_int_t *lglw, lglw_bool_t _bEnable);
  137. static void loc_eventProc (void *_xevent);
  138. static void loc_setProperty (Display *_display, Window _window, const char *_name, void *_value);
  139. static void *loc_getProperty (Display *_display, Window _window, const char *_name);
  140. static void loc_setEventProc (Display *display, Window window);
  141. // TODO: remove and/or improve debug logging for a debug build
  142. // ---------------------------------------------------------------------------- lglw_log
  143. static FILE *logfile;
  144. void lglw_log(const char *logData, ...) {
  145. fprintf(logfile, logData);
  146. fflush(logfile);
  147. printf(logData);
  148. }
  149. // TODO: remove, or maybe not in some specific use cases
  150. // ---------------------------------------------------------------------------- xerror_log
  151. static int xerror_handler(Display *display, XErrorEvent *error) {
  152. char error_text[1024];
  153. XGetErrorText(display, error->error_code, error_text, 1024);
  154. lglw_log("XERROR (%d): %s, %d, %d\n", error->error_code, error_text, error->request_code, error->minor_code);
  155. return 0;
  156. }
  157. // ---------------------------------------------------------------------------- lglw_init
  158. lglw_t lglw_init(int32_t _w, int32_t _h) {
  159. lglw_int_t *lglw = malloc(sizeof(lglw_int_t));
  160. printf("xxx lglw_init: sizeof(uint32_t)=%u sizeof(long)=%u sizeof(void*)=%u\n", sizeof(uint32_t), sizeof(long), sizeof(void*));
  161. // TODO: remove/improve
  162. logfile = fopen("/tmp/lglw_log.txt", "w");
  163. XSetErrorHandler(xerror_handler);
  164. XInitThreads(); // fix GL crash, see <https://forum.juce.com/t/linux-vst-opengl-crash-because-xinitthreads-not-called/22821>
  165. if(NULL != lglw)
  166. {
  167. memset(lglw, 0, sizeof(lglw_int_t));
  168. lglw_log("lglw:lglw_init: 1\n");
  169. if(_w <= 16)
  170. _w = LGLW_DEFAULT_HIDDEN_W;
  171. if(_h <= 16)
  172. _h = LGLW_DEFAULT_HIDDEN_H;
  173. lglw_log("lglw:lglw_init: 2\n");
  174. if(!loc_create_hidden_window(lglw, _w, _h))
  175. {
  176. free(lglw);
  177. lglw = NULL;
  178. }
  179. lglw_log("lglw:lglw_init: 3\n");
  180. }
  181. lglw_log("lglw:lglw_init: EXIT\n");
  182. return lglw;
  183. }
  184. // ---------------------------------------------------------------------------- lglw_exit
  185. void lglw_exit(lglw_t _lglw) {
  186. LGLW(_lglw);
  187. if(NULL != lglw)
  188. {
  189. lglw_log("lglw:lglw_exit: 1\n");
  190. loc_destroy_hidden_window(lglw);
  191. lglw_log("lglw:lglw_exit: 2\n");
  192. fclose(logfile);
  193. free(lglw);
  194. }
  195. }
  196. // ---------------------------------------------------------------------------- lglw_userdata_set
  197. void lglw_userdata_set(lglw_t _lglw, void *_userData) {
  198. LGLW(_lglw);
  199. if(NULL != lglw)
  200. {
  201. lglw_log("lglw:lglw_userdata_set: 1\n");
  202. lglw->user_data = _userData;
  203. }
  204. }
  205. // ---------------------------------------------------------------------------- lglw_userdata_get
  206. void *lglw_userdata_get(lglw_t _lglw) {
  207. LGLW(_lglw);
  208. if(NULL != lglw)
  209. {
  210. lglw_log("lglw:lglw_userdata_get: 1\n");
  211. return lglw->user_data;
  212. }
  213. return NULL;
  214. }
  215. // ---------------------------------------------------------------------------- loc_create_hidden_window
  216. static lglw_bool_t loc_create_hidden_window(lglw_int_t *lglw, int32_t _w, int32_t _h) {
  217. // TODO: compare to 'WindowClass' from Windows implementation
  218. lglw_log("lglw:loc_create_hidden_window: 1\n");
  219. XSetWindowAttributes swa;
  220. int attrib[] = { GLX_RGBA, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE, 24, None };
  221. int screen;
  222. lglw_log("lglw:loc_create_hidden_window: 2\n");
  223. lglw->xdsp = XOpenDisplay(NULL);
  224. screen = DefaultScreen(lglw->xdsp);
  225. lglw_log("lglw:loc_create_hidden_window: 3\n");
  226. lglw->vi = glXChooseVisual(lglw->xdsp, screen, attrib);
  227. lglw_log("lglw:loc_create_hidden_window: 4\n");
  228. if(NULL == lglw->vi)
  229. {
  230. lglw_log("[---] lglw: failed to find GLX Visual for hidden window\n");
  231. return LGLW_FALSE;
  232. }
  233. lglw_log("lglw:loc_create_hidden_window: 5\n");
  234. lglw->ctx = glXCreateContext(lglw->xdsp, lglw->vi, None, True);
  235. lglw_log("lglw:loc_create_hidden_window: 6\n");
  236. if(NULL == lglw->ctx)
  237. {
  238. lglw_log("[---] lglw: failed to create GLX Context for hidden window\n");
  239. return LGLW_FALSE;
  240. }
  241. lglw_log("lglw:loc_create_hidden_window: 7\n");
  242. lglw->cmap = XCreateColormap(lglw->xdsp, RootWindow(lglw->xdsp, lglw->vi->screen),
  243. lglw->vi->visual, AllocNone);
  244. lglw_log("lglw:loc_create_hidden_window: 8\n");
  245. swa.border_pixel = 0;
  246. swa.colormap = lglw->cmap;
  247. lglw->hidden.xwnd = XCreateWindow(lglw->xdsp, DefaultRootWindow(lglw->xdsp),
  248. 0, 0, LGLW_DEFAULT_HIDDEN_W, LGLW_DEFAULT_HIDDEN_H, 0, CopyFromParent, InputOutput,
  249. lglw->vi->visual, CWBorderPixel | CWColormap, &swa);
  250. lglw_log("lglw:loc_create_hidden_window: 9\n");
  251. XSetStandardProperties(lglw->xdsp, lglw->hidden.xwnd, "LGLW_hidden", "LGLW_hidden", None, NULL, 0, NULL);
  252. XSync(lglw->xdsp, False);
  253. lglw_log("lglw:loc_create_hidden_window: EXIT\n");
  254. lglw->hidden.size.x = _w;
  255. lglw->hidden.size.y = _h;
  256. return LGLW_TRUE;
  257. }
  258. // ---------------------------------------------------------------------------- loc_destroy_hidden_window
  259. static void loc_destroy_hidden_window(lglw_int_t *lglw) {
  260. lglw_log("lglw:loc_destroy_hidden_window: 1\n");
  261. if(NULL != lglw->xdsp && NULL != lglw->ctx)
  262. {
  263. glXMakeCurrent(lglw->xdsp, None, NULL);
  264. glXDestroyContext(lglw->xdsp, lglw->ctx);
  265. }
  266. lglw_log("lglw:loc_destroy_hidden_window: 2\n");
  267. if(NULL != lglw->xdsp && 0 != lglw->hidden.xwnd) XDestroyWindow(lglw->xdsp, lglw->hidden.xwnd);
  268. lglw_log("lglw:loc_destroy_hidden_window: 3\n");
  269. if(NULL != lglw->xdsp && 0 != lglw->cmap) XFreeColormap(lglw->xdsp, lglw->cmap);
  270. lglw_log("lglw:loc_destroy_hidden_window: 4\n");
  271. if(NULL != lglw->vi) XFree(lglw->vi);
  272. lglw_log("lglw:loc_destroy_hidden_window: 5\n");
  273. XSync(lglw->xdsp, False);
  274. if(NULL != lglw->xdsp) XCloseDisplay(lglw->xdsp);
  275. }
  276. // ---------------------------------------------------------------------------- loc_setEventProc
  277. // https://www.kvraudio.com/forum/viewtopic.php?t=387924
  278. // https://github.com/Ardour/ardour/blob/master/gtk2_ardour/linux_vst_gui_support.cc
  279. // https://discourse.ardour.org/t/overtonedsp-plugins/90115/22
  280. // https://github.com/amsynth/amsynth/blob/4a87798e650c6d71d70274a961c9b8d98fc6da7e/src/amsynth_vst.cpp
  281. // https://github.com/rsenn/eXT2/blob/7f00a09561ded8175ffed2f4912dad74e466a1c7/vstplugins/vstgui/vstgui.cpp
  282. // https://github.com/COx2/DistortionFilter/blob/c6a34fb56b503a6e95bf0975e00f438bbf4ff52a/juce/modules/juce_audio_processors/format_types/juce_VSTPluginFormat.cpp
  283. // Very simple function to test _XEventProc is properly called
  284. static void loc_eventProc(void *_xevent) {
  285. XEvent *xev = (XEvent*)_xevent;
  286. lglw_log("XEventProc\n");
  287. printf("vstgltest<lglw_linux>: XEventProc, xev=%p\n", xev);
  288. if(NULL != xev)
  289. {
  290. LGLW(loc_getProperty(xev->xany.display, xev->xany.window, "_lglw")); // get instance pointer
  291. printf("vstgltest<lglw_linux>: XEventProc, type=%d serial=%lu send_event=%d lglw=%p\n", xev->xany.type, xev->xany.serial, xev->xany.send_event, lglw);
  292. if(NULL != lglw)
  293. {
  294. lglw_bool_t bHandled = LGLW_FALSE;
  295. switch(xev->type)
  296. {
  297. default:
  298. printf("vstgltest<lglw_linux>: unhandled X11 event type=%d\n", xev->type);
  299. break;
  300. case Expose:
  301. printf("vstgltest<lglw_linux>: xev Expose\n");
  302. loc_handle_queued_mouse_warp(lglw);
  303. if(NULL != lglw->redraw.cbk)
  304. {
  305. lglw->redraw.cbk(lglw);
  306. }
  307. break;
  308. // TODO: Should FocusIn/Out be treated like WM_CAPTURECHANGED and reset the grab state?
  309. case FocusIn:
  310. printf("vstgltest<lglw_linux>: xev FocusIn\n");
  311. break;
  312. case FocusOut:
  313. printf("vstgltest<lglw_linux>: xev FocusOut\n");
  314. break;
  315. case EnterNotify:
  316. // printf("vstgltest<lglw_linux>: xev XEnterWindowEvent\n");
  317. ; // empty statement
  318. XEnterWindowEvent *wenter = (XEnterWindowEvent*)xev;
  319. printf("vstgltest<lglw_linux>: xev EnterNotify: mode:%i, detail:%i, state:%d\n", wenter->mode, wenter->detail, wenter->state);
  320. lglw->mouse.p.x = wenter->x;
  321. lglw->mouse.p.y = wenter->y;
  322. loc_handle_mousemotion(lglw);
  323. // EnterNotify messages can be pseudo-motion events (NotifyGrab, NotifyUngrab)
  324. // when buttons are pressed, which would trigger false focus changes
  325. // so, the callback is only sent when a normal entry happens
  326. if (wenter->mode == NotifyNormal)
  327. {
  328. loc_handle_mouseenter(lglw);
  329. }
  330. break;
  331. case LeaveNotify:
  332. // printf("vstgltest<lglw_linux>: xev XLeaveWindowEvent\n");
  333. ; // empty statement
  334. XLeaveWindowEvent *wexit = (XLeaveWindowEvent*)xev;
  335. printf("vstgltest<lglw_linux>: xev LeaveNotify: mode:%i, detail:%i, state:%d\n", wexit->mode, wexit->detail, wexit->state);
  336. // LeaveNotify messages can be pseudo-motion events (NotifyGrab, NotifyUngrab)
  337. // when buttons are pressed, which would trigger false focus changes
  338. // so, the callback is only sent when a normal entry happens
  339. if (wexit->mode == NotifyNormal)
  340. {
  341. loc_handle_mouseleave(lglw);
  342. }
  343. break;
  344. case MotionNotify:
  345. // printf("vstgltest<lglw_linux>: xev MotionNotify\n");
  346. ; // empty statement
  347. XMotionEvent *motion = (XMotionEvent*)xev;
  348. if(LGLW_MOUSE_GRAB_WARP == lglw->mouse.grab.mode)
  349. {
  350. lglw->mouse.grab.b_queue_warp = LGLW_TRUE;
  351. lglw->mouse.p.x += (motion->x - lglw->mouse.grab.last_p.x);
  352. lglw->mouse.p.y += (motion->y - lglw->mouse.grab.last_p.y);
  353. lglw->mouse.grab.last_p.x = motion->x;
  354. lglw->mouse.grab.last_p.y = motion->y;
  355. }
  356. else
  357. {
  358. lglw->mouse.p.x = motion->x;
  359. lglw->mouse.p.y = motion->y;
  360. }
  361. loc_handle_mousemotion(lglw);
  362. break;
  363. case KeyPress:
  364. printf("vstgltest<lglw_linux>: xev KeyPress\n");
  365. XKeyPressedEvent *keyPress = (XKeyPressedEvent*)xev;
  366. KeySym xkp = XLookupKeysym(keyPress, 0);
  367. switch(xkp)
  368. {
  369. default:
  370. printf("vstgltest<lglw_linux>: xev KeyPress: %x or %lu\n", keyPress->keycode, xkp);
  371. if(0u != (lglw->keyboard.kmod_state & LGLW_KMOD_SHIFT))
  372. {
  373. KeySym xkpl;
  374. KeySym xkpu;
  375. XConvertCase(xkp, &xkpl, &xkpu);
  376. bHandled = loc_handle_key(lglw, LGLW_TRUE/*bPressed*/, xkpu);
  377. }
  378. else
  379. {
  380. bHandled = loc_handle_key(lglw, LGLW_TRUE/*bPressed*/, xkp);
  381. }
  382. break;
  383. case NoSymbol:
  384. printf("vstgltest<lglw_linux>: xev UNKNOWN KeyPress: %x\n", keyPress->keycode);
  385. break;
  386. case XK_F1:
  387. bHandled = loc_handle_key(lglw, LGLW_TRUE/*bPressed*/, LGLW_VKEY_F1);
  388. break;
  389. case XK_F2:
  390. bHandled = loc_handle_key(lglw, LGLW_TRUE/*bPressed*/, LGLW_VKEY_F2);
  391. break;
  392. case XK_F3:
  393. bHandled = loc_handle_key(lglw, LGLW_TRUE/*bPressed*/, LGLW_VKEY_F3);
  394. break;
  395. case XK_F4:
  396. bHandled = loc_handle_key(lglw, LGLW_TRUE/*bPressed*/, LGLW_VKEY_F4);
  397. break;
  398. case XK_F5:
  399. bHandled = loc_handle_key(lglw, LGLW_TRUE/*bPressed*/, LGLW_VKEY_F5);
  400. break;
  401. case XK_F6:
  402. bHandled = loc_handle_key(lglw, LGLW_TRUE/*bPressed*/, LGLW_VKEY_F6);
  403. break;
  404. case XK_F7:
  405. bHandled = loc_handle_key(lglw, LGLW_TRUE/*bPressed*/, LGLW_VKEY_F7);
  406. break;
  407. case XK_F8:
  408. bHandled = loc_handle_key(lglw, LGLW_TRUE/*bPressed*/, LGLW_VKEY_F8);
  409. break;
  410. case XK_F9:
  411. bHandled = loc_handle_key(lglw, LGLW_TRUE/*bPressed*/, LGLW_VKEY_F9);
  412. break;
  413. case XK_F10:
  414. bHandled = loc_handle_key(lglw, LGLW_TRUE/*bPressed*/, LGLW_VKEY_F10);
  415. break;
  416. case XK_F11:
  417. bHandled = loc_handle_key(lglw, LGLW_TRUE/*bPressed*/, LGLW_VKEY_F11);
  418. break;
  419. case XK_F12:
  420. bHandled = loc_handle_key(lglw, LGLW_TRUE/*bPressed*/, LGLW_VKEY_F12);
  421. break;
  422. case XK_BackSpace:
  423. bHandled = loc_handle_key(lglw, LGLW_TRUE/*bPressed*/, LGLW_VKEY_BACKSPACE);
  424. break;
  425. case XK_Tab:
  426. bHandled = loc_handle_key(lglw, LGLW_TRUE/*bPressed*/, LGLW_VKEY_TAB);
  427. break;
  428. case XK_Return:
  429. bHandled = loc_handle_key(lglw, LGLW_TRUE/*bPressed*/, LGLW_VKEY_RETURN);
  430. break;
  431. case XK_Escape:
  432. bHandled = loc_handle_key(lglw, LGLW_TRUE/*bPressed*/, LGLW_VKEY_ESCAPE);
  433. break;
  434. case XK_Shift_L:
  435. lglw->keyboard.kmod_state |= LGLW_KMOD_LSHIFT;
  436. bHandled = loc_handle_key(lglw, LGLW_TRUE/*bPressed*/, LGLW_VKEY_LSHIFT);
  437. bHandled = LGLW_FALSE;
  438. break;
  439. case XK_Shift_R:
  440. lglw->keyboard.kmod_state |= LGLW_KMOD_RSHIFT;
  441. bHandled = loc_handle_key(lglw, LGLW_TRUE/*bPressed*/, LGLW_VKEY_RSHIFT);
  442. bHandled = LGLW_FALSE;
  443. break;
  444. case XK_Control_L:
  445. lglw->keyboard.kmod_state |= LGLW_KMOD_LCTRL;
  446. bHandled = loc_handle_key(lglw, LGLW_TRUE/*bPressed*/, LGLW_KMOD_LCTRL);
  447. bHandled = LGLW_FALSE;
  448. break;
  449. case XK_Control_R:
  450. lglw->keyboard.kmod_state |= LGLW_KMOD_RCTRL;
  451. bHandled = loc_handle_key(lglw, LGLW_TRUE/*bPressed*/, LGLW_KMOD_RCTRL);
  452. bHandled = LGLW_FALSE;
  453. break;
  454. }
  455. break;
  456. case KeyRelease:
  457. printf("vstgltest<lglw_linux>: xev KeyRelease\n");
  458. XKeyReleasedEvent *keyRelease = (XKeyReleasedEvent*)xev;
  459. KeySym xkr = XLookupKeysym(keyRelease, 0);
  460. switch(xkr)
  461. {
  462. default:
  463. printf("vstgltest<lglw_linux>: xev KeyRelease: %x or %lu\n", keyRelease->keycode, xkr);
  464. if(0u != (lglw->keyboard.kmod_state & LGLW_KMOD_SHIFT))
  465. {
  466. KeySym xkrl;
  467. KeySym xkru;
  468. XConvertCase(xkr, &xkrl, &xkru);
  469. bHandled = loc_handle_key(lglw, LGLW_FALSE/*bPressed*/, xkru);
  470. }
  471. else
  472. {
  473. bHandled = loc_handle_key(lglw, LGLW_FALSE/*bPressed*/, xkr);
  474. }
  475. break;
  476. case NoSymbol:
  477. printf("vstgltest<lglw_linux>: xev UNKNOWN KeyRelease: %x\n", keyRelease->keycode);
  478. break;
  479. case XK_F1:
  480. bHandled = loc_handle_key(lglw, LGLW_FALSE/*bPressed*/, LGLW_VKEY_F1);
  481. break;
  482. case XK_F2:
  483. bHandled = loc_handle_key(lglw, LGLW_FALSE/*bPressed*/, LGLW_VKEY_F2);
  484. break;
  485. case XK_F3:
  486. bHandled = loc_handle_key(lglw, LGLW_FALSE/*bPressed*/, LGLW_VKEY_F3);
  487. break;
  488. case XK_F4:
  489. bHandled = loc_handle_key(lglw, LGLW_FALSE/*bPressed*/, LGLW_VKEY_F4);
  490. break;
  491. case XK_F5:
  492. bHandled = loc_handle_key(lglw, LGLW_FALSE/*bPressed*/, LGLW_VKEY_F5);
  493. break;
  494. case XK_F6:
  495. bHandled = loc_handle_key(lglw, LGLW_FALSE/*bPressed*/, LGLW_VKEY_F6);
  496. break;
  497. case XK_F7:
  498. bHandled = loc_handle_key(lglw, LGLW_FALSE/*bPressed*/, LGLW_VKEY_F7);
  499. break;
  500. case XK_F8:
  501. bHandled = loc_handle_key(lglw, LGLW_FALSE/*bPressed*/, LGLW_VKEY_F8);
  502. break;
  503. case XK_F9:
  504. bHandled = loc_handle_key(lglw, LGLW_FALSE/*bPressed*/, LGLW_VKEY_F9);
  505. break;
  506. case XK_F10:
  507. bHandled = loc_handle_key(lglw, LGLW_FALSE/*bPressed*/, LGLW_VKEY_F10);
  508. break;
  509. case XK_F11:
  510. bHandled = loc_handle_key(lglw, LGLW_FALSE/*bPressed*/, LGLW_VKEY_F11);
  511. break;
  512. case XK_F12:
  513. bHandled = loc_handle_key(lglw, LGLW_FALSE/*bPressed*/, LGLW_VKEY_F12);
  514. break;
  515. case XK_BackSpace:
  516. bHandled = loc_handle_key(lglw, LGLW_FALSE/*bPressed*/, LGLW_VKEY_BACKSPACE);
  517. break;
  518. case XK_Tab:
  519. bHandled = loc_handle_key(lglw, LGLW_FALSE/*bPressed*/, LGLW_VKEY_TAB);
  520. break;
  521. case XK_Return:
  522. bHandled = loc_handle_key(lglw, LGLW_FALSE/*bPressed*/, LGLW_VKEY_RETURN);
  523. break;
  524. case XK_Escape:
  525. bHandled = loc_handle_key(lglw, LGLW_FALSE/*bPressed*/, LGLW_VKEY_ESCAPE);
  526. break;
  527. case XK_Shift_L:
  528. lglw->keyboard.kmod_state &= ~LGLW_KMOD_LSHIFT;
  529. bHandled = loc_handle_key(lglw, LGLW_FALSE/*bPressed*/, LGLW_VKEY_LSHIFT);
  530. bHandled = LGLW_FALSE;
  531. break;
  532. case XK_Shift_R:
  533. lglw->keyboard.kmod_state &= ~LGLW_KMOD_RSHIFT;
  534. bHandled = loc_handle_key(lglw, LGLW_FALSE/*bPressed*/, LGLW_VKEY_RSHIFT);
  535. bHandled = LGLW_FALSE;
  536. break;
  537. case XK_Control_L:
  538. lglw->keyboard.kmod_state &= ~LGLW_KMOD_LCTRL;
  539. bHandled = loc_handle_key(lglw, LGLW_FALSE/*bPressed*/, LGLW_VKEY_LCTRL);
  540. bHandled = LGLW_FALSE;
  541. break;
  542. case XK_Control_R:
  543. lglw->keyboard.kmod_state &= ~LGLW_KMOD_RCTRL;
  544. bHandled = loc_handle_key(lglw, LGLW_FALSE/*bPressed*/, LGLW_VKEY_RCTRL);
  545. bHandled = LGLW_FALSE;
  546. break;
  547. }
  548. break;
  549. case ButtonPress:
  550. printf("vstgltest<lglw_linux>: xev ButtonPress\n");
  551. XButtonPressedEvent *btnPress = (XButtonPressedEvent*)xev;
  552. lglw->mouse.p.x = btnPress->x;
  553. lglw->mouse.p.y = btnPress->y;
  554. if(0u == (lglw->focus.state & LGLW_FOCUS_MOUSE))
  555. {
  556. loc_handle_mouseenter(lglw);
  557. }
  558. switch(btnPress->button)
  559. {
  560. default:
  561. printf("vstgltest<lglw_linux>: xev ButtonPress unhandled button: %i\n", btnPress->button);
  562. break;
  563. case Button1:
  564. loc_handle_mousebutton(lglw, LGLW_TRUE/*bPressed*/, LGLW_MOUSE_LBUTTON);
  565. break;
  566. case Button2:
  567. loc_handle_mousebutton(lglw, LGLW_TRUE/*bPressed*/, LGLW_MOUSE_RBUTTON);
  568. break;
  569. case Button3:
  570. loc_handle_mousebutton(lglw, LGLW_TRUE/*bPressed*/, LGLW_MOUSE_MBUTTON);
  571. break;
  572. case Button4:
  573. loc_handle_mousebutton(lglw, LGLW_TRUE/*bPressed*/, LGLW_MOUSE_WHEELUP);
  574. break;
  575. case Button5:
  576. loc_handle_mousebutton(lglw, LGLW_TRUE/*bPressed*/, LGLW_MOUSE_WHEELDOWN);
  577. break;
  578. }
  579. break;
  580. case ButtonRelease:
  581. printf("vstgltest<lglw_linux>: xev ButtonRelease\n");
  582. XButtonReleasedEvent *btnRelease = (XButtonReleasedEvent*)xev;
  583. lglw->mouse.p.x = btnRelease->x;
  584. lglw->mouse.p.y = btnRelease->y;
  585. switch(btnRelease->button)
  586. {
  587. default:
  588. printf("vstgltest<lglw_linux>: xev ButtonRelease unhandled button: %i\n", btnRelease->button);
  589. break;
  590. case Button1:
  591. loc_handle_mousebutton(lglw, LGLW_FALSE/*bPressed*/, LGLW_MOUSE_LBUTTON);
  592. break;
  593. case Button2:
  594. loc_handle_mousebutton(lglw, LGLW_FALSE/*bPressed*/, LGLW_MOUSE_RBUTTON);
  595. break;
  596. case Button3:
  597. loc_handle_mousebutton(lglw, LGLW_FALSE/*bPressed*/, LGLW_MOUSE_MBUTTON);
  598. break;
  599. case Button4:
  600. loc_handle_mousebutton(lglw, LGLW_FALSE/*bPressed*/, LGLW_MOUSE_WHEELUP);
  601. break;
  602. case Button5:
  603. loc_handle_mousebutton(lglw, LGLW_FALSE/*bPressed*/, LGLW_MOUSE_WHEELDOWN);
  604. break;
  605. }
  606. break;
  607. case SelectionClear:
  608. printf("vstgltest<lglw_linux>: xev SelectionClear\n");
  609. lglw->clipboard.numChars = 0;
  610. free(lglw->clipboard.data);
  611. break;
  612. case SelectionRequest:
  613. printf("vstgltest<lglw_linux>: xev SelectionRequest\n");
  614. XSelectionRequestEvent *cbReq = (XSelectionRequestEvent*)xev;
  615. XSelectionEvent cbRes;
  616. Atom utf8 = XInternAtom(lglw->xdsp, "UTF8_STRING", False);
  617. cbRes.type = SelectionNotify;
  618. cbRes.requestor = cbReq->requestor;
  619. cbRes.selection = cbReq->selection;
  620. cbRes.target = cbReq->target;
  621. cbRes.time = cbReq->time;
  622. if(cbReq->target == utf8)
  623. {
  624. XChangeProperty(lglw->xdsp, cbReq->requestor, cbReq->property, utf8, 8/*format*/, PropModeReplace,
  625. (unsigned char *)lglw->clipboard.data, lglw->clipboard.numChars);
  626. cbRes.property = cbReq->property;
  627. }
  628. else
  629. {
  630. cbRes.property = None;
  631. }
  632. XSendEvent(lglw->xdsp, cbReq->requestor, True, NoEventMask, (XEvent *)&cbRes);
  633. break;
  634. }
  635. }
  636. }
  637. }
  638. static void loc_setProperty(Display *_display, Window _window, const char *_name, void *_value) {
  639. size_t data = (size_t)_value;
  640. long temp[2];
  641. // Split the 64 bit pointer into a little-endian long array
  642. temp[0] = (long)(data & 0xffffffffUL);
  643. temp[1] = (long)(data >> 32L);
  644. printf("xxx lglw_linux:loc_setProperty: name=\"%s\" value=%p temp[0]=%08x temp[1]=%08x\n", _name, _value, temp[0], temp[1]);
  645. Atom atom = XInternAtom(_display, _name, False/*only_if_exists*/);
  646. // (note) what's quite weird here is that we're writing an array of 32bit values, yet the element format must be 64bit (long)
  647. XChangeProperty(_display, _window,
  648. atom/*property*/,
  649. atom/*type*/,
  650. 32/*format*/,
  651. PropModeReplace/*mode*/,
  652. (unsigned char*)temp/*data*/,
  653. 2/*nelements*/
  654. );
  655. }
  656. static void *loc_getProperty(Display *_display, Window _window, const char *_name) {
  657. int userSize;
  658. unsigned long bytes;
  659. unsigned long userCount;
  660. unsigned char *data;
  661. Atom userType;
  662. Atom atom = XInternAtom(_display, _name, False);
  663. // (note) 64bit properties need to be read with two XGetWindowProperty() calls.
  664. // When using just one call and setting the 'length' to 2, the upper 32bit (second array element) will be 0xFFFFffff.
  665. XGetWindowProperty(_display,
  666. _window,
  667. atom,
  668. 0/*offset*/,
  669. 1/*length*/,
  670. False/*delete*/,
  671. AnyPropertyType,
  672. &userType/*actual_type_return*/,
  673. &userSize/*actual_format_return*/,
  674. &userCount/*nitems_return*/,
  675. &bytes/*bytes_after_return / partial reads*/,
  676. &data);
  677. union {
  678. uint32_t ui[2];
  679. void *any;
  680. } uptr;
  681. uptr.any = 0;
  682. printf("xxx lglw_linux: loc_getProperty: LOWER userSize=%d userCount=%lu bytes=%lu data=%p\n", userSize, userCount, bytes, data);
  683. if(NULL != data)
  684. {
  685. if(userCount >= 1)
  686. {
  687. if(userCount >= 2)
  688. {
  689. printf("xxx loc_getProperty: lo=0x%08x hi=0x%08x\n", ((uint32_t*)data)[0], ((uint32_t*)data)[1]);
  690. }
  691. // lower 32-bit
  692. uptr.ui[0] = *(long*)data;
  693. uptr.ui[1] = 0;
  694. printf("xxx lower=0x%08x\n", uptr.ui[0]);
  695. // // printf("xxx upper=0x%08x\n", uptr.ui[1]);
  696. XFree(data);
  697. // // if(userCount >= 2)
  698. {
  699. XGetWindowProperty(_display,
  700. _window,
  701. atom,
  702. 1/*offset*/,
  703. 1/*length*/,
  704. False/*delete*/,
  705. AnyPropertyType,
  706. &userType/*actual_type_return*/,
  707. &userSize/*actual_format_return*/,
  708. &userCount/*nitems_return*/,
  709. &bytes/*bytes_after_return / partial reads*/,
  710. &data);
  711. printf("xxx lglw_linux: loc_getProperty: UPPER userSize=%d userCount=%lu bytes=%lu data=%p\n", userSize, userCount, bytes, data);
  712. if(NULL != data)
  713. {
  714. // upper 32-bit
  715. uptr.ui[1] = *(long*)data;
  716. printf("xxx upper=0x%08x\n", uptr.ui[1]);
  717. XFree(data);
  718. }
  719. }
  720. }
  721. }
  722. printf("xxx lglw_linux: loc_getProperty: return value=%p\n", uptr.any);
  723. return uptr.any;
  724. }
  725. #ifdef ARCH_X64
  726. #if 0
  727. // Pulled from the Renoise 64-bit callback example
  728. // Unsure what data was supposed to be, but swapping it to a function name did not work
  729. // This does nothing, no event proc found
  730. static void loc_setEventProc (Display *display, Window window) {
  731. size_t data = (size_t)loc_eventProc;
  732. long temp[2];
  733. printf("vstgltest<lglw_linux>: setEventProc (2*32bit). window=%lu loc_eventProc=%p\n", window, &loc_eventProc);
  734. // Split the 64 bit pointer into a little-endian unsigned int array
  735. temp[0] = (uint32_t)(data & 0xffffffffUL);
  736. temp[1] = (uint32_t)(data >> 32L);
  737. Atom atom = XInternAtom(display, "_XEventProc", False);
  738. XChangeProperty(display, window,
  739. atom/*property*/,
  740. atom/*type*/,
  741. 32/*format*/,
  742. PropModeReplace/*mode*/,
  743. (unsigned char*)temp/*data*/,
  744. 2/*nelements*/
  745. );
  746. }
  747. #else
  748. // GPL code pulled from the amsynth example <https://github.com/amsynth/amsynth/blob/4a87798e650c6d71d70274a961c9b8d98fc6da7e/src/amsynth_vst.cpp>
  749. // Simply swapped out the function names, crashes Ardour in the same was as the others
  750. static void loc_setEventProc (Display *display, Window window) {
  751. //
  752. // JUCE calls XGetWindowProperty with long_length = 1 which means it only fetches the lower 32 bits of the address.
  753. // Therefore we need to ensure we return an address in the lower 32-bits of address space.
  754. //
  755. // based on mach_override
  756. static const unsigned char kJumpInstructions[] = {
  757. 0xFF, 0x25, 0x00, 0x00, 0x00, 0x00,
  758. 0x00, 0x00, 0x00, 0x00,
  759. 0x00, 0x00, 0x00, 0x00
  760. };
  761. static const int kJumpAddress = 6;
  762. static char *ptr = 0;
  763. if (!ptr) {
  764. ptr = (char *)mmap(0,
  765. getpagesize()/*PAGE_SIZE*/,
  766. PROT_READ | PROT_WRITE | PROT_EXEC,
  767. MAP_ANONYMOUS | MAP_PRIVATE | MAP_32BIT,
  768. 0, 0);
  769. if (ptr == MAP_FAILED) {
  770. perror("mmap");
  771. ptr = 0;
  772. return;
  773. } else {
  774. memcpy(ptr, kJumpInstructions, sizeof(kJumpInstructions));
  775. *((uint64_t *)(ptr + kJumpAddress)) = (uint64_t)(&loc_eventProc);
  776. msync(ptr, sizeof(kJumpInstructions), MS_INVALIDATE);
  777. printf("vstgltest<lglw_linux>: 64bit trampoline installed\n");
  778. }
  779. }
  780. long temp[2] = {(uint32_t)(((size_t)ptr)&0xFFFFfffful), 0};
  781. Atom atom = XInternAtom(display, "_XEventProc", False);
  782. XChangeProperty(display, window,
  783. atom/*property*/,
  784. atom/*type*/,
  785. 32/*format*/,
  786. PropModeReplace/*mode*/,
  787. (unsigned char *)temp/*data*/,
  788. 2/*nelements*/
  789. );
  790. }
  791. #endif
  792. #else
  793. // Pulled from the eXT2 example
  794. static void loc_setEventProc (Display *display, Window window) {
  795. void* data = (void*)&loc_eventProc; // swapped the function name here
  796. // (note) 32-bit only
  797. Atom atom = XInternAtom(display, "_XEventProc", False);
  798. XChangeProperty(display, window,
  799. atom/*property*/,
  800. atom/*type*/,
  801. 32/*format*/,
  802. PropModeReplace/*mode*/,
  803. (unsigned char*)&data/*data*/,
  804. 1/*nelements*/
  805. );
  806. }
  807. #endif // ARCH_X64
  808. // ---------------------------------------------------------------------------- lglw_window_open
  809. lglw_bool_t lglw_window_open (lglw_t _lglw, void *_parentHWNDOrNull, int32_t _x, int32_t _y, int32_t _w, int32_t _h) {
  810. lglw_bool_t r = LGLW_FALSE;
  811. LGLW(_lglw);
  812. if(NULL != lglw)
  813. {
  814. lglw_log("lglw:lglw_window_open: 1, %p, %i \n", (Window)_parentHWNDOrNull, (Window)_parentHWNDOrNull);
  815. lglw->parent_xwnd = (0 == _parentHWNDOrNull) ? DefaultRootWindow(lglw->xdsp) : (Window)_parentHWNDOrNull;
  816. lglw_log("lglw:lglw_window_open: 2 lglw=%p\n", lglw);
  817. if(_w <= 16)
  818. _w = lglw->hidden.size.x;
  819. lglw_log("lglw:lglw_window_open: 3\n");
  820. if(_h <= 16)
  821. _h = lglw->hidden.size.y;
  822. // TODO: compare to 'WindowClass' from Windows implementation
  823. lglw_log("lglw:lglw_window_open: 4\n");
  824. XSetWindowAttributes swa;
  825. // // XEvent event;
  826. XSync(lglw->xdsp, False);
  827. #if 1
  828. lglw_log("lglw:lglw_window_open: 5\n");
  829. swa.border_pixel = 0;
  830. swa.colormap = lglw->cmap;
  831. // (note) [bsp] setting this to NoEventMask causes all events to be propagated to the parent (host) window.
  832. // The host then reports the event to the plugin by calling its eventProc function (set via "_XEventProc").
  833. swa.event_mask = NoEventMask;/////ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask | ButtonMotionMask | FocusChangeMask;
  834. lglw->win.xwnd = XCreateWindow(lglw->xdsp/*display*/,
  835. DefaultRootWindow(lglw->xdsp)/*parent. see Cameron's comment below.*/,
  836. 0/*x*/,
  837. 0/*y*/,
  838. _w/*width*/,
  839. _h/*height*/,
  840. 0/*border_width*/,
  841. CopyFromParent/*depth*/,
  842. InputOutput/*class*/,
  843. lglw->vi->visual,
  844. CWBorderPixel | CWColormap | CWEventMask/*value_mask*/,
  845. &swa/*attributes*/
  846. );
  847. lglw_log("lglw:lglw_window_open: 6\n");
  848. XSetStandardProperties(lglw->xdsp/*display*/,
  849. lglw->win.xwnd/*window*/,
  850. "LGLW"/*window_name*/,
  851. "LGLW"/*icon_name*/,
  852. None/*icon_pixmap*/,
  853. NULL/*argv*/,
  854. 0/*argc*/,
  855. NULL/*XSizeHints*/
  856. );
  857. // Setup the event proc now, on the parent window as well just for the debug host
  858. // It was simpler to do this than check in the debug host for the reparent event
  859. lglw_log("lglw:lglw_window_open: 7\n");
  860. loc_setEventProc(lglw->xdsp, lglw->win.xwnd);
  861. loc_setProperty(lglw->xdsp, lglw->win.xwnd, "_lglw", (void*)lglw); // set instance pointer
  862. if(0 != _parentHWNDOrNull)
  863. {
  864. loc_setEventProc(lglw->xdsp, lglw->parent_xwnd);
  865. loc_setProperty(lglw->xdsp, lglw->parent_xwnd, "_lglw", (void*)lglw); // set instance pointer
  866. }
  867. // Some hosts only check and store the callback when the Window is reparented
  868. // Since creating the Window with a Parent may or may not do that, but the callback is not set,
  869. // ... it's created as a root window, the callback is set, and then it's reparented
  870. #if 1
  871. // (note) [cameronleger] In Ardour's code-base, the only time it looks for the _XEventProc is during a ReparentNotify event
  872. if (0 != _parentHWNDOrNull)
  873. {
  874. lglw_log("lglw:lglw_window_open: 8\n");
  875. XReparentWindow(lglw->xdsp, lglw->win.xwnd, lglw->parent_xwnd, 0, 0);
  876. }
  877. #endif
  878. lglw->win.b_owner = LGLW_TRUE;
  879. #else
  880. lglw->win.xwnd = (Window)_parentHWNDOrNull;
  881. lglw->win.b_owner = LGLW_FALSE;
  882. #endif
  883. lglw_log("lglw:lglw_window_open: 9\n");
  884. if(lglw->win.b_owner)
  885. {
  886. // // XMapRaised(lglw->xdsp, lglw->win.xwnd);
  887. XMapWindow(lglw->xdsp, lglw->win.xwnd);
  888. }
  889. #if 0
  890. XSelectInput(lglw->xdsp, lglw->win.xwnd,
  891. ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask | ButtonMotionMask | FocusChangeMask
  892. );
  893. XGrabKeyboard(lglw->xdsp, lglw->win.xwnd,
  894. False/*owner_events*/,
  895. GrabModeAsync/*pointer_mode*/,
  896. GrabModeAsync/*keyboard_mode*/,
  897. CurrentTime/*time*/
  898. );
  899. #endif
  900. #if 0
  901. XSetInputFocus(lglw->xdsp/*display*/,
  902. PointerRoot/*focus*/,
  903. RevertToPointerRoot/*revert_to*/,
  904. CurrentTime
  905. );
  906. #endif
  907. XSync(lglw->xdsp, False);
  908. lglw->win.mapped = LGLW_TRUE;
  909. lglw_log("lglw:lglw_window_open: 10\n");
  910. lglw->win.size.x = _w;
  911. lglw->win.size.y = _h;
  912. lglw_log("lglw:lglw_window_open: 11\n");
  913. loc_enable_dropfiles(lglw, (NULL != lglw->dropfiles.cbk));
  914. lglw_log("lglw:lglw_window_open: EXIT\n");
  915. r = LGLW_TRUE;
  916. }
  917. return r;
  918. }
  919. // ---------------------------------------------------------------------------- lglw_window_resize
  920. lglw_bool_t lglw_window_resize (lglw_t _lglw, int32_t _w, int32_t _h) {
  921. lglw_bool_t r = LGLW_FALSE;
  922. LGLW(_lglw);
  923. if(NULL != lglw)
  924. {
  925. if(0 != lglw->win.xwnd)
  926. {
  927. lglw_log("lglw:lglw_window_resize: 1\n");
  928. r = LGLW_TRUE;
  929. lglw_log("lglw:lglw_window_resize: 2\n");
  930. XResizeWindow(lglw->xdsp, lglw->win.xwnd, _w, _h);
  931. XRaiseWindow(lglw->xdsp, lglw->win.xwnd);
  932. lglw_log("lglw:lglw_window_resize: 3\n");
  933. int deltaW = _w - lglw->win.size.x;
  934. int deltaH = _h - lglw->win.size.y;
  935. lglw_log("lglw:lglw_window_resize: 4\n");
  936. lglw->win.size.x = _w;
  937. lglw->win.size.y = _h;
  938. lglw_log("lglw:lglw_window_resize: 5\n");
  939. Window root, parent, *children = NULL;
  940. unsigned int num_children;
  941. lglw_log("lglw:lglw_window_resize: 6\n");
  942. if(!XQueryTree(lglw->xdsp, lglw->win.xwnd, &root, &parent, &children, &num_children))
  943. return r;
  944. lglw_log("lglw:lglw_window_resize: 7\n");
  945. if(children)
  946. XFree((char *)children);
  947. lglw_log("lglw:lglw_window_resize: 8\n");
  948. // Resize parent window (if any)
  949. if(0 != parent)
  950. {
  951. lglw_log("lglw:lglw_window_resize: 8.1\n");
  952. int x, y;
  953. unsigned int width, height;
  954. unsigned int border_width;
  955. unsigned int depth;
  956. lglw_log("lglw:lglw_window_resize: 8.2\n");
  957. if(!XGetGeometry(lglw->xdsp, lglw->win.xwnd, &root, &x, &y, &width, &height, &border_width, &depth))
  958. return r;
  959. lglw_log("lglw:lglw_window_resize: 8.3\n");
  960. XResizeWindow(lglw->xdsp, parent, width + deltaW, height + deltaH);
  961. }
  962. lglw_log("lglw:lglw_window_resize: EXIT\n");
  963. }
  964. }
  965. return r;
  966. }
  967. // ---------------------------------------------------------------------------- lglw_window_close
  968. void lglw_window_close (lglw_t _lglw) {
  969. LGLW(_lglw);
  970. if(NULL != lglw)
  971. {
  972. if(0 != lglw->win.xwnd)
  973. {
  974. lglw_log("lglw:lglw_window_close: 1\n");
  975. lglw_timer_stop(_lglw);
  976. lglw_log("lglw:lglw_window_close: 2\n");
  977. glXMakeCurrent(lglw->xdsp, None, NULL);
  978. lglw_log("lglw:lglw_window_close: 3\n");
  979. if(lglw->win.b_owner)
  980. {
  981. XDestroyWindow(lglw->xdsp, lglw->win.xwnd);
  982. lglw->win.b_owner = LGLW_FALSE;
  983. }
  984. XSync(lglw->xdsp, False);
  985. lglw->win.xwnd = 0;
  986. lglw->win.mapped = LGLW_FALSE;
  987. }
  988. }
  989. lglw_log("lglw:lglw_window_close: EXIT\n");
  990. }
  991. // ---------------------------------------------------------------------------- lglw_window_show
  992. void lglw_window_show(lglw_t _lglw) {
  993. LGLW(_lglw);
  994. if(NULL != lglw)
  995. {
  996. lglw_log("lglw:lglw_window_show: 1\n");
  997. XMapRaised(lglw->xdsp, lglw->win.xwnd);
  998. lglw->win.mapped = LGLW_TRUE;
  999. }
  1000. lglw_log("lglw:lglw_window_show: EXIT\n");
  1001. }
  1002. // ---------------------------------------------------------------------------- lglw_window_hide
  1003. void lglw_window_hide(lglw_t _lglw) {
  1004. LGLW(_lglw);
  1005. if(NULL != lglw)
  1006. {
  1007. lglw_log("lglw:lglw_window_hide: 1\n");
  1008. XUnmapWindow(lglw->xdsp, lglw->win.xwnd);
  1009. lglw->win.mapped = LGLW_FALSE;
  1010. }
  1011. lglw_log("lglw:lglw_window_hide: EXIT\n");
  1012. }
  1013. // ---------------------------------------------------------------------------- lglw_window_is_visible
  1014. lglw_bool_t lglw_window_is_visible(lglw_t _lglw) {
  1015. lglw_bool_t r = LGLW_FALSE;
  1016. LGLW(_lglw);
  1017. // lglw_log("lglw:lglw_window_is_visible: 1\n");
  1018. if(NULL != lglw && 0 != lglw->win.xwnd)
  1019. {
  1020. // lglw_log("lglw:lglw_window_is_visible: 2\n");
  1021. r = lglw->win.mapped;
  1022. }
  1023. // lglw_log("lglw:lglw_window_is_visible: EXIT\n");
  1024. return r;
  1025. }
  1026. // ---------------------------------------------------------------------------- lglw_window_size_get
  1027. void lglw_window_size_get(lglw_t _lglw, int32_t *_retX, int32_t *_retY) {
  1028. LGLW(_lglw);
  1029. lglw_log("lglw:lglw_window_size_get: 1\n");
  1030. if(NULL != lglw)
  1031. {
  1032. if(0 != lglw->win.xwnd)
  1033. {
  1034. if(NULL != _retX)
  1035. *_retX = lglw->win.size.x;
  1036. if(NULL != _retY)
  1037. *_retY = lglw->win.size.y;
  1038. }
  1039. }
  1040. lglw_log("lglw:lglw_window_size_get: EXIT\n");
  1041. }
  1042. // ---------------------------------------------------------------------------- lglw_redraw
  1043. void lglw_redraw(lglw_t _lglw) {
  1044. LGLW(_lglw);
  1045. // (todo) implement me
  1046. if(NULL != lglw)
  1047. {
  1048. if(0 != lglw->win.xwnd)
  1049. {
  1050. // TODO Event Loop
  1051. lglw_log("lglw:lglw_redraw: 1\n");
  1052. XEvent xev;
  1053. xev.xany.type = Expose;
  1054. xev.xany.serial = 0;
  1055. xev.xany.send_event = True;
  1056. xev.xany.display = lglw->xdsp;
  1057. xev.xany.window = lglw->win.xwnd;
  1058. xev.xexpose.x = 0;
  1059. xev.xexpose.y = 0;
  1060. xev.xexpose.width = lglw->win.size.x;
  1061. xev.xexpose.height = lglw->win.size.y;
  1062. xev.xexpose.count = 0;
  1063. XSendEvent(lglw->xdsp, lglw->win.xwnd,
  1064. True/*propagate*/,
  1065. ExposureMask/*event_mask*/,
  1066. &xev
  1067. );
  1068. XFlush(lglw->xdsp);
  1069. }
  1070. }
  1071. }
  1072. // ---------------------------------------------------------------------------- lglw_redraw_callback_set
  1073. void lglw_redraw_callback_set(lglw_t _lglw, lglw_redraw_fxn_t _cbk) {
  1074. LGLW(_lglw);
  1075. if(NULL != lglw)
  1076. {
  1077. lglw->redraw.cbk = _cbk;
  1078. }
  1079. }
  1080. // ---------------------------------------------------------------------------- lglw_glcontext_push
  1081. void lglw_glcontext_push(lglw_t _lglw) {
  1082. LGLW(_lglw);
  1083. if(NULL != lglw)
  1084. {
  1085. lglw->prev.drw = glXGetCurrentDrawable();
  1086. lglw->prev.ctx = glXGetCurrentContext();
  1087. // lglw_log("lglw:lglw_glcontext_push: win.xwnd=%p hidden.xwnd=%p ctx=%p\n",
  1088. // lglw->win.xwnd, lglw->hidden.xwnd, lglw->ctx);
  1089. if(!glXMakeCurrent(lglw->xdsp, (0 == lglw->win.xwnd) ? lglw->hidden.xwnd : lglw->win.xwnd, lglw->ctx))
  1090. {
  1091. 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());
  1092. }
  1093. }
  1094. }
  1095. // ---------------------------------------------------------------------------- lglw_glcontext_pop
  1096. void lglw_glcontext_pop(lglw_t _lglw) {
  1097. LGLW(_lglw);
  1098. if(NULL != lglw)
  1099. {
  1100. // lglw_log("lglw:lglw_glcontext_pop: prev.drw=%p prev.ctx=%p\n",
  1101. // lglw->prev.drw, lglw->prev.ctx);
  1102. if(!glXMakeCurrent(lglw->xdsp, lglw->prev.drw, lglw->prev.ctx))
  1103. {
  1104. lglw_log("[---] lglw_glcontext_pop: glXMakeCurrent() failed. prev.drw=%p ctx=%p glGetError()=%d\n", lglw->prev.drw, lglw->prev.ctx, glGetError());
  1105. }
  1106. }
  1107. }
  1108. // ---------------------------------------------------------------------------- lglw_swap_buffers
  1109. void lglw_swap_buffers(lglw_t _lglw) {
  1110. LGLW(_lglw);
  1111. if(NULL != lglw)
  1112. {
  1113. if(0 != lglw->win.xwnd)
  1114. {
  1115. // lglw_log("lglw:lglw_swap_buffers: 1\n");
  1116. glXSwapBuffers(lglw->xdsp, lglw->win.xwnd);
  1117. }
  1118. }
  1119. }
  1120. // ---------------------------------------------------------------------------- lglw_swap_interval_set
  1121. typedef void (APIENTRY *PFNWGLEXTSWAPINTERVALPROC) (int);
  1122. void lglw_swap_interval_set(lglw_t _lglw, int32_t _ival) {
  1123. LGLW(_lglw);
  1124. if(NULL != lglw)
  1125. {
  1126. lglw_log("lglw:lglw_swap_interval_set: 1\n");
  1127. PFNWGLEXTSWAPINTERVALPROC glXSwapIntervalEXT;
  1128. glXSwapIntervalEXT = (PFNWGLEXTSWAPINTERVALPROC) glXGetProcAddress((const GLubyte*)"glXSwapIntervalEXT");
  1129. if(NULL != glXSwapIntervalEXT)
  1130. {
  1131. lglw_log("lglw:lglw_swap_interval_set: 2\n");
  1132. glXSwapIntervalEXT(_ival);
  1133. lglw->win.swap_interval = _ival;
  1134. }
  1135. }
  1136. }
  1137. // ---------------------------------------------------------------------------- lglw_swap_interval_get
  1138. int32_t lglw_swap_interval_get(lglw_t _lglw) {
  1139. LGLW(_lglw);
  1140. int32_t r = 0;
  1141. if(NULL != lglw)
  1142. {
  1143. r = lglw->win.swap_interval;
  1144. }
  1145. return r;
  1146. }
  1147. // ---------------------------------------------------------------------------- loc_handle_mouseleave
  1148. static void loc_handle_mouseleave(lglw_int_t *lglw) {
  1149. lglw->focus.state &= ~LGLW_FOCUS_MOUSE;
  1150. if(NULL != lglw->focus.cbk)
  1151. {
  1152. lglw->focus.cbk(lglw, lglw->focus.state, LGLW_FOCUS_MOUSE);
  1153. }
  1154. lglw_log("xxx lglw:loc_handle_mouseleave: LEAVE\n");
  1155. }
  1156. // ---------------------------------------------------------------------------- loc_handle_mouseenter
  1157. static void loc_handle_mouseenter(lglw_int_t *lglw) {
  1158. lglw->focus.state |= LGLW_FOCUS_MOUSE;
  1159. if(NULL != lglw->focus.cbk)
  1160. {
  1161. lglw->focus.cbk(lglw, lglw->focus.state, LGLW_FOCUS_MOUSE);
  1162. }
  1163. lglw_log("xxx lglw:loc_handle_mouseenter: LEAVE\n");
  1164. }
  1165. // ---------------------------------------------------------------------------- loc_handle_mousebutton
  1166. static void loc_handle_mousebutton(lglw_int_t *lglw, lglw_bool_t _bPressed, uint32_t _button) {
  1167. if(_bPressed)
  1168. lglw->mouse.button_state |= _button;
  1169. else
  1170. lglw->mouse.button_state &= ~_button;
  1171. if(NULL != lglw->mouse.cbk)
  1172. {
  1173. lglw->mouse.cbk(lglw, lglw->mouse.p.x, lglw->mouse.p.y, lglw->mouse.button_state, _button);
  1174. }
  1175. }
  1176. // ---------------------------------------------------------------------------- loc_handle_mousemotion
  1177. static void loc_handle_mousemotion(lglw_int_t *lglw) {
  1178. if(NULL != lglw->mouse.cbk)
  1179. {
  1180. lglw->mouse.cbk(lglw, lglw->mouse.p.x, lglw->mouse.p.y, lglw->mouse.button_state, 0u/*changedbuttonstate*/);
  1181. }
  1182. }
  1183. // ---------------------------------------------------------------------------- lglw_mouse_callback_set
  1184. void lglw_mouse_callback_set(lglw_t _lglw, lglw_mouse_fxn_t _cbk) {
  1185. LGLW(_lglw);
  1186. if(NULL != lglw)
  1187. {
  1188. lglw->mouse.cbk = _cbk;
  1189. }
  1190. }
  1191. // ---------------------------------------------------------------------------- lglw_mouse_callback_set
  1192. void lglw_focus_callback_set(lglw_t _lglw, lglw_focus_fxn_t _cbk) {
  1193. LGLW(_lglw);
  1194. if(NULL != lglw)
  1195. {
  1196. lglw->focus.cbk = _cbk;
  1197. }
  1198. }
  1199. // ---------------------------------------------------------------------------- loc_handle_key
  1200. static lglw_bool_t loc_handle_key(lglw_int_t *lglw, lglw_bool_t _bPressed, uint32_t _vkey) {
  1201. lglw_bool_t r = LGLW_FALSE;
  1202. if(NULL != lglw->keyboard.cbk)
  1203. {
  1204. r = lglw->keyboard.cbk(lglw, _vkey, lglw->keyboard.kmod_state, _bPressed);
  1205. }
  1206. return r;
  1207. }
  1208. // ---------------------------------------------------------------------------- lglw_keyboard_callback_set
  1209. void lglw_keyboard_callback_set(lglw_t _lglw, lglw_keyboard_fxn_t _cbk) {
  1210. LGLW(_lglw);
  1211. if(NULL != lglw)
  1212. {
  1213. lglw->keyboard.cbk = _cbk;
  1214. }
  1215. }
  1216. // ---------------------------------------------------------------------------- lglw_keyboard_get_modifiers
  1217. uint32_t lglw_keyboard_get_modifiers(lglw_t _lglw) {
  1218. uint32_t r = 0u;
  1219. LGLW(_lglw);
  1220. if(NULL != lglw)
  1221. {
  1222. r = lglw->keyboard.kmod_state;
  1223. }
  1224. return r;
  1225. }
  1226. // ---------------------------------------------------------------------------- lglw_touchkeyboard_show
  1227. void lglw_touchkeyboard_show(lglw_t _lglw, lglw_bool_t _bEnable) {
  1228. LGLW(_lglw);
  1229. // (todo) implement me
  1230. if(NULL != lglw)
  1231. {
  1232. }
  1233. }
  1234. // ---------------------------------------------------------------------------- lglw_mouse_get_buttons
  1235. uint32_t lglw_mouse_get_buttons(lglw_t _lglw) {
  1236. uint32_t r = 0u;
  1237. LGLW(_lglw);
  1238. if(NULL != lglw)
  1239. {
  1240. r = lglw->mouse.button_state;
  1241. }
  1242. return r;
  1243. }
  1244. // ---------------------------------------------------------------------------- lglw_mouse_grab
  1245. void lglw_mouse_grab(lglw_t _lglw, uint32_t _grabMode) {
  1246. LGLW(_lglw);
  1247. if(NULL != lglw)
  1248. {
  1249. if(0 != lglw->win.xwnd)
  1250. {
  1251. if(!lglw->mouse.touch.b_enable)
  1252. {
  1253. if(lglw->mouse.grab.mode != _grabMode)
  1254. {
  1255. lglw_mouse_ungrab(_lglw);
  1256. }
  1257. int result;
  1258. switch(_grabMode)
  1259. {
  1260. default:
  1261. case LGLW_MOUSE_GRAB_NONE:
  1262. break;
  1263. case LGLW_MOUSE_GRAB_CAPTURE:
  1264. result = XGrabPointer(lglw->xdsp, lglw->win.xwnd,
  1265. True/*owner_events*/,
  1266. ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask | PointerMotionHintMask | ButtonMotionMask | KeymapStateMask/*event_mask*/,
  1267. GrabModeAsync/*pointer_mode*/,
  1268. GrabModeAsync/*keyboard_mode*/,
  1269. lglw->win.xwnd/*confine_to*/,
  1270. None/*cursor*/,
  1271. CurrentTime/*time*/);
  1272. if(GrabSuccess != result)
  1273. {
  1274. printf("vstgltest<lglw_linux>: Grab Result: %i\n", result);
  1275. }
  1276. else
  1277. {
  1278. lglw->mouse.grab.mode = _grabMode;
  1279. }
  1280. break;
  1281. case LGLW_MOUSE_GRAB_WARP:
  1282. result = XGrabPointer(lglw->xdsp, lglw->win.xwnd,
  1283. True/*owner_events*/,
  1284. ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask | PointerMotionHintMask | ButtonMotionMask | KeymapStateMask/*event_mask*/,
  1285. GrabModeAsync/*pointer_mode*/,
  1286. GrabModeAsync/*keyboard_mode*/,
  1287. lglw->win.xwnd/*confine_to*/,
  1288. None/*cursor*/,
  1289. CurrentTime/*time*/);
  1290. if(GrabSuccess != result)
  1291. {
  1292. printf("vstgltest<lglw_linux>: Grab Result: %i\n", result);
  1293. }
  1294. else
  1295. {
  1296. lglw_mouse_cursor_show(_lglw, LGLW_FALSE);
  1297. lglw->mouse.grab.p = lglw->mouse.p;
  1298. lglw->mouse.grab.last_p = lglw->mouse.p;
  1299. lglw->mouse.grab.mode = _grabMode;
  1300. }
  1301. break;
  1302. }
  1303. }
  1304. }
  1305. }
  1306. }
  1307. // ---------------------------------------------------------------------------- lglw_mouse_ungrab
  1308. void lglw_mouse_ungrab(lglw_t _lglw) {
  1309. LGLW(_lglw);
  1310. if(NULL != lglw)
  1311. {
  1312. if(0 != lglw->win.xwnd)
  1313. {
  1314. if(!lglw->mouse.touch.b_enable)
  1315. {
  1316. switch(lglw->mouse.grab.mode)
  1317. {
  1318. default:
  1319. case LGLW_MOUSE_GRAB_NONE:
  1320. break;
  1321. case LGLW_MOUSE_GRAB_CAPTURE:
  1322. XUngrabPointer(lglw->xdsp, CurrentTime);
  1323. lglw->mouse.grab.mode = LGLW_MOUSE_GRAB_NONE;
  1324. break;
  1325. case LGLW_MOUSE_GRAB_WARP:
  1326. XUngrabPointer(lglw->xdsp, CurrentTime);
  1327. lglw->mouse.grab.mode = LGLW_MOUSE_GRAB_NONE;
  1328. lglw->mouse.grab.b_queue_warp = LGLW_TRUE;
  1329. lglw_mouse_cursor_show(_lglw, LGLW_TRUE);
  1330. break;
  1331. }
  1332. }
  1333. }
  1334. }
  1335. }
  1336. // ---------------------------------------------------------------------------- lglw_mouse_warp
  1337. void lglw_mouse_warp(lglw_t _lglw, int32_t _x, int32_t _y) {
  1338. LGLW(_lglw);
  1339. if(NULL != lglw)
  1340. {
  1341. if(0 != lglw->win.xwnd)
  1342. {
  1343. XWarpPointer(lglw->xdsp,
  1344. None/*src_w*/,
  1345. lglw->win.xwnd/*dest_w*/,
  1346. 0/*src_x*/,
  1347. 0/*src_y*/,
  1348. 0/*src_width*/,
  1349. 0/*src_height*/,
  1350. _x/*dest_x*/,
  1351. _y/*dest_y*/);
  1352. }
  1353. }
  1354. }
  1355. // ---------------------------------------------------------------------------- loc_handle_queued_mouse_warp
  1356. static void loc_handle_queued_mouse_warp(lglw_int_t *lglw) {
  1357. if(lglw->mouse.grab.b_queue_warp)
  1358. {
  1359. lglw->mouse.grab.b_queue_warp = LGLW_FALSE;
  1360. lglw_mouse_warp(lglw, lglw->mouse.grab.p.x, lglw->mouse.grab.p.y);
  1361. lglw->mouse.grab.last_p = lglw->mouse.grab.p;
  1362. }
  1363. }
  1364. // ---------------------------------------------------------------------------- lglw_mouse_cursor_show
  1365. void lglw_mouse_cursor_show (lglw_t _lglw, lglw_bool_t _bShow) {
  1366. LGLW(_lglw);
  1367. if(NULL != lglw)
  1368. {
  1369. if(LGLW_FALSE == _bShow)
  1370. {
  1371. Pixmap noPxm;
  1372. Cursor noCursor;
  1373. XColor black, dummy;
  1374. static char pxmNoData[] = {0, 0, 0, 0, 0, 0, 0, 0};
  1375. XAllocNamedColor(lglw->xdsp, lglw->cmap, "black", &black, &dummy);
  1376. noPxm = XCreateBitmapFromData(lglw->xdsp, lglw->win.xwnd, pxmNoData, 8, 8);
  1377. noCursor = XCreatePixmapCursor(lglw->xdsp, noPxm, noPxm, &black, &black, 0, 0);
  1378. XDefineCursor(lglw->xdsp, lglw->win.xwnd, noCursor);
  1379. XFreeCursor(lglw->xdsp, noCursor);
  1380. if(noPxm != None)
  1381. {
  1382. XFreePixmap(lglw->xdsp, noPxm);
  1383. }
  1384. }
  1385. else
  1386. {
  1387. XUndefineCursor(lglw->xdsp, lglw->win.xwnd);
  1388. }
  1389. }
  1390. }
  1391. // ---------------------------------------------------------------------------- lglw_timer_start
  1392. void lglw_timer_start(lglw_t _lglw, uint32_t _millisec) {
  1393. LGLW(_lglw);
  1394. // (todo) implement me
  1395. if(NULL != lglw)
  1396. {
  1397. }
  1398. }
  1399. // ---------------------------------------------------------------------------- lglw_timer_stop
  1400. void lglw_timer_stop(lglw_t _lglw) {
  1401. LGLW(_lglw);
  1402. // (todo) implement me
  1403. if(NULL != lglw)
  1404. {
  1405. }
  1406. }
  1407. // ---------------------------------------------------------------------------- lglw_timer_callback_set
  1408. void lglw_timer_callback_set(lglw_t _lglw, lglw_timer_fxn_t _cbk) {
  1409. LGLW(_lglw);
  1410. if(NULL != lglw)
  1411. {
  1412. lglw->timer.cbk = _cbk;
  1413. }
  1414. }
  1415. // ---------------------------------------------------------------------------- loc_enable_dropfiles
  1416. static void loc_enable_dropfiles(lglw_int_t *lglw, lglw_bool_t _bEnable) {
  1417. // (todo) implement me
  1418. }
  1419. // ---------------------------------------------------------------------------- lglw_dropfiles_callback_set
  1420. void lglw_dropfiles_callback_set(lglw_t _lglw, lglw_dropfiles_fxn_t _cbk) {
  1421. LGLW(_lglw);
  1422. if(NULL != _lglw)
  1423. {
  1424. lglw->dropfiles.cbk = _cbk;
  1425. loc_enable_dropfiles(lglw, (NULL != _cbk));
  1426. }
  1427. }
  1428. // ---------------------------------------------------------------------------- loc_touchinput_update
  1429. static void loc_touchinput_update(lglw_int_t *lglw) {
  1430. // (todo) implement me
  1431. }
  1432. // ---------------------------------------------------------------------------- lglw_touchinput_set
  1433. void lglw_touchinput_set(lglw_t _lglw, lglw_bool_t _bEnable) {
  1434. LGLW(_lglw);
  1435. if(NULL != _lglw)
  1436. {
  1437. lglw->mouse.touch.b_enable = _bEnable;
  1438. lglw->mouse.touch.b_update_queued = LGLW_TRUE;
  1439. }
  1440. }
  1441. // ---------------------------------------------------------------------------- lglw_touchinput_get
  1442. lglw_bool_t lglw_touchinput_get(lglw_t _lglw) {
  1443. lglw_bool_t r = LGLW_FALSE;
  1444. LGLW(_lglw);
  1445. if(NULL != _lglw)
  1446. {
  1447. r = lglw->mouse.touch.b_enable;
  1448. }
  1449. return r;
  1450. }
  1451. // ---------------------------------------------------------------------------- lglw_clipboard_text_set
  1452. void lglw_clipboard_text_set(lglw_t _lglw, const uint32_t _numChars, const char *_text) {
  1453. LGLW(_lglw);
  1454. (void)_numChars;
  1455. if(NULL != _text)
  1456. {
  1457. if(NULL != _lglw)
  1458. {
  1459. if(0 != lglw->win.xwnd)
  1460. {
  1461. uint32_t numChars = (0u == _numChars) ? ((uint32_t)strlen(_text)+1u) : _numChars;
  1462. if(numChars > 0u)
  1463. {
  1464. lglw->clipboard.numChars = numChars;
  1465. lglw->clipboard.data = malloc(numChars+1);
  1466. uint32_t i;
  1467. for(i = 0u; i < numChars; i++)
  1468. {
  1469. lglw->clipboard.data[i] = _text[i];
  1470. }
  1471. lglw->clipboard.data[numChars - 1] = 0;
  1472. printf("xxx lglw_clipboard_text_set(%i): %s\n", lglw->clipboard.numChars, lglw->clipboard.data);
  1473. Atom clipboard = XInternAtom(lglw->xdsp, "CLIPBOARD", False);
  1474. XSetSelectionOwner(lglw->xdsp, clipboard, lglw->win.xwnd, CurrentTime);
  1475. XSync(lglw->xdsp, False);
  1476. }
  1477. }
  1478. }
  1479. }
  1480. }
  1481. // ---------------------------------------------------------------------------- loc_is_clipboard_event
  1482. static Bool loc_is_clipboard_event(Display *_display, XEvent *_xevent, XPointer _xarg) {
  1483. return _xevent->type == SelectionNotify;
  1484. }
  1485. // ---------------------------------------------------------------------------- lglw_clipboard_text_get
  1486. void lglw_clipboard_text_get(lglw_t _lglw, uint32_t _maxChars, uint32_t *_retNumChars, char *_retText) {
  1487. LGLW(_lglw);
  1488. if(NULL != _retNumChars)
  1489. *_retNumChars = 0u;
  1490. if(NULL != _retText)
  1491. *_retText = 0;
  1492. if(_maxChars > 0u)
  1493. {
  1494. if(NULL != _lglw)
  1495. {
  1496. if(0 != lglw->win.xwnd)
  1497. {
  1498. Window owner;
  1499. XEvent xev;
  1500. XSelectionEvent *cbReq;
  1501. Atom clipboard = XInternAtom(lglw->xdsp, "CLIPBOARD", False);
  1502. Atom utf8 = XInternAtom(lglw->xdsp, "UTF8_STRING", False);
  1503. Atom target = XInternAtom(lglw->xdsp, "_clipboard_result", False);
  1504. owner = XGetSelectionOwner(lglw->xdsp, clipboard);
  1505. if(owner == None)
  1506. {
  1507. printf("xxx lglw_clipboard_text_get: No Window can provide a clipboard result\n");
  1508. return;
  1509. }
  1510. if(owner == lglw->win.xwnd)
  1511. {
  1512. printf("xxx lglw_clipboard_text_get: We are the owner of the clipboard, skip X interactions\n");
  1513. uint32_t i = 0u;
  1514. for(; i < _maxChars; i++)
  1515. {
  1516. _retText[i] = lglw->clipboard.data[i];
  1517. if(0 == _retText[i])
  1518. break;
  1519. }
  1520. _retText[_maxChars - 1u] = 0;
  1521. if(NULL != _retNumChars)
  1522. *_retNumChars = i;
  1523. printf("xxx lglw_clipboard_text_get: (result on next line)\n%s\n", _retText);
  1524. return;
  1525. }
  1526. XConvertSelection(lglw->xdsp, clipboard, utf8, target, lglw->win.xwnd, CurrentTime);
  1527. XIfEvent(lglw->xdsp, &xev, &loc_is_clipboard_event, None);
  1528. cbReq = (XSelectionEvent*)&xev;
  1529. if(None == cbReq->property)
  1530. {
  1531. printf("xxx lglw_clipboard_text_get: Clipboard was not converted to UTF-8 string\n");
  1532. return;
  1533. }
  1534. Atom returnType;
  1535. int returnFormat;
  1536. unsigned long size, returnSize, bytesLeft;
  1537. unsigned char *propertyValue = NULL;
  1538. XGetWindowProperty(lglw->xdsp, lglw->win.xwnd, target,
  1539. 0/*offset*/,
  1540. 0/*length*/,
  1541. False/*delete*/,
  1542. AnyPropertyType/*req_type*/,
  1543. &returnType/*actual_type_return*/,
  1544. &returnFormat/*actual_format_return*/,
  1545. &returnSize/*nitems_return*/,
  1546. &size/*bytes_after_return*/,
  1547. &propertyValue/*prop_return*/);
  1548. XFree(propertyValue);
  1549. if(utf8 != returnType)
  1550. {
  1551. printf("xxx lglw_clipboard_text_get: Clipboard result is not a UTF-8 string\n");
  1552. return;
  1553. }
  1554. if(8u != returnFormat)
  1555. {
  1556. printf("xxx lglw_clipboard_text_get: Clipboard format is not a char array\n");
  1557. return;
  1558. }
  1559. if(_maxChars < size)
  1560. size = _maxChars;
  1561. size = 1 + ((size - 1) / 4);
  1562. // TODO: Even with the largest current use-case, multiple calls aren't necessary. do it anyway just in case
  1563. XGetWindowProperty(lglw->xdsp, lglw->win.xwnd, target,
  1564. 0/*offset*/,
  1565. size/*length*/,
  1566. True/*delete*/,
  1567. AnyPropertyType/*req_type*/,
  1568. &returnType/*actual_type_return*/,
  1569. &returnFormat/*actual_format_return*/,
  1570. &returnSize/*nitems_return*/,
  1571. &bytesLeft/*bytes_after_return*/,
  1572. &propertyValue/*prop_return*/);
  1573. if(returnSize == 0)
  1574. {
  1575. printf("xxx lglw_clipboard_text_get: No Clipboard result after final request\n");
  1576. return;
  1577. }
  1578. uint32_t i = 0u;
  1579. for(; i < _maxChars; i++)
  1580. {
  1581. _retText[i] = propertyValue[i];
  1582. if(0 == _retText[i])
  1583. break;
  1584. }
  1585. _retText[_maxChars - 1u] = 0;
  1586. if(NULL != _retNumChars)
  1587. *_retNumChars = i;
  1588. printf("xxx lglw_clipboard_text_get: (result on next line)\n%s\n", _retText);
  1589. XFree(propertyValue);
  1590. }
  1591. }
  1592. }
  1593. }