client.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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, *old;
  26. if(!(old = sel))
  27. return;
  28. for(l = &clients; *l && *l != sel; l = &(*l)->next);
  29. *l = sel->next;
  30. old->next = clients; /* pop */
  31. clients = old;
  32. sel = old;
  33. arrange(NULL);
  34. focus(sel);
  35. }
  36. void
  37. max(Arg *arg)
  38. {
  39. if(!sel)
  40. return;
  41. sel->x = sx;
  42. sel->y = sy + bh;
  43. sel->w = sw - 2 * sel->border;
  44. sel->h = sh - 2 * sel->border - bh;
  45. craise(sel);
  46. resize(sel, False);
  47. discard_events(EnterWindowMask);
  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. discard_events(EnterWindowMask);
  101. }
  102. void
  103. tiling(Arg *arg)
  104. {
  105. Client *c;
  106. int n, i, w, h;
  107. w = sw - mw;
  108. arrange = tiling;
  109. for(n = 0, c = clients; c; c = c->next)
  110. if(c->tags[tsel] && !c->floating)
  111. n++;
  112. if(n > 1)
  113. h = (sh - bh) / (n - 1);
  114. else
  115. h = sh - bh;
  116. for(i = 0, c = clients; c; c = c->next) {
  117. if(c->tags[tsel]) {
  118. if(c->floating) {
  119. craise(c);
  120. resize(c, True);
  121. continue;
  122. }
  123. if(n == 1) {
  124. c->x = sx;
  125. c->y = sy + bh;
  126. c->w = sw - 2 * c->border;
  127. c->h = sh - 2 * c->border - bh;
  128. }
  129. else if(i == 0) {
  130. c->x = sx;
  131. c->y = sy + bh;
  132. c->w = mw - 2 * c->border;
  133. c->h = sh - 2 * c->border - bh;
  134. }
  135. else {
  136. c->x = sx + mw;
  137. c->y = sy + (i - 1) * h + bh;
  138. c->w = w - 2 * c->border;
  139. c->h = h - 2 * c->border;
  140. }
  141. resize(c, False);
  142. i++;
  143. }
  144. else
  145. ban_client(c);
  146. }
  147. if(sel && !sel->tags[tsel]) {
  148. if((sel = next(clients))) {
  149. craise(sel);
  150. focus(sel);
  151. }
  152. }
  153. discard_events(EnterWindowMask);
  154. }
  155. void
  156. prevc(Arg *arg)
  157. {
  158. Client *c;
  159. if(!sel)
  160. return;
  161. if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) {
  162. craise(c);
  163. focus(c);
  164. }
  165. }
  166. void
  167. nextc(Arg *arg)
  168. {
  169. Client *c;
  170. if(!sel)
  171. return;
  172. if(!(c = next(sel->next)))
  173. c = next(clients);
  174. if(c) {
  175. craise(c);
  176. c->revert = sel;
  177. focus(c);
  178. }
  179. }
  180. void
  181. ckill(Arg *arg)
  182. {
  183. if(!sel)
  184. return;
  185. if(sel->proto & WM_PROTOCOL_DELWIN)
  186. send_message(sel->win, wm_atom[WMProtocols], wm_atom[WMDelete]);
  187. else
  188. XKillClient(dpy, sel->win);
  189. }
  190. static void
  191. resize_title(Client *c)
  192. {
  193. int i;
  194. c->tw = 0;
  195. for(i = 0; i < TLast; i++)
  196. if(c->tags[i])
  197. c->tw += textw(c->tags[i]) + dc.font.height;
  198. c->tw += textw(c->name) + dc.font.height;
  199. if(c->tw > c->w)
  200. c->tw = c->w + 2;
  201. c->tx = c->x + c->w - c->tw + 2;
  202. c->ty = c->y;
  203. XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
  204. }
  205. void
  206. update_name(Client *c)
  207. {
  208. XTextProperty name;
  209. int n;
  210. char **list = NULL;
  211. name.nitems = 0;
  212. c->name[0] = 0;
  213. XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]);
  214. if(!name.nitems)
  215. XGetWMName(dpy, c->win, &name);
  216. if(!name.nitems)
  217. return;
  218. if(name.encoding == XA_STRING)
  219. strncpy(c->name, (char *)name.value, sizeof(c->name));
  220. else {
  221. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  222. && n > 0 && *list)
  223. {
  224. strncpy(c->name, *list, sizeof(c->name));
  225. XFreeStringList(list);
  226. }
  227. }
  228. XFree(name.value);
  229. resize_title(c);
  230. }
  231. void
  232. update_size(Client *c)
  233. {
  234. XSizeHints size;
  235. long msize;
  236. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  237. size.flags = PSize;
  238. c->flags = size.flags;
  239. if(c->flags & PBaseSize) {
  240. c->basew = size.base_width;
  241. c->baseh = size.base_height;
  242. }
  243. else
  244. c->basew = c->baseh = 0;
  245. if(c->flags & PResizeInc) {
  246. c->incw = size.width_inc;
  247. c->inch = size.height_inc;
  248. }
  249. else
  250. c->incw = c->inch = 0;
  251. if(c->flags & PMaxSize) {
  252. c->maxw = size.max_width;
  253. c->maxh = size.max_height;
  254. }
  255. else
  256. c->maxw = c->maxh = 0;
  257. if(c->flags & PMinSize) {
  258. c->minw = size.min_width;
  259. c->minh = size.min_height;
  260. }
  261. else
  262. c->minw = c->minh = 0;
  263. if(c->flags & PWinGravity)
  264. c->grav = size.win_gravity;
  265. else
  266. c->grav = NorthWestGravity;
  267. }
  268. void
  269. craise(Client *c)
  270. {
  271. XRaiseWindow(dpy, c->win);
  272. XRaiseWindow(dpy, c->title);
  273. }
  274. void
  275. lower(Client *c)
  276. {
  277. XLowerWindow(dpy, c->title);
  278. XLowerWindow(dpy, c->win);
  279. }
  280. void
  281. focus(Client *c)
  282. {
  283. Client *old = sel;
  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. discard_events(EnterWindowMask);
  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. XMapRaised(dpy, c->win);
  358. XMapRaised(dpy, c->title);
  359. XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
  360. GrabModeAsync, GrabModeSync, None, None);
  361. XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
  362. GrabModeAsync, GrabModeSync, None, None);
  363. XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
  364. GrabModeAsync, GrabModeSync, None, None);
  365. if(!c->floating)
  366. c->floating = trans
  367. || ((c->maxw == c->minw) && (c->maxh == c->minh));
  368. arrange(NULL);
  369. if(c->tags[tsel])
  370. focus(c);
  371. else
  372. ban_client(c);
  373. }
  374. void
  375. gravitate(Client *c, Bool invert)
  376. {
  377. int dx = 0, dy = 0;
  378. switch(c->grav) {
  379. case StaticGravity:
  380. case NorthWestGravity:
  381. case NorthGravity:
  382. case NorthEastGravity:
  383. dy = c->border;
  384. break;
  385. case EastGravity:
  386. case CenterGravity:
  387. case WestGravity:
  388. dy = -(c->h / 2) + c->border;
  389. break;
  390. case SouthEastGravity:
  391. case SouthGravity:
  392. case SouthWestGravity:
  393. dy = -c->h;
  394. break;
  395. default:
  396. break;
  397. }
  398. switch (c->grav) {
  399. case StaticGravity:
  400. case NorthWestGravity:
  401. case WestGravity:
  402. case SouthWestGravity:
  403. dx = c->border;
  404. break;
  405. case NorthGravity:
  406. case CenterGravity:
  407. case SouthGravity:
  408. dx = -(c->w / 2) + c->border;
  409. break;
  410. case NorthEastGravity:
  411. case EastGravity:
  412. case SouthEastGravity:
  413. dx = -(c->w + c->border);
  414. break;
  415. default:
  416. break;
  417. }
  418. if(invert) {
  419. dx = -dx;
  420. dy = -dy;
  421. }
  422. c->x += dx;
  423. c->y += dy;
  424. }
  425. void
  426. resize(Client *c, Bool inc)
  427. {
  428. XConfigureEvent e;
  429. if(inc) {
  430. if(c->incw)
  431. c->w -= (c->w - c->basew) % c->incw;
  432. if(c->inch)
  433. c->h -= (c->h - c->baseh) % c->inch;
  434. }
  435. if(c->minw && c->w < c->minw)
  436. c->w = c->minw;
  437. if(c->minh && c->h < c->minh)
  438. c->h = c->minh;
  439. if(c->maxw && c->w > c->maxw)
  440. c->w = c->maxw;
  441. if(c->maxh && c->h > c->maxh)
  442. c->h = c->maxh;
  443. resize_title(c);
  444. XSetWindowBorderWidth(dpy, c->win, 1);
  445. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  446. e.type = ConfigureNotify;
  447. e.event = c->win;
  448. e.window = c->win;
  449. e.x = c->x;
  450. e.y = c->y;
  451. e.width = c->w;
  452. e.height = c->h;
  453. e.border_width = c->border;
  454. e.above = None;
  455. e.override_redirect = False;
  456. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
  457. XFlush(dpy);
  458. }
  459. static int
  460. dummy_error_handler(Display *dsply, XErrorEvent *err)
  461. {
  462. return 0;
  463. }
  464. void
  465. unmanage(Client *c)
  466. {
  467. Client **l;
  468. XGrabServer(dpy);
  469. XSetErrorHandler(dummy_error_handler);
  470. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  471. XDestroyWindow(dpy, c->title);
  472. for(l = &clients; *l && *l != c; l = &(*l)->next);
  473. *l = c->next;
  474. for(l = &clients; *l; l = &(*l)->next)
  475. if((*l)->revert == c)
  476. (*l)->revert = NULL;
  477. if(sel == c)
  478. sel = sel->revert ? sel->revert : clients;
  479. free(c);
  480. XFlush(dpy);
  481. XSetErrorHandler(error_handler);
  482. XUngrabServer(dpy);
  483. arrange(NULL);
  484. if(sel)
  485. focus(sel);
  486. }
  487. Client *
  488. gettitle(Window w)
  489. {
  490. Client *c;
  491. for(c = clients; c; c = c->next)
  492. if(c->title == w)
  493. return c;
  494. return NULL;
  495. }
  496. Client *
  497. getclient(Window w)
  498. {
  499. Client *c;
  500. for(c = clients; c; c = c->next)
  501. if(c->win == w)
  502. return c;
  503. return NULL;
  504. }
  505. void
  506. draw_client(Client *c)
  507. {
  508. int i;
  509. if(c == sel) {
  510. draw_bar();
  511. XUnmapWindow(dpy, c->title);
  512. XSetWindowBorder(dpy, c->win, dc.fg);
  513. return;
  514. }
  515. XSetWindowBorder(dpy, c->win, dc.bg);
  516. XMapWindow(dpy, c->title);
  517. dc.x = dc.y = 0;
  518. dc.w = 0;
  519. for(i = 0; i < TLast; i++) {
  520. if(c->tags[i]) {
  521. dc.x += dc.w;
  522. dc.w = textw(c->tags[i]) + dc.font.height;
  523. drawtext(c->tags[i], True);
  524. }
  525. }
  526. dc.x += dc.w;
  527. dc.w = textw(c->name) + dc.font.height;
  528. drawtext(c->name, True);
  529. XCopyArea(dpy, dc.drawable, c->title, dc.gc,
  530. 0, 0, c->tw, c->th, 0, 0);
  531. XFlush(dpy);
  532. }