about summary refs log blame commit diff stats
path: root/dwm.h
blob: 18672d0fb2e74fc5eadacf24beca544d5c9cbb09 (plain) (tree)
1
2
3
4
5
6
7
8
9
                                                              
                                        


                                                                             


                                                                              
  
                                                                            
                                                                        



                                                                              
  




                                                                              

                                                                             
  


                                                                             

                                                                        
  
                                                              

   
                   
                     
 
                                                   
                                                                     

                                          
                                 
 








                                                                           
               
                        
              
                          
 
                


                    

                           
      
 
                
                       


                                      


                          
                        
 
                             
               
                       
                  
                       
                                                 
                                                       
                                                             
                 
                    
                            
                                     
                   
                     
                     
                      
                   
                    

  


                                                                                        
                                                                     
                                                                         
                                                                                                       
                                                                   
                                                                                       
                                             
                                                                             
                                                                                  
                              
                                                                         
                    
                           
 
              
                                                                       
                                                                                    






                                                                                   
                                                                               

                                                                                          
                                                               
 
            

                                                                                                


                                                                                    
                                                                                   
 
             

                                                                                       
 
            



                                                                                                       
 
           
                                                                                                     

                                                                                     
                                                                    

                                                                                     
 
            
                                                                                                       
                                                                                    
                                                                                                
                                                                                              

            
                                                                                        

                                                                                   


                                                                                                     
                                                                                                       
                                                                                      


                                                                                                      
                                                                                    
                                                                                                             
/* (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
 * See LICENSE file for 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 an array of Bools of the same size as the
 * global tags array to indicate the tags of a client.  For each client dwm
 * creates a small title window, which is resized whenever the (_NET_)WM_NAME
 * properties are updated or the client is moved/resized.
 *
 * Keys and tagging rules are organized as arrays and defined in the config.h
 * file. These arrays are kept static in event.o and tag.o respectively,
 * because no other part of dwm needs access to them.  The current mode is
 * represented by the arrange() function pointer, which wether points to
 * dofloat() or dotile(). 
 *
 * To understand everything else, start reading main.c:main().
 */

#include "config.h"
#include <X11/Xlib.h>

/* mask shorthands, used in event.c and client.c */
#define BUTTONMASK		(ButtonPressMask | ButtonReleaseMask)
/* other stuff used in different places */
#define BORDERPX		1
#define PROTODELWIN		1

enum { NetSupported, NetWMName, NetLast };		/* EWMH atoms */
enum { WMProtocols, WMDelete, WMLast };			/* default atoms */
enum { CurNormal, CurResize, CurMove, CurLast };	/* cursor */
enum { ColFG, ColBG, ColLast };				/* color */

typedef enum {
	TopLeft, TopRight, BotLeft, BotRight
} Corner; /* window corners */

typedef union {
	const char *cmd;
	int i;
} Arg; /* argument type */

typedef struct {
	int ascent;
	int descent;
	int height;
	XFontSet set;
	XFontStruct *xfont;
} Fnt;

typedef struct {
	int x, y, w, h;
	unsigned long norm[ColLast];
	unsigned long sel[ColLast];
	unsigned long status[ColLast];
	Drawable drawable;
	Fnt font;
	GC gc;
} DC; /* draw context */

typedef struct Client Client;
struct Client {
	char name[256];
	int proto;
	int x, y, w, h;
	int rx, ry, rw, rh; /* revert geometry */
	int tx, ty, tw, th; /* title window geometry */
	int basew, baseh, incw, inch, maxw, maxh, minw, minh;
	int grav;
	long flags; 
	unsigned int border;
	Bool isfloat, isfixed, ismax;
	Bool *tags;
	Client *next;
	Client *prev;
	Client *snext;
	Window win;
	Window twin;
};

extern const char *tags[];			/* all tags */
extern char stext[1024];			/* status text */
extern int bx, by, bw, bh, bmw;			/* bar geometry, bar mode label width */
extern int screen, sx, sy, sw, sh;		/* screen geometry */
extern int wax, way, wah, waw;			/* windowarea geometry */
extern unsigned int master, ntags, numlockmask;	/* master percent, number of tags, dynamic lock mask */
extern void (*handler[LASTEvent])(XEvent *);	/* event handler */
extern void (*arrange)(void);			/* arrange function, indicates mode  */
extern Atom wmatom[WMLast], netatom[NetLast];
extern Bool running, issel, *seltag;		/* seltag is array of Bool */
extern Client *clients, *sel, *stack;		/* global client list and stack */
extern Cursor cursor[CurLast];
extern DC dc;					/* global draw context */
extern Display *dpy;
extern Window root, barwin;

