/* See LICENSE file for copyright and license details. * * dynamic window manager is designed like any other X client as well. It is * driven through handling X events. In contrast to other X clients, a window * manager selects for SubstructureRedirectMask on the root window, to receive * events about window (dis-)appearance. Only one X connection at a time is * allowed to select for this event mask. * * Calls to fetch an X event from the event queue are blocking. Due reading * status text from standard input, a select()-driven main loop has been * implemented which selects for reads on the X connection and STDIN_FILENO to * handle all data smoothly. The event handlers of dwm are organized in an * array which is accessed whenever a new event has been fetched. This allows * event dispatching in O(1) time. * * Each child of the root window is called a client, except windows which have * set the override_redirect flag. Clients are organized in a global * doubly-linked client list, the focus history is remembered through a global * stack list. Each client contains a bit array to indicate the tags of a * client. * * Keys and tagging rules are organized as arrays and defined in config.h. * * To understand everything else, start reading main(). */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef XINERAMA #include #endif /* macros */ #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask) #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask)) #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH)) #define ISVISIBLE(x) (x->tags & tagset[seltags]) #define LENGTH(x) (sizeof x / sizeof x[0]) #define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MAXTAGLEN 16 #define MOUSEMASK (BUTTONMASK|PointerMotionMask) #define TAGMASK ((int)((1LL << LENGTH(tags)) - 1)) #define TEXTW(x) (textnw(x, strlen(x)) + dc.font.height) /* enums */ enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */ enum { ColBorder, ColFG, ColBG, ColLast }; /* color */ enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */ enum { WMProtocols, WMDelete, WMName, WMState, WMLast };/* default atoms */ enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle, ClkClientWin, ClkRootWin, ClkLast }; /* clicks */ typedef union { int i; unsigned int ui; float f; void *v; } Arg; typedef struct { unsigned int click; unsigned int mask; unsigned int button; void (*func)(const Arg *arg); const Arg arg; } Button; typedef struct Client Client; struct Client { char name[256]; float mina, maxa; int x, y, w, h; int basew, baseh, incw, inch, maxw, maxh, minw, minh; int bw, oldbw; unsigned int tags; Bool isfixed, isfloating, isurgent; Client *next; Client *snext; Window win; }; typedef struct { int x, y, w, h; unsigned long norm[ColLast]; unsigned long sel[ColLast]; Drawable drawable; GC gc; struct { int ascent; int descent; int height; XFontSet set; XFontStruct *xfont; } font; } DC; /* draw context */ typedef struct { unsigned int mod; KeySym keysym; void (*func)(const Arg *); const Arg arg; } Key; typedef struct { const char *symbol; void (*arrange)(void); } Layout; typedef struct { const char *class; const char *instance; const char *title; unsigned int tags; Bool isfloating; } Rule; /* function declarations */ static void applyrules(Client *c); static void arrange(void); static void attach(Client *c); static void attachstack(Client *c); static void buttonpress(XEvent *e); static void checkotherwm(void); static void cleanup(void); static void clearurgent(void); static void configure(Client *c); static void configurenotify(XEvent *e); static void configurerequest(XEvent *e); static void destroynotify(XEvent *e); static void detach(Client *c); static void detachstack(Client *c); static void die(const char *errstr, ...); static void drawbar(void); static void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]); static void drawtext(const char *text, unsigned long col[ColLast], Bool invert); static void enternotify(XEvent *e); static void expose(XEvent *e); static void focus(Client *c); static void focusin(XEvent *e); static void focusstack(const Arg *arg); static Client *getclient(Window w); static unsigned long getcolor(const char *colstr); static long getstate(Window w); static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size); static void grabbuttons(Client *c, Bool focused); static void grabkeys(void); static void initfont(const char *fontstr); static Bool isoccupied(unsigned int t); static Bool isprotodel(Client *c); static Bool isurgent(unsigned int t); static void keypress(XEvent *e); static void killclient(const Arg *arg); static void manage(Window w, XWindowAttributes *wa); static void mappingnotify(XEvent *e); static void maprequest(XEvent *e); static void monocle(void); static void movemouse(const Arg *arg); static Client *nexttiled(Client *c); static void propertynotify(XEvent *e); static void quit(const Arg *arg); static void resize(Client *c, int x, int y, int w, int h, Bool sizehints); static void resizemouse(const Arg *arg); static void restack(void); static void run(void); static void scan(void); static void setclientstate(Client *c, long state); static void setlayout(const Arg *arg); static void setmfact(const Arg *arg); static void setup(void); static void spawn(const Arg *arg); static void tag(const Arg *arg); static int textnw(const char *text, unsigned int len); static void tile(void); static void togglebar(const Arg *arg); static void togglefloating(const Arg *arg); static void toggletag(const Arg *arg); static void toggleview(const Arg *arg); static void unmanage(Client *c); static void unmapnotify(XEvent *e); static void updatebar(void); static void updategeom(void); static void updatesizehints(Client *c); static void updatetitle(Client *c); static void updatewmhints(Client *c); static void view(const Arg *arg); static int xerror(Display *dpy, XErrorEvent *ee); static int xerrordummy(Display *dpy, XErrorEvent *ee); static int xerrorstart(Display *dpy, XErrorEvent *ee); static void zoom(const Arg *arg); /* variables */ static char stext[256]; static int screen, sx, sy, sw, sh; static int by, bh, blw, wx, wy, ww, wh; static unsigned int seltags = 0, sellt = 0; static int (*xerrorxlib)(Display *, XErrorEvent *); static unsigned int numlockmask = 0; static void (*handler[LASTEvent]) (XEvent *) = { [ButtonPress] = buttonpress, [ConfigureRequest] = configurerequest, [ConfigureNotify] = configurenotify, [DestroyNotify] = destroynotify, [EnterNotify] = enternotify, [Expose] = expose, [FocusIn] = focusin, [KeyPress] = keypress, [MappingNotify] = mappingnotify, [MapRequest] = maprequest, [PropertyNotify] = propertynotify, [UnmapNotify] = unmapnotify }; static Atom wmatom[WMLast], netatom[NetLast]; static Bool otherwm, readin; static Bool running = True; static unsigned int tagset[] = {1, 1}; /* after start, first tag is selected */ static Client *clients = NULL; static Client *sel = NULL; static Client *stack = NULL; static Cursor cursor[CurLast]; static Display *dpy; static DC dc = {0}; static Layout *lt[] = { NULL, NULL }; static Window root, barwin; /* configuration, allows nested code to access above variables */ #include "config.h" /* compile-time check if all tags fit into an unsigned int bit array. */ struct NumTags { char limitexceeded[sizeof(unsigned int) * 8 < LENGTH(tags) ? -1 : 1]; }; /* function implementations */ void applyrules(Client *c) { unsigned int i; Rule *r; XClassHint ch = { 0 }; /* rule matching */ XGetClassHint(dpy, c->win, &ch); for(i = 0; i < LENGTH(rules); i++) { r = &rules[i]; if((!r->title || strstr(c->name, r->title)) && (!r->class || (ch.res_class && strstr(ch.res_class, r->class))) && (!r->instance || (ch.res_name && strstr(ch.res_name, r->instance)))) { c->isfloating = r->isfloating; c->tags |= r->tags & TAGMASK; } } if(ch.res_class) XFree(ch.res_class); if(ch.res_name) XFree(ch.res_name); if(!c->tags) c->tags = tagset[seltags]; } void arrange(void) { Client *c; for(c = clients; c; c = c->next) if(ISVISIBLE(c)) { XMoveWindow(dpy, c->win, c->x, c->y); if(!lt[sellt]->arrange || c->isfloating) resize(c, c->x, c->y, c->w, c->h, True); } else { XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y); } focus(NULL); if(lt[sellt]->arrange) lt[sellt]->arrange(); restack(); } void attach(Client *c) { c->next = clients; clients = c; } void attachstack(Client *c) { c->snext = stack; stack = c; } void buttonpress(XEvent *e) { unsigned int i, x, click; Arg arg = {0}; Client *c; XButtonPressedEvent *ev = &e->xbutton; click = ClkRootWin; if(ev->window == barwin) { i = x = 0; do x += TEXTW(tags[i]); while(ev->x >= x && ++i < LENGTH(tags)); if(i < LENGTH(tags)) { click = ClkTagBar; arg.ui = 1 << i; } else if(ev->x < x + blw) click = ClkLtSymbol; else if(ev->x > wx + ww - TEXTW(stext)) click = ClkStatusText; else click = ClkWinTitle; } else if((c = getclient(ev->window))) { focus(c); click = ClkClientWin; } for(i = 0; i < LENGTH(buttons); i++) if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state)) buttons[i].func(click == ClkTagBar ? &arg : &buttons[i].arg); } void checkotherwm(void) { otherwm = False; XSetErrorHandler(xerrorstart); /* this causes an error if some other window manager is running */ XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask); XSync(dpy, False); if(otherwm) die("dwm: another window manager is already running\n"); XSetErrorHandler(NULL
# Wrappers around file system primitives that take a 'resources' object and
# are thus easier to test.
#
# - start-reading - asynchronously open a file, returning a channel source for
#   receiving the results
# - start-writing - asynchronously open a file, returning a channel sink for
#   the data to write
# - slurp - synchronously read from a file
# - dump - synchronously write to a file

