about summary refs log tree commit diff stats
path: root/dwm.h
blob: eba3a6adcd8b168f8ad54aed0988362267dd9f3a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
 * See LICENSE file for license details.
 */

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

/* mask shorthands, used in event.c and client.c */
#define BUTTONMASK		(ButtonPressMask | ButtonReleaseMask)
#define MOUSEMASK		(BUTTONMASK | PointerMotionMask)
#define PROTODELWIN		1

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

/* atoms */
enum { NetSupported, NetWMName, NetLast };
enum { WMProtocols, WMDelete, WMLast };

/* cursor */
enum { CurNormal, CurResize, CurMove, CurLast };

/* color */
enum { ColFG, ColBG, ColLast };

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

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

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

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

extern const char *tags[];
extern char stext[1024];
extern int bx, by, bw, bh, bmw, mw, screen, sx, sy, sw, sh;
extern unsigned int ntags, numlockmask;
extern void (*handler[LASTEvent])(XEvent *);
extern void (*arrange)(Arg *);
extern Atom wmatom[WMLast], netatom[NetLast];
extern Bool running, issel, maximized, *seltag;
extern Client *clients, *sel;
extern Cursor cursor[CurLast];
extern DC dc;
extern Display *dpy;
extern Window root, barwin;

/* client.c */
extern void ban(Client *c);
extern void focus(Client *c);
extern Client *getclient(Window w);
extern Client *getctitle(Window w);
extern void gravitate(Client *c, Bool invert);
extern void killclient(Arg *arg);
extern void manage(Window w, XWindowAttributes *wa);
extern void resize(Client *c, Bool sizehints, Corner sticky);
extern void setsize(Client *c);
extern void settitle(Client *c);
extern void togglemax(Arg *arg);
extern void unmanage(Client *c);

/* draw.c */
extern void drawall();
extern void drawstatus();
extern void drawtitle(Client *c);
extern unsigned long getcolor(const char *colstr);
extern void setfont(const char *fontstr);
extern unsigned int textw(const char *text);

/* event.c */
extern void grabkeys();
extern void procevent();

/* main.c */
extern int getproto(Window w);
extern void quit(Arg *arg);
extern void sendevent(Window w, Atom a, long value);
extern int xerror(Display *dsply, XErrorEvent *ee);

/* tag.c */
extern void initrregs();
extern Client *getnext(Client *c);
extern Client *getprev(Client *c);
extern void settags(Client *c, Client *trans);
extern void tag(Arg *arg);
extern void toggletag(Arg *arg);

/* util.c */
extern void *emallocz(unsigned int size);
extern void eprint(const char *errstr, ...);
extern void *erealloc(void *ptr, unsigned int size);
extern void spawn(Arg *arg);

