aboutsummaryrefslogtreecommitdiff
path: root/src/libdraw/drawfcall.c
blob: 941153844eaed56b801f447e1b4847cdfd38f96c (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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <mouse.h>
#include <cursor.h>
#include <drawfcall.h>

static int
_stringsize(char *s)
{
	if(s == nil)
		s = "";
	return 4+strlen(s);
}

static int
PUTSTRING(uchar *p, char *s)
{
	int n;

	if(s == nil)
		s = "";
	n = strlen(s);
	PUT(p, n);
	memmove(p+4, s, n);
	return n+4;
}

static int
GETSTRING(uchar *p, char **s)
{
	int n;

	GET(p, n);
	memmove(p, p+4, n);
	*s = (char*)p;
	p[n] = 0;
	return n+4;
}

uint
sizeW2M(Wsysmsg *m)
{
	switch(m->type){
	default:
		return 0;
	case Trdmouse:
	case Rbouncemouse:
	case Rmoveto:
	case Rcursor:
	case Rcursor2:
	case Trdkbd:
	case Trdkbd4:
	case Rlabel:
	case Rctxt:
	case Rinit:
	case Trdsnarf:
	case Rwrsnarf:
	case Ttop:
	case Rtop:
	case Rresize:
		return 4+1+1;
	case Rrdmouse:
		return 4+1+1+4+4+4+4+1;
	case Tbouncemouse:
		return 4+1+1+4+4+4;
	case Tmoveto:
		return 4+1+1+4+4;
	case Tcursor:
		return 4+1+1+4+4+2*16+2*16+1;
	case Tcursor2:
		return 4+1+1+4+4+2*16+2*16+4+4+4*32+4*32+1;
	case Rerror:
		return 4+1+1+_stringsize(m->error);
	case Rrdkbd:
		return 4+1+1+2;
	case Rrdkbd4:
		return 4+1+1+4;
	case Tlabel:
		return 4+1+1+_stringsize(m->label);
	case Tctxt:
		return 4+1+1
			+_stringsize(m->id);
	case Tinit:
		return 4+1+1
			+_stringsize(m->winsize)
			+_stringsize(m->label);
	case Rrdsnarf:
	case Twrsnarf:
		return 4+1+1+_stringsize(m->snarf);
	case Rrddraw:
	case Twrdraw:
		return 4+1+1+4+m->count;
	case Trddraw:
	case Rwrdraw:
		return 4+1+1+4;
	case Tresize:
		return 4+1+1+4*4;
	}
}

uint
convW2M(Wsysmsg *m, uchar *p, uint n)
{
	int nn;

	nn = sizeW2M(m);
	if(n < nn || nn == 0 || n < 6)
		return 0;
	PUT(p, nn);
	p[4] = m->tag;
	p[5] = m->type;

	switch(m->type){
	default:
		return 0;
	case Trdmouse:
	case Rbouncemouse:
	case Rmoveto:
	case Rcursor:
	case Rcursor2:
	case Trdkbd:
	case Trdkbd4:
	case Rlabel:
	case Rctxt:
	case Rinit:
	case Trdsnarf:
	case Rwrsnarf:
	case Ttop:
	case Rtop:
	case Rresize:
		break;
	case Rerror:
		PUTSTRING(p+6, m->error);
		break;
	case Rrdmouse:
		PUT(p+6, m->mouse.xy.x);
		PUT(p+10, m->mouse.xy.y);
		PUT(p+14, m->mouse.buttons);
		PUT(p+18, m->mouse.msec);
		p[19] = m->resized;
		break;
	case Tbouncemouse:
		PUT(p+6, m->mouse.xy.x);
		PUT(p+10, m->mouse.xy.y);
		PUT(p+14, m->mouse.buttons);
		break;
	case Tmoveto:
		PUT(p+6, m->mouse.xy.x);
		PUT(p+10, m->mouse.xy.y);
		break;
	case Tcursor:
		PUT(p+6, m->cursor.offset.x);
		PUT(p+10, m->cursor.offset.y);
		memmove(p+14, m->cursor.clr, sizeof m->cursor.clr);
		memmove(p+46, m->cursor.set, sizeof m->cursor.set);
		p[78] = m->arrowcursor;
		break;
	case Tcursor2:
		PUT(p+6, m->cursor.offset.x);
		PUT(p+10, m->cursor.offset.y);
		memmove(p+14, m->cursor.clr, sizeof m->cursor.clr);
		memmove(p+46, m->cursor.set, sizeof m->cursor.set);
		PUT(p+78, m->cursor2.offset.x);
		PUT(p+82, m->cursor2.offset.y);
		memmove(p+86, m->cursor2.clr, sizeof m->cursor2.clr);
		memmove(p+214, m->cursor2.set, sizeof m->cursor2.set);
		p[342] = m->arrowcursor;
		break;
	case Rrdkbd:
		PUT2(p+6, m->rune);
		break;
	case Rrdkbd4:
		PUT(p+6, m->rune);
		break;
	case Tlabel:
		PUTSTRING(p+6, m->label);
		break;
	case Tctxt:
		PUTSTRING(p+6, m->id);
		break;
	case Tinit:
		p += 6;
		p += PUTSTRING(p, m->winsize);
		p += PUTSTRING(p, m->label);
		break;
	case Rrdsnarf:
	case Twrsnarf:
		PUTSTRING(p+6, m->snarf);
		break;
	case Rrddraw:
	case Twrdraw:
		PUT(p+6, m->count);
		memmove(p+10, m->data, m->count);
		break;
	case Trddraw:
	case Rwrdraw:
		PUT(p+6, m->count);
		break;
	case Tresize:
		PUT(p+6, m->rect.min.x);
		PUT(p+10, m->rect.min.y);
		PUT(p+14, m->rect.max.x);
		PUT(p+18, m->rect.max.y);
		break;
	}
	return nn;
}

uint
convM2W(uchar *p, uint n, Wsysmsg *m)
{
	int nn;

	if(n < 6)
		return 0;
	GET(p, nn);
	if(nn > n)
		return 0;
	m->tag = p[4];
	m->type = p[5];
	switch(m->type){
	default:
		return 0;
	case Trdmouse:
	case Rbouncemouse:
	case Rmoveto:
	case Rcursor:
	case Rcursor2:
	case Trdkbd:
	case Trdkbd4:
	case Rlabel:
	case Rctxt:
	case Rinit:
	case Trdsnarf:
	case Rwrsnarf:
	case Ttop:
	case Rtop:
	case Rresize:
		break;
	case Rerror:
		GETSTRING(p+6, &m->error);
		break;
	case Rrdmouse:
		GET(p+6, m->mouse.xy.x);
		GET(p+10, m->mouse.xy.y);
		GET(p+14, m->mouse.buttons);
		GET(p+18, m->mouse.msec);
		m->resized = p[19];
		break;
	case Tbouncemouse:
		GET(p+6, m->mouse.xy.x);
		GET(p+10, m->mouse.xy.y);
		GET(p+14, m->mouse.buttons);
		break;
	case Tmoveto:
		GET(p+6, m->mouse.xy.x);
		GET(p+10, m->mouse.xy.y);
		break;
	case Tcursor:
		GET(p+6, m->cursor.offset.x);
		GET(p+10, m->cursor.offset.y);
		memmove(m->cursor.clr, p+14, sizeof m->cursor.clr);
		memmove(m->cursor.set, p+46, sizeof m->cursor.set);
		m->arrowcursor = p[78];
		break;
	case Tcursor2:
		GET(p+6, m->cursor.offset.x);
		GET(p+10, m->cursor.offset.y);
		memmove(m->cursor.clr, p+14, sizeof m->cursor.clr);
		memmove(m->cursor.set, p+46, sizeof m->cursor.set);
		GET(p+78, m->cursor2.offset.x);
		GET(p+82, m->cursor2.offset.y);
		memmove(m->cursor2.clr, p+86, sizeof m->cursor2.clr);
		memmove(m->cursor2.set, p+214, sizeof m->cursor2.set);
		m->arrowcursor = p[342];
		break;
	case Rrdkbd:
		GET2(p+6, m->rune);
		break;
	case Rrdkbd4:
		GET(p+6, m->rune);
		break;
	case Tlabel:
		GETSTRING(p+6, &m->label);
		break;
	case Tctxt:
		GETSTRING(p+6, &m->id);
		break;
	case Tinit:
		p += 6;
		p += GETSTRING(p, &m->winsize);
		p += GETSTRING(p, &m->label);
		break;
	case Rrdsnarf:
	case Twrsnarf:
		GETSTRING(p+6, &m->snarf);
		break;
	case Rrddraw:
	case Twrdraw:
		GET(p+6, m->count);
		m->data = p+10;
		break;
	case Trddraw:
	case Rwrdraw:
		GET(p+6, m->count);
		break;
	case Tresize:
		GET(p+6, m->rect.min.x);
		GET(p+10, m->rect.min.y);
		GET(p+14, m->rect.max.x);
		GET(p+18, m->rect.max.y);
		break;
	}
	return nn;
}

int
readwsysmsg(int fd, uchar *buf, uint nbuf)
{
	int n;

	if(nbuf < 6)
		return -1;
	if(readn(fd, buf, 4) != 4)
		return -1;
	GET(buf, n);
	if(n > nbuf)
		return -1;
	if(readn(fd, buf+4, n-4) != n-4)
		return -1;
	return n;
}

int
drawfcallfmt(Fmt *fmt)
{
	Wsysmsg *m;

	m = va_arg(fmt->args, Wsysmsg*);
	fmtprint(fmt, "tag=%d ", m->tag);
	switch(m->type){
	default:
		return fmtprint(fmt, "unknown msg %d", m->type);
	case Rerror:
		return fmtprint(fmt, "Rerror error='%s'", m->error);
	case Trdmouse:
		return fmtprint(fmt, "Trdmouse");
	case Rrdmouse:
		return fmtprint(fmt, "Rrdmouse x=%d y=%d buttons=%d msec=%d resized=%d",
			m->mouse.xy.x, m->mouse.xy.y,
			m->mouse.buttons, m->mouse.msec, m->resized);
	case Tbouncemouse:
		return fmtprint(fmt, "Tbouncemouse x=%d y=%d buttons=%d",
			m->mouse.xy.x, m->mouse.xy.y, m->mouse.buttons);
	case Rbouncemouse:
		return fmtprint(fmt, "Rbouncemouse");
	case Tmoveto:
		return fmtprint(fmt, "Tmoveto x=%d y=%d", m->mouse.xy.x, m->mouse.xy.y);
	case Rmoveto:
		return fmtprint(fmt, "Rmoveto");
	case Tcursor:
		return fmtprint(fmt, "Tcursor arrow=%d", m->arrowcursor);
	case Tcursor2:
		return fmtprint(fmt, "Tcursor2 arrow=%d", m->arrowcursor);
	case Rcursor:
		return fmtprint(fmt, "Rcursor");
	case Rcursor2:
		return fmtprint(fmt, "Rcursor2");
	case Trdkbd:
		return fmtprint(fmt, "Trdkbd");
	case Rrdkbd:
		return fmtprint(fmt, "Rrdkbd rune=%C", m->rune);
	case Trdkbd4:
		return fmtprint(fmt, "Trdkbd4");
	case Rrdkbd4:
		return fmtprint(fmt, "Rrdkbd4 rune=%C", m->rune);
	case Tlabel:
		return fmtprint(fmt, "Tlabel label='%s'", m->label);
	case Rlabel:
		return fmtprint(fmt, "Rlabel");
	case Tctxt:
		return fmtprint(fmt, "Tctxt id='%s'", m->id);
	case Rctxt:
		return fmtprint(fmt, "Rctxt");
	case Tinit:
		return fmtprint(fmt, "Tinit label='%s' winsize='%s'", m->label, m->winsize);
	case Rinit:
		return fmtprint(fmt, "Rinit");
	case Trdsnarf:
		return fmtprint(fmt, "Trdsnarf");
	case Rrdsnarf:
		return fmtprint(fmt, "Rrdsnarf snarf='%s'", m->snarf);
	case Twrsnarf:
		return fmtprint(fmt, "Twrsnarf snarf='%s'", m->snarf);
	case Rwrsnarf:
		return fmtprint(fmt, "Rwrsnarf");
	case Trddraw:
		return fmtprint(fmt, "Trddraw %d", m->count);
	case Rrddraw:
		return fmtprint(fmt, "Rrddraw %d %.*H", m->count, m->count, m->data);
	case Twrdraw:
		return fmtprint(fmt, "Twrdraw %d %.*H", m->count, m->count, m->data);
	case Rwrdraw:
		return fmtprint(fmt, "Rwrdraw %d", m->count);
	case Ttop:
		return fmtprint(fmt, "Ttop");
	case Rtop:
		return fmtprint(fmt, "Rtop");
	case Tresize:
		return fmtprint(fmt, "Tresize %R", m->rect);
	case Rresize:
		return fmtprint(fmt, "Rresize");
	}
}