/* client.c */
extern void ban(Client *c);			/* ban c from screen */
extern void configure(Client *c);		/* send synthetic configure event */
extern void focus(Client *c);			/* focus c, c may be NULL */
extern Client *getclient(Window w);		/* return client of w */
extern Client *getctitle(Window w);		/* return client of title window */
extern void gravitate(Client *c, Bool invert);	/* gravitate c */
extern void killclient(Arg *arg);		/* kill c nicely */
extern void manage(Window w, XWindowAttributes *wa);	/* manage new client */
extern void resize(Client *c, Bool sizehints, Corner sticky); /* resize c*/
extern void resizetitle(Client *c);		/* resizes c->twin correctly */
extern void updatesize(Client *c);			/* update the size structs of c */
extern void updatetitle(Client *c);		/* update the name of c */
extern void unmanage(Client *c);		/* destroy c */

/* draw.c */
extern void drawall(void);			/* draw all visible client titles and the bar */
extern void drawstatus(void);			/* draw the bar */
extern void drawtitle(Client *c);		/* draw title of c */
extern unsigned long getcolor(const char *colstr);	/* return color of colstr */
extern void setfont(const char *fontstr);	/* set the font for DC */
extern unsigned int textw(const char *text);	/* return the width of text in px*/

/* event.c */
extern void grabkeys(void);			/* grab all keys defined in config.h */
extern void procevent(void);			/* process pending X events */

/* main.c */
extern int getproto(Window w);			/* return protocol mask of WMProtocols property of w */
extern void quit(Arg *arg);			/* quit dwm nicely */
extern void sendevent(Window w, Atom a, long value);	/* send synthetic event to w */
extern int xerror(Display *dsply, XErrorEvent *ee);	/* dwm's X error handler */

/* tag.c */
extern void initrregs(void);			/* initialize regexps of rules defined in config.h */
extern Client *getnext(Client *c);		/* returns next visible client */
extern Client *getprev(Client *c);		/* returns previous visible client */
extern void settags(Client *c, Client *trans);	/* sets tags of c */
extern void tag(Arg *arg);			/* tags c with arg's index */
extern void toggletag(Arg *arg);		/* toggles c tags with arg's index */

/* util.c */
extern void *emallocz(unsigned int size);	/* allocates zero-initialized memory, exits on error */
extern void eprint(const char *errstr, ...);	/* prints errstr and exits with 1 */
extern void *erealloc(void *ptr, unsigned int size);	/* reallocates memory, exits on error */
extern void spawn(Arg *arg);			/* forks a new subprocess with to arg's cmd */