/* view.c */
extern void detach(Client *c);
extern void dofloat(Arg *arg);
extern void dotile(Arg *arg);
extern void focusnext(Arg *arg);
extern void focusprev(Arg *arg);
extern Bool isvisible(Client *c);
extern void resizecol(Arg *arg);
extern void restack();
extern void togglemode(Arg *arg);
extern void toggleview(Arg *arg);
extern void view(Arg *arg);
extern void viewall(Arg *arg);
extern void zoom(Arg *arg);
`xmm0`. ### Comparing values Work with variables of any 32-bit type. `addr` variables can only be compared to 0. ``` compare var1, var2/reg compare var1/reg, var2 compare var/eax, n compare var, n ``` Floating-point numbers cannot be compared to literals, and the register must come first. ``` compare var1/xreg1, var2/xreg2 compare var1/xreg1, var2 ``` ### Branches Immediately after a `compare` instruction you can branch on its result. For example: ``` break-if-= ``` This instruction will jump to after the enclosing `{}` block if the previous `compare` detected equality. Here's the list of conditional and unconditional `break` instructions: ``` break break-if-= break-if-!= break-if-< break-if-> break-if-<= break-if->= ``` Similarly, you can jump back to the start of the enclosing `{}` block with `loop`. Here's the list of `loop` instructions. ``` loop loop-if-= loop-if-!= loop-if-< loop-if-> loop-if-<= loop-if->= ``` Additionally, there are special variants for comparing `addr` and `float` values, which results in the following comprehensive list of jumps: ``` break break-if-= break-if-!= break-if-< break-if-addr< break-if-float< break-if-> break-if-addr> break-if-float> break-if-<= break-if-addr<= break-if-float<= break-if->= break-if-addr>= break-if-float>= loop loop-if-= loop-if-!= loop-if-< loop-if-addr< loop-if-float< loop-if-> loop-if-addr> loop-if-float> loop-if-<= loop-if-addr<= loop-if-float<= loop-if->= loop-if-addr>= loop-if-float>= ``` One final property all these jump instructions share: they can take an optional block name to jump to. For example: ``` a: { ... break a #----------| ... # | } # <--| a: { # <--| ... # | b: { # | ... # | loop a #----------| ... } ... } ``` However, there's no way to jump to a block that doesn't contain the `loop` or `break` instruction. ### Integer arithmetic These instructions require variables of non-`addr`, non-`float` types. Add: ``` var1/reg1 <- add var2/reg2 var1/reg <- add var2 add-to var1, var2/reg # var1 += var2 var/reg <- add n add-to var, n ``` Subtract: ``` var1/reg1 <- subtract var2/reg2 var1/reg <- subtract var2 subtract-from var1, var2/reg # var1 -= var2 var/reg <- subtract n subtract-from var, n ``` Add one: ``` var/reg <- increment increment var ``` Subtract one: ``` var/reg <- decrement decrement var ``` Multiply: ``` var1/reg <- multiply var2 ``` The result of a multiply must be a register. Negate: ``` var/reg1 <- negate negate var ``` ### Fractional arithmetic Operations on `float` variables include a few we've seen before and some new ones. Notice here that we mostly use floating-point registers `xmm_`, but still use the general-purpose registers when dereferencing variables of type `(addr float)`. ``` var1/xreg <- add var2/xreg2 var1/xreg <- add var2 var1/xreg <- add *var2/reg2 var1/xreg <- subtract var2/xreg2 var1/xreg <- subtract var2 var1/xreg <- subtract *var2/reg2 var1/xreg <- multiply var2/xreg2 var1/xreg <- multiply var2 var1/xreg <- multiply *var2/reg2 var1/xreg <- divide var2/xreg2 var1/xreg <- divide var2 var1/xreg <- divide *var2/reg2 var1/xreg <- reciprocal var2/xreg2 var1/xreg <- reciprocal var2 var1/xreg <- reciprocal *var2/reg2 var1/xreg <- square-root var2/xreg2 var1/xreg <- square-root var2 var1/xreg <- square-root *var2/reg2 var1/xreg <- inverse-square-root var2/xreg2 var1/xreg <- inverse-square-root var2 var1/xreg <- inverse-square-root *var2/reg2 var1/xreg <- min var2/xreg2 var1/xreg <- min var2 var1/xreg <- min *var2/reg2 var1/xreg <- max var2/xreg2 var1/xreg <- max var2 var1/xreg <- max *var2/reg2 ``` Two instructions in the above list are approximate. According to the Intel manual, `reciprocal` and `inverse-square-root` [go off the rails around the fourth decimal place](linux/x86_approx.md). If you need more precision, use `divide` separately. ### Bitwise boolean operations These require variables of non-`addr`, non-`float` types. And: ``` var1/reg1 <- and var2/reg2 var1/reg <- and var2 and-with var1, var2/reg var/reg <- and n and-with var, n ``` Or: ``` var1/reg1 <- or var2/reg2 var1/reg <- or var2 or-with var1, var2/reg var/reg <- or n or-with var, n ``` Not: ``` var1/reg1 <- not not var ``` Xor: ``` var1/reg1 <- xor var2/reg2 var1/reg <- xor var2 xor-with var1, var2/reg var/reg <- xor n xor-with var, n ``` ### Bitwise shifts Shifts require variables of non-`addr`, non-`float` types. ``` var/reg <- shift-left n var/reg <- shift-right n var/reg <- shift-right-signed n shift-left var, n shift-right var, n shift-right-signed var, n ``` Shifting bits left always inserts zeros on the right. Shifting bits right inserts zeros on the left by default. A _signed_ shift right duplicates the leftmost bit, thereby preserving the sign of an integer. ## Operations on more complex types These instructions work with any type `T`. As before we use `/reg` here to indicate when a variable must live in a register. We also include type constraints after a `:`. ### Addresses and handles You can compute the address of any variable in memory (never in registers): ``` var1/reg: (addr T) <- address var2: T ``` As mentioned up top, `addr` variables can never escape the function where they're computed. You can't store them on the heap, or in compound types. Think of them as short-lived things. To manage long-lived addresses, _allocate_ them on the heap. ``` allocate var: (addr handle T) # var can be in either register or memory ``` Handles can be copied and stored without restriction. However, they're too large to fit in a register. You also can't access their payload directly, you have to first convert them into a short-lived `addr` using _lookup_. ``` var1/eax: (addr T) <- lookup var2: (handle T) # var2 in either register or memory ``` Since handles are large compound types, there's a special helper for comparing them: ``` var/eax: boolean <- handle-equal? var1: (handle T), var2: (handle T) ``` ### Arrays Arrays are declared in two ways: 1. On the stack with a literal size: ``` var x: (array int 3) ``` 2. On the heap with a potentially variable size. For example: ``` var x: (handle array int) var x-ah/eax: (addr handle array int) <- address x populate x-ah, 8 ``` The `8` here can also be an int in a register or memory. (The `-ah` is a common variable naming convention and stands for "address of a handle". Essential for allocating long-lived data on the heap.) You can compute the length of an array, though you'll need an `addr` to do so: ``` var/reg: int <- length arr/reg: (addr array T) ``` To read from or write to an array, use `index` to first obtain an address to read from or modify: ``` var/reg: (addr T) <- index arr/reg: (addr array T), n var/reg: (addr T) <- index arr: (array T len), n ``` Like our notation of `n`, `len` here is required to be a literal. The index requested can also be a variable in a register, with one caveat: ``` var/reg: (addr T) <- index arr/reg: (addr array T), idx/reg: int var/reg: (addr T) <- index arr: (array T len), idx/reg: int ``` The caveat: the size of T must be 1, 2, 4 or 8 bytes. For other sizes of T you'll need to split up the work, performing a `compute-offset` before the `index`. ``` var/reg: (offset T) <- compute-offset arr: (addr array T), idx/reg: int # arr can be in reg or mem var/reg: (offset T) <- compute-offset arr: (addr array T), idx: int # arr can be in reg or mem ``` The result of a `compute-offset` statement can be passed to `index`: ``` var/reg: (addr T) <- index arr/reg: (addr array T), idx/reg: (offset T) ``` ### Streams A common use for arrays is as buffers. Save a few items to a scratch space and then process them. This pattern is so common (we use it in files) that there's special support for it with a built-in type: `stream`. Streams are like arrays in many ways. You can initialize them with a length on the stack: ``` var x: (stream int 3) ``` You can also populate them on the heap: ``` var x: (handle stream int) var x-ah/eax: (addr handle stream int) <- address x populate-stream x-ah, 8 ``` However, streams don't provide random access with an `index` instruction. Instead, you write to them sequentially, and read back what you wrote. ``` read-from-stream s: (addr stream T), out: (addr T) write-to-stream s: (addr stream T), in: (addr T) ``` Streams of bytes are particularly common for managing Unicode text, and there are a few functions to help with them: ``` write s: (addr stream byte), u: (addr array byte) # write u to s, abort if full overflow?/eax: boolean <- try-write s: (addr stream byte), u: (addr array byte) write-stream dest: (addr stream byte), src: (addr stream byte) # bytes append-byte s: (addr stream byte), var: int # write lower byte of var var/eax: byte <- read-byte s: (addr stream byte) # 32-bit graphemes encoded in UTF-8 write-grapheme out: (addr stream byte), g: grapheme g/eax: grapheme <- read-grapheme in: (addr stream byte) ``` You can check if a stream is empty or full: ``` var/eax: boolean <- stream-empty? s: (addr stream) var/eax: boolean <- stream-full? s: (addr stream) ``` You can clear streams: ``` clear-stream f: (addr stream T) ``` You can also rewind them to reread their contents: ``` rewind-stream f: (addr stream T) ``` ## Compound types Primitive types can be combined together using the `type` keyword. For example: ``` type point { x: int y: int } ``` Mu programs are sequences of just `fn` and `type` definitions. Compound types can't include `addr` types for safety reasons (use `handle` instead, which is described below). They also can't currently include `array`, `stream` or `byte` types. Since arrays and streams carry their size with them, supporting them in compound types complicates variable initialization. Instead of defining them inline in a type definition, define a `handle` to them. Bytes shouldn't be used for anything but UTF-8 strings. To access within a compound type, use the `get` instruction. There are two forms. You need either a variable of the type itself (say `T`) in memory, or a variable of type `(addr T)` in a register. ``` var/reg: (addr T_f) <- get var/reg: (addr T), f var/reg: (addr T_f) <- get var: T, f ``` The `f` here is the field name from the `type` definition, and its type `T_f` must match the type of `f` in the `type` definition. For example, some legal instructions for the definition of `point` above: ``` var a/eax: (addr int) <- get p, x var a/eax: (addr int) <- get p, y ``` You can clear compound types using the `clear-object` function: ``` clear-object var: (addr T) ``` You can shallow-copy compound types using the `copy-object` function: ``` copy-object src: (addr T), dest: (addr T) ```