client.c 8.6 KB

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