dmw.c (28276B)
1 /* See LICENSE file for copyright and license details. 2 * 3 * dwm made in wayland is a proper port/rewrite of the original suckless 4 * dynamic window manager reimagined in a better display protocol. Unlike 5 * dwl, this doesn't rely on wlroots (a terrible implementation of the 6 * wayland protocol) and instead uses neuswc/neuwld which is much more 7 * minmal and modern. 8 * 9 * This tries to be 1:1 to dwm, however given the difference between X and 10 * wayland, many of the functions had to be rewritten more so than others, 11 * however the spirit of dwm is still felt. There is some reorganization 12 * of the original functions, however you will be pleased to see that I 13 * have tried to preserve as much as possible of the original dwm. 14 * Unfortunately, some features are lost however, due to neuswc's lack of 15 * support of various popular wayland protocols (such as urgent tags). 16 * 17 * dmw runs on the Wayland event loop. When neuswc finds a new screen or 18 * window it calls newscreen() or newwindow(). Each sets up a table of 19 * handlers and gives neuswc the Monitor or Client they act on. That way 20 * entered() gets the client under the pointer directly. 21 * 22 * Each toplevel window dmw manages is a Client. A dialog with a managed 23 * parent starts out floating on its parent's monitor and tags. As in dwm, 24 * each monitor keeps two lists of the same clients. One gives the layout 25 * its tiling order, the other remembers focus so focusing a window leaves 26 * its place in the layout alone. Each tag is one bit. A client is visible 27 * when it shares a tag with the monitor's current view. 28 * 29 * Keys, buttons and rules live in config.h. When a binding runs spawn(), 30 * the child starts a new session and runs the command. The event loop 31 * reaps exited children on SIGCHLD so dmw can keep handling windows. 32 * 33 * To understand everything else start reading main(). 34 */ 35 #include "bar.h" 36 37 #include <errno.h> 38 #include <fcntl.h> 39 #include <signal.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <swc.h> 44 #include <sys/stat.h> 45 #include <sys/wait.h> 46 #include <unistd.h> 47 #include <wayland-server.h> 48 #include <wld/wld.h> 49 #include <xkbcommon/xkbcommon-keysyms.h> 50 51 #include "util.h" 52 53 /* macros */ 54 #define ISVISIBLE(c) ((c)->tags & (c)->mon->tagset[(c)->mon->seltags]) 55 #define TAGMASK ((1u << LENGTH(tags)) - 1) 56 57 /* enums */ 58 enum { Button1 = 0x110, Button2 = 0x112, Button3 = 0x111 }; /* keep config.h button names portable across bsd/linux */ 59 enum { Stacked, Tiled, Fullscreen }; /* window states */ 60 enum { SchemeNorm, SchemeSel }; /* color schemes */ 61 enum { ColFg, ColBg, ColBorder }; /* colors */ 62 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle, ClkClientWin, ClkRootWin }; /* clicks */ 63 64 typedef union { 65 int i; 66 unsigned int ui; 67 float f; 68 const void *v; 69 } Arg; 70 71 struct Monitor; 72 struct Layout { 73 const char *symbol; 74 void (*arrange)(struct Monitor *); 75 }; 76 struct Rule { 77 const char *app_id, *title; 78 unsigned int tags; 79 int isfloating, monitor; 80 }; 81 struct Key { 82 unsigned int mod, keysym; 83 void (*func)(const Arg *); 84 const Arg arg; 85 }; 86 struct Button { 87 unsigned int click, mask, button; 88 void (*func)(const Arg *); 89 const Arg arg; 90 }; 91 struct Client { 92 struct swc_window *win; 93 struct Monitor *mon; 94 struct Client *next, *snext; 95 struct wl_event_source *pending; 96 struct swc_rectangle saved; 97 unsigned int tags; 98 int isfloating, isfullscreen, mapped, mode; 99 }; 100 struct Monitor { 101 struct Bar bar; 102 struct Client *clients, *stack, *sel; 103 struct Monitor *next; 104 const struct Layout *lt[2]; 105 unsigned int seltags, sellt, tagset[2]; 106 int num, showbar, nmaster; 107 float mfact; 108 char ltsymbol[32]; 109 }; 110 111 /* function declarations */ 112 static void arrange(struct Monitor *); 113 static void focus(struct Client *); 114 static void focusmon(const Arg *), focusstack(const Arg *), incnmaster(const Arg *); 115 static void killclient(const Arg *), movemouse(const Arg *), quit(const Arg *); 116 static void resizemouse(const Arg *), setlayout(const Arg *), setmfact(const Arg *); 117 static void spawn(const Arg *), tag(const Arg *), tagmon(const Arg *), togglebar(const Arg *); 118 static void togglefloating(const Arg *), togglefullscreen(const Arg *), toggletag(const Arg *); 119 static void toggleview(const Arg *), view(const Arg *), zoom(const Arg *); 120 static void monocle(struct Monitor *), tile(struct Monitor *); 121 122 /* configuration */ 123 #include "config.h" 124 125 /* compile-time check if all tags fit into an unsigned int bit array. */ 126 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; }; 127 128 /* global vars */ 129 static struct Monitor *mons, *selmon; 130 static int shutting_down; 131 static struct wl_display *display; 132 static struct wl_event_loop *loop; 133 static uint32_t scheme[2][3]; 134 static char status[256] = "dmw-" VERSION; 135 static struct { 136 struct Client *c; 137 struct wl_event_source *timer; 138 struct swc_rectangle start; 139 int x, y, resize, moved; 140 uint32_t button; 141 } drag; 142 143 /* function implementations */ 144 static struct Client * 145 findclient(struct swc_window *win) 146 { 147 struct Monitor *m; 148 struct Client *c; 149 150 for (m = mons; m; m = m->next) 151 for (c = m->clients; c; c = c->next) 152 if (c->win == win) 153 return c; 154 return NULL; 155 } 156 157 static struct Client * 158 nexttiled(struct Client *c) 159 { 160 for (; c && (c->isfloating || c->isfullscreen || !ISVISIBLE(c)); c = c->next); 161 return c; 162 } 163 164 static void 165 detach(struct Client *c) 166 { 167 struct Client **p; 168 169 for (p = &c->mon->clients; *p != c; p = &(*p)->next); 170 *p = c->next; 171 for (p = &c->mon->stack; *p != c; p = &(*p)->snext); 172 *p = c->snext; 173 if (c->mon->sel == c) 174 c->mon->sel = NULL; 175 } 176 177 static void 178 attach(struct Client *c, struct Monitor *m) 179 { 180 c->mon = m; 181 c->next = m->clients; 182 m->clients = c; 183 c->snext = m->stack; 184 m->stack = c; 185 } 186 187 static void 188 drawbar(struct Monitor *m) 189 { 190 bar_update(&m->bar, m->showbar && !(m->sel && m->sel->isfullscreen)); 191 } 192 193 static void 194 restack(struct Monitor *m) 195 { 196 struct Client *c = m->sel; 197 198 if (c && ISVISIBLE(c)) 199 swc_window_raise(c->win); 200 } 201 202 static void 203 focus(struct Client *c) 204 { 205 struct Client **p; 206 struct Monitor *m; 207 208 if (!selmon) 209 return; 210 if (!c || !ISVISIBLE(c)) 211 for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext); 212 if (c) { 213 selmon = c->mon; 214 for (p = &selmon->stack; *p != c; p = &(*p)->snext); 215 *p = c->snext; 216 c->snext = selmon->stack; 217 selmon->stack = c; 218 } 219 selmon->sel = c; 220 restack(selmon); 221 swc_window_focus(c ? c->win : NULL); 222 for (m = mons; m; m = m->next) { 223 struct Client *i; 224 for (i = m->clients; i; i = i->next) 225 swc_window_set_border(i->win, scheme[i == c][ColBorder], 226 i->isfullscreen ? 0 : borderpx, 0, 0); 227 drawbar(m); 228 } 229 } 230 231 static int 232 dimension(int value, unsigned int min, unsigned int max) 233 { 234 value = MAX(value, (int)barfont->height + 2); 235 if (min && (unsigned int)value < min) 236 value = min; 237 if (max && (unsigned int)value > max) 238 value = MAX(min, max); 239 return value; 240 } 241 242 static int 243 place(struct Client *c, int x, int y, int w, int h) 244 { 245 struct swc_rectangle g = { x + borderpx, y + borderpx, 246 dimension(w - 2 * (int)borderpx, c->win->min_width, c->win->max_width), 247 dimension(h - 2 * (int)borderpx, c->win->min_height, c->win->max_height) }; 248 249 swc_window_set_geometry(c->win, &g); 250 return g.height + 2 * borderpx; 251 } 252 253 static void 254 tile(struct Monitor *m) 255 { 256 struct swc_rectangle g = m->bar.screen->usable_geometry; 257 struct Client *c; 258 int i, n, h, mw, my, ty; 259 260 for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++); 261 mw = n <= m->nmaster ? g.width : m->nmaster ? g.width * m->mfact : 0; 262 for (i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) { 263 if (i < m->nmaster) { 264 h = place(c, g.x, g.y + my, mw, ((int)g.height - my) / (MIN(n, m->nmaster) - i)); 265 if (my + h < (int)g.height) 266 my += h; 267 } else { 268 h = place(c, g.x + mw, g.y + ty, g.width - mw, ((int)g.height - ty) / (n - i)); 269 if (ty + h < (int)g.height) 270 ty += h; 271 } 272 } 273 } 274 275 static void 276 monocle(struct Monitor *m) 277 { 278 struct swc_rectangle g = m->bar.screen->usable_geometry; 279 struct Client *c; 280 int n = 0; 281 282 for (c = m->clients; c; c = c->next) 283 if (ISVISIBLE(c)) 284 n++; 285 if (n) 286 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n); 287 for (c = nexttiled(m->clients); c; c = nexttiled(c->next)) 288 place(c, g.x, g.y, g.width, g.height); 289 } 290 291 static void 292 arrange(struct Monitor *m) 293 { 294 const struct Layout *lt = m->lt[m->sellt]; 295 struct Client *c; 296 int mode; 297 298 if (!m->sel || !ISVISIBLE(m->sel)) 299 for (m->sel = m->stack; m->sel && !ISVISIBLE(m->sel); m->sel = m->sel->snext); 300 for (c = m->clients; c; c = c->next) { 301 mode = c->isfullscreen ? Fullscreen : lt->arrange && !c->isfloating ? Tiled : Stacked; 302 if (mode != c->mode) { 303 if (mode == Fullscreen) 304 swc_window_set_fullscreen(c->win, m->bar.screen); 305 else if (mode == Tiled) 306 swc_window_set_tiled(c->win); 307 else 308 swc_window_set_stacked(c->win); 309 c->mode = mode; 310 swc_window_set_z(c->win, mode == Tiled ? SWC_Z_WINDOW : SWC_Z_WINDOW + 1); 311 } 312 if (ISVISIBLE(c)) { 313 swc_window_show(c->win); 314 if (c->isfullscreen) 315 swc_window_set_geometry(c->win, &m->bar.screen->geometry); 316 } else 317 swc_window_hide(c->win); 318 } 319 snprintf(m->ltsymbol, sizeof m->ltsymbol, "%s", lt->symbol); 320 if (lt->arrange) 321 lt->arrange(m); 322 restack(m); 323 drawbar(m); 324 } 325 326 static struct Monitor * 327 dirtomon(int dir) 328 { 329 struct Monitor *m; 330 331 if (dir > 0) 332 return selmon->next ? selmon->next : mons; 333 for (m = mons; m->next && m->next != selmon; m = m->next); 334 return m; 335 } 336 337 static void 338 focusmon(const Arg *arg) 339 { 340 selmon = dirtomon(arg->i); 341 focus(NULL); 342 } 343 344 static void 345 sendmon(struct Client *c, struct Monitor *m) 346 { 347 struct Monitor *old = c->mon; 348 349 if (m == old) 350 return; 351 detach(c); 352 attach(c, m); 353 c->tags = m->tagset[m->seltags]; 354 if (!drag.c) 355 swc_window_set_position(c->win, m->bar.screen->usable_geometry.x + borderpx, 356 m->bar.screen->usable_geometry.y + borderpx); 357 focus(NULL); 358 arrange(old); 359 arrange(m); 360 } 361 362 static void 363 focusstack(const Arg *arg) 364 { 365 struct Client *c = NULL, *i; 366 367 if (!selmon->sel || (selmon->sel->isfullscreen && lockfullscreen)) 368 return; 369 if (arg->i > 0) { 370 for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next); 371 if (!c) 372 for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next); 373 } else { 374 for (i = selmon->clients; i != selmon->sel; i = i->next) 375 if (ISVISIBLE(i)) 376 c = i; 377 if (!c) 378 for (; i; i = i->next) 379 if (ISVISIBLE(i)) 380 c = i; 381 } 382 if (c) 383 focus(c); 384 } 385 386 static void 387 incnmaster(const Arg *arg) 388 { 389 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0); 390 arrange(selmon); 391 } 392 393 static void 394 setlayout(const Arg *arg) 395 { 396 if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt]) 397 selmon->sellt ^= 1; 398 if (arg && arg->v) 399 selmon->lt[selmon->sellt] = arg->v; 400 arrange(selmon); 401 } 402 403 static void 404 setmfact(const Arg *arg) 405 { 406 float f; 407 408 if (!arg || !selmon->lt[selmon->sellt]->arrange) 409 return; 410 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0; 411 if (f < 0.05 || f > 0.95) 412 return; 413 selmon->mfact = f; 414 arrange(selmon); 415 } 416 417 static void 418 tag(const Arg *arg) 419 { 420 if (selmon->sel && arg->ui & TAGMASK) { 421 selmon->sel->tags = arg->ui & TAGMASK; 422 focus(NULL); 423 arrange(selmon); 424 } 425 } 426 427 static void 428 tagmon(const Arg *arg) 429 { 430 if (!selmon->sel || !mons->next) 431 return; 432 sendmon(selmon->sel, dirtomon(arg->i)); 433 } 434 435 static void 436 toggletag(const Arg *arg) 437 { 438 unsigned int newtags; 439 440 if (!selmon->sel) 441 return; 442 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK); 443 if (newtags) { 444 selmon->sel->tags = newtags; 445 focus(NULL); 446 arrange(selmon); 447 } 448 } 449 450 static void 451 toggleview(const Arg *arg) 452 { 453 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK); 454 455 if (newtagset) { 456 selmon->tagset[selmon->seltags] = newtagset; 457 focus(NULL); 458 arrange(selmon); 459 } 460 } 461 462 static void 463 view(const Arg *arg) 464 { 465 if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags]) 466 return; 467 selmon->seltags ^= 1; /* toggle sel tagset */ 468 if (arg->ui & TAGMASK) 469 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK; 470 focus(NULL); 471 arrange(selmon); 472 } 473 474 static void 475 zoom(const Arg *arg) 476 { 477 struct Client *c = selmon->sel; 478 479 if (!selmon->lt[selmon->sellt]->arrange || !c || c->isfloating || c->isfullscreen) 480 return; 481 if (c == nexttiled(selmon->clients) && !(c = nexttiled(c->next))) 482 return; 483 detach(c); 484 attach(c, selmon); 485 focus(c); 486 arrange(selmon); 487 } 488 489 static int 490 fixed(struct Client *c) 491 { 492 return c->win->min_width && c->win->min_height && 493 c->win->min_width == c->win->max_width && 494 c->win->min_height == c->win->max_height; 495 } 496 497 static void 498 setfullscreen(void *data, bool on) 499 { 500 struct Client *c = data; 501 502 if (c->isfullscreen == on) 503 return; 504 if (on) 505 swc_window_get_geometry(c->win, &c->saved); 506 c->isfullscreen = on; 507 arrange(c->mon); 508 if (!on && c->mode == Stacked && c->saved.width && c->saved.height) 509 swc_window_set_geometry(c->win, &c->saved); 510 swc_window_set_border(c->win, scheme[c->mon == selmon && selmon->sel == c][ColBorder], 511 on ? 0 : borderpx, 0, 0); 512 } 513 514 static void 515 togglefullscreen(const Arg *arg) 516 { 517 if (selmon->sel) 518 setfullscreen(selmon->sel, !selmon->sel->isfullscreen); 519 } 520 521 static void 522 togglefloating(const Arg *arg) 523 { 524 struct Client *c = selmon->sel; 525 if (c && !c->isfullscreen) { 526 c->isfloating = !c->isfloating || fixed(c); 527 arrange(selmon); 528 } 529 } 530 531 static void 532 togglebar(const Arg *arg) 533 { 534 selmon->showbar = !selmon->showbar; 535 drawbar(selmon); 536 } 537 538 static void 539 killclient(const Arg *arg) 540 { 541 if (selmon->sel) 542 swc_window_close(selmon->sel->win); 543 } 544 545 static void 546 quit(const Arg *arg) 547 { 548 wl_display_terminate(display); 549 } 550 551 static void 552 spawn(const Arg *arg) 553 { 554 char *const *cmd = arg->v; 555 pid_t pid; 556 sigset_t mask; 557 558 if ((pid = fork()) < 0) { 559 perror("dmw: fork"); 560 return; 561 } 562 if (pid == 0) { 563 setsid(); 564 sigemptyset(&mask); 565 sigprocmask(SIG_SETMASK, &mask, NULL); 566 if (!isatty(STDIN_FILENO)) { 567 int fd = open("/dev/null", O_RDONLY); 568 if (fd >= 0 && fd != STDIN_FILENO) { 569 dup2(fd, STDIN_FILENO); 570 close(fd); 571 } 572 } 573 execvp(cmd[0], cmd); 574 perror(cmd[0]); 575 _exit(127); 576 } 577 } 578 579 static void 580 pointerpos(int *x, int *y) 581 { 582 int32_t fx, fy; 583 584 swc_cursor_position(&fx, &fy); 585 *x = wl_fixed_to_int(fx); 586 *y = wl_fixed_to_int(fy); 587 } 588 589 static struct Monitor * 590 pointmon(int x, int y) 591 { 592 struct Monitor *m; 593 struct swc_rectangle g; 594 595 for (m = mons; m; m = m->next) { 596 g = m->bar.screen->geometry; 597 if (x >= g.x && y >= g.y && x - g.x < (int)g.width && y - g.y < (int)g.height) 598 return m; 599 } 600 return selmon; 601 } 602 603 static int 604 dragmotion(void *data) 605 { 606 struct Client *c = drag.c; 607 struct swc_rectangle g = drag.start, area; 608 int x, y, dx, dy, right, bottom; 609 610 if (!c || c->isfullscreen || !ISVISIBLE(c)) 611 return 0; 612 pointerpos(&x, &y); 613 area = pointmon(x, y)->bar.screen->usable_geometry; 614 dx = x - drag.x; 615 dy = y - drag.y; 616 if (!drag.moved && c->mode == Tiled && MAX(abs(dx), abs(dy)) <= (int)snap) 617 goto next; 618 if (!drag.moved && (dx || dy)) { 619 if (c->mode == Tiled) { 620 c->isfloating = 1; 621 arrange(c->mon); 622 } 623 drag.moved = 1; 624 } 625 if (!drag.moved) 626 goto next; 627 if (drag.resize) { 628 g.width = dimension((int)g.width + dx, c->win->min_width, c->win->max_width); 629 g.height = dimension((int)g.height + dy, c->win->min_height, c->win->max_height); 630 swc_window_set_size(c->win, g.width, g.height); 631 } else { 632 g.x += dx; 633 g.y += dy; 634 right = area.x + (int)area.width - borderpx - g.width; 635 bottom = area.y + (int)area.height - borderpx - g.height; 636 if (abs(g.x - area.x - (int)borderpx) < (int)snap) 637 g.x = area.x + borderpx; 638 else if (abs(g.x - right) < (int)snap) 639 g.x = right; 640 if (abs(g.y - area.y - (int)borderpx) < (int)snap) 641 g.y = area.y + borderpx; 642 else if (abs(g.y - bottom) < (int)snap) 643 g.y = bottom; 644 swc_window_set_position(c->win, g.x, g.y); 645 } 646 next: 647 wl_event_source_timer_update(drag.timer, MAX(1, 1000 / MAX(1, refreshrate))); 648 return 0; 649 } 650 651 static void 652 startdrag(int resize) 653 { 654 struct Client *c = selmon->sel; 655 if (drag.c || !c || c->isfullscreen || !swc_window_get_geometry(c->win, &drag.start)) 656 return; 657 drag.c = c; 658 drag.resize = resize; 659 drag.moved = 0; 660 pointerpos(&drag.x, &drag.y); 661 dragmotion(NULL); 662 } 663 664 static void 665 movemouse(const Arg *arg) 666 { 667 startdrag(0); 668 } 669 670 static void 671 resizemouse(const Arg *arg) 672 { 673 startdrag(1); 674 } 675 676 struct Bar * 677 bar_for_screen(struct swc_screen *screen) 678 { 679 struct Monitor *m; 680 681 for (m = mons; m; m = m->next) 682 if (m->bar.screen == screen) 683 return &m->bar; 684 return NULL; 685 } 686 687 static int 688 label(struct wld_renderer *r, int x, int w, int pad, const char *s, int selected, int box, int filled) 689 { 690 int h = barfont->height + 2; 691 int size = MAX(2, h / 6); 692 uint32_t *col = scheme[selected]; 693 694 if (r && w > 0) { 695 wld_set_clip(r, x, 0, w, h); 696 wld_fill_rectangle(r, col[ColBg], x, 0, w, h); 697 wld_draw_text(r, barfont, col[ColFg], x + pad, 698 (h - (int)barfont->height) / 2 + barfont->ascent, s, -1, NULL); 699 if (box) { 700 wld_fill_rectangle(r, col[ColFg], x + 1, 1, size, size); 701 if (!filled && size > 2) 702 wld_fill_rectangle(r, col[ColBg], x + 2, 2, size - 2, size - 2); 703 } 704 } 705 return x + w; 706 } 707 708 static unsigned int 709 baritems(struct Monitor *m, struct wld_renderer *r, int px, Arg *arg) 710 { 711 struct Client *c; 712 unsigned int i, occ = 0, bit; 713 int x = 0, w = m->bar.width, end, pad = barfont->height; 714 715 for (c = m->clients; c; c = c->next) 716 occ |= c->tags; 717 for (i = 0; i < LENGTH(tags); i++) { 718 bit = 1u << i; 719 x = label(r, x, textwidth(tags[i]) + pad, pad / 2, tags[i], !!(m->tagset[m->seltags] & bit), 720 occ & bit, m == selmon && m->sel && (m->sel->tags & bit)); 721 if (px >= 0 && px < x) { 722 arg->ui = bit; 723 return ClkTagBar; 724 } 725 } 726 x = label(r, x, textwidth(m->ltsymbol) + pad, pad / 2, m->ltsymbol, 0, 0, 0); 727 if (px >= 0 && px < x) 728 return ClkLtSymbol; 729 end = m == selmon ? MAX(x, w - textwidth(status) - 2) : w; 730 c = m->sel; 731 label(r, x, end - x, pad / 2, c && c->win->title ? c->win->title : "", 732 m == selmon && c, c && c->isfloating, c && fixed(c)); 733 if (end < w) 734 label(r, end, w - end, 0, status, 0, 0, 0); 735 return px >= end ? ClkStatusText : ClkWinTitle; 736 } 737 738 void 739 bar_paint(struct Bar *bar, struct wld_renderer *r) 740 { 741 struct Monitor *m = wl_container_of(bar, m, bar); 742 baritems(m, r, -1, NULL); 743 } 744 745 static void 746 click(unsigned int region, unsigned int mask, unsigned int button, const Arg *tagarg) 747 { 748 size_t i; 749 for (i = 0; i < LENGTH(buttons); i++) { 750 const struct Button *b = &buttons[i]; 751 if (b->click == region && b->mask == mask && b->button == button && b->func) 752 b->func(region == ClkTagBar && !b->arg.ui ? tagarg : &b->arg); 753 } 754 } 755 756 static void 757 clickbar(struct Monitor *m, int x, unsigned int mask, unsigned int button) 758 { 759 Arg arg = {0}; 760 unsigned int region; 761 762 if (selmon != m) { 763 selmon = m; 764 focus(NULL); 765 } 766 region = baritems(m, NULL, x, &arg); 767 click(region, mask, button, &arg); 768 } 769 770 void 771 bar_click(struct Bar *bar, int x, uint32_t button) 772 { 773 struct Monitor *m = wl_container_of(bar, m, bar); 774 clickbar(m, x, 0, button); 775 } 776 777 static void 778 buttonbinding(void *data, uint32_t time, uint32_t button, uint32_t state) 779 { 780 const struct Button *binding = data; 781 struct Client *c; 782 struct Monitor *m; 783 struct swc_rectangle g; 784 int x, y, by; 785 786 pointerpos(&x, &y); 787 m = pointmon(x, y); 788 if (!state) { 789 if (drag.c && button == drag.button) { 790 c = drag.c; 791 dragmotion(NULL); 792 if (drag.moved && m != c->mon) { 793 sendmon(c, m); 794 focus(c); 795 } 796 drag.c = NULL; 797 } 798 return; 799 } 800 if (!m) 801 return; 802 g = m->bar.screen->geometry; 803 by = topbar ? g.y : g.y + (int)g.height - m->bar.height; 804 if (m->bar.shown && y >= by && y < by + m->bar.height) { 805 clickbar(m, x - g.x, binding->mask, button); 806 return; 807 } 808 c = findclient(swc_window_at(x, y)); 809 if (c) 810 focus(c); 811 if (!drag.c) 812 drag.button = button; 813 click(c ? ClkClientWin : ClkRootWin, binding->mask, button, NULL); 814 } 815 816 static void 817 keybinding(void *data, uint32_t time, uint32_t key, uint32_t state) 818 { 819 const struct Key *k = data; 820 if (state && selmon && k->func) 821 k->func(&k->arg); 822 } 823 824 static void 825 bindings(void) 826 { 827 size_t i, j; 828 for (i = 0; i < LENGTH(keys); i++) 829 if (swc_add_binding(SWC_BINDING_KEY, keys[i].mod, keys[i].keysym, keybinding, (void *)&keys[i]) < 0) 830 die("dmw: cannot bind keysym %#x", keys[i].keysym); 831 for (i = 0; i < LENGTH(buttons); i++) { 832 if (!buttons[i].mask && buttons[i].click < ClkClientWin) 833 continue; 834 for (j = 0; j < i; j++) 835 if (buttons[j].mask == buttons[i].mask && buttons[j].button == buttons[i].button && 836 (buttons[j].mask || buttons[j].click >= ClkClientWin)) 837 break; 838 if (j == i && swc_add_binding(SWC_BINDING_BUTTON, buttons[i].mask, buttons[i].button, 839 buttonbinding, (void *)&buttons[i]) < 0) 840 die("dmw: cannot bind button %u", buttons[i].button); 841 } 842 } 843 844 static void 845 applyrules(struct Client *c) 846 { 847 struct Monitor *m, *target = c->mon; 848 struct Client *parent = findclient(c->win->parent); 849 const struct Rule *r; 850 unsigned int mask = 0; 851 852 if (parent) { 853 target = parent->mon; 854 mask = parent->tags; 855 c->isfloating = 1; 856 } else { 857 for (r = rules; r < rules + LENGTH(rules); r++) { 858 if ((r->title && (!c->win->title || !strstr(c->win->title, r->title))) || 859 (r->app_id && (!c->win->app_id || !strstr(c->win->app_id, r->app_id)))) 860 continue; 861 c->isfloating = r->isfloating; 862 mask |= r->tags; 863 for (m = mons; m; m = m->next) 864 if (m->num == r->monitor) 865 target = m; 866 } 867 } 868 if (target != c->mon) { 869 m = c->mon; 870 detach(c); 871 attach(c, target); 872 arrange(m); 873 } 874 c->tags = mask & TAGMASK ? mask & TAGMASK : target->tagset[target->seltags]; 875 c->isfloating |= fixed(c); 876 } 877 878 static void 879 manage(void *data) 880 { 881 struct Client *c = data; 882 struct swc_rectangle g; 883 884 c->pending = NULL; 885 applyrules(c); 886 g = c->mon->bar.screen->usable_geometry; 887 swc_window_set_border(c->win, scheme[SchemeNorm][ColBorder], c->isfullscreen ? 0 : borderpx, 0, 0); 888 swc_window_set_position(c->win, g.x + borderpx, g.y + borderpx); 889 if (c->mon == selmon && ISVISIBLE(c)) 890 focus(c); 891 else if (ISVISIBLE(c)) 892 c->mon->sel = c; 893 arrange(c->mon); 894 } 895 896 static void 897 parentchanged(void *data) 898 { 899 struct Client *c = data; 900 901 if (!c->pending && c->win->parent) { 902 applyrules(c); 903 arrange(c->mon); 904 focus(NULL); 905 } 906 } 907 908 static void 909 metadatachanged(void *data) 910 { 911 struct Client *c = data; 912 913 if (!c->pending && !c->mapped) { 914 applyrules(c); 915 if (c->mon == selmon) 916 focus(ISVISIBLE(c) ? c : NULL); 917 arrange(c->mon); 918 } else 919 drawbar(c->mon); 920 } 921 922 static void 923 sizechanged(void *data) 924 { 925 struct Client *c = data; 926 struct swc_rectangle g; 927 928 if (!c->mapped && swc_window_get_geometry(c->win, &g) && g.width && g.height) { 929 c->mapped = 1; 930 if (fixed(c) && !c->isfloating) { 931 c->isfloating = 1; 932 arrange(c->mon); 933 } 934 } 935 } 936 937 static void 938 entered(void *data) 939 { 940 struct Client *c = data; 941 if (!drag.c && !c->pending && ISVISIBLE(c) && (c->mon != selmon || selmon->sel != c)) 942 focus(c); 943 } 944 945 static void 946 unmanage(void *data) 947 { 948 struct Client *c = data; 949 struct Monitor *m = c->mon; 950 951 if (drag.c == c) 952 drag.c = NULL; 953 if (c->pending) 954 wl_event_source_remove(c->pending); 955 detach(c); 956 free(c); 957 if (shutting_down) 958 return; 959 if (m == selmon) 960 focus(NULL); 961 arrange(m); 962 } 963 964 static void 965 newwindow(struct swc_window *win) 966 { 967 static const struct swc_window_handler handler = { 968 .destroy = unmanage, .entered = entered, .title_changed = metadatachanged, 969 .app_id_changed = metadatachanged, .parent_changed = parentchanged, 970 .size_changed = sizechanged, .fullscreen = setfullscreen, 971 }; 972 struct Client *c; 973 974 if (!selmon) 975 return; 976 c = ecalloc(1, sizeof *c); 977 c->win = win; 978 c->mode = -1; 979 attach(c, selmon); 980 swc_window_set_handler(win, &handler, c); 981 win->motion_throttle_ms = 1000 / MAX(1, refreshrate); 982 /* wait for the title and app id before matching rules for a new toplevel: */ 983 c->pending = wl_event_loop_add_idle(loop, manage, c); 984 if (!c->pending) 985 die("dmw: scheduling new window:"); 986 } 987 988 static void 989 screenentered(void *data) 990 { 991 if (selmon != data && !drag.c) { 992 selmon = data; 993 focus(NULL); 994 } 995 } 996 997 static void 998 screenchanged(void *data) 999 { 1000 struct Monitor *m = data; 1001 struct swc_rectangle area = m->bar.screen->usable_geometry, g; 1002 struct Client *c; 1003 int x, y; 1004 1005 for (c = m->clients; c; c = c->next) { 1006 if (c->mode != Stacked || !swc_window_get_geometry(c->win, &g)) 1007 continue; 1008 x = area.x + (int)borderpx; 1009 y = area.y + (int)borderpx; 1010 swc_window_set_position(c->win, 1011 MAX(x, MIN(g.x, area.x + (int)area.width - (int)g.width - (int)borderpx)), 1012 MAX(y, MIN(g.y, area.y + (int)area.height - (int)g.height - (int)borderpx))); 1013 } 1014 arrange(m); 1015 } 1016 1017 static void 1018 screenclosed(void *data) 1019 { 1020 struct Monitor *m = data, **p; 1021 struct Client *c; 1022 1023 for (p = &mons; *p != m; p = &(*p)->next); 1024 *p = m->next; 1025 if (selmon == m) 1026 selmon = mons; 1027 while ((c = m->clients) && mons) { 1028 detach(c); 1029 attach(c, mons); 1030 c->tags = mons->tagset[mons->seltags]; 1031 } 1032 if (c) 1033 die("dmw: last output removed"); 1034 bar_close(&m->bar); 1035 free(m); 1036 if (mons && !shutting_down) { 1037 arrange(mons); 1038 focus(NULL); 1039 } 1040 } 1041 1042 static void 1043 newscreen(struct swc_screen *screen) 1044 { 1045 static const struct swc_screen_handler handler = { 1046 .destroy = screenclosed, .entered = screenentered, 1047 .geometry_changed = screenchanged, .usable_geometry_changed = screenchanged, 1048 }; 1049 struct Monitor **p, *m = ecalloc(1, sizeof *m); 1050 1051 for (p = &mons; *p; p = &(*p)->next) 1052 m->num = (*p)->num + 1; 1053 *p = m; 1054 m->bar.screen = screen; 1055 m->mfact = mfact; 1056 m->nmaster = nmaster; 1057 m->showbar = showbar; 1058 m->tagset[0] = m->tagset[1] = 1; 1059 m->lt[0] = &layouts[0]; 1060 m->lt[1] = &layouts[1 % LENGTH(layouts)]; 1061 if (!selmon) 1062 selmon = m; 1063 swc_screen_set_handler(screen, &handler, m); 1064 arrange(m); 1065 } 1066 1067 static struct wl_event_source *status_source; 1068 1069 static int 1070 readstatus(int fd, uint32_t events, void *data) 1071 { 1072 static char line[sizeof status]; 1073 static size_t len; 1074 struct Monitor *m; 1075 char buf[4096]; 1076 ssize_t i, n; 1077 1078 n = read(fd, buf, sizeof buf); 1079 if (n < 0 && (errno == EINTR || errno == EAGAIN)) 1080 return 0; 1081 for (i = 0; i < n; i++) { 1082 if (buf[i] == '\n') { 1083 line[len] = '\0'; 1084 memcpy(status, line, len + 1); 1085 len = 0; 1086 } else if (len + 1 < sizeof line) 1087 line[len++] = buf[i]; 1088 } 1089 if (n <= 0) { 1090 if (len) { 1091 line[len] = '\0'; 1092 memcpy(status, line, len + 1); 1093 } 1094 if (status_source) { 1095 wl_event_source_remove(status_source); 1096 status_source = NULL; 1097 } 1098 } 1099 for (m = mons; m; m = m->next) 1100 drawbar(m); 1101 return n > 0; 1102 } 1103 1104 static int 1105 signalhandler(int sig, void *data) 1106 { 1107 if (sig == SIGCHLD) 1108 while (waitpid(-1, NULL, WNOHANG) > 0); 1109 else 1110 quit(NULL); 1111 return 0; 1112 } 1113 1114 int 1115 main(int argc, char **argv) 1116 { 1117 static const struct swc_manager manager = { .new_screen = newscreen, .new_window = newwindow }; 1118 const char *socket; 1119 struct stat st; 1120 struct wl_event_source *signals[3]; 1121 int i, j, sig[] = { SIGCHLD, SIGTERM, SIGINT }; 1122 1123 if (argc == 2 && !strcmp(argv[1], "-v")) { 1124 puts("dmw-" VERSION); 1125 return 0; 1126 } 1127 if (argc != 1) 1128 die("usage: dmw [-v]"); 1129 if (!(display = wl_display_create())) 1130 die("dmw: cannot create Wayland display"); 1131 loop = wl_display_get_event_loop(display); 1132 for (i = 0; i < 2; i++) 1133 for (j = 0; j < 3; j++) 1134 if (!wld_lookup_named_color(colors[i][j], &scheme[i][j])) 1135 die("dmw: unknown color %s", colors[i][j]); 1136 if (!swc_initialize(display, loop, &manager)) 1137 die("dmw: cannot initialize neuswc"); 1138 if (!(socket = wl_display_add_socket_auto(display)) || setenv("WAYLAND_DISPLAY", socket, 1) < 0) 1139 die("dmw: cannot publish Wayland socket:"); 1140 unsetenv("WAYLAND_SOCKET"); 1141 bar_init(display, font, topbar); 1142 bindings(); 1143 drag.timer = wl_event_loop_add_timer(loop, dragmotion, NULL); 1144 if (!drag.timer) 1145 die("dmw: cannot create drag timer:"); 1146 for (i = 0; i < 3; i++) 1147 if (!(signals[i] = wl_event_loop_add_signal(loop, sig[i], signalhandler, NULL))) 1148 die("dmw: cannot watch signal:"); 1149 if (!fstat(STDIN_FILENO, &st)) { 1150 if (S_ISFIFO(st.st_mode) || S_ISSOCK(st.st_mode)) { 1151 status_source = wl_event_loop_add_fd(loop, STDIN_FILENO, WL_EVENT_READABLE, readstatus, NULL); 1152 if (!status_source) 1153 die("dmw: cannot watch status input:"); 1154 } else if (S_ISREG(st.st_mode)) 1155 while (readstatus(STDIN_FILENO, 0, NULL)); 1156 } 1157 wl_display_run(display); 1158 shutting_down = 1; 1159 for (struct Monitor *m = mons; m; m = m->next) 1160 bar_close(&m->bar); 1161 bar_finish(); 1162 wl_display_destroy_clients(display); 1163 for (i = 0; i < 3; i++) 1164 wl_event_source_remove(signals[i]); 1165 if (status_source) 1166 wl_event_source_remove(status_source); 1167 wl_event_source_remove(drag.timer); 1168 swc_finalize(); 1169 wl_display_destroy(display); 1170 return 0; 1171 }