container resources [
  lock:bool
  data:&:@:resource
]

container resource [
  name:text
  contents:text
]

def start-reading resources:&:resources, filename:text -> contents:&:source:char, error?:bool [
  local-scope
  load-inputs
  error? <- copy false
  {
    break-unless resources
    # fake file system
    contents, error? <- start-reading-from-fake-resource resources, filename
    return
  }
  # real file system
  file:num <- $open-file-for-reading filename
  return-unless file, null/no-contents, true/error
  contents:&:source:char, sink:&:sink:char <- new-channel 30
  start-running receive-from-file file, sink
]

def slurp resources:&:resources, filename:text -> contents:text, error?:bool [
  local-scope
  load-inputs
  source:&:source:char, error?:bool <- start-reading resources, filename
  return-if error?, null/no-contents
  buf:&:buffer:char <- new-buffer 30/capacity
  {
    c:char, done?:bool, source <- read source
    break-if done?
    buf <- append buf, c
    loop
  }
  contents <- buffer-to-array buf
]

def start-reading-from-fake-resource resources:&:resources, resource:text -> contents:&:source:char, error?:bool [
  local-scope
  load-inputs
  error? <- copy false
  i:num <- copy 0
  data:&:@:resource <- get *resources, data:offset
  len:num <- length *data
  {
    done?:bool <- greater-or-equal i, len
    break-if done?
    tmp:resource <- index *data, i
    i <- add i, 1
    curr-resource:text <- get tmp, name:offset
    found?:bool <- equal resource, curr-resource
    loop-unless found?
    contents:&:source:char, sink:&:sink:char <- new-channel 30
    curr-contents:text <- get tmp, contents:offset
    start-running receive-from-text curr-contents, sink
    return
  }
  return null/no-such-resource, true/error-found
]

def receive-from-file file:num, sink:&:sink:char -> sink:&:sink:char [
  local-scope
  load-inputs
  {
    c:char, eof?:bool <- $read-from-file file
    break-if eof?
    sink <- write sink, c
    loop
  }
  sink <- close sink
  file <- $close-file file
]

def receive-from-text contents:text, sink:&:sink:char -> sink:&:sink:char [
  local-scope
  load-inputs
  i:num <- copy 0
  len:num <- length *contents
  {
    done?:bool <- greater-or-equal i, len
    break-if done?
    c:char <- index *contents, i
    sink <- write sink, c
    i <- add i, 1
    loop
  }
  sink <- close sink
]

def start-writing resources:&:resources, filename:text -> sink:&:sink:char, routine-id:num, error?:bool [
  local-scope
  load-inputs
  error?