-
Notifications
You must be signed in to change notification settings - Fork 33
/
remotewb.c
511 lines (415 loc) · 10.4 KB
/
remotewb.c
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/*
* Copyright (c) 2018 Niklas Ekström
*/
#include <exec/types.h>
#include <exec/ports.h>
#include <exec/tasks.h>
#include <exec/nodes.h>
#include <exec/lists.h>
#include <exec/execbase.h>
#include <exec/memory.h>
#include <devices/input.h>
#include <devices/inputevent.h>
#include <graphics/gfx.h>
#include <graphics/gfxbase.h>
#include <graphics/view.h>
#include <intuition/intuition.h>
#include <intuition/intuitionbase.h>
#include <intuition/screens.h>
#include <libraries/dos.h>
#include <hardware/intbits.h>
#include <proto/alib.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/intuition.h>
#include <proto/graphics.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "../a314device/a314.h"
#include "../a314device/proto_a314.h"
struct IntuitionBase *IntuitionBase = NULL;
struct GfxBase *GfxBase = NULL;
struct Library *A314Base = NULL;
struct MsgPort *mp;
ULONG socket;
struct A314_IORequest *cmsg;
struct A314_IORequest *rmsg;
UBYTE arbuf[256];
struct A314_IORequest *wmsg;
UBYTE awbuf[256];
BOOL pending_a314_read = FALSE;
BOOL pending_a314_write = FALSE;
BOOL pending_a314_reset = FALSE;
BOOL stream_closed = FALSE;
struct MsgPort *id_mp;
struct IOStdReq *id_req;
struct InputEvent generated_event;
struct VBlankData
{
struct Task *task;
ULONG signal;
};
struct VBlankData vblank_data;
extern void VBlankServer();
struct Interrupt vblank_interrupt;
void start_a314_cmd(struct A314_IORequest *msg, UWORD command, char *buffer, int length)
{
msg->a314_Request.io_Command = command;
msg->a314_Request.io_Error = 0;
msg->a314_Socket = socket;
msg->a314_Buffer = buffer;
msg->a314_Length = length;
SendIO((struct IORequest *)msg);
}
LONG a314_connect(char *name)
{
socket = time(NULL);
start_a314_cmd(cmsg, A314_CONNECT, name, strlen(name));
return WaitIO((struct IORequest *)cmsg);
}
void start_a314_read()
{
start_a314_cmd(rmsg, A314_READ, arbuf, 255);
pending_a314_read = TRUE;
}
void start_a314_write(int length)
{
start_a314_cmd(wmsg, A314_WRITE, awbuf, length);
pending_a314_write = TRUE;
}
LONG sync_a314_write(int length)
{
start_a314_write(length);
pending_a314_write = FALSE;
return WaitIO((struct IORequest *)wmsg);
}
void start_a314_reset()
{
start_a314_cmd(cmsg, A314_RESET, NULL, 0);
pending_a314_reset = TRUE;
}
LONG sync_a314_reset()
{
start_a314_reset();
pending_a314_reset = FALSE;
return WaitIO((struct IORequest *)cmsg);
}
struct Screen *find_wb_screen()
{
struct Screen *screen = IntuitionBase->FirstScreen;
while (screen)
{
if ((screen->Flags & SCREENTYPE) == WBENCHSCREEN)
return screen;
screen = screen->NextScreen;
}
return NULL;
}
int blen = 0;
void append_ulong(ULONG x)
{
*((ULONG *)&awbuf[blen]) = x;
blen += 4;
}
void append_uword(UWORD x)
{
*((UWORD *)&awbuf[blen]) = x;
blen += 2;
}
WORD last_b = 0;
WORD kbd_qual = 0;
void send_generated_mouse_event(WORD dx, WORD dy, WORD b)
{
generated_event.ie_NextEvent = NULL;
generated_event.ie_Class = IECLASS_RAWMOUSE;
generated_event.ie_SubClass = 0;
UWORD code = IECODE_NOBUTTON;
if (!(last_b & 1) && (b & 1))
code = IECODE_LBUTTON;
else if ((last_b & 1) && !(b & 1))
code = IECODE_UP_PREFIX | IECODE_LBUTTON;
else if (!(last_b & 2) && (b & 2))
code = IECODE_RBUTTON;
else if ((last_b & 2) && !(b & 2))
code = IECODE_UP_PREFIX | IECODE_RBUTTON;
generated_event.ie_Code = code;
UWORD qual = IEQUALIFIER_RELATIVEMOUSE | kbd_qual;
if (b & 1)
qual |= IEQUALIFIER_LEFTBUTTON;
if (b & 2)
qual |= IEQUALIFIER_RBUTTON;
generated_event.ie_Qualifier = qual;
generated_event.ie_X = dx;
generated_event.ie_Y = dy;
id_req->io_Data = (void *)&generated_event;
id_req->io_Command = IND_WRITEEVENT;
id_req->io_Flags = 0;
id_req->io_Length = sizeof(struct InputEvent);
DoIO((struct IORequest *)id_req);
}
void send_generated_keyboard_event(UBYTE up, UBYTE kc)
{
generated_event.ie_NextEvent = NULL;
generated_event.ie_Class = IECLASS_RAWKEY;
generated_event.ie_SubClass = 0;
generated_event.ie_Code = up | kc;
UWORD qual = kbd_qual;
if (last_b & 1)
qual |= IEQUALIFIER_LEFTBUTTON;
if (last_b & 2)
qual |= IEQUALIFIER_RBUTTON;
switch (kc)
{
case 0x5a:
case 0x5b:
case 0x5c:
case 0x5d:
case 0x3d:
case 0x3e:
case 0x3f:
case 0x4a:
case 0x2d:
case 0x2e:
case 0x2f:
case 0x5e:
case 0x1d:
case 0x1e:
case 0x1f:
case 0x43:
case 0x0f:
case 0x3c:
qual |= IEQUALIFIER_NUMERICPAD;
break;
}
generated_event.ie_Qualifier = qual;
generated_event.ie_X = 0;
generated_event.ie_Y = 0;
id_req->io_Data = (void *)&generated_event;
id_req->io_Command = IND_WRITEEVENT;
id_req->io_Flags = 0;
id_req->io_Length = sizeof(struct InputEvent);
DoIO((struct IORequest *)id_req);
}
void handle_a314_read_completed()
{
pending_a314_read = FALSE;
if (stream_closed)
return;
int res = rmsg->a314_Request.io_Error;
if (res == A314_READ_OK)
{
int length = rmsg->a314_Length;
WORD *p = (WORD *)&arbuf[0];
WORD cx = IntuitionBase->MouseX;
WORD cy = IntuitionBase->MouseY;
while (length > 0)
{
WORD x = *p++;
if (x & 0x4000)
{
UBYTE up = (x & 0x2000) ? 0x80 : 0;
UBYTE kc = x & 0x7f;
if (kc >= 0x60 && kc <= 0x67)
{
UWORD qual = 1 << (kc - 0x60);
if (up)
kbd_qual &= ~qual;
else
kbd_qual |= qual;
}
send_generated_keyboard_event(up, kc);
length -= 2;
}
else
{
WORD y = *p++;
WORD b = *p++;
if (b == last_b)
{
send_generated_mouse_event(x - cx, y - cy, last_b);
cx = x;
cy = y;
}
else
{
if (cx != x || cy != y)
{
send_generated_mouse_event(x - cx, y - cy, last_b);
cx = x;
cy = y;
}
send_generated_mouse_event(0, 0, b);
last_b = b;
}
length -= 6;
}
}
start_a314_read();
}
else if (res == A314_READ_EOS)
{
start_a314_reset();
stream_closed = TRUE;
}
else if (res == A314_READ_RESET)
stream_closed = TRUE;
}
void handle_vblank_signal()
{
blen = 0;
append_uword(IntuitionBase->MouseX);
append_uword(IntuitionBase->MouseY);
if (!pending_a314_write)
start_a314_write(blen);
}
int main()
{
mp = CreatePort(NULL, 0);
cmsg = (struct A314_IORequest *)CreateExtIO(mp, sizeof(struct A314_IORequest));
if (OpenDevice(A314_NAME, 0, (struct IORequest *)cmsg, 0) != 0)
{
printf("Unable to open a314.device\n");
goto fail_out1;
}
A314Base = &(cmsg->a314_Request.io_Device->dd_Library);
wmsg = (struct A314_IORequest *)CreateExtIO(mp, sizeof(struct A314_IORequest));
rmsg = (struct A314_IORequest *)CreateExtIO(mp, sizeof(struct A314_IORequest));
memcpy(wmsg, cmsg, sizeof(struct A314_IORequest));
memcpy(rmsg, cmsg, sizeof(struct A314_IORequest));
if (a314_connect("remotewb") != A314_CONNECT_OK)
{
printf("Unable to connect to remotewb service\n");
goto fail_out2;
}
IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0);
GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 0);
Forbid();
struct Screen *screen = find_wb_screen();
if (!screen)
{
Permit();
printf("Unable to find workbench screen\n");
sync_a314_reset();
goto fail_out3;
}
struct BitMap *bm = &(screen->BitMap);
if (screen->Width != 640 || screen->Height != 256 || bm->Depth != 3 || (bm->BytesPerRow != 80 && bm->BytesPerRow != 240))
{
Permit();
printf("Wrong screen resolution; it is %hdx%hdx%hhu but must be 640x256x3\n", screen->Width, screen->Height, bm->Depth);
sync_a314_reset();
goto fail_out3;
}
append_uword(screen->Width);
append_uword(screen->Height);
append_uword(bm->Depth);
append_uword(bm->BytesPerRow);
ULONG ptr = TranslateAddressA314(bm->Planes[0]);
if (ptr == -1)
{
int depth = bm->Depth;
int size = 80 * 256;
UBYTE *p = AllocMem(size * depth, MEMF_A314 | MEMF_CHIP);
if (!p)
{
Permit();
printf("Unable to allocate enough A314 chip memory\n");
sync_a314_reset();
goto fail_out3;
}
if (bm->BytesPerRow == 80)
{
UBYTE *old_planes[8];
for (int i = 0; i < depth; i++)
{
UBYTE *op = bm->Planes[i];
UBYTE *np = p + (i * size);
memcpy(np, op, size);
old_planes[i] = op;
bm->Planes[i] = np;
}
RemakeDisplay();
for (int i = 0; i < depth; i++)
FreeMem(old_planes[i], size);
ptr = TranslateAddressA314(bm->Planes[0]);
}
else if (bm->BytesPerRow == 240)
{
UBYTE *old_ptr = bm->Planes[0];
memcpy(p, old_ptr, size * depth);
for (int i = 0; i < depth; i++)
bm->Planes[i] = p + (i * 80);
RemakeDisplay();
FreeMem(old_ptr, size * depth);
ptr = TranslateAddressA314(bm->Planes[0]);
}
}
append_ulong(ptr);
struct ColorMap *cm = screen->ViewPort.ColorMap;
append_uword(cm->Count);
for (int i = 0; i < cm->Count; i++)
append_uword(((UWORD *)cm->ColorTable)[i] & 0xfff);
Permit();
sync_a314_write(blen);
id_mp = CreatePort(NULL, 0);
id_req = (struct IOStdReq *)CreateExtIO(id_mp, sizeof(struct IOStdReq));
OpenDevice("input.device", 0, (struct IORequest *)id_req, 0);
start_a314_read();
BYTE vblank_sigbit = AllocSignal(-1);
ULONG vblanksig = 1 << vblank_sigbit;
ULONG portsig = 1 << mp->mp_SigBit;
vblank_data.task = FindTask(NULL);
vblank_data.signal = vblanksig;
vblank_interrupt.is_Node.ln_Type = NT_INTERRUPT;
vblank_interrupt.is_Node.ln_Pri = -60;
vblank_interrupt.is_Node.ln_Name = NULL;
vblank_interrupt.is_Data = (APTR)&vblank_data;
vblank_interrupt.is_Code = VBlankServer;
AddIntServer(INTB_VERTB, &vblank_interrupt);
printf("Press ctrl-c to exit...\n");
while (TRUE)
{
ULONG signal = Wait(vblanksig | portsig | SIGBREAKF_CTRL_C);
if (signal & vblanksig)
{
handle_vblank_signal();
}
if (signal & portsig)
{
struct Message *msg;
while (msg = GetMsg(mp))
{
if (msg == (struct Message *)rmsg)
handle_a314_read_completed();
else if (msg == (struct Message *)wmsg)
pending_a314_write = FALSE;
else if (msg == (struct Message *)cmsg)
pending_a314_reset = FALSE;
}
}
if (signal & SIGBREAKF_CTRL_C)
{
start_a314_reset();
stream_closed = TRUE;
}
if (stream_closed && !pending_a314_read && !pending_a314_write && !pending_a314_reset)
break;
}
RemIntServer(INTB_VERTB, &vblank_interrupt);
FreeSignal(vblank_sigbit);
CloseDevice((struct IORequest *)id_req);
DeleteExtIO((struct IORequest *)id_req);
DeletePort(id_mp);
fail_out3:
CloseLibrary((struct Library *)GfxBase);
CloseLibrary((struct Library *)IntuitionBase);;
fail_out2:
CloseDevice((struct IORequest *)cmsg);
DeleteExtIO((struct IORequest *)rmsg);
DeleteExtIO((struct IORequest *)wmsg);
fail_out1:
DeleteExtIO((struct IORequest *)cmsg);
DeletePort(mp);
return 0;
}