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.

1584 lines
50KB

  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. static void loc_eventProc (void *_xevent);
  136. static void loc_setProperty (Display *_display, Window _window, const char *_name, void *_value);
  137. static void *loc_getProperty (Display *_display, Window _window, const char *_name);
  138. static void loc_setEventProc (Display *display, Window window);
  139. // ---------------------------------------------------------------------------- module vars
  140. static lglw_int_t *khook_lglw = NULL; // currently key-hooked lglw instance (one at a time)
  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. switch(xev->type)
  295. {
  296. default:
  297. printf("vstgltest<lglw_linux>: unhandled X11 event type=%d\n", xev->type);
  298. break;
  299. case Expose:
  300. printf("vstgltest<lglw_linux>: xev Expose\n");
  301. if(NULL != lglw->redraw.cbk)
  302. {
  303. lglw->redraw.cbk(lglw);
  304. }
  305. break;
  306. case FocusIn:
  307. printf("vstgltest<lglw_linux>: xev FocusIn\n");
  308. break;
  309. case FocusOut:
  310. printf("vstgltest<lglw_linux>: xev FocusOut\n");
  311. break;
  312. case EnterNotify:
  313. XEnterWindowEvent *wenter = (XEnterWindowEvent*)xev;
  314. printf("vstgltest<lglw_linux>: xev EnterNotify: mode:%i, detail:%i, state:%d\n", wenter->mode, wenter->detail, wenter->state);
  315. lglw->mouse.p.x = wenter->x;
  316. lglw->mouse.p.y = wenter->y;
  317. loc_handle_mousemotion(lglw);
  318. // EnterNotify messages can be pseudo-motion events (NotifyGrab, NotifyUngrab)
  319. // when buttons are pressed, which would trigger false focus changes
  320. // so, the callback is only sent when a normal entry happens
  321. if (wenter->mode == NotifyNormal)
  322. {
  323. loc_handle_mouseenter(lglw);
  324. }
  325. break;
  326. case LeaveNotify:
  327. XLeaveWindowEvent *wexit = (XLeaveWindowEvent*)xev;
  328. printf("vstgltest<lglw_linux>: xev LeaveNotify: mode:%i, detail:%i, state:%d\n", wexit->mode, wexit->detail, wexit->state);
  329. // LeaveNotify messages can be pseudo-motion events (NotifyGrab, NotifyUngrab)
  330. // when buttons are pressed, which would trigger false focus changes
  331. // so, the callback is only sent when a normal entry happens
  332. if (wexit->mode == NotifyNormal)
  333. {
  334. loc_handle_mouseleave(lglw);
  335. }
  336. break;
  337. case MotionNotify:
  338. // printf("vstgltest<lglw_linux>: xev MotionNotify\n");
  339. ; // empty statement
  340. XMotionEvent *motion = (XMotionEvent*)xev;
  341. if(LGLW_MOUSE_GRAB_WARP == lglw->mouse.grab.mode)
  342. {
  343. lglw->mouse.grab.b_queue_warp = LGLW_TRUE;
  344. lglw->mouse.p.x += (motion->x - lglw->mouse.grab.last_p.x);
  345. lglw->mouse.p.y += (motion->y - lglw->mouse.grab.last_p.y);
  346. lglw->mouse.grab.last_p.x = motion->x;
  347. lglw->mouse.grab.last_p.y = motion->y;
  348. }
  349. else
  350. {
  351. lglw->mouse.p.x = motion->x;
  352. lglw->mouse.p.y = motion->y;
  353. }
  354. loc_handle_mousemotion(lglw);
  355. break;
  356. case KeyPress:
  357. printf("vstgltest<lglw_linux>: xev KeyPress\n");
  358. lglw_redraw(lglw);
  359. break;
  360. case KeyRelease:
  361. printf("vstgltest<lglw_linux>: xev KeyRelease\n");
  362. break;
  363. case ButtonPress:
  364. printf("vstgltest<lglw_linux>: xev ButtonPress\n");
  365. XButtonPressedEvent *btnPress = (XButtonPressedEvent*)xev;
  366. lglw->mouse.p.x = btnPress->x;
  367. lglw->mouse.p.y = btnPress->y;
  368. if(0u == (lglw->focus.state & LGLW_FOCUS_MOUSE))
  369. {
  370. loc_handle_mouseenter(lglw);
  371. }
  372. switch(btnPress->button)
  373. {
  374. default:
  375. printf("vstgltest<lglw_linux>: xev ButtonPress unhandled button: %i\n", btnPress->button);
  376. break;
  377. case Button1:
  378. loc_handle_mousebutton(lglw, LGLW_TRUE/*bPressed*/, LGLW_MOUSE_LBUTTON);
  379. break;
  380. case Button2:
  381. loc_handle_mousebutton(lglw, LGLW_TRUE/*bPressed*/, LGLW_MOUSE_RBUTTON);
  382. break;
  383. case Button3:
  384. loc_handle_mousebutton(lglw, LGLW_TRUE/*bPressed*/, LGLW_MOUSE_MBUTTON);
  385. break;
  386. }
  387. break;
  388. case ButtonRelease:
  389. printf("vstgltest<lglw_linux>: xev ButtonRelease\n");
  390. XButtonReleasedEvent *btnRelease = (XButtonReleasedEvent*)xev;
  391. lglw->mouse.p.x = btnRelease->x;
  392. lglw->mouse.p.y = btnRelease->y;
  393. switch(btnRelease->button)
  394. {
  395. default:
  396. printf("vstgltest<lglw_linux>: xev ButtonRelease unhandled button: %i\n", btnRelease->button);
  397. break;
  398. case Button1:
  399. loc_handle_mousebutton(lglw, LGLW_FALSE/*bPressed*/, LGLW_MOUSE_LBUTTON);
  400. break;
  401. case Button2:
  402. loc_handle_mousebutton(lglw, LGLW_FALSE/*bPressed*/, LGLW_MOUSE_RBUTTON);
  403. break;
  404. case Button3:
  405. loc_handle_mousebutton(lglw, LGLW_FALSE/*bPressed*/, LGLW_MOUSE_MBUTTON);
  406. break;
  407. }
  408. break;
  409. }
  410. }
  411. }
  412. }
  413. static void loc_setProperty(Display *_display, Window _window, const char *_name, void *_value) {
  414. size_t data = (size_t)_value;
  415. long temp[2];
  416. // Split the 64 bit pointer into a little-endian long array
  417. temp[0] = (long)(data & 0xffffffffUL);
  418. temp[1] = (long)(data >> 32L);
  419. printf("xxx lglw_linux:loc_setProperty: name=\"%s\" value=%p temp[0]=%08x temp[1]=%08x\n", _name, _value, temp[0], temp[1]);
  420. Atom atom = XInternAtom(_display, _name, False/*only_if_exists*/);
  421. // (note) what's quite weird here is that we're writing an array of 32bit values, yet the element format must be 64bit (long)
  422. XChangeProperty(_display, _window,
  423. atom/*property*/,
  424. atom/*type*/,
  425. 32/*format*/,
  426. PropModeReplace/*mode*/,
  427. (unsigned char*)temp/*data*/,
  428. 2/*nelements*/
  429. );
  430. }
  431. static void *loc_getProperty(Display *_display, Window _window, const char *_name) {
  432. int userSize;
  433. unsigned long bytes;
  434. unsigned long userCount;
  435. unsigned char *data;
  436. Atom userType;
  437. Atom atom = XInternAtom(_display, _name, False);
  438. // (note) 64bit properties need to be read with two XGetWindowProperty() calls.
  439. // When using just one call and setting the 'length' to 2, the upper 32bit (second array element) will be 0xFFFFffff.
  440. XGetWindowProperty(_display,
  441. _window,
  442. atom,
  443. 0/*offset*/,
  444. 1/*length*/,
  445. False/*delete*/,
  446. AnyPropertyType,
  447. &userType/*actual_type_return*/,
  448. &userSize/*actual_format_return*/,
  449. &userCount/*nitems_return*/,
  450. &bytes/*bytes_after_return / partial reads*/,
  451. &data);
  452. union {
  453. uint32_t ui[2];
  454. void *any;
  455. } uptr;
  456. uptr.any = 0;
  457. printf("xxx lglw_linux: loc_getProperty: LOWER userSize=%d userCount=%lu bytes=%lu data=%p\n", userSize, userCount, bytes, data);
  458. if(NULL != data)
  459. {
  460. if(userCount >= 1)
  461. {
  462. if(userCount >= 2)
  463. {
  464. printf("xxx loc_getProperty: lo=0x%08x hi=0x%08x\n", ((uint32_t*)data)[0], ((uint32_t*)data)[1]);
  465. }
  466. // lower 32-bit
  467. uptr.ui[0] = *(long*)data;
  468. uptr.ui[1] = 0;
  469. printf("xxx lower=0x%08x\n", uptr.ui[0]);
  470. // // printf("xxx upper=0x%08x\n", uptr.ui[1]);
  471. XFree(data);
  472. // // if(userCount >= 2)
  473. {
  474. XGetWindowProperty(_display,
  475. _window,
  476. atom,
  477. 1/*offset*/,
  478. 1/*length*/,
  479. False/*delete*/,
  480. AnyPropertyType,
  481. &userType/*actual_type_return*/,
  482. &userSize/*actual_format_return*/,
  483. &userCount/*nitems_return*/,
  484. &bytes/*bytes_after_return / partial reads*/,
  485. &data);
  486. printf("xxx lglw_linux: loc_getProperty: UPPER userSize=%d userCount=%lu bytes=%lu data=%p\n", userSize, userCount, bytes, data);
  487. if(NULL != data)
  488. {
  489. // upper 32-bit
  490. uptr.ui[1] = *(long*)data;
  491. printf("xxx upper=0x%08x\n", uptr.ui[1]);
  492. XFree(data);
  493. }
  494. }
  495. }
  496. }
  497. printf("xxx lglw_linux: loc_getProperty: return value=%p\n", uptr.any);
  498. return uptr.any;
  499. }
  500. #ifdef ARCH_X64
  501. #if 0
  502. // Pulled from the Renoise 64-bit callback example
  503. // Unsure what data was supposed to be, but swapping it to a function name did not work
  504. // This does nothing, no event proc found
  505. static void loc_setEventProc (Display *display, Window window) {
  506. size_t data = (size_t)loc_eventProc;
  507. long temp[2];
  508. printf("vstgltest<lglw_linux>: setEventProc (2*32bit). window=%lu loc_eventProc=%p\n", window, &loc_eventProc);
  509. // Split the 64 bit pointer into a little-endian unsigned int array
  510. temp[0] = (uint32_t)(data & 0xffffffffUL);
  511. temp[1] = (uint32_t)(data >> 32L);
  512. Atom atom = XInternAtom(display, "_XEventProc", False);
  513. XChangeProperty(display, window,
  514. atom/*property*/,
  515. atom/*type*/,
  516. 32/*format*/,
  517. PropModeReplace/*mode*/,
  518. (unsigned char*)temp/*data*/,
  519. 2/*nelements*/
  520. );
  521. }
  522. #else
  523. // GPL code pulled from the amsynth example <https://github.com/amsynth/amsynth/blob/4a87798e650c6d71d70274a961c9b8d98fc6da7e/src/amsynth_vst.cpp>
  524. // Simply swapped out the function names, crashes Ardour in the same was as the others
  525. static void loc_setEventProc (Display *display, Window window) {
  526. //
  527. // JUCE calls XGetWindowProperty with long_length = 1 which means it only fetches the lower 32 bits of the address.
  528. // Therefore we need to ensure we return an address in the lower 32-bits of address space.
  529. //
  530. // based on mach_override
  531. static const unsigned char kJumpInstructions[] = {
  532. 0xFF, 0x25, 0x00, 0x00, 0x00, 0x00,
  533. 0x00, 0x00, 0x00, 0x00,
  534. 0x00, 0x00, 0x00, 0x00
  535. };
  536. static const int kJumpAddress = 6;
  537. static char *ptr = 0;
  538. if (!ptr) {
  539. ptr = (char *)mmap(0,
  540. getpagesize()/*PAGE_SIZE*/,
  541. PROT_READ | PROT_WRITE | PROT_EXEC,
  542. MAP_ANONYMOUS | MAP_PRIVATE | MAP_32BIT,
  543. 0, 0);
  544. if (ptr == MAP_FAILED) {
  545. perror("mmap");
  546. ptr = 0;
  547. return;
  548. } else {
  549. memcpy(ptr, kJumpInstructions, sizeof(kJumpInstructions));
  550. *((uint64_t *)(ptr + kJumpAddress)) = (uint64_t)(&loc_eventProc);
  551. msync(ptr, sizeof(kJumpInstructions), MS_INVALIDATE);
  552. printf("vstgltest<lglw_linux>: 64bit trampoline installed\n");
  553. }
  554. }
  555. long temp[2] = {(uint32_t)(((size_t)ptr)&0xFFFFfffful), 0};
  556. Atom atom = XInternAtom(display, "_XEventProc", False);
  557. XChangeProperty(display, window,
  558. atom/*property*/,
  559. atom/*type*/,
  560. 32/*format*/,
  561. PropModeReplace/*mode*/,
  562. (unsigned char *)temp/*data*/,
  563. 2/*nelements*/
  564. );
  565. }
  566. #endif
  567. #else
  568. // Pulled from the eXT2 example
  569. static void loc_setEventProc (Display *display, Window window) {
  570. void* data = (void*)&loc_eventProc; // swapped the function name here
  571. // (note) 32-bit only
  572. Atom atom = XInternAtom(display, "_XEventProc", False);
  573. XChangeProperty(display, window,
  574. atom/*property*/,
  575. atom/*type*/,
  576. 32/*format*/,
  577. PropModeReplace/*mode*/,
  578. (unsigned char*)&data/*data*/,
  579. 1/*nelements*/
  580. );
  581. }
  582. #endif // ARCH_X64
  583. // ---------------------------------------------------------------------------- lglw_window_open
  584. lglw_bool_t lglw_window_open (lglw_t _lglw, void *_parentHWNDOrNull, int32_t _x, int32_t _y, int32_t _w, int32_t _h) {
  585. lglw_bool_t r = LGLW_FALSE;
  586. LGLW(_lglw);
  587. if(NULL != lglw)
  588. {
  589. lglw_log("lglw:lglw_window_open: 1, %p, %i \n", (Window)_parentHWNDOrNull, (Window)_parentHWNDOrNull);
  590. lglw->parent_xwnd = (0 == _parentHWNDOrNull) ? DefaultRootWindow(lglw->xdsp) : (Window)_parentHWNDOrNull;
  591. lglw_log("lglw:lglw_window_open: 2 lglw=%p\n", lglw);
  592. if(_w <= 16)
  593. _w = lglw->hidden.size.x;
  594. lglw_log("lglw:lglw_window_open: 3\n");
  595. if(_h <= 16)
  596. _h = lglw->hidden.size.y;
  597. // TODO: compare to 'WindowClass' from Windows implementation
  598. lglw_log("lglw:lglw_window_open: 4\n");
  599. XSetWindowAttributes swa;
  600. // // XEvent event;
  601. XSync(lglw->xdsp, False);
  602. #if 1
  603. lglw_log("lglw:lglw_window_open: 5\n");
  604. swa.border_pixel = 0;
  605. swa.colormap = lglw->cmap;
  606. // (note) [bsp] setting this to NoEventMask causes all events to be propagated to the parent (host) window.
  607. // The host then reports the event to the plugin by calling its eventProc function (set via "_XEventProc").
  608. swa.event_mask = NoEventMask;/////ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask | ButtonMotionMask | FocusChangeMask;
  609. lglw->win.xwnd = XCreateWindow(lglw->xdsp/*display*/,
  610. DefaultRootWindow(lglw->xdsp)/*parent. see Cameron's comment below.*/,
  611. 0/*x*/,
  612. 0/*y*/,
  613. _w/*width*/,
  614. _h/*height*/,
  615. 0/*border_width*/,
  616. CopyFromParent/*depth*/,
  617. InputOutput/*class*/,
  618. lglw->vi->visual,
  619. CWBorderPixel | CWColormap | CWEventMask/*value_mask*/,
  620. &swa/*attributes*/
  621. );
  622. lglw_log("lglw:lglw_window_open: 6\n");
  623. XSetStandardProperties(lglw->xdsp/*display*/,
  624. lglw->win.xwnd/*window*/,
  625. "LGLW"/*window_name*/,
  626. "LGLW"/*icon_name*/,
  627. None/*icon_pixmap*/,
  628. NULL/*argv*/,
  629. 0/*argc*/,
  630. NULL/*XSizeHints*/
  631. );
  632. // Setup the event proc now, on the parent window as well just for the debug host
  633. // It was simpler to do this than check in the debug host for the reparent event
  634. lglw_log("lglw:lglw_window_open: 7\n");
  635. loc_setEventProc(lglw->xdsp, lglw->win.xwnd);
  636. loc_setProperty(lglw->xdsp, lglw->win.xwnd, "_lglw", (void*)lglw); // set instance pointer
  637. if(0 != _parentHWNDOrNull)
  638. {
  639. loc_setEventProc(lglw->xdsp, lglw->parent_xwnd);
  640. loc_setProperty(lglw->xdsp, lglw->parent_xwnd, "_lglw", (void*)lglw); // set instance pointer
  641. }
  642. // Some hosts only check and store the callback when the Window is reparented
  643. // Since creating the Window with a Parent may or may not do that, but the callback is not set,
  644. // ... it's created as a root window, the callback is set, and then it's reparented
  645. #if 1
  646. // (note) [cameronleger] In Ardour's code-base, the only time it looks for the _XEventProc is during a ReparentNotify event
  647. if (0 != _parentHWNDOrNull)
  648. {
  649. lglw_log("lglw:lglw_window_open: 8\n");
  650. XReparentWindow(lglw->xdsp, lglw->win.xwnd, lglw->parent_xwnd, 0, 0);
  651. }
  652. #endif
  653. lglw->win.b_owner = LGLW_TRUE;
  654. #else
  655. lglw->win.xwnd = (Window)_parentHWNDOrNull;
  656. lglw->win.b_owner = LGLW_FALSE;
  657. #endif
  658. lglw_log("lglw:lglw_window_open: 9\n");
  659. if(lglw->win.b_owner)
  660. {
  661. // // XMapRaised(lglw->xdsp, lglw->win.xwnd);
  662. XMapWindow(lglw->xdsp, lglw->win.xwnd);
  663. }
  664. #if 0
  665. XSelectInput(lglw->xdsp, lglw->win.xwnd,
  666. ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask | ButtonMotionMask | FocusChangeMask
  667. );
  668. XGrabKeyboard(lglw->xdsp, lglw->win.xwnd,
  669. False/*owner_events*/,
  670. GrabModeAsync/*pointer_mode*/,
  671. GrabModeAsync/*keyboard_mode*/,
  672. CurrentTime/*time*/
  673. );
  674. #endif
  675. #if 0
  676. XSetInputFocus(lglw->xdsp/*display*/,
  677. PointerRoot/*focus*/,
  678. RevertToPointerRoot/*revert_to*/,
  679. CurrentTime
  680. );
  681. #endif
  682. XSync(lglw->xdsp, False);
  683. lglw->win.mapped = LGLW_TRUE;
  684. lglw_log("lglw:lglw_window_open: 10\n");
  685. lglw->win.size.x = _w;
  686. lglw->win.size.y = _h;
  687. lglw_log("lglw:lglw_window_open: 11\n");
  688. loc_enable_dropfiles(lglw, (NULL != lglw->dropfiles.cbk));
  689. lglw_log("lglw:lglw_window_open: EXIT\n");
  690. r = LGLW_TRUE;
  691. }
  692. return r;
  693. }
  694. // ---------------------------------------------------------------------------- lglw_window_resize
  695. lglw_bool_t lglw_window_resize (lglw_t _lglw, int32_t _w, int32_t _h) {
  696. lglw_bool_t r = LGLW_FALSE;
  697. LGLW(_lglw);
  698. if(NULL != lglw)
  699. {
  700. if(0 != lglw->win.xwnd)
  701. {
  702. lglw_log("lglw:lglw_window_resize: 1\n");
  703. r = LGLW_TRUE;
  704. lglw_log("lglw:lglw_window_resize: 2\n");
  705. XResizeWindow(lglw->xdsp, lglw->win.xwnd, _w, _h);
  706. XRaiseWindow(lglw->xdsp, lglw->win.xwnd);
  707. lglw_log("lglw:lglw_window_resize: 3\n");
  708. int deltaW = _w - lglw->win.size.x;
  709. int deltaH = _h - lglw->win.size.y;
  710. lglw_log("lglw:lglw_window_resize: 4\n");
  711. lglw->win.size.x = _w;
  712. lglw->win.size.y = _h;
  713. lglw_log("lglw:lglw_window_resize: 5\n");
  714. Window root, parent, *children = NULL;
  715. unsigned int num_children;
  716. lglw_log("lglw:lglw_window_resize: 6\n");
  717. if(!XQueryTree(lglw->xdsp, lglw->win.xwnd, &root, &parent, &children, &num_children))
  718. return r;
  719. lglw_log("lglw:lglw_window_resize: 7\n");
  720. if(children)
  721. XFree((char *)children);
  722. lglw_log("lglw:lglw_window_resize: 8\n");
  723. // Resize parent window (if any)
  724. if(0 != parent)
  725. {
  726. lglw_log("lglw:lglw_window_resize: 8.1\n");
  727. int x, y;
  728. unsigned int width, height;
  729. unsigned int border_width;
  730. unsigned int depth;
  731. lglw_log("lglw:lglw_window_resize: 8.2\n");
  732. if(!XGetGeometry(lglw->xdsp, lglw->win.xwnd, &root, &x, &y, &width, &height, &border_width, &depth))
  733. return r;
  734. lglw_log("lglw:lglw_window_resize: 8.3\n");
  735. XResizeWindow(lglw->xdsp, parent, width + deltaW, height + deltaH);
  736. }
  737. lglw_log("lglw:lglw_window_resize: EXIT\n");
  738. }
  739. }
  740. return r;
  741. }
  742. // ---------------------------------------------------------------------------- lglw_window_close
  743. void lglw_window_close (lglw_t _lglw) {
  744. LGLW(_lglw);
  745. if(NULL != lglw)
  746. {
  747. if(0 != lglw->win.xwnd)
  748. {
  749. lglw_log("lglw:lglw_window_close: 1\n");
  750. lglw_timer_stop(_lglw);
  751. lglw_log("lglw:lglw_window_close: 2\n");
  752. loc_key_unhook(lglw);
  753. lglw_log("lglw:lglw_window_close: 3\n");
  754. glXMakeCurrent(lglw->xdsp, None, NULL);
  755. lglw_log("lglw:lglw_window_close: 4\n");
  756. if(lglw->win.b_owner)
  757. {
  758. XDestroyWindow(lglw->xdsp, lglw->win.xwnd);
  759. lglw->win.b_owner = LGLW_FALSE;
  760. }
  761. XSync(lglw->xdsp, False);
  762. lglw->win.xwnd = 0;
  763. lglw->win.mapped = LGLW_FALSE;
  764. }
  765. }
  766. lglw_log("lglw:lglw_window_close: EXIT\n");
  767. }
  768. // ---------------------------------------------------------------------------- lglw_window_show
  769. void lglw_window_show(lglw_t _lglw) {
  770. LGLW(_lglw);
  771. if(NULL != lglw)
  772. {
  773. lglw_log("lglw:lglw_window_show: 1\n");
  774. XMapRaised(lglw->xdsp, lglw->win.xwnd);
  775. lglw->win.mapped = LGLW_TRUE;
  776. }
  777. lglw_log("lglw:lglw_window_show: EXIT\n");
  778. }
  779. // ---------------------------------------------------------------------------- lglw_window_hide
  780. void lglw_window_hide(lglw_t _lglw) {
  781. LGLW(_lglw);
  782. if(NULL != lglw)
  783. {
  784. lglw_log("lglw:lglw_window_hide: 1\n");
  785. XUnmapWindow(lglw->xdsp, lglw->win.xwnd);
  786. lglw->win.mapped = LGLW_FALSE;
  787. }
  788. lglw_log("lglw:lglw_window_hide: EXIT\n");
  789. }
  790. // ---------------------------------------------------------------------------- lglw_window_is_visible
  791. lglw_bool_t lglw_window_is_visible(lglw_t _lglw) {
  792. lglw_bool_t r = LGLW_FALSE;
  793. LGLW(_lglw);
  794. // lglw_log("lglw:lglw_window_is_visible: 1\n");
  795. if(NULL != lglw && 0 != lglw->win.xwnd)
  796. {
  797. // lglw_log("lglw:lglw_window_is_visible: 2\n");
  798. r = lglw->win.mapped;
  799. }
  800. // lglw_log("lglw:lglw_window_is_visible: EXIT\n");
  801. return r;
  802. }
  803. // ---------------------------------------------------------------------------- lglw_window_size_get
  804. void lglw_window_size_get(lglw_t _lglw, int32_t *_retX, int32_t *_retY) {
  805. LGLW(_lglw);
  806. lglw_log("lglw:lglw_window_size_get: 1\n");
  807. if(NULL != lglw)
  808. {
  809. if(0 != lglw->win.xwnd)
  810. {
  811. if(NULL != _retX)
  812. *_retX = lglw->win.size.x;
  813. if(NULL != _retY)
  814. *_retY = lglw->win.size.y;
  815. }
  816. }
  817. lglw_log("lglw:lglw_window_size_get: EXIT\n");
  818. }
  819. // ---------------------------------------------------------------------------- lglw_redraw
  820. void lglw_redraw(lglw_t _lglw) {
  821. LGLW(_lglw);
  822. // (todo) implement me
  823. if(NULL != lglw)
  824. {
  825. if(0 != lglw->win.xwnd)
  826. {
  827. // TODO Event Loop
  828. lglw_log("lglw:lglw_redraw: 1\n");
  829. XEvent xev;
  830. xev.xany.type = Expose;
  831. xev.xany.serial = 0;
  832. xev.xany.send_event = True;
  833. xev.xany.display = lglw->xdsp;
  834. xev.xany.window = lglw->win.xwnd;
  835. xev.xexpose.x = 0;
  836. xev.xexpose.y = 0;
  837. xev.xexpose.width = lglw->win.size.x;
  838. xev.xexpose.height = lglw->win.size.y;
  839. xev.xexpose.count = 0;
  840. XSendEvent(lglw->xdsp, lglw->win.xwnd,
  841. True/*propagate*/,
  842. ExposureMask/*event_mask*/,
  843. &xev
  844. );
  845. XFlush(lglw->xdsp);
  846. }
  847. }
  848. }
  849. // ---------------------------------------------------------------------------- lglw_redraw_callback_set
  850. void lglw_redraw_callback_set(lglw_t _lglw, lglw_redraw_fxn_t _cbk) {
  851. LGLW(_lglw);
  852. if(NULL != lglw)
  853. {
  854. lglw->redraw.cbk = _cbk;
  855. }
  856. }
  857. // ---------------------------------------------------------------------------- lglw_glcontext_push
  858. void lglw_glcontext_push(lglw_t _lglw) {
  859. LGLW(_lglw);
  860. if(NULL != lglw)
  861. {
  862. lglw->prev.drw = glXGetCurrentDrawable();
  863. lglw->prev.ctx = glXGetCurrentContext();
  864. // lglw_log("lglw:lglw_glcontext_push: win.xwnd=%p hidden.xwnd=%p ctx=%p\n",
  865. // lglw->win.xwnd, lglw->hidden.xwnd, lglw->ctx);
  866. if(!glXMakeCurrent(lglw->xdsp, (0 == lglw->win.xwnd) ? lglw->hidden.xwnd : lglw->win.xwnd, lglw->ctx))
  867. {
  868. 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());
  869. }
  870. }
  871. }
  872. // ---------------------------------------------------------------------------- lglw_glcontext_pop
  873. void lglw_glcontext_pop(lglw_t _lglw) {
  874. LGLW(_lglw);
  875. if(NULL != lglw)
  876. {
  877. // lglw_log("lglw:lglw_glcontext_pop: prev.drw=%p prev.ctx=%p\n",
  878. // lglw->prev.drw, lglw->prev.ctx);
  879. if(!glXMakeCurrent(lglw->xdsp, lglw->prev.drw, lglw->prev.ctx))
  880. {
  881. lglw_log("[---] lglw_glcontext_pop: glXMakeCurrent() failed. prev.drw=%p ctx=%p glGetError()=%d\n", lglw->prev.drw, lglw->prev.ctx, glGetError());
  882. }
  883. }
  884. }
  885. // ---------------------------------------------------------------------------- lglw_swap_buffers
  886. void lglw_swap_buffers(lglw_t _lglw) {
  887. LGLW(_lglw);
  888. if(NULL != lglw)
  889. {
  890. if(0 != lglw->win.xwnd)
  891. {
  892. // lglw_log("lglw:lglw_swap_buffers: 1\n");
  893. glXSwapBuffers(lglw->xdsp, lglw->win.xwnd);
  894. }
  895. }
  896. }
  897. // ---------------------------------------------------------------------------- lglw_swap_interval_set
  898. typedef void (APIENTRY *PFNWGLEXTSWAPINTERVALPROC) (int);
  899. void lglw_swap_interval_set(lglw_t _lglw, int32_t _ival) {
  900. LGLW(_lglw);
  901. if(NULL != lglw)
  902. {
  903. lglw_log("lglw:lglw_swap_interval_set: 1\n");
  904. PFNWGLEXTSWAPINTERVALPROC glXSwapIntervalEXT;
  905. glXSwapIntervalEXT = (PFNWGLEXTSWAPINTERVALPROC) glXGetProcAddress((const GLubyte*)"glXSwapIntervalEXT");
  906. if(NULL != glXSwapIntervalEXT)
  907. {
  908. lglw_log("lglw:lglw_swap_interval_set: 2\n");
  909. glXSwapIntervalEXT(_ival);
  910. lglw->win.swap_interval = _ival;
  911. }
  912. }
  913. }
  914. // ---------------------------------------------------------------------------- lglw_swap_interval_get
  915. int32_t lglw_swap_interval_get(lglw_t _lglw) {
  916. LGLW(_lglw);
  917. int32_t r = 0;
  918. if(NULL != lglw)
  919. {
  920. r = lglw->win.swap_interval;
  921. }
  922. return r;
  923. }
  924. // ---------------------------------------------------------------------------- loc_key_hook
  925. static void loc_key_hook(lglw_int_t *lglw) {
  926. loc_key_unhook(lglw);
  927. // (todo) implement me
  928. khook_lglw = lglw;
  929. }
  930. // ---------------------------------------------------------------------------- loc_key_unhook
  931. static void loc_key_unhook(lglw_int_t *lglw) {
  932. // (todo) implement me
  933. if(khook_lglw == lglw)
  934. khook_lglw = NULL;
  935. }
  936. // ---------------------------------------------------------------------------- loc_handle_mouseleave
  937. static void loc_handle_mouseleave(lglw_int_t *lglw) {
  938. loc_key_unhook(lglw);
  939. lglw->focus.state &= ~LGLW_FOCUS_MOUSE;
  940. if(NULL != lglw->focus.cbk)
  941. {
  942. lglw->focus.cbk(lglw, lglw->focus.state, LGLW_FOCUS_MOUSE);
  943. }
  944. lglw_log("xxx lglw:loc_handle_mouseleave: LEAVE\n");
  945. }
  946. // ---------------------------------------------------------------------------- loc_handle_mouseenter
  947. static void loc_handle_mouseenter(lglw_int_t *lglw) {
  948. loc_key_hook(lglw);
  949. lglw->focus.state |= LGLW_FOCUS_MOUSE;
  950. if(NULL != lglw->focus.cbk)
  951. {
  952. lglw->focus.cbk(lglw, lglw->focus.state, LGLW_FOCUS_MOUSE);
  953. }
  954. lglw_log("xxx lglw:loc_handle_mouseenter: LEAVE\n");
  955. }
  956. // ---------------------------------------------------------------------------- loc_handle_mousebutton
  957. static void loc_handle_mousebutton(lglw_int_t *lglw, lglw_bool_t _bPressed, uint32_t _button) {
  958. if(_bPressed)
  959. lglw->mouse.button_state |= _button;
  960. else
  961. lglw->mouse.button_state &= ~_button;
  962. if(NULL != lglw->mouse.cbk)
  963. {
  964. lglw->mouse.cbk(lglw, lglw->mouse.p.x, lglw->mouse.p.y, lglw->mouse.button_state, _button);
  965. }
  966. }
  967. // ---------------------------------------------------------------------------- loc_handle_mousemotion
  968. static void loc_handle_mousemotion(lglw_int_t *lglw) {
  969. if(NULL != lglw->mouse.cbk)
  970. {
  971. lglw->mouse.cbk(lglw, lglw->mouse.p.x, lglw->mouse.p.y, lglw->mouse.button_state, 0u/*changedbuttonstate*/);
  972. }
  973. }
  974. // ---------------------------------------------------------------------------- lglw_mouse_callback_set
  975. void lglw_mouse_callback_set(lglw_t _lglw, lglw_mouse_fxn_t _cbk) {
  976. LGLW(_lglw);
  977. if(NULL != lglw)
  978. {
  979. lglw->mouse.cbk = _cbk;
  980. }
  981. }
  982. // ---------------------------------------------------------------------------- lglw_mouse_callback_set
  983. void lglw_focus_callback_set(lglw_t _lglw, lglw_focus_fxn_t _cbk) {
  984. LGLW(_lglw);
  985. if(NULL != lglw)
  986. {
  987. lglw->focus.cbk = _cbk;
  988. }
  989. }
  990. // ---------------------------------------------------------------------------- loc_handle_key
  991. static lglw_bool_t loc_handle_key(lglw_int_t *lglw, lglw_bool_t _bPressed, uint32_t _vkey) {
  992. lglw_bool_t r = LGLW_FALSE;
  993. if(NULL != lglw->keyboard.cbk)
  994. {
  995. r = lglw->keyboard.cbk(lglw, _vkey, lglw->keyboard.kmod_state, _bPressed);
  996. }
  997. return r;
  998. }
  999. // ---------------------------------------------------------------------------- lglw_keyboard_callback_set
  1000. void lglw_keyboard_callback_set(lglw_t _lglw, lglw_keyboard_fxn_t _cbk) {
  1001. LGLW(_lglw);
  1002. if(NULL != lglw)
  1003. {
  1004. lglw->keyboard.cbk = _cbk;
  1005. }
  1006. }
  1007. // ---------------------------------------------------------------------------- lglw_keyboard_get_modifiers
  1008. uint32_t lglw_keyboard_get_modifiers(lglw_t _lglw) {
  1009. uint32_t r = 0u;
  1010. LGLW(_lglw);
  1011. if(NULL != lglw)
  1012. {
  1013. r = lglw->keyboard.kmod_state;
  1014. }
  1015. return r;
  1016. }
  1017. // ---------------------------------------------------------------------------- lglw_touchkeyboard_show
  1018. void lglw_touchkeyboard_show(lglw_t _lglw, lglw_bool_t _bEnable) {
  1019. LGLW(_lglw);
  1020. // (todo) implement me
  1021. if(NULL != lglw)
  1022. {
  1023. }
  1024. }
  1025. // ---------------------------------------------------------------------------- lglw_mouse_get_buttons
  1026. uint32_t lglw_mouse_get_buttons(lglw_t _lglw) {
  1027. uint32_t r = 0u;
  1028. LGLW(_lglw);
  1029. if(NULL != lglw)
  1030. {
  1031. r = lglw->mouse.button_state;
  1032. }
  1033. return r;
  1034. }
  1035. // ---------------------------------------------------------------------------- lglw_mouse_grab
  1036. void lglw_mouse_grab(lglw_t _lglw, uint32_t _grabMode) {
  1037. LGLW(_lglw);
  1038. if(NULL != lglw)
  1039. {
  1040. if(0 != lglw->win.xwnd)
  1041. {
  1042. if(!lglw->mouse.touch.b_enable)
  1043. {
  1044. if(lglw->mouse.grab.mode != _grabMode)
  1045. {
  1046. lglw_mouse_ungrab(_lglw);
  1047. }
  1048. int result;
  1049. switch(_grabMode)
  1050. {
  1051. default:
  1052. case LGLW_MOUSE_GRAB_NONE:
  1053. break;
  1054. case LGLW_MOUSE_GRAB_CAPTURE:
  1055. result = XGrabPointer(lglw->xdsp, lglw->win.xwnd,
  1056. True/*owner_events*/,
  1057. ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask | PointerMotionHintMask | ButtonMotionMask | KeymapStateMask/*event_mask*/,
  1058. GrabModeAsync/*pointer_mode*/,
  1059. GrabModeAsync/*keyboard_mode*/,
  1060. lglw->win.xwnd/*confine_to*/,
  1061. None/*cursor*/,
  1062. CurrentTime/*time*/);
  1063. if(GrabSuccess != result)
  1064. {
  1065. printf("vstgltest<lglw_linux>: Grab Result: %i\n", result);
  1066. }
  1067. else
  1068. {
  1069. lglw->mouse.grab.mode = _grabMode;
  1070. }
  1071. break;
  1072. case LGLW_MOUSE_GRAB_WARP:
  1073. result = XGrabPointer(lglw->xdsp, lglw->win.xwnd,
  1074. True/*owner_events*/,
  1075. ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask | PointerMotionHintMask | ButtonMotionMask | KeymapStateMask/*event_mask*/,
  1076. GrabModeAsync/*pointer_mode*/,
  1077. GrabModeAsync/*keyboard_mode*/,
  1078. lglw->win.xwnd/*confine_to*/,
  1079. None/*cursor*/,
  1080. CurrentTime/*time*/);
  1081. if(GrabSuccess != result)
  1082. {
  1083. printf("vstgltest<lglw_linux>: Grab Result: %i\n", result);
  1084. }
  1085. else
  1086. {
  1087. lglw_mouse_cursor_show(_lglw, LGLW_FALSE);
  1088. lglw->mouse.grab.p = lglw->mouse.p;
  1089. lglw->mouse.grab.last_p = lglw->mouse.p;
  1090. lglw->mouse.grab.mode = _grabMode;
  1091. }
  1092. break;
  1093. }
  1094. }
  1095. }
  1096. }
  1097. }
  1098. // ---------------------------------------------------------------------------- lglw_mouse_ungrab
  1099. void lglw_mouse_ungrab(lglw_t _lglw) {
  1100. LGLW(_lglw);
  1101. if(NULL != lglw)
  1102. {
  1103. if(0 != lglw->win.xwnd)
  1104. {
  1105. if(!lglw->mouse.touch.b_enable)
  1106. {
  1107. switch(lglw->mouse.grab.mode)
  1108. {
  1109. default:
  1110. case LGLW_MOUSE_GRAB_NONE:
  1111. break;
  1112. case LGLW_MOUSE_GRAB_CAPTURE:
  1113. XUngrabPointer(lglw->xdsp, CurrentTime);
  1114. lglw->mouse.grab.mode = LGLW_MOUSE_GRAB_NONE;
  1115. break;
  1116. case LGLW_MOUSE_GRAB_WARP:
  1117. XUngrabPointer(lglw->xdsp, CurrentTime);
  1118. lglw->mouse.grab.mode = LGLW_MOUSE_GRAB_NONE;
  1119. lglw->mouse.grab.b_queue_warp = LGLW_TRUE;
  1120. lglw_mouse_cursor_show(_lglw, LGLW_TRUE);
  1121. break;
  1122. }
  1123. }
  1124. }
  1125. }
  1126. }
  1127. // ---------------------------------------------------------------------------- lglw_mouse_warp
  1128. void lglw_mouse_warp(lglw_t _lglw, int32_t _x, int32_t _y) {
  1129. LGLW(_lglw);
  1130. if(NULL != lglw)
  1131. {
  1132. if(0 != lglw->win.xwnd)
  1133. {
  1134. XWarpPointer(lglw->xdsp,
  1135. None/*src_w*/,
  1136. lglw->win.xwnd/*dest_w*/,
  1137. 0/*src_x*/,
  1138. 0/*src_y*/,
  1139. 0/*src_width*/,
  1140. 0/*src_height*/,
  1141. _x/*dest_x*/,
  1142. _y/*dest_y*/);
  1143. }
  1144. }
  1145. }
  1146. // ---------------------------------------------------------------------------- loc_handle_queued_mouse_warp
  1147. static void loc_handle_queued_mouse_warp(lglw_int_t *lglw) {
  1148. if(lglw->mouse.grab.b_queue_warp)
  1149. {
  1150. lglw->mouse.grab.b_queue_warp = LGLW_FALSE;
  1151. lglw_mouse_warp(lglw, lglw->mouse.grab.p.x, lglw->mouse.grab.p.y);
  1152. lglw->mouse.grab.last_p = lglw->mouse.grab.p;
  1153. }
  1154. }
  1155. // ---------------------------------------------------------------------------- lglw_mouse_cursor_show
  1156. void lglw_mouse_cursor_show (lglw_t _lglw, lglw_bool_t _bShow) {
  1157. LGLW(_lglw);
  1158. if(NULL != lglw)
  1159. {
  1160. if(LGLW_FALSE == _bShow)
  1161. {
  1162. Pixmap noPxm;
  1163. Cursor noCursor;
  1164. XColor black, dummy;
  1165. static char pxmNoData[] = {0, 0, 0, 0, 0, 0, 0, 0};
  1166. XAllocNamedColor(lglw->xdsp, lglw->cmap, "black", &black, &dummy);
  1167. noPxm = XCreateBitmapFromData(lglw->xdsp, lglw->win.xwnd, pxmNoData, 8, 8);
  1168. noCursor = XCreatePixmapCursor(lglw->xdsp, noPxm, noPxm, &black, &black, 0, 0);
  1169. XDefineCursor(lglw->xdsp, lglw->win.xwnd, noCursor);
  1170. XFreeCursor(lglw->xdsp, noCursor);
  1171. if(noPxm != None)
  1172. {
  1173. XFreePixmap(lglw->xdsp, noPxm);
  1174. }
  1175. }
  1176. else
  1177. {
  1178. XUndefineCursor(lglw->xdsp, lglw->win.xwnd);
  1179. }
  1180. }
  1181. }
  1182. // ---------------------------------------------------------------------------- lglw_timer_start
  1183. void lglw_timer_start(lglw_t _lglw, uint32_t _millisec) {
  1184. LGLW(_lglw);
  1185. // (todo) implement me
  1186. if(NULL != lglw)
  1187. {
  1188. }
  1189. }
  1190. // ---------------------------------------------------------------------------- lglw_timer_stop
  1191. void lglw_timer_stop(lglw_t _lglw) {
  1192. LGLW(_lglw);
  1193. // (todo) implement me
  1194. if(NULL != lglw)
  1195. {
  1196. }
  1197. }
  1198. // ---------------------------------------------------------------------------- lglw_timer_callback_set
  1199. void lglw_timer_callback_set(lglw_t _lglw, lglw_timer_fxn_t _cbk) {
  1200. LGLW(_lglw);
  1201. if(NULL != lglw)
  1202. {
  1203. lglw->timer.cbk = _cbk;
  1204. }
  1205. }
  1206. // ---------------------------------------------------------------------------- loc_enable_dropfiles
  1207. static void loc_enable_dropfiles(lglw_int_t *lglw, lglw_bool_t _bEnable) {
  1208. // (todo) implement me
  1209. }
  1210. // ---------------------------------------------------------------------------- lglw_dropfiles_callback_set
  1211. void lglw_dropfiles_callback_set(lglw_t _lglw, lglw_dropfiles_fxn_t _cbk) {
  1212. LGLW(_lglw);
  1213. if(NULL != _lglw)
  1214. {
  1215. lglw->dropfiles.cbk = _cbk;
  1216. loc_enable_dropfiles(lglw, (NULL != _cbk));
  1217. }
  1218. }
  1219. // ---------------------------------------------------------------------------- loc_touchinput_update
  1220. static void loc_touchinput_update(lglw_int_t *lglw) {
  1221. // (todo) implement me
  1222. }
  1223. // ---------------------------------------------------------------------------- lglw_touchinput_set
  1224. void lglw_touchinput_set(lglw_t _lglw, lglw_bool_t _bEnable) {
  1225. LGLW(_lglw);
  1226. if(NULL != _lglw)
  1227. {
  1228. lglw->mouse.touch.b_enable = _bEnable;
  1229. lglw->mouse.touch.b_update_queued = LGLW_TRUE;
  1230. }
  1231. }
  1232. // ---------------------------------------------------------------------------- lglw_touchinput_get
  1233. lglw_bool_t lglw_touchinput_get(lglw_t _lglw) {
  1234. lglw_bool_t r = LGLW_FALSE;
  1235. LGLW(_lglw);
  1236. if(NULL != _lglw)
  1237. {
  1238. r = lglw->mouse.touch.b_enable;
  1239. }
  1240. return r;
  1241. }
  1242. // ---------------------------------------------------------------------------- lglw_clipboard_text_set
  1243. void lglw_clipboard_text_set(lglw_t _lglw, const uint32_t _numChars, const char *_text) {
  1244. LGLW(_lglw);
  1245. (void)_numChars;
  1246. // (todo) implement me
  1247. if(NULL != _text)
  1248. {
  1249. (void)lglw;
  1250. }
  1251. }
  1252. // ---------------------------------------------------------------------------- lglw_clipboard_text_get
  1253. void lglw_clipboard_text_get(lglw_t _lglw, uint32_t _maxChars, uint32_t *_retNumChars, char *_retText) {
  1254. LGLW(_lglw);
  1255. if(NULL != _retNumChars)
  1256. *_retNumChars = 0u;
  1257. if(NULL != _retText)
  1258. *_retText = 0;
  1259. if(_maxChars > 0u)
  1260. {
  1261. if(NULL != _lglw)
  1262. {
  1263. // (todo) implement me
  1264. (void)lglw;
  1265. }
  1266. }
  1267. }