-
Notifications
You must be signed in to change notification settings - Fork 1
/
blit.c
225 lines (206 loc) · 4.58 KB
/
blit.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
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <draw.h>
#include <keyboard.h>
#include <mouse.h>
#include <cursor.h>
#include "dat.h"
#include "fns.h"
int baud = 40000;
int scale = 1;
Rectangle picr;
Image *tmp, *bg;
Channel *keych, *uartrxch, *uarttxch;
Mousectl *mc;
int daddr;
u16int dstat;
u8int invert;
int vblctr, uartrxctr;
Rectangle updated;
u32int colbgv, colfgv;
Image *colbg, *colfg;
int realcolors;
static void
screeninit(void)
{
Point p;
p = divpt(addpt(screen->r.min, screen->r.max), 2);
picr = (Rectangle){subpt(p, Pt(scale * SX/2, scale * SY/2)), addpt(p, Pt(scale * SX/2, scale * SY/2))};
if(picr.min.x < screen->r.min.x){
picr.max.x += screen->r.min.x - picr.min.x;
picr.min.x = screen->r.min.x;
}
if(picr.min.y < screen->r.min.y){
picr.max.y += screen->r.min.y - picr.min.y;
picr.min.y = screen->r.min.y;
}
if(tmp != nil) freeimage(tmp);
tmp = allocimage(display, Rect(0, 0, scale * SX, scale > 1 ? 1 : scale * SY), CHAN1(CMap, 1), scale > 1, 0);
if(bg != nil) freeimage(bg);
bg = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, 0xCCCCCCFF);
colbg = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, colbgv);
colfg = allocimage(display, Rect(0, 0, 1, 1), screen->chan, 1, colfgv);
draw(screen, screen->r, bg, nil, ZP);
updated = Rect(0, 0, SX, SY);
}
static void
redraw(void)
{
static uchar pic[SX*SY/8];
ushort *p;
uchar *q;
int o, n;
Mouse m;
Rectangle r;
if(nbrecv(mc->resizec, 0) == 1){
if(getwindow(display, Refnone) < 0)
sysfatal("resize failed: %r");
screeninit();
}
while(nbrecv(mc->c, &m) > 0){
if(ptinrect(m.xy, picr)){
mousex = picr.max.x - m.xy.x - 1;
mousey = picr.max.y - m.xy.y - 1;
}
n = m.buttons >> 2 & 1 | m.buttons & 2 | m.buttons << 2 & 4;
if(n != mousebut){
mousebut = n;
irq |= INTMOUSE;
}
}
if(Dy(updated) <= 0 || Dx(updated) <= 0)
return;
assert(daddr + sizeof(pic) <= sizeof(ram));
r = tmp->r;
if(updated.min.y > r.min.y)
r.min.y = updated.min.y;
if(updated.max.y < r.max.y)
r.max.y = updated.max.y;
o = r.min.y*(SX/8);
p = ram + (daddr + o) / 2;
q = pic + o;
for(n = Dy(r)*(SX/16); --n >= 0; ){
*q++ = invert ^ *p >> 8;
*q++ = invert ^ *p++;
}
loadimage(tmp, r, pic+o, Dy(r)*(SX/8));
if(realcolors){
draw(screen, rectaddpt(r, picr.min), colfg, nil, r.min);
draw(screen, rectaddpt(r, picr.min), colbg, tmp, r.min);
}else
draw(screen, rectaddpt(r, picr.min), tmp, nil, r.min);
updated = Rect(SX, SY, 0, 0);
flushimage(display, 1);
}
static uchar
keymap[] = {
[Kup-KF] 0xf1,
// [Kdown] 0xf2,
[Kleft-KF] 0xf3,
[Kright-KF] 0xf4,
[1] 0xf6, /* PF1 */
[2] 0xf7, /* PF2 */
[3] 0xf8, /* PF3 */
[4] 0xf9, /* PF4 */
[12] 0xfe, /* SET-UP */
[Kpgdown-KF] 0xb0, /* SCROLL */
[Kins-KF] 0xe0, /* BREAK */
};
static void
keyproc(void *unused)
{
Keyboardctl *kbdctl;
int ch, rc;
Rune r;
if((kbdctl = initkeyboard(nil)) == nil)
sysfatal("open keyboard: %r");
for(;;){
rc = recv(kbdctl->c, &r);
if(rc <= 0)
sysfatal("read keyboard: %r");
if(r == Kend){
closekeyboard(kbdctl);
threadexitsall(nil);
}
ch = r;
if(ch == '\n') ch = '\r';
if(ch == Kdown) ch = 0xf2;
else if(ch >= KF){
if(ch >= KF + nelem(keymap)) continue;
ch = keymap[ch - KF];
if(ch == 0) continue;
}else if(ch >= 0x80) continue;
send(keych, &ch);
}
}
void
usage(void)
{
fprint(2, "usage: %s [-b baud] [-C bg,fg] [-d] [-t [net!]host[!service]]\n", argv0);
threadexitsall("usage");
}
void
threadmain(int argc, char **argv)
{
int n, ms;
static Cursor blank;
char *telnet;
char *p;
extern int diag;
ms = 0;
telnet = nil;
ARGBEGIN{
case 'b':
baud = strtol(EARGF(usage()), &p, 0);
if(*p != 0) usage();
break;
case 't':
telnet = EARGF(usage());
break;
case 'C':
if(realcolors) usage();
realcolors++;
p = EARGF(usage());
colbgv = strtol(p, &p, 16) << 8 | 0xff;
if(*p++ != ',') usage();
colfgv = strtol(p, &p, 16) << 8 | 0xff;
if(*p != 0) usage();
break;
case 'd':
diag++;
break;
case 'm':
ms++;
break;
default: usage();
}ARGEND;
if(argc != 0) usage();
keych = chancreate(sizeof(int), 64);
uartrxch = chancreate(sizeof(int), 128);
uarttxch = chancreate(sizeof(int), 128);
if(telnet != nil) telnetinit(telnet);
meminit();
if(initdraw(nil, nil, nil) < 0)
sysfatal("initdraw: %r");
screeninit();
proccreate(keyproc, nil, mainstacksize);
mc = initmouse(nil, screen);
if(mc == nil)
sysfatal("initmouse: %r");
if(ms == 0)
setcursor(mc, &blank);
cpureset();
for(;;){
keycheck();
n = step();
vblctr += n;
if(vblctr >= VBLDIV){
irq |= INTVBL;
redraw();
vblctr -= VBLDIV;
}
if(uartrxctr > 0)
uartrxctr -= n;
}
}