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.

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