SDL_x11events.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #if SDL_VIDEO_DRIVER_X11
  20. #include <sys/types.h>
  21. #include <sys/time.h>
  22. #include <signal.h>
  23. #include <unistd.h>
  24. #include <limits.h> /* For INT_MAX */
  25. #include "SDL_x11video.h"
  26. #include "SDL_x11touch.h"
  27. #include "SDL_x11xinput2.h"
  28. #include "SDL_x11xfixes.h"
  29. #include "../../core/unix/SDL_poll.h"
  30. #include "../../events/SDL_events_c.h"
  31. #include "../../events/SDL_mouse_c.h"
  32. #include "../../events/SDL_touch_c.h"
  33. #include "SDL_hints.h"
  34. #include "SDL_timer.h"
  35. #include "SDL_syswm.h"
  36. #include <stdio.h>
  37. /*#define DEBUG_XEVENTS*/
  38. #ifndef _NET_WM_MOVERESIZE_SIZE_TOPLEFT
  39. #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
  40. #endif
  41. #ifndef _NET_WM_MOVERESIZE_SIZE_TOP
  42. #define _NET_WM_MOVERESIZE_SIZE_TOP 1
  43. #endif
  44. #ifndef _NET_WM_MOVERESIZE_SIZE_TOPRIGHT
  45. #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
  46. #endif
  47. #ifndef _NET_WM_MOVERESIZE_SIZE_RIGHT
  48. #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
  49. #endif
  50. #ifndef _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT
  51. #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
  52. #endif
  53. #ifndef _NET_WM_MOVERESIZE_SIZE_BOTTOM
  54. #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
  55. #endif
  56. #ifndef _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT
  57. #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
  58. #endif
  59. #ifndef _NET_WM_MOVERESIZE_SIZE_LEFT
  60. #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
  61. #endif
  62. #ifndef _NET_WM_MOVERESIZE_MOVE
  63. #define _NET_WM_MOVERESIZE_MOVE 8
  64. #endif
  65. typedef struct {
  66. unsigned char *data;
  67. int format, count;
  68. Atom type;
  69. } SDL_x11Prop;
  70. /* Reads property
  71. Must call X11_XFree on results
  72. */
  73. static void X11_ReadProperty(SDL_x11Prop *p, Display *disp, Window w, Atom prop)
  74. {
  75. unsigned char *ret=NULL;
  76. Atom type;
  77. int fmt;
  78. unsigned long count;
  79. unsigned long bytes_left;
  80. int bytes_fetch = 0;
  81. do {
  82. if (ret != 0) X11_XFree(ret);
  83. X11_XGetWindowProperty(disp, w, prop, 0, bytes_fetch, False, AnyPropertyType, &type, &fmt, &count, &bytes_left, &ret);
  84. bytes_fetch += bytes_left;
  85. } while (bytes_left != 0);
  86. p->data=ret;
  87. p->format=fmt;
  88. p->count=count;
  89. p->type=type;
  90. }
  91. /* Find text-uri-list in a list of targets and return it's atom
  92. if available, else return None */
  93. static Atom X11_PickTarget(Display *disp, Atom list[], int list_count)
  94. {
  95. Atom request = None;
  96. char *name;
  97. int i;
  98. for (i=0; i < list_count && request == None; i++) {
  99. name = X11_XGetAtomName(disp, list[i]);
  100. if ((SDL_strcmp("text/uri-list", name) == 0) || (SDL_strcmp("text/plain", name) == 0)) {
  101. request = list[i];
  102. }
  103. X11_XFree(name);
  104. }
  105. return request;
  106. }
  107. /* Wrapper for X11_PickTarget for a maximum of three targets, a special
  108. case in the Xdnd protocol */
  109. static Atom X11_PickTargetFromAtoms(Display *disp, Atom a0, Atom a1, Atom a2)
  110. {
  111. int count=0;
  112. Atom atom[3];
  113. if (a0 != None) atom[count++] = a0;
  114. if (a1 != None) atom[count++] = a1;
  115. if (a2 != None) atom[count++] = a2;
  116. return X11_PickTarget(disp, atom, count);
  117. }
  118. struct KeyRepeatCheckData
  119. {
  120. XEvent *event;
  121. SDL_bool found;
  122. };
  123. static Bool X11_KeyRepeatCheckIfEvent(Display *display, XEvent *chkev,
  124. XPointer arg)
  125. {
  126. struct KeyRepeatCheckData *d = (struct KeyRepeatCheckData *) arg;
  127. if (chkev->type == KeyPress &&
  128. chkev->xkey.keycode == d->event->xkey.keycode &&
  129. chkev->xkey.time - d->event->xkey.time < 2)
  130. d->found = SDL_TRUE;
  131. return False;
  132. }
  133. /* Check to see if this is a repeated key.
  134. (idea shamelessly lifted from GII -- thanks guys! :)
  135. */
  136. static SDL_bool X11_KeyRepeat(Display *display, XEvent *event)
  137. {
  138. XEvent dummyev;
  139. struct KeyRepeatCheckData d;
  140. d.event = event;
  141. d.found = SDL_FALSE;
  142. if (X11_XPending(display))
  143. X11_XCheckIfEvent(display, &dummyev, X11_KeyRepeatCheckIfEvent,
  144. (XPointer) &d);
  145. return d.found;
  146. }
  147. static SDL_bool
  148. X11_IsWheelEvent(Display * display,XEvent * event,int * xticks,int * yticks)
  149. {
  150. /* according to the xlib docs, no specific mouse wheel events exist.
  151. However, the defacto standard is that the vertical wheel is X buttons
  152. 4 (up) and 5 (down) and a horizontal wheel is 6 (left) and 7 (right). */
  153. /* Xlib defines "Button1" through 5, so we just use literals here. */
  154. switch (event->xbutton.button) {
  155. case 4: *yticks = 1; return SDL_TRUE;
  156. case 5: *yticks = -1; return SDL_TRUE;
  157. case 6: *xticks = 1; return SDL_TRUE;
  158. case 7: *xticks = -1; return SDL_TRUE;
  159. default: break;
  160. }
  161. return SDL_FALSE;
  162. }
  163. /* Decodes URI escape sequences in string buf of len bytes
  164. (excluding the terminating NULL byte) in-place. Since
  165. URI-encoded characters take three times the space of
  166. normal characters, this should not be an issue.
  167. Returns the number of decoded bytes that wound up in
  168. the buffer, excluding the terminating NULL byte.
  169. The buffer is guaranteed to be NULL-terminated but
  170. may contain embedded NULL bytes.
  171. On error, -1 is returned.
  172. */
  173. static int X11_URIDecode(char *buf, int len) {
  174. int ri, wi, di;
  175. char decode = '\0';
  176. if (buf == NULL || len < 0) {
  177. errno = EINVAL;
  178. return -1;
  179. }
  180. if (len == 0) {
  181. len = SDL_strlen(buf);
  182. }
  183. for (ri = 0, wi = 0, di = 0; ri < len && wi < len; ri += 1) {
  184. if (di == 0) {
  185. /* start decoding */
  186. if (buf[ri] == '%') {
  187. decode = '\0';
  188. di += 1;
  189. continue;
  190. }
  191. /* normal write */
  192. buf[wi] = buf[ri];
  193. wi += 1;
  194. continue;
  195. } else if (di == 1 || di == 2) {
  196. char off = '\0';
  197. char isa = buf[ri] >= 'a' && buf[ri] <= 'f';
  198. char isA = buf[ri] >= 'A' && buf[ri] <= 'F';
  199. char isn = buf[ri] >= '0' && buf[ri] <= '9';
  200. if (!(isa || isA || isn)) {
  201. /* not a hexadecimal */
  202. int sri;
  203. for (sri = ri - di; sri <= ri; sri += 1) {
  204. buf[wi] = buf[sri];
  205. wi += 1;
  206. }
  207. di = 0;
  208. continue;
  209. }
  210. /* itsy bitsy magicsy */
  211. if (isn) {
  212. off = 0 - '0';
  213. } else if (isa) {
  214. off = 10 - 'a';
  215. } else if (isA) {
  216. off = 10 - 'A';
  217. }
  218. decode |= (buf[ri] + off) << (2 - di) * 4;
  219. if (di == 2) {
  220. buf[wi] = decode;
  221. wi += 1;
  222. di = 0;
  223. } else {
  224. di += 1;
  225. }
  226. continue;
  227. }
  228. }
  229. buf[wi] = '\0';
  230. return wi;
  231. }
  232. /* Convert URI to local filename
  233. return filename if possible, else NULL
  234. */
  235. static char* X11_URIToLocal(char* uri) {
  236. char *file = NULL;
  237. SDL_bool local;
  238. if (SDL_memcmp(uri,"file:/",6) == 0) uri += 6; /* local file? */
  239. else if (SDL_strstr(uri,":/") != NULL) return file; /* wrong scheme */
  240. local = uri[0] != '/' || (uri[0] != '\0' && uri[1] == '/');
  241. /* got a hostname? */
  242. if (!local && uri[0] == '/' && uri[2] != '/') {
  243. char* hostname_end = SDL_strchr(uri+1, '/');
  244. if (hostname_end != NULL) {
  245. char hostname[ 257 ];
  246. if (gethostname(hostname, 255) == 0) {
  247. hostname[ 256 ] = '\0';
  248. if (SDL_memcmp(uri+1, hostname, hostname_end - (uri+1)) == 0) {
  249. uri = hostname_end + 1;
  250. local = SDL_TRUE;
  251. }
  252. }
  253. }
  254. }
  255. if (local) {
  256. file = uri;
  257. /* Convert URI escape sequences to real characters */
  258. X11_URIDecode(file, 0);
  259. if (uri[1] == '/') {
  260. file++;
  261. } else {
  262. file--;
  263. }
  264. }
  265. return file;
  266. }
  267. #if SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS
  268. static void X11_HandleGenericEvent(SDL_VideoData *videodata, XEvent *xev)
  269. {
  270. /* event is a union, so cookie == &event, but this is type safe. */
  271. XGenericEventCookie *cookie = &xev->xcookie;
  272. if (X11_XGetEventData(videodata->display, cookie)) {
  273. X11_HandleXinput2Event(videodata, cookie);
  274. /* Send a SDL_SYSWMEVENT if the application wants them.
  275. * Since event data is only available until XFreeEventData is called,
  276. * the *only* way for an application to access it is to register an event filter/watcher
  277. * and do all the processing on the SDL_SYSWMEVENT inside the callback. */
  278. if (SDL_GetEventState(SDL_SYSWMEVENT) == SDL_ENABLE) {
  279. SDL_SysWMmsg wmmsg;
  280. SDL_VERSION(&wmmsg.version);
  281. wmmsg.subsystem = SDL_SYSWM_X11;
  282. wmmsg.msg.x11.event = *xev;
  283. SDL_SendSysWMEvent(&wmmsg);
  284. }
  285. X11_XFreeEventData(videodata->display, cookie);
  286. }
  287. }
  288. #endif /* SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS */
  289. static unsigned
  290. X11_GetNumLockModifierMask(_THIS)
  291. {
  292. SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
  293. Display *display = viddata->display;
  294. unsigned num_mask = 0;
  295. int i, j;
  296. XModifierKeymap *xmods;
  297. unsigned n;
  298. xmods = X11_XGetModifierMapping(display);
  299. n = xmods->max_keypermod;
  300. for(i = 3; i < 8; i++) {
  301. for(j = 0; j < n; j++) {
  302. KeyCode kc = xmods->modifiermap[i * n + j];
  303. if (viddata->key_layout[kc] == SDL_SCANCODE_NUMLOCKCLEAR) {
  304. num_mask = 1 << i;
  305. break;
  306. }
  307. }
  308. }
  309. X11_XFreeModifiermap(xmods);
  310. return num_mask;
  311. }
  312. static unsigned
  313. X11_GetScrollLockModifierMask(_THIS)
  314. {
  315. SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
  316. Display *display = viddata->display;
  317. unsigned num_mask = 0;
  318. int i, j;
  319. XModifierKeymap *xmods;
  320. unsigned n;
  321. xmods = X11_XGetModifierMapping(display);
  322. n = xmods->max_keypermod;
  323. for(i = 3; i < 8; i++) {
  324. for(j = 0; j < n; j++) {
  325. KeyCode kc = xmods->modifiermap[i * n + j];
  326. if (viddata->key_layout[kc] == SDL_SCANCODE_SCROLLLOCK) {
  327. num_mask = 1 << i;
  328. break;
  329. }
  330. }
  331. }
  332. X11_XFreeModifiermap(xmods);
  333. return num_mask;
  334. }
  335. void
  336. X11_ReconcileKeyboardState(_THIS)
  337. {
  338. SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
  339. Display *display = viddata->display;
  340. char keys[32];
  341. int keycode;
  342. Window junk_window;
  343. int x, y;
  344. unsigned int mask;
  345. const Uint8 *keyboardState;
  346. X11_XQueryKeymap(display, keys);
  347. /* Sync up the keyboard modifier state */
  348. if (X11_XQueryPointer(display, DefaultRootWindow(display), &junk_window, &junk_window, &x, &y, &x, &y, &mask)) {
  349. SDL_ToggleModState(KMOD_CAPS, (mask & LockMask) != 0);
  350. SDL_ToggleModState(KMOD_NUM, (mask & X11_GetNumLockModifierMask(_this)) != 0);
  351. SDL_ToggleModState(KMOD_SCROLL, (mask & X11_GetScrollLockModifierMask(_this)) != 0);
  352. }
  353. keyboardState = SDL_GetKeyboardState(0);
  354. for (keycode = 0; keycode < 256; ++keycode) {
  355. SDL_Scancode scancode = viddata->key_layout[keycode];
  356. SDL_bool x11KeyPressed = (keys[keycode / 8] & (1 << (keycode % 8))) != 0;
  357. SDL_bool sdlKeyPressed = keyboardState[scancode] == SDL_PRESSED;
  358. if (x11KeyPressed && !sdlKeyPressed) {
  359. /* Only update modifier state for keys that are pressed in another application */
  360. switch (SDL_GetKeyFromScancode(scancode)) {
  361. case SDLK_LCTRL:
  362. case SDLK_RCTRL:
  363. case SDLK_LSHIFT:
  364. case SDLK_RSHIFT:
  365. case SDLK_LALT:
  366. case SDLK_RALT:
  367. case SDLK_LGUI:
  368. case SDLK_RGUI:
  369. case SDLK_MODE:
  370. SDL_SendKeyboardKey(SDL_PRESSED, scancode);
  371. break;
  372. default:
  373. break;
  374. }
  375. } else if (!x11KeyPressed && sdlKeyPressed) {
  376. SDL_SendKeyboardKey(SDL_RELEASED, scancode);
  377. }
  378. }
  379. }
  380. static void
  381. X11_DispatchFocusIn(_THIS, SDL_WindowData *data)
  382. {
  383. #ifdef DEBUG_XEVENTS
  384. printf("window %p: Dispatching FocusIn\n", data);
  385. #endif
  386. SDL_SetKeyboardFocus(data->window);
  387. X11_ReconcileKeyboardState(_this);
  388. #ifdef X_HAVE_UTF8_STRING
  389. if (data->ic) {
  390. X11_XSetICFocus(data->ic);
  391. }
  392. #endif
  393. #ifdef SDL_USE_IME
  394. SDL_IME_SetFocus(SDL_TRUE);
  395. #endif
  396. if (data->flashing_window) {
  397. X11_FlashWindow(_this, data->window, SDL_FLASH_CANCEL);
  398. }
  399. }
  400. static void
  401. X11_DispatchFocusOut(_THIS, SDL_WindowData *data)
  402. {
  403. #ifdef DEBUG_XEVENTS
  404. printf("window %p: Dispatching FocusOut\n", data);
  405. #endif
  406. /* If another window has already processed a focus in, then don't try to
  407. * remove focus here. Doing so will incorrectly remove focus from that
  408. * window, and the focus lost event for this window will have already
  409. * been dispatched anyway. */
  410. if (data->window == SDL_GetKeyboardFocus()) {
  411. SDL_SetKeyboardFocus(NULL);
  412. }
  413. #ifdef X_HAVE_UTF8_STRING
  414. if (data->ic) {
  415. X11_XUnsetICFocus(data->ic);
  416. }
  417. #endif
  418. #ifdef SDL_USE_IME
  419. SDL_IME_SetFocus(SDL_FALSE);
  420. #endif
  421. }
  422. static void
  423. X11_DispatchMapNotify(SDL_WindowData *data)
  424. {
  425. SDL_Window *window = data->window;
  426. SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESTORED, 0, 0);
  427. SDL_SendWindowEvent(window, SDL_WINDOWEVENT_SHOWN, 0, 0);
  428. if (!(window->flags & SDL_WINDOW_HIDDEN) && (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
  429. SDL_UpdateWindowGrab(window);
  430. }
  431. }
  432. static void
  433. X11_DispatchUnmapNotify(SDL_WindowData *data)
  434. {
  435. SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
  436. SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MINIMIZED, 0, 0);
  437. }
  438. static void
  439. InitiateWindowMove(_THIS, const SDL_WindowData *data, const SDL_Point *point)
  440. {
  441. SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
  442. SDL_Window* window = data->window;
  443. Display *display = viddata->display;
  444. XEvent evt;
  445. /* !!! FIXME: we need to regrab this if necessary when the drag is done. */
  446. X11_XUngrabPointer(display, 0L);
  447. X11_XFlush(display);
  448. evt.xclient.type = ClientMessage;
  449. evt.xclient.window = data->xwindow;
  450. evt.xclient.message_type = X11_XInternAtom(display, "_NET_WM_MOVERESIZE", True);
  451. evt.xclient.format = 32;
  452. evt.xclient.data.l[0] = window->x + point->x;
  453. evt.xclient.data.l[1] = window->y + point->y;
  454. evt.xclient.data.l[2] = _NET_WM_MOVERESIZE_MOVE;
  455. evt.xclient.data.l[3] = Button1;
  456. evt.xclient.data.l[4] = 0;
  457. X11_XSendEvent(display, DefaultRootWindow(display), False, SubstructureRedirectMask | SubstructureNotifyMask, &evt);
  458. X11_XSync(display, 0);
  459. }
  460. static void
  461. InitiateWindowResize(_THIS, const SDL_WindowData *data, const SDL_Point *point, int direction)
  462. {
  463. SDL_VideoData *viddata = (SDL_VideoData *) _this->driverdata;
  464. SDL_Window* window = data->window;
  465. Display *display = viddata->display;
  466. XEvent evt;
  467. if (direction < _NET_WM_MOVERESIZE_SIZE_TOPLEFT || direction > _NET_WM_MOVERESIZE_SIZE_LEFT)
  468. return;
  469. /* !!! FIXME: we need to regrab this if necessary when the drag is done. */
  470. X11_XUngrabPointer(display, 0L);
  471. X11_XFlush(display);
  472. evt.xclient.type = ClientMessage;
  473. evt.xclient.window = data->xwindow;
  474. evt.xclient.message_type = X11_XInternAtom(display, "_NET_WM_MOVERESIZE", True);
  475. evt.xclient.format = 32;
  476. evt.xclient.data.l[0] = window->x + point->x;
  477. evt.xclient.data.l[1] = window->y + point->y;
  478. evt.xclient.data.l[2] = direction;
  479. evt.xclient.data.l[3] = Button1;
  480. evt.xclient.data.l[4] = 0;
  481. X11_XSendEvent(display, DefaultRootWindow(display), False, SubstructureRedirectMask | SubstructureNotifyMask, &evt);
  482. X11_XSync(display, 0);
  483. }
  484. static SDL_bool
  485. ProcessHitTest(_THIS, const SDL_WindowData *data, const XEvent *xev)
  486. {
  487. SDL_Window *window = data->window;
  488. if (window->hit_test) {
  489. const SDL_Point point = { xev->xbutton.x, xev->xbutton.y };
  490. const SDL_HitTestResult rc = window->hit_test(window, &point, window->hit_test_data);
  491. static const int directions[] = {
  492. _NET_WM_MOVERESIZE_SIZE_TOPLEFT, _NET_WM_MOVERESIZE_SIZE_TOP,
  493. _NET_WM_MOVERESIZE_SIZE_TOPRIGHT, _NET_WM_MOVERESIZE_SIZE_RIGHT,
  494. _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT, _NET_WM_MOVERESIZE_SIZE_BOTTOM,
  495. _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT, _NET_WM_MOVERESIZE_SIZE_LEFT
  496. };
  497. switch (rc) {
  498. case SDL_HITTEST_DRAGGABLE:
  499. InitiateWindowMove(_this, data, &point);
  500. return SDL_TRUE;
  501. case SDL_HITTEST_RESIZE_TOPLEFT:
  502. case SDL_HITTEST_RESIZE_TOP:
  503. case SDL_HITTEST_RESIZE_TOPRIGHT:
  504. case SDL_HITTEST_RESIZE_RIGHT:
  505. case SDL_HITTEST_RESIZE_BOTTOMRIGHT:
  506. case SDL_HITTEST_RESIZE_BOTTOM:
  507. case SDL_HITTEST_RESIZE_BOTTOMLEFT:
  508. case SDL_HITTEST_RESIZE_LEFT:
  509. InitiateWindowResize(_this, data, &point, directions[rc - SDL_HITTEST_RESIZE_TOPLEFT]);
  510. return SDL_TRUE;
  511. default: return SDL_FALSE;
  512. }
  513. }
  514. return SDL_FALSE;
  515. }
  516. static void
  517. X11_UpdateUserTime(SDL_WindowData *data, const unsigned long latest)
  518. {
  519. if (latest && (latest != data->user_time)) {
  520. SDL_VideoData *videodata = data->videodata;
  521. Display *display = videodata->display;
  522. X11_XChangeProperty(display, data->xwindow, videodata->_NET_WM_USER_TIME,
  523. XA_CARDINAL, 32, PropModeReplace,
  524. (const unsigned char *) &latest, 1);
  525. #ifdef DEBUG_XEVENTS
  526. printf("window %p: updating _NET_WM_USER_TIME to %lu\n", data, latest);
  527. #endif
  528. data->user_time = latest;
  529. }
  530. }
  531. static void
  532. X11_HandleClipboardEvent(_THIS, const XEvent *xevent)
  533. {
  534. int i;
  535. SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
  536. Display *display = videodata->display;
  537. SDL_assert(videodata->clipboard_window != None);
  538. SDL_assert(xevent->xany.window == videodata->clipboard_window);
  539. switch (xevent->type) {
  540. /* Copy the selection from our own CUTBUFFER to the requested property */
  541. case SelectionRequest: {
  542. const XSelectionRequestEvent *req = &xevent->xselectionrequest;
  543. XEvent sevent;
  544. int seln_format, mime_formats;
  545. unsigned long nbytes;
  546. unsigned long overflow;
  547. unsigned char *seln_data;
  548. Atom supportedFormats[SDL_X11_CLIPBOARD_MIME_TYPE_MAX+1];
  549. Atom XA_TARGETS = X11_XInternAtom(display, "TARGETS", 0);
  550. #ifdef DEBUG_XEVENTS
  551. printf("window CLIPBOARD: SelectionRequest (requestor = %ld, target = %ld)\n",
  552. req->requestor, req->target);
  553. #endif
  554. SDL_zero(sevent);
  555. sevent.xany.type = SelectionNotify;
  556. sevent.xselection.selection = req->selection;
  557. sevent.xselection.target = None;
  558. sevent.xselection.property = None; /* tell them no by default */
  559. sevent.xselection.requestor = req->requestor;
  560. sevent.xselection.time = req->time;
  561. /* !!! FIXME: We were probably storing this on the root window
  562. because an SDL window might go away...? but we don't have to do
  563. this now (or ever, really). */
  564. if (req->target == XA_TARGETS) {
  565. supportedFormats[0] = XA_TARGETS;
  566. mime_formats = 1;
  567. for (i = 0; i < SDL_X11_CLIPBOARD_MIME_TYPE_MAX; ++i)
  568. supportedFormats[mime_formats++] = X11_GetSDLCutBufferClipboardExternalFormat(display, i);
  569. X11_XChangeProperty(display, req->requestor, req->property,
  570. XA_ATOM, 32, PropModeReplace,
  571. (unsigned char*)supportedFormats,
  572. mime_formats);
  573. sevent.xselection.property = req->property;
  574. sevent.xselection.target = XA_TARGETS;
  575. } else {
  576. for (i = 0; i < SDL_X11_CLIPBOARD_MIME_TYPE_MAX; ++i) {
  577. if (X11_GetSDLCutBufferClipboardExternalFormat(display, i) != req->target)
  578. continue;
  579. if (X11_XGetWindowProperty(display, DefaultRootWindow(display),
  580. X11_GetSDLCutBufferClipboardType(display, i, req->selection), 0, INT_MAX/4, False, X11_GetSDLCutBufferClipboardInternalFormat(display, i),
  581. &sevent.xselection.target, &seln_format, &nbytes,
  582. &overflow, &seln_data) == Success) {
  583. if (seln_format != None) {
  584. X11_XChangeProperty(display, req->requestor, req->property,
  585. sevent.xselection.target, seln_format, PropModeReplace,
  586. seln_data, nbytes);
  587. sevent.xselection.property = req->property;
  588. X11_XFree(seln_data);
  589. break;
  590. } else {
  591. X11_XFree(seln_data);
  592. }
  593. }
  594. }
  595. }
  596. X11_XSendEvent(display, req->requestor, False, 0, &sevent);
  597. X11_XSync(display, False);
  598. }
  599. break;
  600. case SelectionNotify: {
  601. #ifdef DEBUG_XEVENTS
  602. printf("window CLIPBOARD: SelectionNotify (requestor = %ld, target = %ld)\n",
  603. xevent->xselection.requestor, xevent->xselection.target);
  604. #endif
  605. videodata->selection_waiting = SDL_FALSE;
  606. }
  607. break;
  608. case SelectionClear: {
  609. /* !!! FIXME: cache atoms */
  610. Atom XA_CLIPBOARD = X11_XInternAtom(display, "CLIPBOARD", 0);
  611. #ifdef DEBUG_XEVENTS
  612. printf("window CLIPBOARD: SelectionClear (requestor = %ld, target = %ld)\n",
  613. xevent->xselection.requestor, xevent->xselection.target);
  614. #endif
  615. if (xevent->xselectionclear.selection == XA_PRIMARY ||
  616. (XA_CLIPBOARD != None && xevent->xselectionclear.selection == XA_CLIPBOARD)) {
  617. SDL_SendClipboardUpdate();
  618. }
  619. }
  620. break;
  621. }
  622. }
  623. static Bool
  624. isMapNotify(Display *display, XEvent *ev, XPointer arg)
  625. {
  626. XUnmapEvent *unmap;
  627. unmap = (XUnmapEvent*) arg;
  628. return ev->type == MapNotify &&
  629. ev->xmap.window == unmap->window &&
  630. ev->xmap.serial == unmap->serial;
  631. }
  632. static Bool
  633. isReparentNotify(Display *display, XEvent *ev, XPointer arg)
  634. {
  635. XUnmapEvent *unmap;
  636. unmap = (XUnmapEvent*) arg;
  637. return ev->type == ReparentNotify &&
  638. ev->xreparent.window == unmap->window &&
  639. ev->xreparent.serial == unmap->serial;
  640. }
  641. static int
  642. XLookupStringAsUTF8(XKeyEvent *event_struct, char *buffer_return, int bytes_buffer, KeySym *keysym_return, XComposeStatus *status_in_out)
  643. {
  644. int result = X11_XLookupString(event_struct, buffer_return, bytes_buffer, keysym_return, status_in_out);
  645. if (result > 0) {
  646. char *utf8_text = SDL_iconv_string("UTF-8", "ISO-8859-1", buffer_return, result);
  647. if (utf8_text) {
  648. SDL_strlcpy(buffer_return, utf8_text, bytes_buffer);
  649. SDL_free(utf8_text);
  650. return SDL_strlen(buffer_return);
  651. } else {
  652. return 0;
  653. }
  654. }
  655. return result;
  656. }
  657. static void
  658. X11_DispatchEvent(_THIS, XEvent *xevent)
  659. {
  660. SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
  661. Display *display;
  662. SDL_WindowData *data;
  663. int orig_event_type;
  664. KeyCode orig_keycode;
  665. XClientMessageEvent m;
  666. int i;
  667. SDL_assert(videodata != NULL);
  668. display = videodata->display;
  669. /* Save the original keycode for dead keys, which are filtered out by
  670. the XFilterEvent() call below.
  671. */
  672. orig_event_type = xevent->type;
  673. if (orig_event_type == KeyPress || orig_event_type == KeyRelease) {
  674. orig_keycode = xevent->xkey.keycode;
  675. } else {
  676. orig_keycode = 0;
  677. }
  678. /* filter events catchs XIM events and sends them to the correct handler */
  679. if (X11_XFilterEvent(xevent, None) == True) {
  680. #if 0
  681. printf("Filtered event type = %d display = %d window = %d\n",
  682. xevent->type, xevent->xany.display, xevent->xany.window);
  683. #endif
  684. /* Make sure dead key press/release events are sent */
  685. /* But only if we're using one of the DBus IMEs, otherwise
  686. some XIM IMEs will generate duplicate events */
  687. if (orig_keycode) {
  688. #if defined(HAVE_IBUS_IBUS_H) || defined(HAVE_FCITX)
  689. SDL_Scancode scancode = videodata->key_layout[orig_keycode];
  690. videodata->filter_code = orig_keycode;
  691. videodata->filter_time = xevent->xkey.time;
  692. if (orig_event_type == KeyPress) {
  693. SDL_SendKeyboardKey(SDL_PRESSED, scancode);
  694. } else {
  695. SDL_SendKeyboardKey(SDL_RELEASED, scancode);
  696. }
  697. #endif
  698. }
  699. return;
  700. }
  701. #if SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS
  702. if(xevent->type == GenericEvent) {
  703. X11_HandleGenericEvent(videodata, xevent);
  704. return;
  705. }
  706. #endif
  707. #if SDL_VIDEO_DRIVER_X11_XRANDR
  708. if (videodata->xrandr_event_base && (xevent->type == (videodata->xrandr_event_base + RRNotify))) {
  709. X11_HandleXRandREvent(_this, xevent);
  710. }
  711. #endif
  712. /* Send a SDL_SYSWMEVENT if the application wants them */
  713. if (SDL_GetEventState(SDL_SYSWMEVENT) == SDL_ENABLE) {
  714. SDL_SysWMmsg wmmsg;
  715. SDL_VERSION(&wmmsg.version);
  716. wmmsg.subsystem = SDL_SYSWM_X11;
  717. wmmsg.msg.x11.event = *xevent;
  718. SDL_SendSysWMEvent(&wmmsg);
  719. }
  720. #if 0
  721. printf("type = %d display = %d window = %d\n",
  722. xevent->type, xevent->xany.display, xevent->xany.window);
  723. #endif
  724. if ((videodata->clipboard_window != None) &&
  725. (videodata->clipboard_window == xevent->xany.window)) {
  726. X11_HandleClipboardEvent(_this, xevent);
  727. return;
  728. }
  729. data = NULL;
  730. if (videodata && videodata->windowlist) {
  731. for (i = 0; i < videodata->numwindows; ++i) {
  732. if ((videodata->windowlist[i] != NULL) &&
  733. (videodata->windowlist[i]->xwindow == xevent->xany.window)) {
  734. data = videodata->windowlist[i];
  735. break;
  736. }
  737. }
  738. }
  739. if (!data) {
  740. /* The window for KeymapNotify, etc events is 0 */
  741. if (xevent->type == KeymapNotify) {
  742. #ifdef DEBUG_XEVENTS
  743. printf("window %p: KeymapNotify!\n", data);
  744. #endif
  745. if (SDL_GetKeyboardFocus() != NULL) {
  746. X11_ReconcileKeyboardState(_this);
  747. }
  748. } else if (xevent->type == MappingNotify) {
  749. /* Has the keyboard layout changed? */
  750. const int request = xevent->xmapping.request;
  751. #ifdef DEBUG_XEVENTS
  752. printf("window %p: MappingNotify!\n", data);
  753. #endif
  754. if ((request == MappingKeyboard) || (request == MappingModifier)) {
  755. X11_XRefreshKeyboardMapping(&xevent->xmapping);
  756. }
  757. X11_UpdateKeymap(_this, SDL_TRUE);
  758. } else if (xevent->type == PropertyNotify && videodata && videodata->windowlist) {
  759. char* name_of_atom = X11_XGetAtomName(display, xevent->xproperty.atom);
  760. if (SDL_strncmp(name_of_atom, "_ICC_PROFILE", sizeof("_ICC_PROFILE") - 1) == 0) {
  761. XWindowAttributes attrib;
  762. int screennum;
  763. for (i = 0; i < videodata->numwindows; ++i) {
  764. if (videodata->windowlist[i] != NULL) {
  765. data = videodata->windowlist[i];
  766. X11_XGetWindowAttributes(display, data->xwindow, &attrib);
  767. screennum = X11_XScreenNumberOfScreen(attrib.screen);
  768. if (screennum == 0 && SDL_strcmp(name_of_atom, "_ICC_PROFILE") == 0) {
  769. SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_ICCPROF_CHANGED, 0, 0);
  770. } else if (SDL_strncmp(name_of_atom, "_ICC_PROFILE_", sizeof("_ICC_PROFILE_") - 1) == 0 && SDL_strlen(name_of_atom) > sizeof("_ICC_PROFILE_") - 1) {
  771. int iccscreennum = SDL_atoi(&name_of_atom[sizeof("_ICC_PROFILE_") - 1]);
  772. if (screennum == iccscreennum) {
  773. SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_ICCPROF_CHANGED, 0, 0);
  774. }
  775. }
  776. }
  777. }
  778. }
  779. if (name_of_atom) X11_XFree(name_of_atom);
  780. }
  781. return;
  782. }
  783. switch (xevent->type) {
  784. /* Gaining mouse coverage? */
  785. case EnterNotify:{
  786. SDL_Mouse *mouse = SDL_GetMouse();
  787. #ifdef DEBUG_XEVENTS
  788. printf("window %p: EnterNotify! (%d,%d,%d)\n", data,
  789. xevent->xcrossing.x,
  790. xevent->xcrossing.y,
  791. xevent->xcrossing.mode);
  792. if (xevent->xcrossing.mode == NotifyGrab)
  793. printf("Mode: NotifyGrab\n");
  794. if (xevent->xcrossing.mode == NotifyUngrab)
  795. printf("Mode: NotifyUngrab\n");
  796. #endif
  797. SDL_SetMouseFocus(data->window);
  798. mouse->last_x = xevent->xcrossing.x;
  799. mouse->last_y = xevent->xcrossing.y;
  800. #if SDL_VIDEO_DRIVER_X11_XFIXES
  801. {
  802. /* Only create the barriers if we have input focus */
  803. SDL_WindowData* windowdata = (SDL_WindowData *) data->window->driverdata;
  804. if ((data->pointer_barrier_active == SDL_TRUE) && windowdata->window->flags & SDL_WINDOW_INPUT_FOCUS) {
  805. X11_ConfineCursorWithFlags(_this, windowdata->window, &windowdata->barrier_rect, X11_BARRIER_HANDLED_BY_EVENT);
  806. }
  807. }
  808. #endif
  809. if (!mouse->relative_mode) {
  810. SDL_SendMouseMotion(data->window, 0, 0, xevent->xcrossing.x, xevent->xcrossing.y);
  811. }
  812. /* We ungrab in LeaveNotify, so we may need to grab again here */
  813. SDL_UpdateWindowGrab(data->window);
  814. }
  815. break;
  816. /* Losing mouse coverage? */
  817. case LeaveNotify:{
  818. #ifdef DEBUG_XEVENTS
  819. printf("window %p: LeaveNotify! (%d,%d,%d)\n", data,
  820. xevent->xcrossing.x,
  821. xevent->xcrossing.y,
  822. xevent->xcrossing.mode);
  823. if (xevent->xcrossing.mode == NotifyGrab)
  824. printf("Mode: NotifyGrab\n");
  825. if (xevent->xcrossing.mode == NotifyUngrab)
  826. printf("Mode: NotifyUngrab\n");
  827. #endif
  828. if (!SDL_GetMouse()->relative_mode) {
  829. SDL_SendMouseMotion(data->window, 0, 0, xevent->xcrossing.x, xevent->xcrossing.y);
  830. }
  831. if (xevent->xcrossing.mode != NotifyGrab &&
  832. xevent->xcrossing.mode != NotifyUngrab &&
  833. xevent->xcrossing.detail != NotifyInferior) {
  834. /* In order for interaction with the window decorations and menu to work properly
  835. on Mutter, we need to ungrab the keyboard when the the mouse leaves. */
  836. if (!(data->window->flags & SDL_WINDOW_FULLSCREEN)) {
  837. X11_SetWindowKeyboardGrab(_this, data->window, SDL_FALSE);
  838. }
  839. SDL_SetMouseFocus(NULL);
  840. }
  841. }
  842. break;
  843. /* Gaining input focus? */
  844. case FocusIn:{
  845. if (xevent->xfocus.mode == NotifyGrab || xevent->xfocus.mode == NotifyUngrab) {
  846. /* Someone is handling a global hotkey, ignore it */
  847. #ifdef DEBUG_XEVENTS
  848. printf("window %p: FocusIn (NotifyGrab/NotifyUngrab, ignoring)\n", data);
  849. #endif
  850. break;
  851. }
  852. if (xevent->xfocus.detail == NotifyInferior || xevent->xfocus.detail == NotifyPointer) {
  853. #ifdef DEBUG_XEVENTS
  854. printf("window %p: FocusIn (NotifyInferior/NotifyPointer, ignoring)\n", data);
  855. #endif
  856. break;
  857. }
  858. #ifdef DEBUG_XEVENTS
  859. printf("window %p: FocusIn!\n", data);
  860. #endif
  861. if (!videodata->last_mode_change_deadline) /* no recent mode changes */
  862. {
  863. data->pending_focus = PENDING_FOCUS_NONE;
  864. data->pending_focus_time = 0;
  865. X11_DispatchFocusIn(_this, data);
  866. }
  867. else
  868. {
  869. data->pending_focus = PENDING_FOCUS_IN;
  870. data->pending_focus_time = SDL_GetTicks() + PENDING_FOCUS_TIME;
  871. }
  872. data->last_focus_event_time = SDL_GetTicks();
  873. }
  874. break;
  875. /* Losing input focus? */
  876. case FocusOut:{
  877. if (xevent->xfocus.mode == NotifyGrab || xevent->xfocus.mode == NotifyUngrab) {
  878. /* Someone is handling a global hotkey, ignore it */
  879. #ifdef DEBUG_XEVENTS
  880. printf("window %p: FocusOut (NotifyGrab/NotifyUngrab, ignoring)\n", data);
  881. #endif
  882. break;
  883. }
  884. if (xevent->xfocus.detail == NotifyInferior || xevent->xfocus.detail == NotifyPointer) {
  885. /* We still have focus if a child gets focus. We also don't
  886. care about the position of the pointer when the keyboard
  887. focus changed. */
  888. #ifdef DEBUG_XEVENTS
  889. printf("window %p: FocusOut (NotifyInferior/NotifyPointer, ignoring)\n", data);
  890. #endif
  891. break;
  892. }
  893. #ifdef DEBUG_XEVENTS
  894. printf("window %p: FocusOut!\n", data);
  895. #endif
  896. if (!videodata->last_mode_change_deadline) /* no recent mode changes */
  897. {
  898. data->pending_focus = PENDING_FOCUS_NONE;
  899. data->pending_focus_time = 0;
  900. X11_DispatchFocusOut(_this, data);
  901. }
  902. else
  903. {
  904. data->pending_focus = PENDING_FOCUS_OUT;
  905. data->pending_focus_time = SDL_GetTicks() + PENDING_FOCUS_TIME;
  906. }
  907. #if SDL_VIDEO_DRIVER_X11_XFIXES
  908. /* Disable confinement if it is activated. */
  909. if (data->pointer_barrier_active == SDL_TRUE) {
  910. X11_ConfineCursorWithFlags(_this, data->window, NULL, X11_BARRIER_HANDLED_BY_EVENT);
  911. }
  912. #endif /* SDL_VIDEO_DRIVER_X11_XFIXES */
  913. }
  914. break;
  915. /* Key press/release? */
  916. case KeyPress:
  917. case KeyRelease: {
  918. KeyCode keycode = xevent->xkey.keycode;
  919. KeySym keysym = NoSymbol;
  920. char text[SDL_TEXTINPUTEVENT_TEXT_SIZE];
  921. Status status = 0;
  922. SDL_bool handled_by_ime = SDL_FALSE;
  923. #ifdef DEBUG_XEVENTS
  924. printf("window %p: %s (X11 keycode = 0x%X)\n", data, (xevent->type == KeyPress ? "KeyPress" : "KeyRelease"), xevent->xkey.keycode);
  925. #endif
  926. #if 1
  927. if (videodata->key_layout[keycode] == SDL_SCANCODE_UNKNOWN && keycode) {
  928. int min_keycode, max_keycode;
  929. X11_XDisplayKeycodes(display, &min_keycode, &max_keycode);
  930. keysym = X11_KeyCodeToSym(_this, keycode, xevent->xkey.state >> 13);
  931. fprintf(stderr,
  932. "The key you just pressed is not recognized by SDL. To help get this fixed, please report this to the SDL forums/mailing list <https://discourse.libsdl.org/> X11 KeyCode %d (%d), X11 KeySym 0x%lX (%s).\n",
  933. keycode, keycode - min_keycode, keysym,
  934. X11_XKeysymToString(keysym));
  935. }
  936. #endif
  937. /* */
  938. SDL_zeroa(text);
  939. #ifdef X_HAVE_UTF8_STRING
  940. if (data->ic && xevent->type == KeyPress) {
  941. X11_Xutf8LookupString(data->ic, &xevent->xkey, text, sizeof(text),
  942. &keysym, &status);
  943. } else {
  944. XLookupStringAsUTF8(&xevent->xkey, text, sizeof(text), &keysym, NULL);
  945. }
  946. #else
  947. XLookupStringAsUTF8(&xevent->xkey, text, sizeof(text), &keysym, NULL);
  948. #endif
  949. #ifdef SDL_USE_IME
  950. if(SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE){
  951. handled_by_ime = SDL_IME_ProcessKeyEvent(keysym, keycode, (xevent->type == KeyPress ? SDL_PRESSED : SDL_RELEASED));
  952. }
  953. #endif
  954. if (!handled_by_ime) {
  955. if (xevent->type == KeyPress) {
  956. /* Don't send the key if it looks like a duplicate of a filtered key sent by an IME */
  957. if (xevent->xkey.keycode != videodata->filter_code || xevent->xkey.time != videodata->filter_time) {
  958. SDL_SendKeyboardKey(SDL_PRESSED, videodata->key_layout[keycode]);
  959. }
  960. if (*text) {
  961. SDL_SendKeyboardText(text);
  962. }
  963. } else {
  964. if (X11_KeyRepeat(display, xevent)) {
  965. /* We're about to get a repeated key down, ignore the key up */
  966. break;
  967. }
  968. SDL_SendKeyboardKey(SDL_RELEASED, videodata->key_layout[keycode]);
  969. }
  970. }
  971. if (xevent->type == KeyPress) {
  972. X11_UpdateUserTime(data, xevent->xkey.time);
  973. }
  974. }
  975. break;
  976. /* Have we been iconified? */
  977. case UnmapNotify:{
  978. XEvent ev;
  979. #ifdef DEBUG_XEVENTS
  980. printf("window %p: UnmapNotify!\n", data);
  981. #endif
  982. if (X11_XCheckIfEvent(display, &ev, &isReparentNotify, (XPointer)&xevent->xunmap)) {
  983. X11_XCheckIfEvent(display, &ev, &isMapNotify, (XPointer)&xevent->xunmap);
  984. } else {
  985. X11_DispatchUnmapNotify(data);
  986. }
  987. #if SDL_VIDEO_DRIVER_X11_XFIXES
  988. /* Disable confinement if the window gets hidden. */
  989. if (data->pointer_barrier_active == SDL_TRUE) {
  990. X11_ConfineCursorWithFlags(_this, data->window, NULL, X11_BARRIER_HANDLED_BY_EVENT);
  991. }
  992. #endif /* SDL_VIDEO_DRIVER_X11_XFIXES */
  993. }
  994. break;
  995. /* Have we been restored? */
  996. case MapNotify:{
  997. #ifdef DEBUG_XEVENTS
  998. printf("window %p: MapNotify!\n", data);
  999. #endif
  1000. X11_DispatchMapNotify(data);
  1001. #if SDL_VIDEO_DRIVER_X11_XFIXES
  1002. /* Enable confinement if it was activated. */
  1003. if (data->pointer_barrier_active == SDL_TRUE) {
  1004. X11_ConfineCursorWithFlags(_this, data->window, &data->barrier_rect, X11_BARRIER_HANDLED_BY_EVENT);
  1005. }
  1006. #endif /* SDL_VIDEO_DRIVER_X11_XFIXES */
  1007. }
  1008. break;
  1009. /* Have we been resized or moved? */
  1010. case ConfigureNotify:{
  1011. #ifdef DEBUG_XEVENTS
  1012. printf("window %p: ConfigureNotify! (position: %d,%d, size: %dx%d)\n", data,
  1013. xevent->xconfigure.x, xevent->xconfigure.y,
  1014. xevent->xconfigure.width, xevent->xconfigure.height);
  1015. #endif
  1016. /* Real configure notify events are relative to the parent, synthetic events are absolute. */
  1017. if (!xevent->xconfigure.send_event) {
  1018. unsigned int NumChildren;
  1019. Window ChildReturn, Root, Parent;
  1020. Window * Children;
  1021. /* Translate these coodinates back to relative to root */
  1022. X11_XQueryTree(data->videodata->display, xevent->xconfigure.window, &Root, &Parent, &Children, &NumChildren);
  1023. X11_XTranslateCoordinates(xevent->xconfigure.display,
  1024. Parent, DefaultRootWindow(xevent->xconfigure.display),
  1025. xevent->xconfigure.x, xevent->xconfigure.y,
  1026. &xevent->xconfigure.x, &xevent->xconfigure.y,
  1027. &ChildReturn);
  1028. }
  1029. if (xevent->xconfigure.x != data->last_xconfigure.x ||
  1030. xevent->xconfigure.y != data->last_xconfigure.y) {
  1031. SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED,
  1032. xevent->xconfigure.x, xevent->xconfigure.y);
  1033. #ifdef SDL_USE_IME
  1034. if(SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE){
  1035. /* Update IME candidate list position */
  1036. SDL_IME_UpdateTextRect(NULL);
  1037. }
  1038. #endif
  1039. }
  1040. if (xevent->xconfigure.width != data->last_xconfigure.width ||
  1041. xevent->xconfigure.height != data->last_xconfigure.height) {
  1042. SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED,
  1043. xevent->xconfigure.width,
  1044. xevent->xconfigure.height);
  1045. }
  1046. data->last_xconfigure = xevent->xconfigure;
  1047. }
  1048. break;
  1049. /* Have we been requested to quit (or another client message?) */
  1050. case ClientMessage:{
  1051. static int xdnd_version=0;
  1052. if (xevent->xclient.message_type == videodata->XdndEnter) {
  1053. SDL_bool use_list = xevent->xclient.data.l[1] & 1;
  1054. data->xdnd_source = xevent->xclient.data.l[0];
  1055. xdnd_version = (xevent->xclient.data.l[1] >> 24);
  1056. #ifdef DEBUG_XEVENTS
  1057. printf("XID of source window : %ld\n", data->xdnd_source);
  1058. printf("Protocol version to use : %d\n", xdnd_version);
  1059. printf("More then 3 data types : %d\n", (int) use_list);
  1060. #endif
  1061. if (use_list) {
  1062. /* fetch conversion targets */
  1063. SDL_x11Prop p;
  1064. X11_ReadProperty(&p, display, data->xdnd_source, videodata->XdndTypeList);
  1065. /* pick one */
  1066. data->xdnd_req = X11_PickTarget(display, (Atom*)p.data, p.count);
  1067. X11_XFree(p.data);
  1068. } else {
  1069. /* pick from list of three */
  1070. data->xdnd_req = X11_PickTargetFromAtoms(display, xevent->xclient.data.l[2], xevent->xclient.data.l[3], xevent->xclient.data.l[4]);
  1071. }
  1072. }
  1073. else if (xevent->xclient.message_type == videodata->XdndPosition) {
  1074. #ifdef DEBUG_XEVENTS
  1075. Atom act= videodata->XdndActionCopy;
  1076. if(xdnd_version >= 2) {
  1077. act = xevent->xclient.data.l[4];
  1078. }
  1079. printf("Action requested by user is : %s\n", X11_XGetAtomName(display , act));
  1080. #endif
  1081. /* reply with status */
  1082. SDL_memset(&m, 0, sizeof(XClientMessageEvent));
  1083. m.type = ClientMessage;
  1084. m.display = xevent->xclient.display;
  1085. m.window = xevent->xclient.data.l[0];
  1086. m.message_type = videodata->XdndStatus;
  1087. m.format=32;
  1088. m.data.l[0] = data->xwindow;
  1089. m.data.l[1] = (data->xdnd_req != None);
  1090. m.data.l[2] = 0; /* specify an empty rectangle */
  1091. m.data.l[3] = 0;
  1092. m.data.l[4] = videodata->XdndActionCopy; /* we only accept copying anyway */
  1093. X11_XSendEvent(display, xevent->xclient.data.l[0], False, NoEventMask, (XEvent*)&m);
  1094. X11_XFlush(display);
  1095. }
  1096. else if(xevent->xclient.message_type == videodata->XdndDrop) {
  1097. if (data->xdnd_req == None) {
  1098. /* say again - not interested! */
  1099. SDL_memset(&m, 0, sizeof(XClientMessageEvent));
  1100. m.type = ClientMessage;
  1101. m.display = xevent->xclient.display;
  1102. m.window = xevent->xclient.data.l[0];
  1103. m.message_type = videodata->XdndFinished;
  1104. m.format=32;
  1105. m.data.l[0] = data->xwindow;
  1106. m.data.l[1] = 0;
  1107. m.data.l[2] = None; /* fail! */
  1108. X11_XSendEvent(display, xevent->xclient.data.l[0], False, NoEventMask, (XEvent*)&m);
  1109. } else {
  1110. /* convert */
  1111. if(xdnd_version >= 1) {
  1112. X11_XConvertSelection(display, videodata->XdndSelection, data->xdnd_req, videodata->PRIMARY, data->xwindow, xevent->xclient.data.l[2]);
  1113. } else {
  1114. X11_XConvertSelection(display, videodata->XdndSelection, data->xdnd_req, videodata->PRIMARY, data->xwindow, CurrentTime);
  1115. }
  1116. }
  1117. }
  1118. else if ((xevent->xclient.message_type == videodata->WM_PROTOCOLS) &&
  1119. (xevent->xclient.format == 32) &&
  1120. (xevent->xclient.data.l[0] == videodata->_NET_WM_PING)) {
  1121. Window root = DefaultRootWindow(display);
  1122. #ifdef DEBUG_XEVENTS
  1123. printf("window %p: _NET_WM_PING\n", data);
  1124. #endif
  1125. xevent->xclient.window = root;
  1126. X11_XSendEvent(display, root, False, SubstructureRedirectMask | SubstructureNotifyMask, xevent);
  1127. break;
  1128. }
  1129. else if ((xevent->xclient.message_type == videodata->WM_PROTOCOLS) &&
  1130. (xevent->xclient.format == 32) &&
  1131. (xevent->xclient.data.l[0] == videodata->WM_DELETE_WINDOW)) {
  1132. #ifdef DEBUG_XEVENTS
  1133. printf("window %p: WM_DELETE_WINDOW\n", data);
  1134. #endif
  1135. SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
  1136. break;
  1137. }
  1138. else if ((xevent->xclient.message_type == videodata->WM_PROTOCOLS) &&
  1139. (xevent->xclient.format == 32) &&
  1140. (xevent->xclient.data.l[0] == videodata->WM_TAKE_FOCUS)) {
  1141. #ifdef DEBUG_XEVENTS
  1142. printf("window %p: WM_TAKE_FOCUS\n", data);
  1143. #endif
  1144. SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_TAKE_FOCUS, 0, 0);
  1145. break;
  1146. }
  1147. }
  1148. break;
  1149. /* Do we need to refresh ourselves? */
  1150. case Expose:{
  1151. #ifdef DEBUG_XEVENTS
  1152. printf("window %p: Expose (count = %d)\n", data, xevent->xexpose.count);
  1153. #endif
  1154. SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED, 0, 0);
  1155. }
  1156. break;
  1157. case MotionNotify:{
  1158. SDL_Mouse *mouse = SDL_GetMouse();
  1159. if(!mouse->relative_mode || mouse->relative_mode_warp) {
  1160. #ifdef DEBUG_MOTION
  1161. printf("window %p: X11 motion: %d,%d\n", data, xevent->xmotion.x, xevent->xmotion.y);
  1162. #endif
  1163. SDL_SendMouseMotion(data->window, 0, 0, xevent->xmotion.x, xevent->xmotion.y);
  1164. }
  1165. }
  1166. break;
  1167. case ButtonPress:{
  1168. int xticks = 0, yticks = 0;
  1169. #ifdef DEBUG_XEVENTS
  1170. printf("window %p: ButtonPress (X11 button = %d)\n", data, xevent->xbutton.button);
  1171. #endif
  1172. if (X11_IsWheelEvent(display,xevent,&xticks, &yticks)) {
  1173. SDL_SendMouseWheel(data->window, 0, (float) -xticks, (float) yticks, SDL_MOUSEWHEEL_NORMAL);
  1174. } else {
  1175. SDL_bool ignore_click = SDL_FALSE;
  1176. int button = xevent->xbutton.button;
  1177. if(button == Button1) {
  1178. if (ProcessHitTest(_this, data, xevent)) {
  1179. SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_HIT_TEST, 0, 0);
  1180. break; /* don't pass this event on to app. */
  1181. }
  1182. }
  1183. else if(button > 7) {
  1184. /* X button values 4-7 are used for scrolling, so X1 is 8, X2 is 9, ...
  1185. => subtract (8-SDL_BUTTON_X1) to get value SDL expects */
  1186. button -= (8-SDL_BUTTON_X1);
  1187. }
  1188. if (data->last_focus_event_time) {
  1189. const int X11_FOCUS_CLICK_TIMEOUT = 10;
  1190. if (!SDL_TICKS_PASSED(SDL_GetTicks(), data->last_focus_event_time + X11_FOCUS_CLICK_TIMEOUT)) {
  1191. ignore_click = !SDL_GetHintBoolean(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, SDL_FALSE);
  1192. }
  1193. data->last_focus_event_time = 0;
  1194. }
  1195. if (!ignore_click) {
  1196. SDL_SendMouseButton(data->window, 0, SDL_PRESSED, button);
  1197. }
  1198. }
  1199. X11_UpdateUserTime(data, xevent->xbutton.time);
  1200. }
  1201. break;
  1202. case ButtonRelease:{
  1203. int button = xevent->xbutton.button;
  1204. /* The X server sends a Release event for each Press for wheels. Ignore them. */
  1205. int xticks = 0, yticks = 0;
  1206. #ifdef DEBUG_XEVENTS
  1207. printf("window %p: ButtonRelease (X11 button = %d)\n", data, xevent->xbutton.button);
  1208. #endif
  1209. if (!X11_IsWheelEvent(display, xevent, &xticks, &yticks)) {
  1210. if (button > 7) {
  1211. /* see explanation at case ButtonPress */
  1212. button -= (8-SDL_BUTTON_X1);
  1213. }
  1214. SDL_SendMouseButton(data->window, 0, SDL_RELEASED, button);
  1215. }
  1216. }
  1217. break;
  1218. case PropertyNotify:{
  1219. #ifdef DEBUG_XEVENTS
  1220. unsigned char *propdata;
  1221. int status, real_format;
  1222. Atom real_type;
  1223. unsigned long items_read, items_left;
  1224. char *name = X11_XGetAtomName(display, xevent->xproperty.atom);
  1225. if (name) {
  1226. printf("window %p: PropertyNotify: %s %s time=%lu\n", data, name, (xevent->xproperty.state == PropertyDelete) ? "deleted" : "changed", xevent->xproperty.time);
  1227. X11_XFree(name);
  1228. }
  1229. status = X11_XGetWindowProperty(display, data->xwindow, xevent->xproperty.atom, 0L, 8192L, False, AnyPropertyType, &real_type, &real_format, &items_read, &items_left, &propdata);
  1230. if (status == Success && items_read > 0) {
  1231. if (real_type == XA_INTEGER) {
  1232. int *values = (int *)propdata;
  1233. printf("{");
  1234. for (i = 0; i < items_read; i++) {
  1235. printf(" %d", values[i]);
  1236. }
  1237. printf(" }\n");
  1238. } else if (real_type == XA_CARDINAL) {
  1239. if (real_format == 32) {
  1240. Uint32 *values = (Uint32 *)propdata;
  1241. printf("{");
  1242. for (i = 0; i < items_read; i++) {
  1243. printf(" %d", values[i]);
  1244. }
  1245. printf(" }\n");
  1246. } else if (real_format == 16) {
  1247. Uint16 *values = (Uint16 *)propdata;
  1248. printf("{");
  1249. for (i = 0; i < items_read; i++) {
  1250. printf(" %d", values[i]);
  1251. }
  1252. printf(" }\n");
  1253. } else if (real_format == 8) {
  1254. Uint8 *values = (Uint8 *)propdata;
  1255. printf("{");
  1256. for (i = 0; i < items_read; i++) {
  1257. printf(" %d", values[i]);
  1258. }
  1259. printf(" }\n");
  1260. }
  1261. } else if (real_type == XA_STRING ||
  1262. real_type == videodata->UTF8_STRING) {
  1263. printf("{ \"%s\" }\n", propdata);
  1264. } else if (real_type == XA_ATOM) {
  1265. Atom *atoms = (Atom *)propdata;
  1266. printf("{");
  1267. for (i = 0; i < items_read; i++) {
  1268. char *atomname = X11_XGetAtomName(display, atoms[i]);
  1269. if (atomname) {
  1270. printf(" %s", atomname);
  1271. X11_XFree(atomname);
  1272. }
  1273. }
  1274. printf(" }\n");
  1275. } else {
  1276. char *atomname = X11_XGetAtomName(display, real_type);
  1277. printf("Unknown type: %ld (%s)\n", real_type, atomname ? atomname : "UNKNOWN");
  1278. if (atomname) {
  1279. X11_XFree(atomname);
  1280. }
  1281. }
  1282. }
  1283. if (status == Success) {
  1284. X11_XFree(propdata);
  1285. }
  1286. #endif /* DEBUG_XEVENTS */
  1287. /* Take advantage of this moment to make sure user_time has a
  1288. valid timestamp from the X server, so if we later try to
  1289. raise/restore this window, _NET_ACTIVE_WINDOW can have a
  1290. non-zero timestamp, even if there's never been a mouse or
  1291. key press to this window so far. Note that we don't try to
  1292. set _NET_WM_USER_TIME here, though. That's only for legit
  1293. user interaction with the window. */
  1294. if (!data->user_time) {
  1295. data->user_time = xevent->xproperty.time;
  1296. }
  1297. if (xevent->xproperty.atom == data->videodata->_NET_WM_STATE) {
  1298. /* Get the new state from the window manager.
  1299. Compositing window managers can alter visibility of windows
  1300. without ever mapping / unmapping them, so we handle that here,
  1301. because they use the NETWM protocol to notify us of changes.
  1302. */
  1303. const Uint32 flags = X11_GetNetWMState(_this, xevent->xproperty.window);
  1304. const Uint32 changed = flags ^ data->window->flags;
  1305. if ((changed & SDL_WINDOW_HIDDEN) || (changed & SDL_WINDOW_FULLSCREEN)) {
  1306. if (flags & SDL_WINDOW_HIDDEN) {
  1307. X11_DispatchUnmapNotify(data);
  1308. } else {
  1309. X11_DispatchMapNotify(data);
  1310. }
  1311. }
  1312. if (changed & SDL_WINDOW_MAXIMIZED) {
  1313. if (flags & SDL_WINDOW_MAXIMIZED) {
  1314. SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
  1315. } else {
  1316. SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESTORED, 0, 0);
  1317. }
  1318. }
  1319. } else if (xevent->xproperty.atom == videodata->XKLAVIER_STATE) {
  1320. /* Hack for Ubuntu 12.04 (etc) that doesn't send MappingNotify
  1321. events when the keyboard layout changes (for example,
  1322. changing from English to French on the menubar's keyboard
  1323. icon). Since it changes the XKLAVIER_STATE property, we
  1324. notice and reinit our keymap here. This might not be the
  1325. right approach, but it seems to work. */
  1326. X11_UpdateKeymap(_this, SDL_TRUE);
  1327. } else if (xevent->xproperty.atom == videodata->_NET_FRAME_EXTENTS) {
  1328. Atom type;
  1329. int format;
  1330. unsigned long nitems, bytes_after;
  1331. unsigned char *property;
  1332. if (X11_XGetWindowProperty(display, data->xwindow, videodata->_NET_FRAME_EXTENTS, 0, 16, 0, XA_CARDINAL, &type, &format, &nitems, &bytes_after, &property) == Success) {
  1333. if (type != None && nitems == 4) {
  1334. data->border_left = (int) ((long*)property)[0];
  1335. data->border_right = (int) ((long*)property)[1];
  1336. data->border_top = (int) ((long*)property)[2];
  1337. data->border_bottom = (int) ((long*)property)[3];
  1338. }
  1339. X11_XFree(property);
  1340. #ifdef DEBUG_XEVENTS
  1341. printf("New _NET_FRAME_EXTENTS: left=%d right=%d, top=%d, bottom=%d\n", data->border_left, data->border_right, data->border_top, data->border_bottom);
  1342. #endif
  1343. }
  1344. }
  1345. }
  1346. break;
  1347. case SelectionNotify: {
  1348. Atom target = xevent->xselection.target;
  1349. #ifdef DEBUG_XEVENTS
  1350. printf("window %p: SelectionNotify (requestor = %ld, target = %ld)\n", data,
  1351. xevent->xselection.requestor, xevent->xselection.target);
  1352. #endif
  1353. if (target == data->xdnd_req) {
  1354. /* read data */
  1355. SDL_x11Prop p;
  1356. X11_ReadProperty(&p, display, data->xwindow, videodata->PRIMARY);
  1357. if (p.format == 8) {
  1358. char *saveptr = NULL;
  1359. char *name = X11_XGetAtomName(display, target);
  1360. if (name) {
  1361. char *token = SDL_strtokr((char *) p.data, "\r\n", &saveptr);
  1362. while (token != NULL) {
  1363. if (SDL_strcmp("text/plain", name) == 0) {
  1364. SDL_SendDropText(data->window, token);
  1365. } else if (SDL_strcmp("text/uri-list", name) == 0) {
  1366. char *fn = X11_URIToLocal(token);
  1367. if (fn) {
  1368. SDL_SendDropFile(data->window, fn);
  1369. }
  1370. }
  1371. token = SDL_strtokr(NULL, "\r\n", &saveptr);
  1372. }
  1373. X11_XFree(name);
  1374. }
  1375. SDL_SendDropComplete(data->window);
  1376. }
  1377. X11_XFree(p.data);
  1378. /* send reply */
  1379. SDL_memset(&m, 0, sizeof(XClientMessageEvent));
  1380. m.type = ClientMessage;
  1381. m.display = display;
  1382. m.window = data->xdnd_source;
  1383. m.message_type = videodata->XdndFinished;
  1384. m.format = 32;
  1385. m.data.l[0] = data->xwindow;
  1386. m.data.l[1] = 1;
  1387. m.data.l[2] = videodata->XdndActionCopy;
  1388. X11_XSendEvent(display, data->xdnd_source, False, NoEventMask, (XEvent*)&m);
  1389. X11_XSync(display, False);
  1390. }
  1391. }
  1392. break;
  1393. default:{
  1394. #ifdef DEBUG_XEVENTS
  1395. printf("window %p: Unhandled event %d\n", data, xevent->type);
  1396. #endif
  1397. }
  1398. break;
  1399. }
  1400. }
  1401. static void
  1402. X11_HandleFocusChanges(_THIS)
  1403. {
  1404. SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
  1405. int i;
  1406. if (videodata && videodata->windowlist) {
  1407. for (i = 0; i < videodata->numwindows; ++i) {
  1408. SDL_WindowData *data = videodata->windowlist[i];
  1409. if (data && data->pending_focus != PENDING_FOCUS_NONE) {
  1410. Uint32 now = SDL_GetTicks();
  1411. if (SDL_TICKS_PASSED(now, data->pending_focus_time)) {
  1412. if (data->pending_focus == PENDING_FOCUS_IN) {
  1413. X11_DispatchFocusIn(_this, data);
  1414. } else {
  1415. X11_DispatchFocusOut(_this, data);
  1416. }
  1417. data->pending_focus = PENDING_FOCUS_NONE;
  1418. }
  1419. }
  1420. }
  1421. }
  1422. }
  1423. static Bool
  1424. isAnyEvent(Display *display, XEvent *ev, XPointer arg)
  1425. {
  1426. return True;
  1427. }
  1428. static SDL_bool
  1429. X11_PollEvent(Display *display, XEvent *event)
  1430. {
  1431. if (!X11_XCheckIfEvent(display, event, isAnyEvent, NULL)) {
  1432. return SDL_FALSE;
  1433. }
  1434. return SDL_TRUE;
  1435. }
  1436. void
  1437. X11_SendWakeupEvent(_THIS, SDL_Window *window)
  1438. {
  1439. SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
  1440. Display *req_display = data->request_display;
  1441. Window xwindow = ((SDL_WindowData *) window->driverdata)->xwindow;
  1442. XClientMessageEvent event;
  1443. SDL_memset(&event, 0, sizeof(XClientMessageEvent));
  1444. event.type = ClientMessage;
  1445. event.display = req_display;
  1446. event.send_event = True;
  1447. event.message_type = data->_SDL_WAKEUP;
  1448. event.format = 8;
  1449. X11_XSendEvent(req_display, xwindow, False, NoEventMask, (XEvent *) &event);
  1450. /* XSendEvent returns a status and it could be BadValue or BadWindow. If an
  1451. error happens it is an SDL's internal error and there is nothing we can do here. */
  1452. X11_XFlush(req_display);
  1453. }
  1454. int
  1455. X11_WaitEventTimeout(_THIS, int timeout)
  1456. {
  1457. SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
  1458. Display *display;
  1459. XEvent xevent;
  1460. display = videodata->display;
  1461. SDL_zero(xevent);
  1462. /* Flush and poll to grab any events already read and queued */
  1463. X11_XFlush(display);
  1464. if (X11_PollEvent(display, &xevent)) {
  1465. /* Fall through */
  1466. } else if (timeout == 0) {
  1467. return 0;
  1468. } else {
  1469. /* Use SDL_IOR_NO_RETRY to ensure SIGINT will break us out of our wait */
  1470. int err = SDL_IOReady(ConnectionNumber(display), SDL_IOR_READ | SDL_IOR_NO_RETRY, timeout);
  1471. if (err > 0) {
  1472. if (!X11_PollEvent(display, &xevent)) {
  1473. /* Someone may have beat us to reading the fd. Return 1 here to
  1474. * trigger the normal spurious wakeup logic in the event core. */
  1475. return 1;
  1476. }
  1477. } else if (err == 0) {
  1478. /* Timeout */
  1479. return 0;
  1480. } else {
  1481. /* Error returned from poll()/select() */
  1482. if (errno == EINTR) {
  1483. /* If the wait was interrupted by a signal, we may have generated a
  1484. * SDL_QUIT event. Let the caller know to call SDL_PumpEvents(). */
  1485. return 1;
  1486. } else {
  1487. return err;
  1488. }
  1489. }
  1490. }
  1491. X11_DispatchEvent(_this, &xevent);
  1492. #ifdef SDL_USE_IME
  1493. if(SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE){
  1494. SDL_IME_PumpEvents();
  1495. }
  1496. #endif
  1497. return 1;
  1498. }
  1499. void
  1500. X11_PumpEvents(_THIS)
  1501. {
  1502. SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
  1503. XEvent xevent;
  1504. int i;
  1505. if (data->last_mode_change_deadline) {
  1506. if (SDL_TICKS_PASSED(SDL_GetTicks(), data->last_mode_change_deadline)) {
  1507. data->last_mode_change_deadline = 0; /* assume we're done. */
  1508. }
  1509. }
  1510. /* Update activity every 30 seconds to prevent screensaver */
  1511. if (_this->suspend_screensaver) {
  1512. const Uint32 now = SDL_GetTicks();
  1513. if (!data->screensaver_activity ||
  1514. SDL_TICKS_PASSED(now, data->screensaver_activity + 30000)) {
  1515. X11_XResetScreenSaver(data->display);
  1516. #if SDL_USE_LIBDBUS
  1517. SDL_DBus_ScreensaverTickle();
  1518. #endif
  1519. data->screensaver_activity = now;
  1520. }
  1521. }
  1522. SDL_zero(xevent);
  1523. /* Keep processing pending events */
  1524. while (X11_PollEvent(data->display, &xevent)) {
  1525. X11_DispatchEvent(_this, &xevent);
  1526. }
  1527. #ifdef SDL_USE_IME
  1528. if(SDL_GetEventState(SDL_TEXTINPUT) == SDL_ENABLE){
  1529. SDL_IME_PumpEvents();
  1530. }
  1531. #endif
  1532. /* FIXME: Only need to do this when there are pending focus changes */
  1533. X11_HandleFocusChanges(_this);
  1534. /* FIXME: Only need to do this when there are flashing windows */
  1535. for (i = 0; i < data->numwindows; ++i) {
  1536. if (data->windowlist[i] != NULL &&
  1537. data->windowlist[i]->flash_cancel_time &&
  1538. SDL_TICKS_PASSED(SDL_GetTicks(), data->windowlist[i]->flash_cancel_time)) {
  1539. X11_FlashWindow(_this, data->windowlist[i]->window, SDL_FLASH_CANCEL);
  1540. }
  1541. }
  1542. }
  1543. void
  1544. X11_SuspendScreenSaver(_THIS)
  1545. {
  1546. #if SDL_VIDEO_DRIVER_X11_XSCRNSAVER
  1547. SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
  1548. int dummy;
  1549. int major_version, minor_version;
  1550. #endif /* SDL_VIDEO_DRIVER_X11_XSCRNSAVER */
  1551. #if SDL_USE_LIBDBUS
  1552. if (SDL_DBus_ScreensaverInhibit(_this->suspend_screensaver)) {
  1553. return;
  1554. }
  1555. if (_this->suspend_screensaver) {
  1556. SDL_DBus_ScreensaverTickle();
  1557. }
  1558. #endif
  1559. #if SDL_VIDEO_DRIVER_X11_XSCRNSAVER
  1560. if (SDL_X11_HAVE_XSS) {
  1561. /* X11_XScreenSaverSuspend was introduced in MIT-SCREEN-SAVER 1.1 */
  1562. if (!X11_XScreenSaverQueryExtension(data->display, &dummy, &dummy) ||
  1563. !X11_XScreenSaverQueryVersion(data->display,
  1564. &major_version, &minor_version) ||
  1565. major_version < 1 || (major_version == 1 && minor_version < 1)) {
  1566. return;
  1567. }
  1568. X11_XScreenSaverSuspend(data->display, _this->suspend_screensaver);
  1569. X11_XResetScreenSaver(data->display);
  1570. }
  1571. #endif
  1572. }
  1573. #endif /* SDL_VIDEO_DRIVER_X11 */
  1574. /* vi: set ts=4 sw=4 expandtab: */