168 lines
7.2 KiB

19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
  1. /*
  2. * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3. * See LICENSE file for license details.
  4. *
  5. * dynamic window manager is designed like any other X client as well. It is
  6. * driven through handling X events. In contrast to other X clients, a window
  7. * manager selects for SubstructureRedirectMask on the root window, to receive
  8. * events about window (dis-)appearance. Only one X connection at a time is
  9. * allowed to select for this event mask.
  10. *
  11. * Calls to fetch an X event from the event queue are blocking. Due reading
  12. * status text from standard input, a select()-driven main loop has been
  13. * implemented which selects for reads on the X connection and STDIN_FILENO to
  14. * handle all data smoothly. The event handlers of dwm are organized in an
  15. * array which is accessed whenever a new event has been fetched. This allows
  16. * event dispatching in O(1) time.
  17. *
  18. * Each child of the root window is called a client, except windows which have
  19. * set the override_redirect flag. Clients are organized in a global
  20. * doubly-linked client list, the focus history is remembered through a global
  21. * stack list. Each client contains an array of Bools of the same size as the
  22. * global tags array to indicate the tags of a client. For each client dwm
  23. * creates a small title window, which is resized whenever the (_NET_)WM_NAME
  24. * properties are updated or the client is moved/resized.
  25. *
  26. * Keys and tagging rules are organized as arrays and defined in the config.h
  27. * file. These arrays are kept static in event.o and tag.o respectively,
  28. * because no other part of dwm needs access to them. The current mode is
  29. * represented by the arrange() function pointer, which wether points to
  30. * dofloat() or dotile().
  31. *
  32. * To understand everything else, start reading main.c:main().
  33. */
  34. #include "config.h"
  35. #include <X11/Xlib.h>
  36. /* mask shorthands, used in event.c and client.c */
  37. #define BUTTONMASK (ButtonPressMask | ButtonReleaseMask)
  38. #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
  39. #define PROTODELWIN 1
  40. enum { NetSupported, NetWMName, NetLast }; /* EWMH atoms */
  41. enum { WMProtocols, WMDelete, WMLast }; /* default atoms */
  42. enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
  43. enum { ColFG, ColBG, ColLast }; /* color */
  44. typedef enum {
  45. TopLeft, TopRight, BotLeft, BotRight
  46. } Corner; /* window corners */
  47. typedef union {
  48. const char *cmd;
  49. int i;
  50. } Arg; /* argument type */
  51. typedef struct {
  52. int ascent;
  53. int descent;
  54. int height;
  55. XFontSet set;
  56. XFontStruct *xfont;
  57. } Fnt;
  58. typedef struct {
  59. int x, y, w, h;
  60. unsigned long norm[ColLast];
  61. unsigned long sel[ColLast];
  62. unsigned long status[ColLast];
  63. Drawable drawable;
  64. Fnt font;
  65. GC gc;
  66. } DC; /* draw context */
  67. typedef struct Client Client;
  68. struct Client {
  69. char name[256];
  70. int proto;
  71. int x, y, w, h;
  72. int rx, ry, rw, rh; /* revert geometry */
  73. int tx, ty, tw, th; /* title window geometry */
  74. int basew, baseh, incw, inch, maxw, maxh, minw, minh;
  75. int grav;
  76. long flags;
  77. unsigned int border, weight;
  78. Bool isfloat, ismax;
  79. Bool *tags;
  80. Client *next;
  81. Client *prev;
  82. Client *snext;
  83. Window win;
  84. Window twin;
  85. };
  86. extern const char *tags[]; /* all tags */
  87. extern char stext[1024]; /* status text */
  88. extern int bx, by, bw, bh, bmw; /* bar geometry, bar mode label width */
  89. extern int mw, screen, sx, sy, sw, sh; /* screen geometry, master width */
  90. extern unsigned int ntags, numlockmask; /* number of tags, dynamic lock mask */
  91. extern void (*handler[LASTEvent])(XEvent *); /* event handler */
  92. extern void (*arrange)(Arg *); /* arrange function, indicates mode */
  93. extern Atom wmatom[WMLast], netatom[NetLast];
  94. extern Bool running, issel, *seltag; /* seltag is array of Bool */
  95. extern Client *clients, *sel, *stack; /* global cleint list and stack */
  96. extern Cursor cursor[CurLast];
  97. extern DC dc; /* global draw context */
  98. extern Display *dpy;
  99. extern Window root, barwin;
  100. /* client.c */
  101. extern void ban(Client *c); /* ban c from screen */
  102. extern void focus(Client *c); /* focus c, c may be NULL */
  103. extern Client *getclient(Window w); /* return client of w */
  104. extern Client *getctitle(Window w); /* return client of title window */
  105. extern void gravitate(Client *c, Bool invert); /* gravitate c */
  106. extern void killclient(Arg *arg); /* kill c nicely */
  107. extern void manage(Window w, XWindowAttributes *wa); /* manage new client */
  108. extern void resize(Client *c, Bool sizehints, Corner sticky); /* resize c*/
  109. extern void updatesize(Client *c); /* update the size structs of c */
  110. extern void updatetitle(Client *c); /* update the name of c */
  111. extern void unmanage(Client *c); /* destroy c */
  112. /* draw.c */
  113. extern void drawall(); /* draw all visible client titles and the bar */
  114. extern void drawstatus(); /* draw the bar */
  115. extern void drawtitle(Client *c); /* draw title of c */
  116. extern unsigned long getcolor(const char *colstr); /* return color of colstr */
  117. extern void setfont(const char *fontstr); /* set the font for DC */
  118. extern unsigned int textw(const char *text); /* return the width of text in px*/
  119. /* event.c */
  120. extern void grabkeys(); /* grab all keys defined in config.h */
  121. extern void procevent(); /* process pending X events */
  122. /* main.c */
  123. extern int getproto(Window w); /* return protocol mask of WMProtocols property of w */
  124. extern void quit(Arg *arg); /* quit dwm nicely */
  125. extern void sendevent(Window w, Atom a, long value); /* send synthetic event to w */
  126. extern int xerror(Display *dsply, XErrorEvent *ee); /* dwm's X error handler */
  127. /* tag.c */
  128. extern void initrregs(); /* initialize regexps of rules defined in config.h */
  129. extern Client *getnext(Client *c); /* returns next visible client */
  130. extern Client *getprev(Client *c); /* returns previous visible client */
  131. extern void settags(Client *c, Client *trans); /* sets tags of c */
  132. extern void tag(Arg *arg); /* tags c with arg's index */
  133. extern void toggletag(Arg *arg); /* toggles c tags with arg's index */
  134. /* util.c */
  135. extern void *emallocz(unsigned int size); /* allocates zero-initialized memory, exits on error */
  136. extern void eprint(const char *errstr, ...); /* prints errstr and exits with 1 */
  137. extern void *erealloc(void *ptr, unsigned int size); /* reallocates memory, exits on error */
  138. extern void spawn(Arg *arg); /* forks a new subprocess with to arg's cmd */
  139. /* view.c */
  140. extern void detach(Client *c); /* detaches c from global client list */
  141. extern void dofloat(Arg *arg); /* arranges all windows floating, arg is ignored */
  142. extern void dotile(Arg *arg); /* arranges all windows, arg is ignored */
  143. extern void focusnext(Arg *arg); /* focuses next visible client, arg is ignored */
  144. extern void focusprev(Arg *arg); /* focuses previous visible client, arg is ignored */
  145. extern Bool isvisible(Client *c); /* returns True if client is visible */
  146. extern void resizecol(Arg *arg); /* resizes the master width with arg's index value */
  147. extern void restack(); /* restores z layers of all clients */
  148. extern void togglemode(Arg *arg); /* toggles global arrange function (dotile/dofloat) */
  149. extern void toggleview(Arg *arg); /* toggles the tag with arg's index (in)visible */
  150. extern void view(Arg *arg); /* views the tag with arg's index */
  151. extern void viewall(Arg *arg); /* views all tags, arg is ignored */
  152. extern void zoom(Arg *arg); /* zooms the focused client to master column, arg is ignored */