/* view.c */
extern void detach(Client *c);			/* detaches c from global client list */
extern void dofloat(void);			/* arranges all windows floating */
extern void dotile(void);			/* arranges all windows tiled */
extern void focusnext(Arg *arg);		/* focuses next visible client, arg is ignored  */
extern void focusprev(Arg *arg);		/* focuses previous visible client, arg is ignored */
extern Bool isvisible(Client *c);		/* returns True if client is visible */
extern void resizemaster(Arg *arg);		/* resizes the master percent with arg's index value */
extern void restack(void);			/* restores z layers of all clients */
extern void togglemode(Arg *arg);		/* toggles global arrange function (dotile/dofloat) */
extern void toggleview(Arg *arg);		/* toggles the tag with arg's index (in)visible */
extern void view(Arg *arg);			/* views the tag with arg's index */
extern void viewall(Arg *arg);			/* views all tags, arg is ignored */
extern void zoom(Arg *arg);			/* zooms the focused client to master area, arg is ignored */
an> of "nametemplate": return NAMED_FIELD_NAMETEMPLATE of "edit": return NAMED_FIELD_EDIT return NO_NAMED_FIELD proc consumeField(state: var MailcapParser; entry: var MailcapEntry): Result[bool, string] = state.skipBlanks() if not state.has(): return ok(false) var buf = "" while state.has(): let c = state.consume() case c of ';', '\n': if parseFieldKey(entry, buf) != NO_NAMED_FIELD: return err("Expected command") return ok(c == ';') of '\r': continue of '=': let f = parseFieldKey(entry, buf) let cmd = ?state.consumeCommand() case f of NO_NAMED_FIELD: discard of NAMED_FIELD_TEST: entry.test = cmd of NAMED_FIELD_NAMETEMPLATE: entry.nametemplate = cmd of NAMED_FIELD_EDIT: entry.edit = cmd return ok(state.consume() == ';') else: if c in Controls: return err("line " & $state.line & ": invalid character in field: " & c) buf &= c proc parseMailcap*(stream: Stream): Result[Mailcap, string] = var state = MailcapParser(stream: stream, line: 1) var mailcap: Mailcap while not stream.atEnd(): let c = state.consume() if c == '#': state.skipLine() continue state.reconsume(c) state.skipBlanks() let c2 = state.consume() if c2 == '\n' or c2 == '\r': continue state.reconsume(c2) let t = ?state.consumeTypeField() let mt = t.until('/') #TODO this could be more efficient let subt = t[mt.len + 1 .. ^1] var entry = MailcapEntry( mt: mt, subt: subt, cmd: ?state.consumeCommand() ) if state.consume() == ';': while ?state.consumeField(entry): discard mailcap.add(entry) return ok(mailcap) # Mostly based on w3m's mailcap quote/unquote type UnquoteState = enum usNormal, usQuoted, usPerc, usAttr, usAttrQuoted, usDollar type UnquoteResult* = object canpipe*: bool cmd*: string type QuoteState* = enum qsNormal, qsDoubleQuoted, qsSingleQuoted proc quoteFile*(file: string; qs: QuoteState): string = var s = "" for c in file: case c of '$', '`', '"', '\\': if qs != qsSingleQuoted: s &= '\\' of '\'': if qs == qsSingleQuoted: s &= "'\\'" # then re-open the quote by appending c elif qs == qsNormal: s &= '\\' # double-quoted: append normally of AsciiAlphaNumeric, '_', '.', ':', '/': discard # no need to quote elif qs == qsNormal: s &= '\\' s &= c return s proc unquoteCommand*(ecmd, contentType, outpath: string; url: URL; canpipe: var bool; line = -1): string = var cmd = "" var attrname = "" var state: UnquoteState var qss = @[qsNormal] # quote state stack. len >1 template qs: var QuoteState = qss[^1] for c in ecmd: case state of usQuoted: cmd &= c state = usNormal of usAttrQuoted: attrname &= c.toLowerAscii() state = usAttr of usNormal, usDollar: let prev_dollar = state == usDollar state = usNormal case c of '%': state = usPerc of '\\': state = usQuoted of '\'': if qs == qsSingleQuoted: qs = qsNormal else: qs = qsSingleQuoted cmd &= c of '"': if qs == qsDoubleQuoted: qs = qsNormal else: qs = qsDoubleQuoted cmd &= c of '$': if qs != qsSingleQuoted: state = usDollar cmd &= c of '(': if prev_dollar: qss.add(qsNormal) cmd &= c of ')': if qs != qsSingleQuoted: if qss.len > 1: qss.setLen(qss.len - 1) else: # mismatched parens; probably an invalid shell command... qss[0] = qsNormal cmd &= c else: cmd &= c of usPerc: case c of '%': cmd &= c of 's': cmd &= quoteFile(outpath, qs) canpipe = false of 't': cmd &= quoteFile(contentType.until(';'), qs) of 'u': # Netscape extension if url != nil: # nil in getEditorCommand cmd &= quoteFile($url, qs) of 'd': # line; not used in mailcap, only in getEditorCommand if line != -1: # -1 in mailcap cmd &= $line of '{': state = usAttr continue else: discard state = usNormal of usAttr: if c == '}': let s = contentType.getContentTypeAttr(attrname) cmd &= quoteFile(s, qs) attrname = "" state = usNormal elif c == '\\': state = usAttrQuoted else: attrname &= c return cmd proc unquoteCommand*(ecmd, contentType, outpath: string; url: URL): string = var canpipe: bool return unquoteCommand(ecmd, contentType, outpath, url, canpipe) proc getMailcapEntry*(mailcap: Mailcap; contentType, outpath: string; url: URL): ptr MailcapEntry = let mt = contentType.until('/') if mt.len + 1 >= contentType.len: return nil let st = contentType.until(AsciiWhitespace + {';'}, mt.len + 1) for entry in mailcap: if not (entry.mt.len == 1 and entry.mt[0] == '*') and entry.mt != mt: continue if not (entry.subt.len == 1 and entry.subt[0] == '*') and entry.subt != st: continue if entry.test != "": var canpipe = true let cmd = unquoteCommand(entry.test, contentType, outpath, url, canpipe) if not canpipe: continue if execCmd(cmd) != 0: continue return unsafeAddr entry