tabbed.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  1. /*
  2. * See LICENSE file for copyright and license details.
  3. */
  4. #include <sys/wait.h>
  5. #include <locale.h>
  6. #include <stdarg.h>
  7. #include <unistd.h>
  8. #include <signal.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <X11/Xatom.h>
  13. #include <X11/Xlib.h>
  14. #include <X11/Xproto.h>
  15. #include <X11/Xutil.h>
  16. #include <X11/XKBlib.h>
  17. #include "arg.h"
  18. /* XEMBED messages */
  19. #define XEMBED_EMBEDDED_NOTIFY 0
  20. #define XEMBED_WINDOW_ACTIVATE 1
  21. #define XEMBED_WINDOW_DEACTIVATE 2
  22. #define XEMBED_REQUEST_FOCUS 3
  23. #define XEMBED_FOCUS_IN 4
  24. #define XEMBED_FOCUS_OUT 5
  25. #define XEMBED_FOCUS_NEXT 6
  26. #define XEMBED_FOCUS_PREV 7
  27. /* 8-9 were used for XEMBED_GRAB_KEY/XEMBED_UNGRAB_KEY */
  28. #define XEMBED_MODALITY_ON 10
  29. #define XEMBED_MODALITY_OFF 11
  30. #define XEMBED_REGISTER_ACCELERATOR 12
  31. #define XEMBED_UNREGISTER_ACCELERATOR 13
  32. #define XEMBED_ACTIVATE_ACCELERATOR 14
  33. /* Details for XEMBED_FOCUS_IN: */
  34. #define XEMBED_FOCUS_CURRENT 0
  35. #define XEMBED_FOCUS_FIRST 1
  36. #define XEMBED_FOCUS_LAST 2
  37. /* Macros */
  38. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  39. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  40. #define LENGTH(x) (sizeof((x)) / sizeof(*(x)))
  41. #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask))
  42. #define TEXTW(x) (textnw(x, strlen(x)) + dc.font.height)
  43. enum { ColFG, ColBG, ColLast }; /* color */
  44. enum { WMProtocols, WMDelete, WMName, WMState, WMFullscreen,
  45. XEmbed, WMSelectTab, WMLast }; /* default atoms */
  46. typedef union {
  47. int i;
  48. const void *v;
  49. } Arg;
  50. typedef struct {
  51. unsigned int mod;
  52. KeySym keysym;
  53. void (*func)(const Arg *);
  54. const Arg arg;
  55. } Key;
  56. typedef struct {
  57. int x, y, w, h;
  58. unsigned long norm[ColLast];
  59. unsigned long sel[ColLast];
  60. Drawable drawable;
  61. GC gc;
  62. struct {
  63. int ascent;
  64. int descent;
  65. int height;
  66. XFontSet set;
  67. XFontStruct *xfont;
  68. } font;
  69. } DC; /* draw context */
  70. typedef struct Client {
  71. char name[256];
  72. Window win;
  73. int tabx;
  74. Bool mapped;
  75. Bool closed;
  76. } Client;
  77. /* function declarations */
  78. static void buttonpress(const XEvent *e);
  79. static void cleanup(void);
  80. static void clientmessage(const XEvent *e);
  81. static void configurenotify(const XEvent *e);
  82. static void configurerequest(const XEvent *e);
  83. static void createnotify(const XEvent *e);
  84. static void destroynotify(const XEvent *e);
  85. static void die(const char *errstr, ...);
  86. static void drawbar(void);
  87. static void drawtext(const char *text, unsigned long col[ColLast]);
  88. static void *emallocz(size_t size);
  89. static void *erealloc(void *o, size_t size);
  90. static void expose(const XEvent *e);
  91. static void focus(int c);
  92. static void focusin(const XEvent *e);
  93. static void focusonce(const Arg *arg);
  94. static void fullscreen(const Arg *arg);
  95. static char* getatom(int a);
  96. static int getclient(Window w);
  97. static unsigned long getcolor(const char *colstr);
  98. static int getfirsttab(void);
  99. static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
  100. static void initfont(const char *fontstr);
  101. static Bool isprotodel(int c);
  102. static void keypress(const XEvent *e);
  103. static void killclient(const Arg *arg);
  104. static void manage(Window win);
  105. static void maprequest(const XEvent *e);
  106. static void move(const Arg *arg);
  107. static void movetab(const Arg *arg);
  108. static void propertynotify(const XEvent *e);
  109. static void resize(int c, int w, int h);
  110. static void rotate(const Arg *arg);
  111. static void run(void);
  112. static void sendxembed(int c, long msg, long detail, long d1, long d2);
  113. static void setup(void);
  114. static void setcmd(int argc, char *argv[], int);
  115. static void sigchld(int unused);
  116. static void spawn(const Arg *arg);
  117. static int textnw(const char *text, unsigned int len);
  118. static void unmanage(int c);
  119. static void updatenumlockmask(void);
  120. static void updatetitle(int c);
  121. static int xerror(Display *dpy, XErrorEvent *ee);
  122. static void xsettitle(Window w, const char *str);
  123. /* variables */
  124. static int screen;
  125. static void (*handler[LASTEvent]) (const XEvent *) = {
  126. [ButtonPress] = buttonpress,
  127. [ClientMessage] = clientmessage,
  128. [ConfigureNotify] = configurenotify,
  129. [ConfigureRequest] = configurerequest,
  130. [CreateNotify] = createnotify,
  131. [DestroyNotify] = destroynotify,
  132. [Expose] = expose,
  133. [FocusIn] = focusin,
  134. [KeyPress] = keypress,
  135. [MapRequest] = maprequest,
  136. [PropertyNotify] = propertynotify,
  137. };
  138. static int bh, wx, wy, ww, wh;
  139. static unsigned int numlockmask = 0;
  140. static Bool running = True, nextfocus, doinitspawn = True,
  141. fillagain = False, closelastclient = False;
  142. static Display *dpy;
  143. static DC dc;
  144. static Atom wmatom[WMLast];
  145. static Window root, win;
  146. static Client **clients = NULL;
  147. static int nclients = 0, sel = -1, lastsel = -1;
  148. static int (*xerrorxlib)(Display *, XErrorEvent *);
  149. static int cmd_append_pos = 0;
  150. static char winid[64];
  151. static char **cmd = NULL;
  152. static char *wmname = "tabbed";
  153. static const char *geometry = NULL;
  154. char *argv0;
  155. /* configuration, allows nested code to access above variables */
  156. #include "config.h"
  157. void
  158. buttonpress(const XEvent *e) {
  159. const XButtonPressedEvent *ev = &e->xbutton;
  160. int i;
  161. int fc;
  162. Arg arg;
  163. fc = getfirsttab();
  164. if((fc > 0 && ev->x < TEXTW(before)) || ev->x < 0)
  165. return;
  166. if(ev->y < 0 || ev-> y > bh)
  167. return;
  168. for(i = (fc > 0) ? fc : 0; i < nclients; i++) {
  169. if(clients[i]->tabx > ev->x) {
  170. switch(ev->button) {
  171. case Button1:
  172. focus(i);
  173. break;
  174. case Button2:
  175. focus(i);
  176. killclient(NULL);
  177. break;
  178. case Button4:
  179. case Button5:
  180. arg.i = ev->button == Button4 ? -1 : 1;
  181. rotate(&arg);
  182. break;
  183. }
  184. break;
  185. }
  186. }
  187. }
  188. void
  189. cleanup(void) {
  190. int i;
  191. for(i = 0; i < nclients; i++) {
  192. focus(i);
  193. killclient(NULL);
  194. killclient(NULL);
  195. XReparentWindow(dpy, clients[i]->win, root, 0, 0);
  196. unmanage(i);
  197. }
  198. free(clients);
  199. clients = NULL;
  200. if(dc.font.set) {
  201. XFreeFontSet(dpy, dc.font.set);
  202. } else {
  203. XFreeFont(dpy, dc.font.xfont);
  204. }
  205. XFreePixmap(dpy, dc.drawable);
  206. XFreeGC(dpy, dc.gc);
  207. XDestroyWindow(dpy, win);
  208. XSync(dpy, False);
  209. free(cmd);
  210. }
  211. void
  212. clientmessage(const XEvent *e) {
  213. const XClientMessageEvent *ev = &e->xclient;
  214. if(ev->message_type == wmatom[WMProtocols]
  215. && ev->data.l[0] == wmatom[WMDelete]) {
  216. running = False;
  217. }
  218. }
  219. void
  220. configurenotify(const XEvent *e) {
  221. const XConfigureEvent *ev = &e->xconfigure;
  222. if(ev->window == win && (ev->width != ww || ev->height != wh)) {
  223. ww = ev->width;
  224. wh = ev->height;
  225. XFreePixmap(dpy, dc.drawable);
  226. dc.drawable = XCreatePixmap(dpy, root, ww, wh,
  227. DefaultDepth(dpy, screen));
  228. if(sel > -1)
  229. resize(sel, ww, wh - bh);
  230. XSync(dpy, False);
  231. }
  232. }
  233. void
  234. configurerequest(const XEvent *e) {
  235. const XConfigureRequestEvent *ev = &e->xconfigurerequest;
  236. XWindowChanges wc;
  237. int c;
  238. if((c = getclient(ev->window)) > -1) {
  239. wc.x = 0;
  240. wc.y = bh;
  241. wc.width = ww;
  242. wc.height = wh - bh;
  243. wc.border_width = 0;
  244. wc.sibling = ev->above;
  245. wc.stack_mode = ev->detail;
  246. XConfigureWindow(dpy, clients[c]->win, ev->value_mask, &wc);
  247. }
  248. }
  249. void
  250. createnotify(const XEvent *e) {
  251. const XCreateWindowEvent *ev = &e->xcreatewindow;
  252. if(ev->window != win && getclient(ev->window) < 0)
  253. manage(ev->window);
  254. }
  255. void
  256. destroynotify(const XEvent *e) {
  257. const XDestroyWindowEvent *ev = &e->xdestroywindow;
  258. int c;
  259. if((c = getclient(ev->window)) > -1)
  260. unmanage(c);
  261. }
  262. void
  263. die(const char *errstr, ...) {
  264. va_list ap;
  265. va_start(ap, errstr);
  266. vfprintf(stderr, errstr, ap);
  267. va_end(ap);
  268. exit(EXIT_FAILURE);
  269. }
  270. void
  271. drawbar(void) {
  272. unsigned long *col;
  273. int c, fc, width, n = 0;
  274. char *name = NULL;
  275. if(nclients == 0) {
  276. dc.x = 0;
  277. dc.w = ww;
  278. XFetchName(dpy, win, &name);
  279. drawtext(name ? name : "", dc.norm);
  280. XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, ww, bh, 0, 0);
  281. XSync(dpy, False);
  282. return;
  283. }
  284. width = ww;
  285. clients[nclients-1]->tabx = -1;
  286. fc = getfirsttab();
  287. if(fc > -1)
  288. n = nclients - fc;
  289. if((n * tabwidth) > width) {
  290. dc.w = TEXTW(after);
  291. dc.x = width - dc.w;
  292. drawtext(after, dc.sel);
  293. width -= dc.w;
  294. }
  295. dc.x = 0;
  296. if(fc > 0) {
  297. dc.w = TEXTW(before);
  298. drawtext(before, dc.sel);
  299. dc.x += dc.w;
  300. width -= dc.w;
  301. }
  302. for(c = (fc > 0)? fc : 0; c < nclients && dc.x < width; c++) {
  303. dc.w = tabwidth;
  304. if(c == sel) {
  305. col = dc.sel;
  306. if((n * tabwidth) > width) {
  307. dc.w += width % tabwidth;
  308. } else {
  309. dc.w = width - (n - 1) * tabwidth;
  310. }
  311. } else {
  312. col = dc.norm;
  313. }
  314. drawtext(clients[c]->name, col);
  315. dc.x += dc.w;
  316. clients[c]->tabx = dc.x;
  317. }
  318. XCopyArea(dpy, dc.drawable, win, dc.gc, 0, 0, ww, bh, 0, 0);
  319. XSync(dpy, False);
  320. }
  321. void
  322. drawtext(const char *text, unsigned long col[ColLast]) {
  323. int i, x, y, h, len, olen;
  324. char buf[256];
  325. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  326. XSetForeground(dpy, dc.gc, col[ColBG]);
  327. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  328. if(!text)
  329. return;
  330. olen = strlen(text);
  331. h = dc.font.ascent + dc.font.descent;
  332. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  333. x = dc.x + (h / 2);
  334. /* shorten text if necessary */
  335. for(len = MIN(olen, sizeof(buf));
  336. len && textnw(text, len) > dc.w - h; len--);
  337. if(!len)
  338. return;
  339. memcpy(buf, text, len);
  340. if(len < olen) {
  341. for(i = len; i && i > len - 3; buf[--i] = '.');
  342. }
  343. XSetForeground(dpy, dc.gc, col[ColFG]);
  344. if(dc.font.set) {
  345. XmbDrawString(dpy, dc.drawable, dc.font.set,
  346. dc.gc, x, y, buf, len);
  347. } else {
  348. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  349. }
  350. }
  351. void *
  352. emallocz(size_t size) {
  353. void *p;
  354. if(!(p = calloc(1, size)))
  355. die("tabbed: cannot malloc\n");
  356. return p;
  357. }
  358. void *
  359. erealloc(void *o, size_t size) {
  360. void *p;
  361. if(!(p = realloc(o, size)))
  362. die("tabbed: cannot realloc\n");
  363. return p;
  364. }
  365. void
  366. expose(const XEvent *e) {
  367. const XExposeEvent *ev = &e->xexpose;
  368. if(ev->count == 0 && win == ev->window)
  369. drawbar();
  370. }
  371. void
  372. focus(int c) {
  373. char buf[BUFSIZ] = "tabbed-"VERSION" ::";
  374. size_t i, n;
  375. /* If c, sel and clients are -1, raise tabbed-win itself */
  376. if(nclients == 0) {
  377. cmd[cmd_append_pos] = NULL;
  378. for(i = 0, n = strlen(buf); cmd[i] && n < sizeof(buf); i++)
  379. n += snprintf(&buf[n], sizeof(buf) - n, " %s", cmd[i]);
  380. xsettitle(win, buf);
  381. XRaiseWindow(dpy, win);
  382. return;
  383. }
  384. if(c < 0 || c >= nclients)
  385. return;
  386. resize(c, ww, wh - bh);
  387. XRaiseWindow(dpy, clients[c]->win);
  388. XSetInputFocus(dpy, clients[c]->win, RevertToParent, CurrentTime);
  389. sendxembed(c, XEMBED_FOCUS_IN, XEMBED_FOCUS_CURRENT, 0, 0);
  390. sendxembed(c, XEMBED_WINDOW_ACTIVATE, 0, 0, 0);
  391. xsettitle(win, clients[c]->name);
  392. /* If sel is already c, change nothing. */
  393. if(sel != c) {
  394. lastsel = sel;
  395. sel = c;
  396. }
  397. drawbar();
  398. XSync(dpy, False);
  399. }
  400. void
  401. focusin(const XEvent *e) {
  402. const XFocusChangeEvent *ev = &e->xfocus;
  403. int dummy;
  404. Window focused;
  405. if(ev->mode != NotifyUngrab) {
  406. XGetInputFocus(dpy, &focused, &dummy);
  407. if(focused == win)
  408. focus(sel);
  409. }
  410. }
  411. void
  412. focusonce(const Arg *arg) {
  413. nextfocus = True;
  414. }
  415. void
  416. fullscreen(const Arg *arg) {
  417. XEvent e;
  418. e.type = ClientMessage;
  419. e.xclient.window = win;
  420. e.xclient.message_type = wmatom[WMState];
  421. e.xclient.format = 32;
  422. e.xclient.data.l[0] = 2;
  423. e.xclient.data.l[1] = wmatom[WMFullscreen];
  424. e.xclient.data.l[2] = 0;
  425. XSendEvent(dpy, root, False, SubstructureNotifyMask, &e);
  426. }
  427. char *
  428. getatom(int a) {
  429. static char buf[BUFSIZ];
  430. Atom adummy;
  431. int idummy;
  432. unsigned long ldummy;
  433. unsigned char *p = NULL;
  434. XGetWindowProperty(dpy, win, wmatom[a], 0L, BUFSIZ, False, XA_STRING,
  435. &adummy, &idummy, &ldummy, &ldummy, &p);
  436. if(p) {
  437. strncpy(buf, (char *)p, LENGTH(buf)-1);
  438. } else {
  439. buf[0] = '\0';
  440. }
  441. XFree(p);
  442. return buf;
  443. }
  444. int
  445. getclient(Window w) {
  446. int i;
  447. for(i = 0; i < nclients; i++) {
  448. if(clients[i]->win == w)
  449. return i;
  450. }
  451. return -1;
  452. }
  453. unsigned long
  454. getcolor(const char *colstr) {
  455. Colormap cmap = DefaultColormap(dpy, screen);
  456. XColor color;
  457. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  458. die("tabbed: cannot allocate color '%s'\n", colstr);
  459. return color.pixel;
  460. }
  461. int
  462. getfirsttab(void) {
  463. int c, n, fc;
  464. if(sel < 0)
  465. return -1;
  466. c = sel;
  467. fc = 0;
  468. n = nclients;
  469. if((n * tabwidth) > ww) {
  470. for(; (c * tabwidth) > (ww / 2)
  471. && (n * tabwidth) > ww;
  472. c--, n--, fc++);
  473. }
  474. return fc;
  475. }
  476. Bool
  477. gettextprop(Window w, Atom atom, char *text, unsigned int size) {
  478. char **list = NULL;
  479. int n;
  480. XTextProperty name;
  481. if(!text || size == 0)
  482. return False;
  483. text[0] = '\0';
  484. XGetTextProperty(dpy, w, &name, atom);
  485. if(!name.nitems)
  486. return False;
  487. if(name.encoding == XA_STRING) {
  488. strncpy(text, (char *)name.value, size - 1);
  489. } else {
  490. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  491. && n > 0 && *list) {
  492. strncpy(text, *list, size - 1);
  493. XFreeStringList(list);
  494. }
  495. }
  496. text[size - 1] = '\0';
  497. XFree(name.value);
  498. return True;
  499. }
  500. void
  501. initfont(const char *fontstr) {
  502. char *def, **missing, **font_names;
  503. int i, n;
  504. XFontStruct **xfonts;
  505. missing = NULL;
  506. if(dc.font.set)
  507. XFreeFontSet(dpy, dc.font.set);
  508. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  509. if(missing) {
  510. while(n--)
  511. fprintf(stderr, "tabbed: missing fontset: %s\n", missing[n]);
  512. XFreeStringList(missing);
  513. }
  514. if(dc.font.set) {
  515. dc.font.ascent = dc.font.descent = 0;
  516. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  517. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  518. dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
  519. dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
  520. xfonts++;
  521. }
  522. } else {
  523. if(dc.font.xfont)
  524. XFreeFont(dpy, dc.font.xfont);
  525. dc.font.xfont = NULL;
  526. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
  527. && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed"))) {
  528. die("tabbed: cannot load font: '%s'\n", fontstr);
  529. }
  530. dc.font.ascent = dc.font.xfont->ascent;
  531. dc.font.descent = dc.font.xfont->descent;
  532. }
  533. dc.font.height = dc.font.ascent + dc.font.descent;
  534. }
  535. Bool
  536. isprotodel(int c) {
  537. int i, n;
  538. Atom *protocols;
  539. Bool ret = False;
  540. if(XGetWMProtocols(dpy, clients[c]->win, &protocols, &n)) {
  541. for(i = 0; !ret && i < n; i++) {
  542. if(protocols[i] == wmatom[WMDelete])
  543. ret = True;
  544. }
  545. XFree(protocols);
  546. }
  547. return ret;
  548. }
  549. void
  550. keypress(const XEvent *e) {
  551. const XKeyEvent *ev = &e->xkey;
  552. unsigned int i;
  553. KeySym keysym;
  554. keysym = XkbKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0, 0);
  555. for(i = 0; i < LENGTH(keys); i++) {
  556. if(keysym == keys[i].keysym
  557. && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
  558. && keys[i].func) {
  559. keys[i].func(&(keys[i].arg));
  560. }
  561. }
  562. }
  563. void
  564. killclient(const Arg *arg) {
  565. XEvent ev;
  566. if(sel < 0)
  567. return;
  568. if(isprotodel(sel) && !clients[sel]->closed) {
  569. ev.type = ClientMessage;
  570. ev.xclient.window = clients[sel]->win;
  571. ev.xclient.message_type = wmatom[WMProtocols];
  572. ev.xclient.format = 32;
  573. ev.xclient.data.l[0] = wmatom[WMDelete];
  574. ev.xclient.data.l[1] = CurrentTime;
  575. XSendEvent(dpy, clients[sel]->win, False, NoEventMask, &ev);
  576. clients[sel]->closed = True;
  577. } else {
  578. XKillClient(dpy, clients[sel]->win);
  579. }
  580. }
  581. void
  582. manage(Window w) {
  583. updatenumlockmask();
  584. {
  585. int i, j, nextpos;
  586. unsigned int modifiers[] = { 0, LockMask, numlockmask,
  587. numlockmask|LockMask };
  588. KeyCode code;
  589. Client *c;
  590. XEvent e;
  591. XWithdrawWindow(dpy, w, 0);
  592. XReparentWindow(dpy, w, win, 0, bh);
  593. XSelectInput(dpy, w, PropertyChangeMask
  594. |StructureNotifyMask|EnterWindowMask);
  595. XSync(dpy, False);
  596. for(i = 0; i < LENGTH(keys); i++) {
  597. if((code = XKeysymToKeycode(dpy, keys[i].keysym))) {
  598. for(j = 0; j < LENGTH(modifiers); j++) {
  599. XGrabKey(dpy, code, keys[i].mod
  600. | modifiers[j], w,
  601. True, GrabModeAsync,
  602. GrabModeAsync);
  603. }
  604. }
  605. }
  606. c = emallocz(sizeof(*c));
  607. c->win = w;
  608. nclients++;
  609. clients = erealloc(clients, sizeof(Client *) * nclients);
  610. if(npisrelative) {
  611. nextpos = sel + newposition;
  612. } else {
  613. if(newposition < 0) {
  614. nextpos = nclients - newposition;
  615. } else {
  616. nextpos = newposition;
  617. }
  618. }
  619. if(nextpos >= nclients)
  620. nextpos = nclients - 1;
  621. if(nextpos < 0)
  622. nextpos = 0;
  623. if(nclients > 1 && nextpos < nclients - 1) {
  624. memmove(&clients[nextpos + 1], &clients[nextpos],
  625. sizeof(Client *) *
  626. (nclients - nextpos - 1));
  627. }
  628. clients[nextpos] = c;
  629. updatetitle(nextpos);
  630. XLowerWindow(dpy, w);
  631. XMapWindow(dpy, w);
  632. e.xclient.window = w;
  633. e.xclient.type = ClientMessage;
  634. e.xclient.message_type = wmatom[XEmbed];
  635. e.xclient.format = 32;
  636. e.xclient.data.l[0] = CurrentTime;
  637. e.xclient.data.l[1] = XEMBED_EMBEDDED_NOTIFY;
  638. e.xclient.data.l[2] = 0;
  639. e.xclient.data.l[3] = win;
  640. e.xclient.data.l[4] = 0;
  641. XSendEvent(dpy, root, False, NoEventMask, &e);
  642. XSync(dpy, False);
  643. /* Adjust sel before focus does set it to lastsel. */
  644. if(sel >= nextpos)
  645. sel++;
  646. focus((nextfocus)? nextpos : ((sel < 0)? 0 : sel));
  647. nextfocus = foreground;
  648. }
  649. }
  650. void
  651. maprequest(const XEvent *e) {
  652. const XMapRequestEvent *ev = &e->xmaprequest;
  653. if(getclient(ev->window) < 0)
  654. manage(ev->window);
  655. }
  656. void
  657. move(const Arg *arg) {
  658. if(arg->i >= 0 && arg->i < nclients)
  659. focus(arg->i);
  660. }
  661. void
  662. movetab(const Arg *arg) {
  663. int c;
  664. Client *new;
  665. if(sel < 0 || (arg->i == 0))
  666. return;
  667. c = sel + arg->i;
  668. while(c >= nclients)
  669. c -= nclients;
  670. while(c < 0)
  671. c += nclients;
  672. new = clients[c];
  673. clients[c] = clients[sel];
  674. clients[sel] = new;
  675. sel = c;
  676. drawbar();
  677. }
  678. void
  679. propertynotify(const XEvent *e) {
  680. const XPropertyEvent *ev = &e->xproperty;
  681. int c;
  682. char* selection = NULL;
  683. Arg arg;
  684. if(ev->state == PropertyNewValue && ev->atom == wmatom[WMSelectTab]) {
  685. selection = getatom(WMSelectTab);
  686. if(!strncmp(selection, "0x", 2)) {
  687. arg.i = getclient(strtoul(selection, NULL, 0));
  688. move(&arg);
  689. } else {
  690. cmd[cmd_append_pos] = selection;
  691. arg.v = cmd;
  692. spawn(&arg);
  693. }
  694. } else if(ev->state != PropertyDelete && ev->atom == XA_WM_NAME
  695. && (c = getclient(ev->window)) > -1) {
  696. updatetitle(c);
  697. }
  698. }
  699. void
  700. resize(int c, int w, int h) {
  701. XConfigureEvent ce;
  702. XWindowChanges wc;
  703. ce.x = 0;
  704. ce.y = bh;
  705. ce.width = wc.width = w;
  706. ce.height = wc.height = h;
  707. ce.type = ConfigureNotify;
  708. ce.display = dpy;
  709. ce.event = clients[c]->win;
  710. ce.window = clients[c]->win;
  711. ce.above = None;
  712. ce.override_redirect = False;
  713. ce.border_width = 0;
  714. XConfigureWindow(dpy, clients[c]->win, CWWidth|CWHeight, &wc);
  715. XSendEvent(dpy, clients[c]->win, False, StructureNotifyMask,
  716. (XEvent *)&ce);
  717. }
  718. void
  719. rotate(const Arg *arg) {
  720. int nsel = -1;
  721. if(sel < 0)
  722. return;
  723. if(arg->i == 0) {
  724. if(lastsel > -1)
  725. focus(lastsel);
  726. } else if(sel > -1) {
  727. /* Rotating in an arg->i step around the clients. */
  728. nsel = sel + arg->i;
  729. while(nsel >= nclients)
  730. nsel -= nclients;
  731. while(nsel < 0)
  732. nsel += nclients;
  733. focus(nsel);
  734. }
  735. }
  736. void
  737. run(void) {
  738. XEvent ev;
  739. /* main event loop */
  740. XSync(dpy, False);
  741. drawbar();
  742. if(doinitspawn == True)
  743. spawn(NULL);
  744. while(running) {
  745. XNextEvent(dpy, &ev);
  746. if(handler[ev.type])
  747. (handler[ev.type])(&ev); /* call handler */
  748. }
  749. }
  750. void
  751. sendxembed(int c, long msg, long detail, long d1, long d2) {
  752. XEvent e = { 0 };
  753. e.xclient.window = clients[c]->win;
  754. e.xclient.type = ClientMessage;
  755. e.xclient.message_type = wmatom[XEmbed];
  756. e.xclient.format = 32;
  757. e.xclient.data.l[0] = CurrentTime;
  758. e.xclient.data.l[1] = msg;
  759. e.xclient.data.l[2] = detail;
  760. e.xclient.data.l[3] = d1;
  761. e.xclient.data.l[4] = d2;
  762. XSendEvent(dpy, clients[c]->win, False, NoEventMask, &e);
  763. }
  764. void
  765. setcmd(int argc, char *argv[], int replace) {
  766. int i;
  767. cmd = emallocz((argc+3) * sizeof(*cmd));
  768. if (argc == 0)
  769. return;
  770. for(i = 0; i < argc; i++)
  771. cmd[i] = argv[i];
  772. cmd[(replace > 0)? replace : argc] = winid;
  773. cmd_append_pos = argc + !replace;
  774. cmd[cmd_append_pos] = cmd[cmd_append_pos+1] = NULL;
  775. }
  776. void
  777. setup(void) {
  778. int bitm, tx, ty, tw, th, dh, dw, isfixed;
  779. XClassHint class_hint;
  780. XSizeHints *size_hint;
  781. /* clean up any zombies immediately */
  782. sigchld(0);
  783. /* init screen */
  784. screen = DefaultScreen(dpy);
  785. root = RootWindow(dpy, screen);
  786. initfont(font);
  787. bh = dc.h = dc.font.height + 2;
  788. /* init atoms */
  789. wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
  790. wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  791. wmatom[XEmbed] = XInternAtom(dpy, "_XEMBED", False);
  792. wmatom[WMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
  793. wmatom[WMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
  794. wmatom[WMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
  795. wmatom[WMSelectTab] = XInternAtom(dpy, "_TABBED_SELECT_TAB", False);
  796. /* init appearance */
  797. wx = 0;
  798. wy = 0;
  799. ww = 800;
  800. wh = 600;
  801. isfixed = 0;
  802. if(geometry) {
  803. tx = ty = tw = th = 0;
  804. bitm = XParseGeometry(geometry, &tx, &ty, (unsigned *)&tw,
  805. (unsigned *)&th);
  806. if(bitm & XValue)
  807. wx = tx;
  808. if(bitm & YValue)
  809. wy = ty;
  810. if(bitm & WidthValue)
  811. ww = tw;
  812. if(bitm & HeightValue)
  813. wh = th;
  814. if(bitm & XNegative && wx == 0)
  815. wx = -1;
  816. if(bitm & YNegative && wy == 0)
  817. wy = -1;
  818. if(bitm & (HeightValue|WidthValue))
  819. isfixed = 1;
  820. dw = DisplayWidth(dpy, screen);
  821. dh = DisplayHeight(dpy, screen);
  822. if(wx < 0)
  823. wx = dw + wx - ww - 1;
  824. if(wy < 0)
  825. wy = dh + wy - wh - 1;
  826. }
  827. dc.norm[ColBG] = getcolor(normbgcolor);
  828. dc.norm[ColFG] = getcolor(normfgcolor);
  829. dc.sel[ColBG] = getcolor(selbgcolor);
  830. dc.sel[ColFG] = getcolor(selfgcolor);
  831. dc.drawable = XCreatePixmap(dpy, root, ww, wh,
  832. DefaultDepth(dpy, screen));
  833. dc.gc = XCreateGC(dpy, root, 0, 0);
  834. if(!dc.font.set)
  835. XSetFont(dpy, dc.gc, dc.font.xfont->fid);
  836. win = XCreateSimpleWindow(dpy, root, wx, wy, ww, wh, 0,
  837. dc.norm[ColFG], dc.norm[ColBG]);
  838. XMapRaised(dpy, win);
  839. XSelectInput(dpy, win, SubstructureNotifyMask|FocusChangeMask|
  840. ButtonPressMask|ExposureMask|KeyPressMask|PropertyChangeMask|
  841. StructureNotifyMask|SubstructureRedirectMask);
  842. xerrorxlib = XSetErrorHandler(xerror);
  843. class_hint.res_name = wmname;
  844. class_hint.res_class = "tabbed";
  845. XSetClassHint(dpy, win, &class_hint);
  846. size_hint = XAllocSizeHints();
  847. if(!isfixed) {
  848. size_hint->flags = PSize;
  849. size_hint->height = wh;
  850. size_hint->width = ww;
  851. } else {
  852. size_hint->flags = PMaxSize | PMinSize;
  853. size_hint->min_width = size_hint->max_width = ww;
  854. size_hint->min_height = size_hint->max_height = wh;
  855. }
  856. XSetWMProperties(dpy, win, NULL, NULL, NULL, 0, size_hint, NULL, NULL);
  857. XFree(size_hint);
  858. XSetWMProtocols(dpy, win, &wmatom[WMDelete], 1);
  859. snprintf(winid, sizeof(winid), "%lu", win);
  860. setenv("XEMBED", winid, 1);
  861. nextfocus = foreground;
  862. focus(-1);
  863. }
  864. void
  865. sigchld(int unused) {
  866. if(signal(SIGCHLD, sigchld) == SIG_ERR)
  867. die("tabbed: cannot install SIGCHLD handler");
  868. while(0 < waitpid(-1, NULL, WNOHANG));
  869. }
  870. void
  871. spawn(const Arg *arg) {
  872. if(fork() == 0) {
  873. if(dpy)
  874. close(ConnectionNumber(dpy));
  875. setsid();
  876. if(arg && arg->v) {
  877. execvp(((char **)arg->v)[0], (char **)arg->v);
  878. fprintf(stderr, "tabbed: execvp %s",
  879. ((char **)arg->v)[0]);
  880. } else {
  881. cmd[cmd_append_pos] = NULL;
  882. execvp(cmd[0], cmd);
  883. fprintf(stderr, "tabbed: execvp %s", cmd[0]);
  884. }
  885. perror(" failed");
  886. exit(0);
  887. }
  888. }
  889. int
  890. textnw(const char *text, unsigned int len) {
  891. XRectangle r;
  892. if(dc.font.set) {
  893. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  894. return r.width;
  895. }
  896. return XTextWidth(dc.font.xfont, text, len);
  897. }
  898. void
  899. unmanage(int c) {
  900. if(c < 0 || c >= nclients) {
  901. drawbar();
  902. XSync(dpy, False);
  903. return;
  904. }
  905. if(!nclients) {
  906. return;
  907. } else if(c == 0) {
  908. /* First client. */
  909. nclients--;
  910. free(clients[0]);
  911. memmove(&clients[0], &clients[1], sizeof(Client *) * nclients);
  912. } else if(c == nclients - 1) {
  913. /* Last client. */
  914. nclients--;
  915. free(clients[c]);
  916. clients = erealloc(clients, sizeof(Client *) * nclients);
  917. } else {
  918. /* Somewhere inbetween. */
  919. free(clients[c]);
  920. memmove(&clients[c], &clients[c+1],
  921. sizeof(Client *) * (nclients - (c + 1)));
  922. nclients--;
  923. }
  924. if(nclients <= 0) {
  925. sel = -1;
  926. lastsel = -1;
  927. if (closelastclient) {
  928. running = False;
  929. } else if (fillagain && running) {
  930. spawn(NULL);
  931. }
  932. } else {
  933. if(c == lastsel) {
  934. lastsel = -1;
  935. } else if(lastsel > c) {
  936. lastsel--;
  937. }
  938. lastsel = MIN(lastsel, nclients - 1);
  939. if(c == sel) {
  940. /* Note that focus() will never set lastsel == sel,
  941. * so if here lastsel == sel, it was decreased by above if() clause
  942. * and was actually (sel + 1) before.
  943. */
  944. if(lastsel > 0) {
  945. focus(lastsel);
  946. } else {
  947. focus(0);
  948. lastsel = 1;
  949. }
  950. } else {
  951. if(sel > c)
  952. sel -= 1;
  953. if(sel >= nclients)
  954. sel = nclients - 1;
  955. focus(sel);
  956. }
  957. }
  958. drawbar();
  959. XSync(dpy, False);
  960. }
  961. void
  962. updatenumlockmask(void) {
  963. unsigned int i, j;
  964. XModifierKeymap *modmap;
  965. numlockmask = 0;
  966. modmap = XGetModifierMapping(dpy);
  967. for(i = 0; i < 8; i++) {
  968. for(j = 0; j < modmap->max_keypermod; j++) {
  969. if(modmap->modifiermap[i * modmap->max_keypermod + j]
  970. == XKeysymToKeycode(dpy,
  971. XK_Num_Lock)) {
  972. numlockmask = (1 << i);
  973. }
  974. }
  975. }
  976. XFreeModifiermap(modmap);
  977. }
  978. void
  979. updatetitle(int c) {
  980. if(!gettextprop(clients[c]->win, wmatom[WMName],
  981. clients[c]->name, sizeof(clients[c]->name))) {
  982. gettextprop(clients[c]->win, XA_WM_NAME,
  983. clients[c]->name, sizeof(clients[c]->name));
  984. }
  985. if(sel == c)
  986. xsettitle(win, clients[c]->name);
  987. drawbar();
  988. }
  989. /* There's no way to check accesses to destroyed windows, thus those cases are
  990. * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
  991. * default error handler, which may call exit. */
  992. int
  993. xerror(Display *dpy, XErrorEvent *ee) {
  994. if(ee->error_code == BadWindow
  995. || (ee->request_code == X_SetInputFocus
  996. && ee->error_code == BadMatch)
  997. || (ee->request_code == X_PolyText8
  998. && ee->error_code == BadDrawable)
  999. || (ee->request_code == X_PolyFillRectangle
  1000. && ee->error_code == BadDrawable)
  1001. || (ee->request_code == X_PolySegment
  1002. && ee->error_code == BadDrawable)
  1003. || (ee->request_code == X_ConfigureWindow
  1004. && ee->error_code == BadMatch)
  1005. || (ee->request_code == X_GrabButton
  1006. && ee->error_code == BadAccess)
  1007. || (ee->request_code == X_GrabKey
  1008. && ee->error_code == BadAccess)
  1009. || (ee->request_code == X_CopyArea
  1010. && ee->error_code == BadDrawable)) {
  1011. return 0;
  1012. }
  1013. fprintf(stderr, "tabbed: fatal error: request code=%d, error code=%d\n",
  1014. ee->request_code, ee->error_code);
  1015. return xerrorxlib(dpy, ee); /* may call exit */
  1016. }
  1017. void
  1018. xsettitle(Window w, const char *str) {
  1019. XTextProperty xtp;
  1020. if(XmbTextListToTextProperty(dpy, (char **)&str, 1, XCompoundTextStyle,
  1021. &xtp) == Success) {
  1022. XSetTextProperty(dpy, w, &xtp, wmatom[WMName]);
  1023. XSetTextProperty(dpy, w, &xtp, XA_WM_NAME);
  1024. XFree(xtp.value);
  1025. }
  1026. }
  1027. char *argv0;
  1028. void
  1029. usage(void) {
  1030. die("usage: %s [-dfhsv] [-g geometry] [-n name] [-p [s+/-]pos] [-r narg] "
  1031. "[-u color] [-U color] [-t color] [-T color] command...\n", argv0);
  1032. }
  1033. int
  1034. main(int argc, char *argv[]) {
  1035. Bool detach = False;
  1036. int replace = 0;
  1037. char *pstr;
  1038. ARGBEGIN {
  1039. case 'c':
  1040. closelastclient = True;
  1041. fillagain = False;
  1042. break;
  1043. case 'd':
  1044. detach = True;
  1045. break;
  1046. case 'f':
  1047. fillagain = True;
  1048. break;
  1049. case 'g':
  1050. geometry = EARGF(usage());
  1051. break;
  1052. case 'n':
  1053. wmname = EARGF(usage());
  1054. break;
  1055. case 'p':
  1056. pstr = EARGF(usage());
  1057. if(pstr[0] == 's') {
  1058. npisrelative = True;
  1059. newposition = atoi(&pstr[1]);
  1060. } else {
  1061. newposition = atoi(pstr);
  1062. }
  1063. break;
  1064. case 'r':
  1065. replace = atoi(EARGF(usage()));
  1066. break;
  1067. case 's':
  1068. doinitspawn = False;
  1069. break;
  1070. case 'v':
  1071. die("tabbed-"VERSION", © 2009-2012"
  1072. " tabbed engineers, see LICENSE"
  1073. " for details.\n");
  1074. break;
  1075. case 't':
  1076. selbgcolor = EARGF(usage());
  1077. break;
  1078. case 'T':
  1079. selfgcolor = EARGF(usage());
  1080. break;
  1081. case 'u':
  1082. normbgcolor = EARGF(usage());
  1083. break;
  1084. case 'U':
  1085. normfgcolor = EARGF(usage());
  1086. break;
  1087. default:
  1088. case 'h':
  1089. usage();
  1090. } ARGEND;
  1091. if(argc < 1) {
  1092. doinitspawn = False;
  1093. fillagain = False;
  1094. }
  1095. setcmd(argc, argv, replace);
  1096. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  1097. fprintf(stderr, "tabbed: no locale support\n");
  1098. if(!(dpy = XOpenDisplay(NULL)))
  1099. die("tabbed: cannot open display\n");
  1100. setup();
  1101. printf("0x%lx\n", win);
  1102. fflush(NULL);
  1103. if(detach) {
  1104. if(fork() == 0) {
  1105. fclose(stdout);
  1106. } else {
  1107. if(dpy)
  1108. close(ConnectionNumber(dpy));
  1109. return EXIT_SUCCESS;
  1110. }
  1111. }
  1112. run();
  1113. cleanup();
  1114. XCloseDisplay(dpy);
  1115. return EXIT_SUCCESS;
  1116. }