-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdl-srv.c
More file actions
351 lines (307 loc) · 6.82 KB
/
Copy pathsdl-srv.c
File metadata and controls
351 lines (307 loc) · 6.82 KB
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
/* devdraw-sdl - SDL 2.0-based implementation of Plan 9 from User Space devdraw
* Copyright (c) 2018 Oskar Sveinsen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <cursor.h>
#include <memdraw.h>
#include <mouse.h>
#include <drawfcall.h>
#include <SDL.h>
#include "devdraw.h"
#define HANDLER(type) ((type >> 1) - 1)
#define DEFAULT_WINW 640
#define DEFAULT_WINH 480
#define DEFAULT_WINFLAGS SDL_WINDOW_RESIZABLE
#define BUFSZ_EVTQ 32
#define BUFSZ_RDDRAW 0x10000
typedef union Evt Evt;
typedef struct Evtq Evtq;
union Evt {
Rune kbd;
};
struct Evtq {
uint ri, wi;
int dir; /* content type: >0 tags, 0 empty, <0 evts */
union {
uchar tag;
Evt evt;
} buf[BUFSZ_EVTQ];
};
static uint matchtag(Evtq, uchar, Evt *);
static uint matchevt(Evtq, Evt, uchar *);
static void reply(Wsysmsg *msg);
static void replyerrstr(Wsysmsg *msg);
static void replyerrsdl(Wsysmsg *msg);
static void handle_rdmouse(Wsysmsg *);
static void handle_moveto(Wsysmsg *);
static void handle_cursor(Wsysmsg *);
static void handle_bouncemouse(Wsysmsg *);
static void handle_rdkbd(Wsysmsg *);
static void handle_label(Wsysmsg *);
static void handle_init(Wsysmsg *);
static void handle_rdsnarf(Wsysmsg *);
static void handle_wrsnarf(Wsysmsg *);
static void handle_rddraw(Wsysmsg *);
static void handle_wrdraw(Wsysmsg *);
static void handle_top(Wsysmsg *);
static void handle_resize(Wsysmsg *);
void (*handlers[])(Wsysmsg *) = {
[HANDLER(Trdmouse)] = &handle_rdmouse,
[HANDLER(Tmoveto)] = &handle_moveto,
[HANDLER(Tcursor)] = &handle_cursor,
[HANDLER(Tbouncemouse)] = &handle_bouncemouse,
[HANDLER(Trdkbd)] = &handle_rdkbd,
[HANDLER(Tlabel)] = &handle_label,
[HANDLER(Tinit)] = &handle_init,
[HANDLER(Trdsnarf)] = &handle_rdsnarf,
[HANDLER(Twrsnarf)] = &handle_wrsnarf,
[HANDLER(Trddraw)] = &handle_rddraw,
[HANDLER(Twrdraw)] = &handle_wrdraw,
[HANDLER(Ttop)] = &handle_top,
[HANDLER(Tresize)] = &handle_resize
};
SDL_Window *win;
Evtq kbdq;
uint
matchtag(Evtq q, uchar tag, Evt *evt)
{
if (q.dir < 0) {
*evt = q.buf[q.ri].evt;
if (++q.ri >= nelem(q.buf))
q.ri = 0;
if (q.ri == q.wi)
q.dir = 0;
return 1;
} else {
if (q.wi == q.ri) {
Wsysmsg err;
err.tag = q.buf[q.wi].tag;
err.type = Rerror;
err.error = "tag queue overflow";
reply(&err);
if (++q.ri >= nelem(q.buf))
q.ri = 0;
}
q.buf[q.wi].tag = tag;
if (++q.wi >= nelem(q.buf))
q.wi = 0;
q.dir = 1;
return 0;
}
}
uint
matchevt(Evtq q, Evt evt, uchar *tag)
{
if (q.dir > 0) {
*tag = q.buf[q.ri].tag;
if (++q.ri >= nelem(q.buf))
q.ri = 0;
if (q.ri == q.wi)
q.dir = 0;
return 1;
} else {
if (q.wi == q.ri && ++q.ri >= nelem(q.buf))
q.ri = 0;
q.buf[q.wi].evt = evt;
if (++q.wi >= nelem(q.buf))
q.wi = 0;
q.dir = -1;
return 0;
}
}
void
reply(Wsysmsg *msg)
{
static uchar *buf;
static int nbuf;
uint n;
msg->type |= 1;
if ((n = sizeW2M(msg)) > nbuf) {
free(buf);
if (!(buf = malloc(nbuf = n)))
sysfatal("malloc: %r");
}
convW2M(msg, buf, n);
if (write(1, buf, n) != n)
sysfatal("write: %r");
}
void
replyerrstr(Wsysmsg *msg)
{
char err[ERRMAX];
rerrstr(err, sizeof(err));
msg->type = Rerror;
msg->error = err;
reply(msg);
}
void
replyerrsdl(Wsysmsg *msg)
{
msg->type = Rerror;
msg->error = SDL_GetError();
reply(msg);
SDL_ClearError();
}
void
handle_moveto(Wsysmsg *msg)
{
SDL_WarpMouseInWindow(win, msg->mouse.xy.x, msg->mouse.xy.y);
reply(msg);
}
void
handle_cursor(Wsysmsg *msg)
{
SDL_FreeCursor(SDL_GetCursor()); /* cursor will be reset to default as a side effect */
if (!msg->arrowcursor) {
Cursor *cur;
SDL_Cursor *sdlcur;
cur = &msg->cursor;
sdlcur = SDL_CreateCursor(cur->set, cur->clr, 16, 16, cur->offset.x, cur->offset.y);
if (!sdlcur) {
replyerrsdl(msg);
return;
}
SDL_SetCursor(sdlcur);
}
reply(msg);
}
void
handle_rdkbd(Wsysmsg *msg)
{
Evt evt;
if (matchtag(kbdq, msg->tag, &evt)) {
msg->rune = evt.kbd;
reply(msg);
}
}
void
handle_label(Wsysmsg *msg)
{
SDL_SetWindowTitle(win, msg->label);
reply(msg);
}
void
handle_init(Wsysmsg *msg)
{
int x, y, w, h;
x = y = SDL_WINDOWPOS_UNDEFINED;
w = DEFAULT_WINW;
h = DEFAULT_WINH;
if (*msg->winsize) {
Rectangle r;
int havemin;
if (parsewinsize(msg->winsize, &r, &havemin) < 0) {
replyerrstr(msg);
return;
}
if (havemin) {
x = r.min.x;
y = r.min.y;
}
w = Dx(r);
h = Dy(r);
}
if (win = SDL_CreateWindow(msg->label, x, y, w, h, DEFAULT_WINFLAGS))
reply(msg);
else
replyerrsdl(msg);
}
void
handle_rdsnarf(Wsysmsg *msg)
{
if (msg->snarf = SDL_GetClipboardText()) {
reply(msg);
SDL_free(msg->snarf);
} else {
replyerrsdl(msg);
}
}
void
handle_wrsnarf(Wsysmsg *msg)
{
if (SDL_SetClipboardText(msg->snarf))
replyerrsdl(msg);
else
reply(msg);
}
void
handle_rddraw(Wsysmsg *msg)
{
uchar buf[BUFSZ_RDDRAW];
msg->data = buf;
if (msg->count > sizeof(buf))
msg->count = sizeof(buf);
if ((msg->count = _drawmsgread(buf, msg->count)) >= 0)
reply(msg);
else
replyerrstr(msg);
}
void
handle_wrdraw(Wsysmsg *msg)
{
if (_drawmsgwrite(msg->data, msg->count) >= 0)
reply(msg);
else
replyerrstr(msg);
}
void
handle_top(Wsysmsg *msg)
{
SDL_RaiseWindow(win);
reply(msg);
}
void
handle_resize(Wsysmsg *msg)
{
SDL_SetWindowSize(win, Dx(msg->rect), Dy(msg->rect));
reply(msg);
}
void
main(int argc, char *argv[])
{
uchar *buf;
int nbuf;
Wsysmsg msg;
ARGBEGIN {
defualt:
} ARGEND
if (SDL_Init(SDL_INIT_VIDEO) < 0)
sysfatal("failed to initialize SDL: %s", SDL_GetError());
buf = malloc(nbuf = 4);
while (read(1, buf, 4) == 4) {
int n;
GET(buf, n);
if (n > nbuf) {
free(buf);
if (!(buf = malloc(4 + n)))
sysfatal("malloc: %r");
PUT(buf, n);
nbuf = n;
}
if (readn(3, buf + 4, n - 4) != 4)
sysfatal("message too short");
if (!convM2W(buf, n, &msg))
sysfatal("invalid message");
handlers[HANDLER(msg.type)](&msg);
}
}