summaryrefslogtreecommitdiff
path: root/display.myr
blob: d82c13f10436957851952344c466c1acf23fb3ce (plain)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
use std
use iter
use fileutil
use sys

use "xproto-gen"
use "types"
use "errors"
use "conn"

pkg xmyrb =
	const dial	: (-> std.result(display#, err))
	const dialdpy	: (dpy : byte[:] -> std.result(display#, err))

	const screens	: (dpy : display# -> screen[:])
	const screen	: (dpy : display# -> screen#)
	const black	: (dpy : display# -> uint32)
	const white	: (dpy : display# -> uint32)
	const xid	: (dpy : display# -> xid)
	const setup	: (dpy : display# -> setup#)
;;

const xid = {dpy
	dpy.nextid++
	-> dpy.nextid
}

const setup = {dpy
	-> (dpy.setup : setup#)
}

const screen = {dpy
	var s = setup(dpy)
	-> &s.roots[dpy.screen]
}

const screens = {dpy
	var s = setup(dpy)
	-> s.roots
}

const black = {dpy
	-> screen(dpy).black_pixel
}

const white = {dpy
	-> screen(dpy).white_pixel
}

const dial = {
	match std.getenv("DISPLAY")
	| `std.Some d:	-> dialdpy(d)
	| `std.None:	-> `std.Err `Eparse
	;;
}

const dialdpy = {dpy
	var ret, buf : byte[128]

	match parsedpy(buf[:], dpy)
	| `std.Ok (ds, host, num, scr):
		match std.dial(ds)
		| `std.Ok fd:	ret = mkdisplay(fd, host, num, scr)
		| `std.Err e:	ret =`std.Err `Econn
		;;
		-> ret
	| `std.Err e:
		-> `std.Err e
	;;
}

const parsedpy = {buf, dpy
	var host, num, scr, ds

	match std.strfind(dpy, ":")
	| `std.None:
		-> `std.Err `Eparse
	| `std.Some i:
		host = dpy[:i]
		match std.strfind(dpy[i+1:], ".")
		| `std.None:
			num = std.getv(std.intparse(dpy[i+1:]), -1)
			scr = 0
		| `std.Some p:
			num = std.getv(std.intparse(dpy[i+1:p]), -1)
			scr = std.getv(std.intparse(dpy[p+1:]), -1)
		;;
	;;

	if scr == -1 || num == -1
		-> `std.Err `Eparse
	;;

	match host
	| "":	ds = std.bfmt(buf[:], "unix!/tmp/.X11-unix/X{}", num)
	| _:	ds = std.bfmt(buf[:], "tcp!{}!{}", host, num + 6000)
	;;

	-> `std.Ok (ds, host, num, scr)
}

const mkdisplay = {fd, host, dpynum, scrnum
	var islocal, dpy, auth, xauthpath

	match std.getenv("HOME")
	| `std.Some h: xauthpath = std.fmt("{}/.Xauthority", h)
	| `std.None: -> `std.Err `Eparse
	;;

	auth = parsemagic(xauthpath)
	std.slfree(xauthpath)
	if host.len == 0
		islocal = true
	;;
	dpy = std.mk([
		.num = dpynum,
		.screen = scrnum,

		.fd=fd,
		.resp=std.mkht(),
		.rbuf=std.slalloc(32*std.KiB),
		.roff=0,
		.rlen=0,
		.wbuf=std.slalloc(32*std.KiB),
		.woff=0,
		
		.auth = auth,
		.host = std.sldup(host),
	])

	match handshake(dpy)
	| `std.Ok s:	
		dpy.setup = (s : void#)
	| `std.Err e:	
		freedisplay(dpy)
		-> `std.Err e
	;;
	-> `std.Ok dpy
}

const freedisplay = {dpy
	std.free(dpy.setup)
	std.free(dpy)
}

const handshake = {dpy
	var req : byte[512]
	var auth, zauth, valid, sz
	var buf, setup
	var rightaddr
	
	zauth = [
		.family=0,
		.addr="",
		.num=0,
		.name="",
		.data=""
	]
	auth = &zauth
	rightaddr = getrightaddr(dpy)
	for a : iter.byref(dpy.auth)
		match std.intparse(a.num)
		| `std.None: /* skip */
		| `std.Some n:
			valid = false
			if a.family != AuthfamWild && !std.sleq(a.addr, rightaddr)
				continue
			;;
			match a.family
			| AuthfamWild:	valid = true
			| AuthfamLocal:	valid = std.sleq(dpy.host, "")
			/* localhost does hostname lookup, so it should be last */
			| AuthfamLhost:	valid = islocalip(dpy.host)
			| _:		valid = false
			;;
			if valid && n == dpy.num
				auth = a
				break
			;;
		;;
	;;
	std.slfree(rightaddr)

	/*
	 packed by hand because our donemsg function overwrites the assumed
	 location of the size bytes. This message has no size field.
	*/
	sz = 0
	req[sz] = ('l' : byte);				sz += 1 /* byte_order */
	req[sz] = 0; 					sz += 1 /* unused */
	std.putle16(req[sz:sz+2], 11);			sz += 2 /* major version */
	std.putle16(req[sz:sz+2], 0);			sz += 2 /* minor version */
	std.putle16(req[sz:sz+2], auth.name.len);	sz += 2 /* auth name len */
	std.putle16(req[sz:sz+2], auth.data.len);	sz += 2 /* auth data len */
	std.putle16(req[sz:sz+2], 0);			sz += 2 /* pad */
	std.slcp(req[sz:sz+auth.name.len], auth.name);	sz += auth.name.len
	if sz % 4 != 0
		std.slfill(req[sz:sz+4-(sz%4)], 0);	sz += 4-(sz%4)
	;;
	std.slcp(req[sz:sz+auth.data.len], auth.data);	sz += auth.data.len
	std.writeall(dpy.fd, req[:sz])

	setup = std.zalloc()

	/* 
	 * Ugly: X11's first response isn't a generic response.
	 * We need to read this shit manually. First, get the generic
	 * error/success message: 6 bytes.
	 */
	buf = std.slalloc(8)
	match std.readall(dpy.fd, buf[:6])
	| `std.Ok 6:	/* ok */
	| `std.Ok _:	-> `std.Err `Eproto
	| `std.Err _:	-> `std.Err `Econn
	;;
	/* If it's an error, return a failure error. */
	if buf[0] == 0
		std.close(dpy.fd)
		-> `std.Err `Eproto
	;;
	/* 
	 * If it's a success, we have a size. Read it, then read
	 * the tail of the message.
	 */
	match std.readall(dpy.fd, buf[6:8])
	| `std.Ok 2:	/* ok */
	| `std.Ok _:	std.put("there\n"); -> `std.Err `Eproto
	| `std.Err _:	-> `std.Err `Econn
	;;

	sz = 4*std.getle16(buf[6:8])
	std.slgrow(&buf, sz + 8)
	match std.readall(dpy.fd, buf[8:])
	| `std.Ok n:	
		if n != sz
			-> `std.Err `Eproto
		;;
	| `std.Err _:	-> `std.Err `Econn
	;;

	unpacksetup(buf, 0, setup)
	std.slfree(buf)
	dpy.nextid = (setup.resource_id_base : xid)
	-> `std.Ok setup
}

const getrightaddr = {dpy
	if dpy.host.len > 0
		-> std.sldup(dpy.host)
	;;

	var u : sys.utsname
	sys.uname(&u)
	match std.strfind(u.node[:], "\0")
	| `std.Some l: -> std.sldup(u.node[:l])
	| `std.None: -> std.sldup(u.node[:])
	;;
}

const parsemagic = {path -> auth[:]
	var data, buf, auth, ret

	buf = ""
	ret = [][:]
	match std.slurp(path)
	| `std.Err e:	-> [][:]
	| `std.Ok d:	buf = d
	;;

	data = buf
	while data.len > 2
		auth.family = getnum(&data)
		auth.addr = getstr(&data)
		auth.num = getstr(&data)
		auth.name = getstr(&data)
		auth.data = getstr(&data)
		std.slpush(&ret, auth)
	;;

	std.slfree(buf)
	-> ret
}

const getnum = {datap
	var n
	
	n = std.getbe16(datap#[0:2])
	datap# = datap#[2:]
	-> n
}

const getstr = {datap
	var len, str
		
	len = getnum(datap)
	str = std.sldup(datap#[:len])
	datap# = datap#[len:]
	-> str
}

const islocalip = {host
	const loopback = \
		"\x00\x00\x00\x00" \
		"\x00\x00\x00\x00" \
		"\x00\x00\x00\x00" \
		"\x00\x00\x00\x01" \

	match std.resolve(host)
	| `std.Err _:		-> false
	| `std.Ok inf:
		match inf[0].addr
		| `std.Ipv4 a:	-> a[0] == 127
		| `std.Ipv6 a:	-> std.sleq(a[:], loopback)
		;;
	;;
}