You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

479 lines
8.3 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  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)))
  63. c = getnext(clients);
  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 & PROTODELWIN)
  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. c->border = 1;
  191. setsize(c);
  192. if(c->h != sh && c->y < bh)
  193. c->y = c->ty = bh;
  194. c->proto = getproto(c->win);
  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 | EnterWindowMask;
  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. if(clients)
  206. clients->prev = c;
  207. c->next = clients;
  208. clients = c;
  209. XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
  210. GrabModeAsync, GrabModeSync, None, None);
  211. XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
  212. GrabModeAsync, GrabModeSync, None, None);
  213. XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
  214. GrabModeAsync, GrabModeSync, None, None);
  215. settags(c);
  216. if(!c->isfloat)
  217. c->isfloat = trans
  218. || (c->maxw && c->minw &&
  219. c->maxw == c->minw && c->maxh == c->minh)
  220. || (c->w == sw && c->h == sh);
  221. settitle(c);
  222. arrange(NULL);
  223. /* mapping the window now prevents flicker */
  224. XMapRaised(dpy, c->win);
  225. XMapRaised(dpy, c->title);
  226. if(c->tags[tsel])
  227. focus(c);
  228. }
  229. void
  230. resize(Client *c, Bool sizehints, Corner sticky)
  231. {
  232. int bottom = c->y + c->h;
  233. int right = c->x + c->w;
  234. XConfigureEvent e;
  235. XWindowChanges wc;
  236. if(sizehints) {
  237. if(c->incw)
  238. c->w -= (c->w - c->basew) % c->incw;
  239. if(c->inch)
  240. c->h -= (c->h - c->baseh) % c->inch;
  241. if(c->minw && c->w < c->minw)
  242. c->w = c->minw;
  243. if(c->minh && c->h < c->minh)
  244. c->h = c->minh;
  245. if(c->maxw && c->w > c->maxw)
  246. c->w = c->maxw;
  247. if(c->maxh && c->h > c->maxh)
  248. c->h = c->maxh;
  249. }
  250. if(c->x > right) /* might happen on restart */
  251. c->x = right - c->w;
  252. if(c->y > bottom)
  253. c->y = bottom - c->h;
  254. if(sticky == TopRight || sticky == BotRight)
  255. c->x = right - c->w;
  256. if(sticky == BotLeft || sticky == BotRight)
  257. c->y = bottom - c->h;
  258. resizetitle(c);
  259. wc.x = c->x;
  260. wc.y = c->y;
  261. wc.width = c->w;
  262. wc.height = c->h;
  263. wc.border_width = 1;
  264. XConfigureWindow(dpy, c->win,
  265. CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  266. e.type = ConfigureNotify;
  267. e.event = c->win;
  268. e.window = c->win;
  269. e.x = c->x;
  270. e.y = c->y;
  271. e.width = c->w;
  272. e.height = c->h;
  273. e.border_width = c->border;
  274. e.above = None;
  275. e.override_redirect = False;
  276. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
  277. XSync(dpy, False);
  278. }
  279. void
  280. setsize(Client *c)
  281. {
  282. long msize;
  283. XSizeHints size;
  284. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  285. size.flags = PSize;
  286. c->flags = size.flags;
  287. if(c->flags & PBaseSize) {
  288. c->basew = size.base_width;
  289. c->baseh = size.base_height;
  290. }
  291. else
  292. c->basew = c->baseh = 0;
  293. if(c->flags & PResizeInc) {
  294. c->incw = size.width_inc;
  295. c->inch = size.height_inc;
  296. }
  297. else
  298. c->incw = c->inch = 0;
  299. if(c->flags & PMaxSize) {
  300. c->maxw = size.max_width;
  301. c->maxh = size.max_height;
  302. }
  303. else
  304. c->maxw = c->maxh = 0;
  305. if(c->flags & PMinSize) {
  306. c->minw = size.min_width;
  307. c->minh = size.min_height;
  308. }
  309. else
  310. c->minw = c->minh = 0;
  311. if(c->flags & PWinGravity)
  312. c->grav = size.win_gravity;
  313. else
  314. c->grav = NorthWestGravity;
  315. }
  316. void
  317. settitle(Client *c)
  318. {
  319. char **list = NULL;
  320. int n;
  321. XTextProperty name;
  322. name.nitems = 0;
  323. c->name[0] = 0;
  324. XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
  325. if(!name.nitems)
  326. XGetWMName(dpy, c->win, &name);
  327. if(!name.nitems)
  328. return;
  329. if(name.encoding == XA_STRING)
  330. strncpy(c->name, (char *)name.value, sizeof(c->name));
  331. else {
  332. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  333. && n > 0 && *list)
  334. {
  335. strncpy(c->name, *list, sizeof(c->name));
  336. XFreeStringList(list);
  337. }
  338. }
  339. XFree(name.value);
  340. resizetitle(c);
  341. }
  342. void
  343. togglemax(Arg *arg)
  344. {
  345. int ox, oy, ow, oh;
  346. XEvent ev;
  347. if(!sel)
  348. return;
  349. if((sel->ismax = !sel->ismax)) {
  350. ox = sel->x;
  351. oy = sel->y;
  352. ow = sel->w;
  353. oh = sel->h;
  354. sel->x = sx;
  355. sel->y = sy + bh;
  356. sel->w = sw - 2 * sel->border;
  357. sel->h = sh - 2 * sel->border - bh;
  358. higher(sel);
  359. resize(sel, False, TopLeft);
  360. sel->x = ox;
  361. sel->y = oy;
  362. sel->w = ow;
  363. sel->h = oh;
  364. }
  365. else
  366. resize(sel, False, TopLeft);
  367. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  368. }
  369. void
  370. unmanage(Client *c)
  371. {
  372. XGrabServer(dpy);
  373. XSetErrorHandler(xerrordummy);
  374. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  375. XDestroyWindow(dpy, c->title);
  376. if(c->prev)
  377. c->prev->next = c->next;
  378. if(c->next)
  379. c->next->prev = c->prev;
  380. if(c == clients)
  381. clients = c->next;
  382. if(sel == c) {
  383. sel = getnext(c->next);
  384. if(!sel)
  385. sel = getprev(c->prev);
  386. if(!sel)
  387. sel = clients;
  388. }
  389. free(c);
  390. XSync(dpy, False);
  391. XSetErrorHandler(xerror);
  392. XUngrabServer(dpy);
  393. arrange(NULL);
  394. if(sel)
  395. focus(sel);
  396. }
  397. void
  398. zoom(Arg *arg)
  399. {
  400. Client *c;
  401. if(!sel)
  402. return;
  403. if(sel == getnext(clients) && sel->next) {
  404. if((c = getnext(sel->next)))
  405. sel = c;
  406. }
  407. /* pop */
  408. if(sel->prev)
  409. sel->prev->next = sel->next;
  410. if(sel->next)
  411. sel->next->prev = sel->prev;
  412. sel->prev = NULL;
  413. if(clients)
  414. clients->prev = sel;
  415. sel->next = clients;
  416. clients = sel;
  417. arrange(NULL);
  418. focus(sel);
  419. }