client.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*
  2. * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3. * See LICENSE file for license details.
  4. */
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <X11/Xatom.h>
  9. #include <X11/Xutil.h>
  10. #include "dwm.h"
  11. void (*arrange)(Arg *) = tiling;
  12. static Rule rule[] = {
  13. /* class instance tags floating */
  14. { "Firefox-bin", "Gecko", { [Twww] = "www" }, False },
  15. };
  16. static Client *
  17. next(Client *c)
  18. {
  19. for(; c && !c->tags[tsel]; c = c->next);
  20. return c;
  21. }
  22. void
  23. zoom(Arg *arg)
  24. {
  25. Client **l;
  26. if(!sel)
  27. return;
  28. if(sel == next(clients))
  29. sel = next(sel->next);
  30. for(l = &clients; *l && *l != sel; l = &(*l)->next);
  31. *l = sel->next;
  32. sel->next = clients; /* pop */
  33. clients = sel;
  34. arrange(NULL);
  35. focus(sel);
  36. }
  37. void
  38. max(Arg *arg)
  39. {
  40. if(!sel)
  41. return;
  42. sel->x = sx;
  43. sel->y = sy + bh;
  44. sel->w = sw - 2 * sel->border;
  45. sel->h = sh - 2 * sel->border - bh;
  46. craise(sel);
  47. resize(sel, False);
  48. }
  49. void
  50. view(Arg *arg)
  51. {
  52. Client *c;
  53. tsel = arg->i;
  54. arrange(NULL);
  55. for(c = clients; c; c = next(c->next))
  56. draw_client(c);
  57. draw_bar();
  58. }
  59. void
  60. tappend(Arg *arg)
  61. {
  62. if(!sel)
  63. return;
  64. sel->tags[arg->i] = tags[arg->i];
  65. arrange(NULL);
  66. }
  67. void
  68. ttrunc(Arg *arg)
  69. {
  70. int i;
  71. if(!sel)
  72. return;
  73. for(i = 0; i < TLast; i++)
  74. sel->tags[i] = NULL;
  75. tappend(arg);
  76. }
  77. static void
  78. ban_client(Client *c)
  79. {
  80. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  81. XMoveWindow(dpy, c->title, c->tx + 2 * sw, c->ty);
  82. }
  83. void
  84. floating(Arg *arg)
  85. {
  86. Client *c;
  87. arrange = floating;
  88. for(c = clients; c; c = c->next) {
  89. if(c->tags[tsel])
  90. resize(c, True);
  91. else
  92. ban_client(c);
  93. }
  94. if(sel && !sel->tags[tsel]) {
  95. if((sel = next(clients))) {
  96. craise(sel);
  97. focus(sel);
  98. }
  99. }
  100. }
  101. void
  102. tiling(Arg *arg)
  103. {
  104. Client *c;
  105. int n, i, w, h;
  106. w = sw - mw;
  107. arrange = tiling;
  108. for(n = 0, c = clients; c; c = c->next)
  109. if(c->tags[tsel] && !c->floating)
  110. n++;
  111. if(n > 1)
  112. h = (sh - bh) / (n - 1);
  113. else
  114. h = sh - bh;
  115. for(i = 0, c = clients; c; c = c->next) {
  116. if(c->tags[tsel]) {
  117. if(c->floating) {
  118. craise(c);
  119. resize(c, True);
  120. continue;
  121. }
  122. if(n == 1) {
  123. c->x = sx;
  124. c->y = sy + bh;
  125. c->w = sw - 2 * c->border;
  126. c->h = sh - 2 * c->border - bh;
  127. }
  128. else if(i == 0) {
  129. c->x = sx;
  130. c->y = sy + bh;
  131. c->w = mw - 2 * c->border;
  132. c->h = sh - 2 * c->border - bh;
  133. }
  134. else {
  135. c->x = sx + mw;
  136. c->y = sy + (i - 1) * h + bh;
  137. c->w = w - 2 * c->border;
  138. c->h = h - 2 * c->border;
  139. }
  140. resize(c, False);
  141. i++;
  142. }
  143. else
  144. ban_client(c);
  145. }
  146. if(!sel || (sel && !sel->tags[tsel])) {
  147. if((sel = next(clients))) {
  148. craise(sel);
  149. focus(sel);
  150. }
  151. }
  152. }
  153. void
  154. prevc(Arg *arg)
  155. {
  156. Client *c;
  157. if(!sel)
  158. return;
  159. if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) {
  160. craise(c);
  161. focus(c);
  162. }
  163. }
  164. void
  165. nextc(Arg *arg)
  166. {
  167. Client *c;
  168. if(!sel)
  169. return;
  170. if(!(c = next(sel->next)))
  171. c = next(clients);
  172. if(c) {
  173. craise(c);
  174. c->revert = sel;
  175. focus(c);
  176. }
  177. }
  178. void
  179. ckill(Arg *arg)
  180. {
  181. if(!sel)
  182. return;
  183. if(sel->proto & WM_PROTOCOL_DELWIN)
  184. send_message(sel->win, wm_atom[WMProtocols], wm_atom[WMDelete]);
  185. else
  186. XKillClient(dpy, sel->win);
  187. }
  188. static void
  189. resize_title(Client *c)
  190. {
  191. int i;
  192. c->tw = 0;
  193. for(i = 0; i < TLast; i++)
  194. if(c->tags[i])
  195. c->tw += textw(c->tags[i]) + dc.font.height;
  196. c->tw += textw(c->name) + dc.font.height;
  197. if(c->tw > c->w)
  198. c->tw = c->w + 2;
  199. c->tx = c->x + c->w - c->tw + 2;
  200. c->ty = c->y;
  201. XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
  202. }
  203. void
  204. update_name(Client *c)
  205. {
  206. XTextProperty name;
  207. int n;
  208. char **list = NULL;
  209. name.nitems = 0;
  210. c->name[0] = 0;
  211. XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]);
  212. if(!name.nitems)
  213. XGetWMName(dpy, c->win, &name);
  214. if(!name.nitems)
  215. return;
  216. if(name.encoding == XA_STRING)
  217. strncpy(c->name, (char *)name.value, sizeof(c->name));
  218. else {
  219. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  220. && n > 0 && *list)
  221. {
  222. strncpy(c->name, *list, sizeof(c->name));
  223. XFreeStringList(list);
  224. }
  225. }
  226. XFree(name.value);
  227. resize_title(c);
  228. }
  229. void
  230. update_size(Client *c)
  231. {
  232. XSizeHints size;
  233. long msize;
  234. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  235. size.flags = PSize;
  236. c->flags = size.flags;
  237. if(c->flags & PBaseSize) {
  238. c->basew = size.base_width;
  239. c->baseh = size.base_height;
  240. }
  241. else
  242. c->basew = c->baseh = 0;
  243. if(c->flags & PResizeInc) {
  244. c->incw = size.width_inc;
  245. c->inch = size.height_inc;
  246. }
  247. else
  248. c->incw = c->inch = 0;
  249. if(c->flags & PMaxSize) {
  250. c->maxw = size.max_width;
  251. c->maxh = size.max_height;
  252. }
  253. else
  254. c->maxw = c->maxh = 0;
  255. if(c->flags & PMinSize) {
  256. c->minw = size.min_width;
  257. c->minh = size.min_height;
  258. }
  259. else
  260. c->minw = c->minh = 0;
  261. if(c->flags & PWinGravity)
  262. c->grav = size.win_gravity;
  263. else
  264. c->grav = NorthWestGravity;
  265. }
  266. void
  267. craise(Client *c)
  268. {
  269. XRaiseWindow(dpy, c->win);
  270. XRaiseWindow(dpy, c->title);
  271. }
  272. void
  273. lower(Client *c)
  274. {
  275. XLowerWindow(dpy, c->title);
  276. XLowerWindow(dpy, c->win);
  277. }
  278. void
  279. focus(Client *c)
  280. {
  281. Client *old = sel;
  282. XEvent ev;
  283. XFlush(dpy);
  284. sel = c;
  285. if(old && old != c)
  286. draw_client(old);
  287. draw_client(c);
  288. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  289. XFlush(dpy);
  290. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  291. }
  292. static void
  293. init_tags(Client *c)
  294. {
  295. XClassHint ch;
  296. static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0;
  297. unsigned int i, j;
  298. Bool matched = False;
  299. if(!len) {
  300. c->tags[tsel] = tags[tsel];
  301. return;
  302. }
  303. if(XGetClassHint(dpy, c->win, &ch)) {
  304. if(ch.res_class && ch.res_name) {
  305. for(i = 0; i < len; i++)
  306. if(!strncmp(rule[i].class, ch.res_class, sizeof(rule[i].class))
  307. && !strncmp(rule[i].instance, ch.res_name, sizeof(rule[i].instance)))
  308. {
  309. for(j = 0; j < TLast; j++)
  310. c->tags[j] = rule[i].tags[j];
  311. c->floating = rule[i].floating;
  312. matched = True;
  313. break;
  314. }
  315. }
  316. if(ch.res_class)
  317. XFree(ch.res_class);
  318. if(ch.res_name)
  319. XFree(ch.res_name);
  320. }
  321. if(!matched)
  322. c->tags[tsel] = tags[tsel];
  323. }
  324. void
  325. manage(Window w, XWindowAttributes *wa)
  326. {
  327. Client *c, **l;
  328. XSetWindowAttributes twa;
  329. Window trans;
  330. c = emallocz(sizeof(Client));
  331. c->win = w;
  332. c->tx = c->x = wa->x;
  333. c->ty = c->y = wa->y;
  334. if(c->y < bh)
  335. c->ty = c->y += bh;
  336. c->tw = c->w = wa->width;
  337. c->h = wa->height;
  338. c->th = bh;
  339. c->border = 1;
  340. c->proto = win_proto(c->win);
  341. update_size(c);
  342. XSelectInput(dpy, c->win,
  343. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  344. XGetTransientForHint(dpy, c->win, &trans);
  345. twa.override_redirect = 1;
  346. twa.background_pixmap = ParentRelative;
  347. twa.event_mask = ExposureMask;
  348. c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
  349. 0, DefaultDepth(dpy, screen), CopyFromParent,
  350. DefaultVisual(dpy, screen),
  351. CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
  352. update_name(c);
  353. init_tags(c);
  354. for(l = &clients; *l; l = &(*l)->next);
  355. c->next = *l; /* *l == nil */
  356. *l = c;
  357. XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
  358. GrabModeAsync, GrabModeSync, None, None);
  359. XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
  360. GrabModeAsync, GrabModeSync, None, None);
  361. XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
  362. GrabModeAsync, GrabModeSync, None, None);
  363. if(!c->floating)
  364. c->floating = trans
  365. || ((c->maxw == c->minw) && (c->maxh == c->minh));
  366. arrange(NULL);
  367. /* mapping the window now prevents flicker */
  368. if(c->tags[tsel]) {
  369. XMapRaised(dpy, c->win);
  370. XMapRaised(dpy, c->title);
  371. focus(c);
  372. }
  373. else {
  374. ban_client(c);
  375. XMapRaised(dpy, c->win);
  376. XMapRaised(dpy, c->title);
  377. }
  378. }
  379. void
  380. gravitate(Client *c, Bool invert)
  381. {
  382. int dx = 0, dy = 0;
  383. switch(c->grav) {
  384. case StaticGravity:
  385. case NorthWestGravity:
  386. case NorthGravity:
  387. case NorthEastGravity:
  388. dy = c->border;
  389. break;
  390. case EastGravity:
  391. case CenterGravity:
  392. case WestGravity:
  393. dy = -(c->h / 2) + c->border;
  394. break;
  395. case SouthEastGravity:
  396. case SouthGravity:
  397. case SouthWestGravity:
  398. dy = -c->h;
  399. break;
  400. default:
  401. break;
  402. }
  403. switch (c->grav) {
  404. case StaticGravity:
  405. case NorthWestGravity:
  406. case WestGravity:
  407. case SouthWestGravity:
  408. dx = c->border;
  409. break;
  410. case NorthGravity:
  411. case CenterGravity:
  412. case SouthGravity:
  413. dx = -(c->w / 2) + c->border;
  414. break;
  415. case NorthEastGravity:
  416. case EastGravity:
  417. case SouthEastGravity:
  418. dx = -(c->w + c->border);
  419. break;
  420. default:
  421. break;
  422. }
  423. if(invert) {
  424. dx = -dx;
  425. dy = -dy;
  426. }
  427. c->x += dx;
  428. c->y += dy;
  429. }
  430. void
  431. resize(Client *c, Bool inc)
  432. {
  433. XConfigureEvent e;
  434. if(inc) {
  435. if(c->incw)
  436. c->w -= (c->w - c->basew) % c->incw;
  437. if(c->inch)
  438. c->h -= (c->h - c->baseh) % c->inch;
  439. }
  440. if(c->minw && c->w < c->minw)
  441. c->w = c->minw;
  442. if(c->minh && c->h < c->minh)
  443. c->h = c->minh;
  444. if(c->maxw && c->w > c->maxw)
  445. c->w = c->maxw;
  446. if(c->maxh && c->h > c->maxh)
  447. c->h = c->maxh;
  448. resize_title(c);
  449. XSetWindowBorderWidth(dpy, c->win, 1);
  450. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  451. e.type = ConfigureNotify;
  452. e.event = c->win;
  453. e.window = c->win;
  454. e.x = c->x;
  455. e.y = c->y;
  456. e.width = c->w;
  457. e.height = c->h;
  458. e.border_width = c->border;
  459. e.above = None;
  460. e.override_redirect = False;
  461. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
  462. XFlush(dpy);
  463. }
  464. static int
  465. dummy_error_handler(Display *dsply, XErrorEvent *err)
  466. {
  467. return 0;
  468. }
  469. void
  470. unmanage(Client *c)
  471. {
  472. Client **l;
  473. XGrabServer(dpy);
  474. XSetErrorHandler(dummy_error_handler);
  475. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  476. XDestroyWindow(dpy, c->title);
  477. for(l = &clients; *l && *l != c; l = &(*l)->next);
  478. *l = c->next;
  479. for(l = &clients; *l; l = &(*l)->next)
  480. if((*l)->revert == c)
  481. (*l)->revert = NULL;
  482. if(sel == c)
  483. sel = sel->revert ? sel->revert : clients;
  484. free(c);
  485. XFlush(dpy);
  486. XSetErrorHandler(error_handler);
  487. XUngrabServer(dpy);
  488. arrange(NULL);
  489. if(sel)
  490. focus(sel);
  491. }
  492. Client *
  493. gettitle(Window w)
  494. {
  495. Client *c;
  496. for(c = clients; c; c = c->next)
  497. if(c->title == w)
  498. return c;
  499. return NULL;
  500. }
  501. Client *
  502. getclient(Window w)
  503. {
  504. Client *c;
  505. for(c = clients; c; c = c->next)
  506. if(c->win == w)
  507. return c;
  508. return NULL;
  509. }
  510. void
  511. draw_client(Client *c)
  512. {
  513. int i;
  514. if(c == sel) {
  515. draw_bar();
  516. XUnmapWindow(dpy, c->title);
  517. XSetWindowBorder(dpy, c->win, dc.fg);
  518. return;
  519. }
  520. XSetWindowBorder(dpy, c->win, dc.bg);
  521. XMapWindow(dpy, c->title);
  522. dc.x = dc.y = 0;
  523. dc.w = 0;
  524. for(i = 0; i < TLast; i++) {
  525. if(c->tags[i]) {
  526. dc.x += dc.w;
  527. dc.w = textw(c->tags[i]) + dc.font.height;
  528. drawtext(c->tags[i], True);
  529. }
  530. }
  531. dc.x += dc.w;
  532. dc.w = textw(c->name) + dc.font.height;
  533. drawtext(c->name, True);
  534. XCopyArea(dpy, dc.drawable, c->title, dc.gc,
  535. 0, 0, c->tw, c->th, 0, 0);
  536. XFlush(dpy);
  537. }