client.c 10 KB

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