mouse.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * mouse.c
  3. * Copyright © 2008, 2009 Martin Duquesnoy <xorg62@gmail.com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of the nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "wmfs.h"
  33. /** Draw the border when a client in dragging/resizing with mouse
  34. */
  35. void
  36. mouse_dragborder(XRectangle geo, GC g)
  37. {
  38. XDrawRectangle(dpy, ROOT, g,
  39. geo.x - BORDH / 2,
  40. geo.y - (TBARH - (BORDH / 2)),
  41. geo.width + BORDH,
  42. geo.height + TBARH);
  43. return;
  44. }
  45. /** Move a client in tile grid with the mouse
  46. *\param c Client double pointer
  47. */
  48. void
  49. mouse_move_tile_client(Client **c)
  50. {
  51. Client *sc;
  52. Window w;
  53. int d;
  54. if(!((*c)->flags & TileFlag) && !((*c)->flags & LMaxFlag))
  55. return;
  56. XQueryPointer(dpy, ROOT, &w, &w, &d, &d, &d, &d, (uint*)&d);
  57. if(((sc = client_gb_win(w)) || (sc = client_gb_frame(w)) || (sc = client_gb_titlebar(w)))
  58. && (*c)->win != sc->win && !((*c)->flags & HideFlag) && !(sc->flags & HideFlag))
  59. {
  60. client_swap(sc, *c);
  61. client_focus(sc);
  62. swap_ptr((void**)c, (void**)&sc);
  63. }
  64. return;
  65. }
  66. /** Move a client from one tag to another with dah mouse
  67. *\param c client pointer
  68. */
  69. void
  70. mouse_move_tag_client(Client *c)
  71. {
  72. Window w;
  73. int i, d, s;
  74. if(!(c->flags & TileFlag) && !(c->flags & LMaxFlag))
  75. return;
  76. s = c->screen;
  77. XQueryPointer(dpy, infobar[selscreen].bar->win, &w, &w, &d, &d, &d, &d, (uint*)&d);
  78. for(i = 1; i < conf.ntag[selscreen] + 1; ++i)
  79. if(infobar[selscreen].tags[i]->win == w
  80. && tags[selscreen][i].layout.func != freelayout)
  81. {
  82. c->screen = selscreen;
  83. c->tag = i;
  84. tags[c->screen][c->tag].request_update = True;
  85. arrange(s, True);
  86. if(s != c->screen)
  87. arrange(c->screen, True);
  88. }
  89. return;
  90. }
  91. /** Move the client with the mouse
  92. * \param c Client pointer
  93. */
  94. void
  95. mouse_move(Client *c)
  96. {
  97. int ocx, ocy, mx, my;
  98. int dint;
  99. uint duint;
  100. Window dw;
  101. XRectangle geo = c->geo;
  102. XGCValues xgc;
  103. GC gci;
  104. XEvent ev;
  105. if((c->flags & MaxFlag) || (c->flags & FSSFlag))
  106. return;
  107. ocx = c->geo.x;
  108. ocy = c->geo.y;
  109. if(XGrabPointer(dpy, ROOT, False, MouseMask, GrabModeAsync, GrabModeAsync,
  110. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  111. return;
  112. if(!(c->flags & TileFlag) && !(c->flags & LMaxFlag))
  113. XGrabServer(dpy);
  114. /* Set the GC for the rectangle */
  115. xgc.function = GXinvert;
  116. xgc.subwindow_mode = IncludeInferiors;
  117. xgc.line_width = BORDH;
  118. gci = XCreateGC(dpy, ROOT, GCFunction | GCSubwindowMode | GCLineWidth, &xgc);
  119. if(!(c->flags & TileFlag) && !(c->flags & LMaxFlag))
  120. mouse_dragborder(c->geo, gci);
  121. XQueryPointer(dpy, ROOT, &dw, &dw, &mx, &my, &dint, &dint, &duint);
  122. do
  123. {
  124. XMaskEvent(dpy, MouseMask | SubstructureRedirectMask, &ev);
  125. screen_get_sel();
  126. if(ev.type == MotionNotify)
  127. {
  128. mouse_move_tile_client(&c);
  129. mouse_move_tag_client(c);
  130. /* To move a client normally, in freelayout */
  131. if(!(c->flags & TileFlag) && !(c->flags & LMaxFlag))
  132. {
  133. mouse_dragborder(geo, gci);
  134. geo.x = (ocx + (ev.xmotion.x - mx));
  135. geo.y = (ocy + (ev.xmotion.y - my));
  136. /*
  137. * Need to draw 2 times the same rectangle because
  138. * it is draw with the revert color; revert + revert = normal
  139. */
  140. mouse_dragborder(geo, gci);
  141. }
  142. }
  143. else if(ev.type == MapRequest
  144. || ev.type == ConfigureRequest)
  145. getevent(ev);
  146. }
  147. while(ev.type != ButtonRelease);
  148. /* One time again to delete all the trace on the window */
  149. if(!(c->flags & TileFlag) && !(c->flags & LMaxFlag))
  150. {
  151. mouse_dragborder(geo, gci);
  152. client_moveresize(c, geo, False);
  153. frame_update(c);
  154. XUngrabServer(dpy);
  155. }
  156. client_update_attributes(c);
  157. XUngrabPointer(dpy, CurrentTime);
  158. XFreeGC(dpy, gci);
  159. return;
  160. }
  161. /** Resize a client with the mouse
  162. * \param c Client pointer
  163. */
  164. void
  165. mouse_resize(Client *c)
  166. {
  167. XRectangle geo = c->geo, ogeo = c->geo;
  168. Position pos = Right;
  169. XEvent ev;
  170. Window w;
  171. int d, u, omx, omy;
  172. XGCValues xgc;
  173. GC gci;
  174. float mwf = tags[selscreen][seltag[selscreen]].mwfact;
  175. if((c->flags & MaxFlag)
  176. || (c->flags & LMaxFlag)
  177. || (c->flags & FSSFlag))
  178. return;
  179. XQueryPointer(dpy, ROOT, &w, &w, &omx, &omy, &d, &d, (uint *)&u);
  180. if((omx - c->geo.x) < (c->geo.width / 2))
  181. pos = Left;
  182. if(XGrabPointer(dpy, ROOT, False, MouseMask, GrabModeAsync, GrabModeAsync, None,
  183. cursor[((c->flags & TileFlag) ? CurResize : ((pos == Right) ? CurRightResize : CurLeftResize))],
  184. CurrentTime) != GrabSuccess)
  185. return;
  186. if(!(c->flags & TileFlag))
  187. XGrabServer(dpy);
  188. /* Set the GC for the rectangle */
  189. xgc.function = GXinvert;
  190. xgc.subwindow_mode = IncludeInferiors;
  191. xgc.line_width = BORDH;
  192. gci = XCreateGC(dpy, ROOT, GCFunction | GCSubwindowMode | GCLineWidth, &xgc);
  193. if(!(c->flags & TileFlag))
  194. {
  195. if(pos == Right)
  196. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->geo.width + conf.client.borderheight, c->geo.height);
  197. else
  198. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, 0, c->geo.height);
  199. mouse_dragborder(c->geo, gci);
  200. }
  201. do
  202. {
  203. XMaskEvent(dpy, MouseMask | SubstructureRedirectMask, &ev);
  204. if(ev.type == MotionNotify)
  205. {
  206. /* To resize MWFACT in tile mode */
  207. if((c->flags & TileFlag)
  208. && tags[selscreen][seltag[selscreen]].layout.func != grid)
  209. {
  210. if(tags[selscreen][seltag[selscreen]].layout.func == tile)
  211. mwf += (ROUND(ev.xmotion.x_root) - omx) / (sgeo[c->screen].width);
  212. else if(tags[selscreen][seltag[selscreen]].layout.func == tile_left)
  213. mwf -= (ROUND(ev.xmotion.x_root) - omx) / (sgeo[c->screen].width);
  214. else if(tags[selscreen][seltag[selscreen]].layout.func == tile_top)
  215. mwf -= (ROUND(ev.xmotion.y_root) - omy) / (sgeo[c->screen].height);
  216. else
  217. mwf += (ROUND(ev.xmotion.y_root) - omy) / (sgeo[c->screen].height);
  218. omx = ROUND(ev.xmotion.x_root);
  219. omy = ROUND(ev.xmotion.y_root);
  220. tags[selscreen][seltag[selscreen]].mwfact = (mwf < 0.05) ? 0.05 : ((mwf > 0.95) ? 0.95 : mwf);
  221. }
  222. /* Free mode */
  223. else if(!(c->flags & TileFlag))
  224. {
  225. mouse_dragborder(geo, gci);
  226. if(pos == Right)
  227. {
  228. geo.width = ((ev.xmotion.x - c->geo.x < c->minw) ? c->minw : ev.xmotion.x - c->geo.x);
  229. geo.height = ((ev.xmotion.y - c->geo.y < c->minh) ? c->minh : ev.xmotion.y - c->geo.y);
  230. }
  231. else
  232. {
  233. geo.x = (geo.width != c->maxw) ? c->geo.x - (c->geo.x - ev.xmotion.x) : geo.x;
  234. geo.width = ((c->geo.width + (c->geo.x - geo.x) < c->minw)
  235. ? c->minw && (geo.x = (c->geo.x + c->geo.width) - c->minw)
  236. : c->geo.width + (c->geo.x - geo.x));
  237. geo.height = ((ev.xmotion.y - c->geo.y <= c->minh) ? c->minh : ev.xmotion.y - c->geo.y);
  238. }
  239. client_geo_hints(&geo, c);
  240. mouse_dragborder((ogeo = geo), gci);
  241. XSync(dpy, False);
  242. }
  243. }
  244. }
  245. while(ev.type != ButtonRelease);
  246. if(!(c->flags & TileFlag))
  247. {
  248. mouse_dragborder(ogeo, gci);
  249. client_moveresize(c, geo, True);
  250. frame_update(c);
  251. XUngrabServer(dpy);
  252. }
  253. else
  254. tags[selscreen][seltag[selscreen]].layout.func(c->screen);
  255. client_update_attributes(c);
  256. XUngrabPointer(dpy, CurrentTime);
  257. XFreeGC(dpy, gci);
  258. return;
  259. }
  260. /** Grab buttons
  261. * \param c Client pointer
  262. * \param focused For know if c is or not focused
  263. */
  264. void
  265. mouse_grabbuttons(Client *c, Bool focused)
  266. {
  267. int i;
  268. uint but[] = {Button1, Button2, Button3, Button4, Button5};
  269. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  270. if(focused)
  271. for(i = 0; i < LEN(but); ++i)
  272. {
  273. XGrabButton(dpy, but[i], conf.client.mod, c->win, False,
  274. ButtonMask, GrabModeAsync,GrabModeSync, None, None);
  275. XGrabButton(dpy, but[i], conf.client.mod|LockMask, c->win, False,
  276. ButtonMask, GrabModeAsync,GrabModeSync, None, None);
  277. XGrabButton(dpy, but[i], conf.client.mod|numlockmask, c->win, False,
  278. ButtonMask, GrabModeAsync,GrabModeSync, None, None);
  279. XGrabButton(dpy, but[i], conf.client.mod|LockMask|numlockmask, c->win, False,
  280. ButtonMask, GrabModeAsync,GrabModeSync, None, None);
  281. }
  282. else
  283. XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
  284. ButtonMask, GrabModeAsync, GrabModeSync, None, None);
  285. return;
  286. }
  287. /** Move the selected client
  288. * \param cmd uicb_t type unused
  289. */
  290. void
  291. uicb_mouse_move(uicb_t cmd)
  292. {
  293. CHECK(sel);
  294. mouse_move(sel);
  295. return;
  296. }
  297. /** Reisze the selected client
  298. * \param cmd uicb_t type unused
  299. */
  300. void
  301. uicb_mouse_resize(uicb_t cmd)
  302. {
  303. CHECK(sel);
  304. mouse_resize(sel);
  305. return;
  306. }