client.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3. * See LICENSE file for license details.
  4. */
  5. #include "dwm.h"
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <X11/Xatom.h>
  9. #include <X11/Xutil.h>
  10. /* static functions */
  11. static void
  12. resizetitle(Client *c)
  13. {
  14. int i;
  15. c->tw = 0;
  16. for(i = 0; i < TLast; i++)
  17. if(c->tags[i])
  18. c->tw += textw(c->tags[i]);
  19. c->tw += textw(c->name);
  20. if(c->tw > c->w)
  21. c->tw = c->w + 2;
  22. c->tx = c->x + c->w - c->tw + 2;
  23. c->ty = c->y;
  24. XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
  25. }
  26. static int
  27. xerrordummy(Display *dsply, XErrorEvent *ee)
  28. {
  29. return 0;
  30. }
  31. /* extern functions */
  32. void
  33. ban(Client *c)
  34. {
  35. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  36. XMoveWindow(dpy, c->title, c->tx + 2 * sw, c->ty);
  37. }
  38. void
  39. focus(Client *c)
  40. {
  41. Client *old = sel;
  42. XEvent ev;
  43. sel = c;
  44. if(old && old != c)
  45. drawtitle(old);
  46. drawtitle(c);
  47. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  48. XSync(dpy, False);
  49. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  50. }
  51. void
  52. focusnext(Arg *arg)
  53. {
  54. Client *c;
  55. if(!sel)
  56. return;
  57. if(!(c = getnext(sel->next, tsel)))
  58. c = getnext(clients, tsel);
  59. if(c) {
  60. higher(c);
  61. c->revert = sel;
  62. focus(c);
  63. }
  64. }
  65. void
  66. focusprev(Arg *arg)
  67. {
  68. Client *c;
  69. if(!sel)
  70. return;
  71. if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) {
  72. higher(c);
  73. focus(c);
  74. }
  75. }
  76. Client *
  77. getclient(Window w)
  78. {
  79. Client *c;
  80. for(c = clients; c; c = c->next)
  81. if(c->win == w)
  82. return c;
  83. return NULL;
  84. }
  85. Client *
  86. getctitle(Window w)
  87. {
  88. Client *c;
  89. for(c = clients; c; c = c->next)
  90. if(c->title == w)
  91. return c;
  92. return NULL;
  93. }
  94. void
  95. gravitate(Client *c, Bool invert)
  96. {
  97. int dx = 0, dy = 0;
  98. switch(c->grav) {
  99. case StaticGravity:
  100. case NorthWestGravity:
  101. case NorthGravity:
  102. case NorthEastGravity:
  103. dy = c->border;
  104. break;
  105. case EastGravity:
  106. case CenterGravity:
  107. case WestGravity:
  108. dy = -(c->h / 2) + c->border;
  109. break;
  110. case SouthEastGravity:
  111. case SouthGravity:
  112. case SouthWestGravity:
  113. dy = -c->h;
  114. break;
  115. default:
  116. break;
  117. }
  118. switch (c->grav) {
  119. case StaticGravity:
  120. case NorthWestGravity:
  121. case WestGravity:
  122. case SouthWestGravity:
  123. dx = c->border;
  124. break;
  125. case NorthGravity:
  126. case CenterGravity:
  127. case SouthGravity:
  128. dx = -(c->w / 2) + c->border;
  129. break;
  130. case NorthEastGravity:
  131. case EastGravity:
  132. case SouthEastGravity:
  133. dx = -(c->w + c->border);
  134. break;
  135. default:
  136. break;
  137. }
  138. if(invert) {
  139. dx = -dx;
  140. dy = -dy;
  141. }
  142. c->x += dx;
  143. c->y += dy;
  144. }
  145. void
  146. higher(Client *c)
  147. {
  148. XRaiseWindow(dpy, c->win);
  149. XRaiseWindow(dpy, c->title);
  150. }
  151. void
  152. killclient(Arg *arg)
  153. {
  154. if(!sel)
  155. return;
  156. if(sel->proto & WM_PROTOCOL_DELWIN)
  157. sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
  158. else
  159. XKillClient(dpy, sel->win);
  160. }
  161. void
  162. lower(Client *c)
  163. {
  164. XLowerWindow(dpy, c->title);
  165. XLowerWindow(dpy, c->win);
  166. }
  167. void
  168. manage(Window w, XWindowAttributes *wa)
  169. {
  170. Client *c;
  171. XSetWindowAttributes twa;
  172. Window trans;
  173. c = emallocz(sizeof(Client));
  174. c->win = w;
  175. c->tx = c->x = wa->x;
  176. c->ty = c->y = wa->y;
  177. if(c->y < bh)
  178. c->ty = c->y += bh;
  179. c->tw = c->w = wa->width;
  180. c->h = wa->height;
  181. c->th = bh;
  182. c->border = 1;
  183. c->proto = getproto(c->win);
  184. setsize(c);
  185. XSelectInput(dpy, c->win,
  186. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  187. XGetTransientForHint(dpy, c->win, &trans);
  188. twa.override_redirect = 1;
  189. twa.background_pixmap = ParentRelative;
  190. twa.event_mask = ExposureMask;
  191. c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
  192. 0, DefaultDepth(dpy, screen), CopyFromParent,
  193. DefaultVisual(dpy, screen),
  194. CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
  195. settitle(c);
  196. settags(c);
  197. c->next = clients;
  198. clients = c;
  199. XGrabButton(dpy, Button1, ControlMask, c->win, False, ButtonPressMask,
  200. GrabModeAsync, GrabModeSync, None, None);
  201. XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
  202. GrabModeAsync, GrabModeSync, None, None);
  203. XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
  204. GrabModeAsync, GrabModeSync, None, None);
  205. XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
  206. GrabModeAsync, GrabModeSync, None, None);
  207. if(!c->isfloat)
  208. c->isfloat = trans
  209. || ((c->maxw == c->minw) && (c->maxh == c->minh));
  210. arrange(NULL);
  211. /* mapping the window now prevents flicker */
  212. if(c->tags[tsel]) {
  213. XMapRaised(dpy, c->win);
  214. XMapRaised(dpy, c->title);
  215. focus(c);
  216. }
  217. else {
  218. ban(c);
  219. XMapRaised(dpy, c->win);
  220. XMapRaised(dpy, c->title);
  221. XSync(dpy, False);
  222. }
  223. }
  224. void
  225. maximize(Arg *arg)
  226. {
  227. if(!sel)
  228. return;
  229. sel->x = sx;
  230. sel->y = sy + bh;
  231. sel->w = sw - 2 * sel->border;
  232. sel->h = sh - 2 * sel->border - bh;
  233. higher(sel);
  234. resize(sel, False);
  235. }
  236. void
  237. pop(Client *c)
  238. {
  239. Client **l;
  240. for(l = &clients; *l && *l != c; l = &(*l)->next);
  241. *l = c->next;
  242. c->next = clients; /* pop */
  243. clients = c;
  244. arrange(NULL);
  245. }
  246. void
  247. resize(Client *c, Bool inc)
  248. {
  249. XConfigureEvent e;
  250. if(inc) {
  251. if(c->incw)
  252. c->w -= (c->w - c->basew) % c->incw;
  253. if(c->inch)
  254. c->h -= (c->h - c->baseh) % c->inch;
  255. }
  256. if(c->x > sw) /* might happen on restart */
  257. c->x = sw - c->w;
  258. if(c->y > sh)
  259. c->ty = c->y = sh - c->h;
  260. if(c->minw && c->w < c->minw)
  261. c->w = c->minw;
  262. if(c->minh && c->h < c->minh)
  263. c->h = c->minh;
  264. if(c->maxw && c->w > c->maxw)
  265. c->w = c->maxw;
  266. if(c->maxh && c->h > c->maxh)
  267. c->h = c->maxh;
  268. resizetitle(c);
  269. XSetWindowBorderWidth(dpy, c->win, 1);
  270. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  271. e.type = ConfigureNotify;
  272. e.event = c->win;
  273. e.window = c->win;
  274. e.x = c->x;
  275. e.y = c->y;
  276. e.width = c->w;
  277. e.height = c->h;
  278. e.border_width = c->border;
  279. e.above = None;
  280. e.override_redirect = False;
  281. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
  282. XSync(dpy, False);
  283. }
  284. void
  285. setsize(Client *c)
  286. {
  287. XSizeHints size;
  288. long msize;
  289. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  290. size.flags = PSize;
  291. c->flags = size.flags;
  292. if(c->flags & PBaseSize) {
  293. c->basew = size.base_width;
  294. c->baseh = size.base_height;
  295. }
  296. else
  297. c->basew = c->baseh = 0;
  298. if(c->flags & PResizeInc) {
  299. c->incw = size.width_inc;
  300. c->inch = size.height_inc;
  301. }
  302. else
  303. c->incw = c->inch = 0;
  304. if(c->flags & PMaxSize) {
  305. c->maxw = size.max_width;
  306. c->maxh = size.max_height;
  307. }
  308. else
  309. c->maxw = c->maxh = 0;
  310. if(c->flags & PMinSize) {
  311. c->minw = size.min_width;
  312. c->minh = size.min_height;
  313. }
  314. else
  315. c->minw = c->minh = 0;
  316. if(c->flags & PWinGravity)
  317. c->grav = size.win_gravity;
  318. else
  319. c->grav = NorthWestGravity;
  320. }
  321. void
  322. settitle(Client *c)
  323. {
  324. XTextProperty name;
  325. int n;
  326. char **list = NULL;
  327. name.nitems = 0;
  328. c->name[0] = 0;
  329. XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  330. if(!name.nitems)
  331. XGetWMName(dpy, c->win, &name);
  332. if(!name.nitems)
  333. return;
  334. if(name.encoding == XA_STRING)
  335. strncpy(c->name, (char *)name.value, sizeof(c->name));
  336. else {
  337. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  338. && n > 0 && *list)
  339. {
  340. strncpy(c->name, *list, sizeof(c->name));
  341. XFreeStringList(list);
  342. }
  343. }
  344. XFree(name.value);
  345. resizetitle(c);
  346. }
  347. void
  348. unmanage(Client *c)
  349. {
  350. Client **l;
  351. XGrabServer(dpy);
  352. XSetErrorHandler(xerrordummy);
  353. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  354. XDestroyWindow(dpy, c->title);
  355. for(l = &clients; *l && *l != c; l = &(*l)->next);
  356. *l = c->next;
  357. for(l = &clients; *l; l = &(*l)->next)
  358. if((*l)->revert == c)
  359. (*l)->revert = NULL;
  360. if(sel == c)
  361. sel = sel->revert ? sel->revert : clients;
  362. free(c);
  363. XSync(dpy, False);
  364. XSetErrorHandler(xerror);
  365. XUngrabServer(dpy);
  366. arrange(NULL);
  367. if(sel)
  368. focus(sel);
  369. }
  370. void
  371. zoom(Arg *arg)
  372. {
  373. Client *c;
  374. if(!sel)
  375. return;
  376. if(sel == getnext(clients, tsel) && sel->next) {
  377. if((c = getnext(sel->next, tsel)))
  378. sel = c;
  379. }
  380. pop(sel);
  381. focus(sel);
  382. }