SDL_x11mouse.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2015 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 <X11/cursorfont.h>
  21. #include "SDL_assert.h"
  22. #include "SDL_x11video.h"
  23. #include "SDL_x11mouse.h"
  24. #include "SDL_x11xinput2.h"
  25. #include "../../events/SDL_mouse_c.h"
  26. /* FIXME: Find a better place to put this... */
  27. static Cursor x11_empty_cursor = None;
  28. static Display *
  29. GetDisplay(void)
  30. {
  31. return ((SDL_VideoData *)SDL_GetVideoDevice()->driverdata)->display;
  32. }
  33. static Cursor
  34. X11_CreateEmptyCursor()
  35. {
  36. if (x11_empty_cursor == None) {
  37. Display *display = GetDisplay();
  38. char data[1];
  39. XColor color;
  40. Pixmap pixmap;
  41. SDL_zero(data);
  42. color.red = color.green = color.blue = 0;
  43. pixmap = X11_XCreateBitmapFromData(display, DefaultRootWindow(display),
  44. data, 1, 1);
  45. if (pixmap) {
  46. x11_empty_cursor = X11_XCreatePixmapCursor(display, pixmap, pixmap,
  47. &color, &color, 0, 0);
  48. X11_XFreePixmap(display, pixmap);
  49. }
  50. }
  51. return x11_empty_cursor;
  52. }
  53. static void
  54. X11_DestroyEmptyCursor(void)
  55. {
  56. if (x11_empty_cursor != None) {
  57. X11_XFreeCursor(GetDisplay(), x11_empty_cursor);
  58. x11_empty_cursor = None;
  59. }
  60. }
  61. static SDL_Cursor *
  62. X11_CreateDefaultCursor()
  63. {
  64. SDL_Cursor *cursor;
  65. cursor = SDL_calloc(1, sizeof(*cursor));
  66. if (cursor) {
  67. /* None is used to indicate the default cursor */
  68. cursor->driverdata = (void*)None;
  69. } else {
  70. SDL_OutOfMemory();
  71. }
  72. return cursor;
  73. }
  74. #if SDL_VIDEO_DRIVER_X11_XCURSOR
  75. static Cursor
  76. X11_CreateXCursorCursor(SDL_Surface * surface, int hot_x, int hot_y)
  77. {
  78. Display *display = GetDisplay();
  79. Cursor cursor = None;
  80. XcursorImage *image;
  81. image = X11_XcursorImageCreate(surface->w, surface->h);
  82. if (!image) {
  83. SDL_OutOfMemory();
  84. return None;
  85. }
  86. image->xhot = hot_x;
  87. image->yhot = hot_y;
  88. image->delay = 0;
  89. SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
  90. SDL_assert(surface->pitch == surface->w * 4);
  91. SDL_memcpy(image->pixels, surface->pixels, surface->h * surface->pitch);
  92. cursor = X11_XcursorImageLoadCursor(display, image);
  93. X11_XcursorImageDestroy(image);
  94. return cursor;
  95. }
  96. #endif /* SDL_VIDEO_DRIVER_X11_XCURSOR */
  97. static Cursor
  98. X11_CreatePixmapCursor(SDL_Surface * surface, int hot_x, int hot_y)
  99. {
  100. Display *display = GetDisplay();
  101. XColor fg, bg;
  102. Cursor cursor = None;
  103. Uint32 *ptr;
  104. Uint8 *data_bits, *mask_bits;
  105. Pixmap data_pixmap, mask_pixmap;
  106. int x, y;
  107. unsigned int rfg, gfg, bfg, rbg, gbg, bbg, fgBits, bgBits;
  108. unsigned int width_bytes = ((surface->w + 7) & ~7) / 8;
  109. data_bits = SDL_calloc(1, surface->h * width_bytes);
  110. if (!data_bits) {
  111. SDL_OutOfMemory();
  112. return None;
  113. }
  114. mask_bits = SDL_calloc(1, surface->h * width_bytes);
  115. if (!mask_bits) {
  116. SDL_free(data_bits);
  117. SDL_OutOfMemory();
  118. return None;
  119. }
  120. /* Code below assumes ARGB pixel format */
  121. SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
  122. rfg = gfg = bfg = rbg = gbg = bbg = fgBits = bgBits = 0;
  123. for (y = 0; y < surface->h; ++y) {
  124. ptr = (Uint32 *)((Uint8 *)surface->pixels + y * surface->pitch);
  125. for (x = 0; x < surface->w; ++x) {
  126. int alpha = (*ptr >> 24) & 0xff;
  127. int red = (*ptr >> 16) & 0xff;
  128. int green = (*ptr >> 8) & 0xff;
  129. int blue = (*ptr >> 0) & 0xff;
  130. if (alpha > 25) {
  131. mask_bits[y * width_bytes + x / 8] |= (0x01 << (x % 8));
  132. if ((red + green + blue) > 0x40) {
  133. fgBits++;
  134. rfg += red;
  135. gfg += green;
  136. bfg += blue;
  137. data_bits[y * width_bytes + x / 8] |= (0x01 << (x % 8));
  138. } else {
  139. bgBits++;
  140. rbg += red;
  141. gbg += green;
  142. bbg += blue;
  143. }
  144. }
  145. ++ptr;
  146. }
  147. }
  148. if (fgBits) {
  149. fg.red = rfg * 257 / fgBits;
  150. fg.green = gfg * 257 / fgBits;
  151. fg.blue = bfg * 257 / fgBits;
  152. }
  153. else fg.red = fg.green = fg.blue = 0;
  154. if (bgBits) {
  155. bg.red = rbg * 257 / bgBits;
  156. bg.green = gbg * 257 / bgBits;
  157. bg.blue = bbg * 257 / bgBits;
  158. }
  159. else bg.red = bg.green = bg.blue = 0;
  160. data_pixmap = X11_XCreateBitmapFromData(display, DefaultRootWindow(display),
  161. (char*)data_bits,
  162. surface->w, surface->h);
  163. mask_pixmap = X11_XCreateBitmapFromData(display, DefaultRootWindow(display),
  164. (char*)mask_bits,
  165. surface->w, surface->h);
  166. cursor = X11_XCreatePixmapCursor(display, data_pixmap, mask_pixmap,
  167. &fg, &bg, hot_x, hot_y);
  168. X11_XFreePixmap(display, data_pixmap);
  169. X11_XFreePixmap(display, mask_pixmap);
  170. return cursor;
  171. }
  172. static SDL_Cursor *
  173. X11_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
  174. {
  175. SDL_Cursor *cursor;
  176. cursor = SDL_calloc(1, sizeof(*cursor));
  177. if (cursor) {
  178. Cursor x11_cursor = None;
  179. #if SDL_VIDEO_DRIVER_X11_XCURSOR
  180. if (SDL_X11_HAVE_XCURSOR) {
  181. x11_cursor = X11_CreateXCursorCursor(surface, hot_x, hot_y);
  182. }
  183. #endif
  184. if (x11_cursor == None) {
  185. x11_cursor = X11_CreatePixmapCursor(surface, hot_x, hot_y);
  186. }
  187. cursor->driverdata = (void*)x11_cursor;
  188. } else {
  189. SDL_OutOfMemory();
  190. }
  191. return cursor;
  192. }
  193. static SDL_Cursor *
  194. X11_CreateSystemCursor(SDL_SystemCursor id)
  195. {
  196. SDL_Cursor *cursor;
  197. unsigned int shape;
  198. switch(id)
  199. {
  200. default:
  201. SDL_assert(0);
  202. return NULL;
  203. /* X Font Cursors reference: */
  204. /* http://tronche.com/gui/x/xlib/appendix/b/ */
  205. case SDL_SYSTEM_CURSOR_ARROW: shape = XC_left_ptr; break;
  206. case SDL_SYSTEM_CURSOR_IBEAM: shape = XC_xterm; break;
  207. case SDL_SYSTEM_CURSOR_WAIT: shape = XC_watch; break;
  208. case SDL_SYSTEM_CURSOR_CROSSHAIR: shape = XC_tcross; break;
  209. case SDL_SYSTEM_CURSOR_WAITARROW: shape = XC_watch; break;
  210. case SDL_SYSTEM_CURSOR_SIZENWSE: shape = XC_fleur; break;
  211. case SDL_SYSTEM_CURSOR_SIZENESW: shape = XC_fleur; break;
  212. case SDL_SYSTEM_CURSOR_SIZEWE: shape = XC_sb_h_double_arrow; break;
  213. case SDL_SYSTEM_CURSOR_SIZENS: shape = XC_sb_v_double_arrow; break;
  214. case SDL_SYSTEM_CURSOR_SIZEALL: shape = XC_fleur; break;
  215. case SDL_SYSTEM_CURSOR_NO: shape = XC_pirate; break;
  216. case SDL_SYSTEM_CURSOR_HAND: shape = XC_hand2; break;
  217. }
  218. cursor = SDL_calloc(1, sizeof(*cursor));
  219. if (cursor) {
  220. Cursor x11_cursor;
  221. x11_cursor = X11_XCreateFontCursor(GetDisplay(), shape);
  222. cursor->driverdata = (void*)x11_cursor;
  223. } else {
  224. SDL_OutOfMemory();
  225. }
  226. return cursor;
  227. }
  228. static void
  229. X11_FreeCursor(SDL_Cursor * cursor)
  230. {
  231. Cursor x11_cursor = (Cursor)cursor->driverdata;
  232. if (x11_cursor != None) {
  233. X11_XFreeCursor(GetDisplay(), x11_cursor);
  234. }
  235. SDL_free(cursor);
  236. }
  237. static int
  238. X11_ShowCursor(SDL_Cursor * cursor)
  239. {
  240. Cursor x11_cursor = 0;
  241. if (cursor) {
  242. x11_cursor = (Cursor)cursor->driverdata;
  243. } else {
  244. x11_cursor = X11_CreateEmptyCursor();
  245. }
  246. /* FIXME: Is there a better way than this? */
  247. {
  248. SDL_VideoDevice *video = SDL_GetVideoDevice();
  249. Display *display = GetDisplay();
  250. SDL_Window *window;
  251. SDL_WindowData *data;
  252. for (window = video->windows; window; window = window->next) {
  253. data = (SDL_WindowData *)window->driverdata;
  254. if (x11_cursor != None) {
  255. X11_XDefineCursor(display, data->xwindow, x11_cursor);
  256. } else {
  257. X11_XUndefineCursor(display, data->xwindow);
  258. }
  259. }
  260. X11_XFlush(display);
  261. }
  262. return 0;
  263. }
  264. static void
  265. X11_WarpMouse(SDL_Window * window, int x, int y)
  266. {
  267. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  268. Display *display = data->videodata->display;
  269. X11_XWarpPointer(display, None, data->xwindow, 0, 0, 0, 0, x, y);
  270. X11_XSync(display, False);
  271. }
  272. static int
  273. X11_WarpMouseGlobal(int x, int y)
  274. {
  275. Display *display = GetDisplay();
  276. X11_XWarpPointer(display, None, DefaultRootWindow(display), 0, 0, 0, 0, x, y);
  277. X11_XSync(display, False);
  278. return 0;
  279. }
  280. static int
  281. X11_SetRelativeMouseMode(SDL_bool enabled)
  282. {
  283. #if SDL_VIDEO_DRIVER_X11_XINPUT2
  284. if(X11_Xinput2IsInitialized())
  285. return 0;
  286. #else
  287. SDL_Unsupported();
  288. #endif
  289. return -1;
  290. }
  291. static int
  292. X11_CaptureMouse(SDL_Window *window)
  293. {
  294. Display *display = GetDisplay();
  295. if (window) {
  296. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  297. const unsigned int mask = ButtonPressMask | ButtonReleaseMask | PointerMotionMask | FocusChangeMask;
  298. const int rc = X11_XGrabPointer(display, data->xwindow, False,
  299. mask, GrabModeAsync, GrabModeAsync,
  300. None, None, CurrentTime);
  301. if (rc != GrabSuccess) {
  302. return SDL_SetError("X server refused mouse capture");
  303. }
  304. } else {
  305. X11_XUngrabPointer(display, CurrentTime);
  306. }
  307. X11_XSync(display, False);
  308. return 0;
  309. }
  310. static Uint32
  311. X11_GetGlobalMouseState(int *x, int *y)
  312. {
  313. Display *display = GetDisplay();
  314. const int num_screens = SDL_GetNumVideoDisplays();
  315. int i;
  316. /* !!! FIXME: should we XSync() here first? */
  317. for (i = 0; i < num_screens; i++) {
  318. SDL_DisplayData *data = (SDL_DisplayData *) SDL_GetDisplayDriverData(i);
  319. if (data != NULL) {
  320. Window root, child;
  321. int rootx, rooty, winx, winy;
  322. unsigned int mask;
  323. if (X11_XQueryPointer(display, RootWindow(display, data->screen), &root, &child, &rootx, &rooty, &winx, &winy, &mask)) {
  324. XWindowAttributes root_attrs;
  325. Uint32 retval = 0;
  326. retval |= (mask & Button1Mask) ? SDL_BUTTON_LMASK : 0;
  327. retval |= (mask & Button2Mask) ? SDL_BUTTON_MMASK : 0;
  328. retval |= (mask & Button3Mask) ? SDL_BUTTON_RMASK : 0;
  329. /* SDL_DisplayData->x,y point to screen origin, and adding them to mouse coordinates relative to root window doesn't do the right thing
  330. * (observed on dual monitor setup with primary display being the rightmost one - mouse was offset to the right).
  331. *
  332. * Adding root position to root-relative coordinates seems to be a better way to get absolute position. */
  333. X11_XGetWindowAttributes(display, root, &root_attrs);
  334. *x = root_attrs.x + rootx;
  335. *y = root_attrs.y + rooty;
  336. return retval;
  337. }
  338. }
  339. }
  340. SDL_assert(0 && "The pointer wasn't on any X11 screen?!");
  341. return 0;
  342. }
  343. void
  344. X11_InitMouse(_THIS)
  345. {
  346. SDL_Mouse *mouse = SDL_GetMouse();
  347. mouse->CreateCursor = X11_CreateCursor;
  348. mouse->CreateSystemCursor = X11_CreateSystemCursor;
  349. mouse->ShowCursor = X11_ShowCursor;
  350. mouse->FreeCursor = X11_FreeCursor;
  351. mouse->WarpMouse = X11_WarpMouse;
  352. mouse->WarpMouseGlobal = X11_WarpMouseGlobal;
  353. mouse->SetRelativeMouseMode = X11_SetRelativeMouseMode;
  354. mouse->CaptureMouse = X11_CaptureMouse;
  355. mouse->GetGlobalMouseState = X11_GetGlobalMouseState;
  356. SDL_SetDefaultCursor(X11_CreateDefaultCursor());
  357. }
  358. void
  359. X11_QuitMouse(_THIS)
  360. {
  361. X11_DestroyEmptyCursor();
  362. }
  363. #endif /* SDL_VIDEO_DRIVER_X11 */
  364. /* vi: set ts=4 sw=4 expandtab: */