-
Notifications
You must be signed in to change notification settings - Fork 1
/
view.c
495 lines (457 loc) · 7.63 KB
/
view.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
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <thread.h>
#include <mouse.h>
#include <keyboard.h>
#include <cursor.h>
#include <plumb.h>
#include "a.h"
enum
{
Emouse,
Eresize,
Ekeyboard,
Eplumb,
};
Cursor cross = {
{-8, -8},
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
},
{ 0x00, 0x00, 0x01, 0x00, 0x03, 0x80, 0x07, 0xc0,
0x01, 0x00, 0x11, 0x10, 0x31, 0x18, 0x7f, 0xfc,
0x31, 0x18, 0x11, 0x10, 0x01, 0x00, 0x07, 0xc0,
0x03, 0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
},
};
int mainstacksize=16384;
Mousectl *mctl;
Keyboardctl *kctl;
Image *bg;
Image *orig;
Image *img;
Point pos;
int zoomlevel;
int lfd;
const char* zoomlevels[] = {
"25%", "33%", "50%", "75%",
"100%",
"150%", "200%", "300%", "400%",
};
enum
{
Defaultzoomlevel = 4,
Nzoomlevels = nelem(zoomlevels),
};
enum
{
Mzcat,
Mzoomin,
Mzoomout,
Morigsize,
Mrcat,
Mrotcw,
Mrotccw,
Mflip,
Mflop,
Mmcat,
Mpipeto,
};
char *menu2str[] =
{
"_Zoom",
"in",
"out",
"reinit.",
"_Rotate",
"cw",
"ccw",
"flip",
"flop",
"_Misc",
"pipe",
nil,
};
Menu menu2 = { menu2str };
enum
{
Mopen,
Msave,
Mexport,
Mexit,
};
char *menu3str[] =
{
"open",
"save",
"export",
"exit",
nil,
};
Menu menu3 = { menu3str };
void redraw(void);
void zoom(void);
void
setlabel(char *label)
{
if(lfd < 0)
return;
fprint(lfd, "%s", label);
}
void
setimage(Image *i, int move)
{
if(img != nil && img != orig){
freeimage(img);
img = nil;
}
freeimage(orig);
orig = i;
if(zoomlevel != Defaultzoomlevel)
zoom();
else
img = orig;
if(move)
pos = subpt(ZP, img->r.min);
redraw();
}
int
loadfromfile(char *filename)
{
Image *i;
i = load(filename);
if(i == nil)
return -1;
zoomlevel = Defaultzoomlevel;
setimage(i, 1);
setlabel(filename);
return 0;
}
void
initbg(void)
{
Image *gray;
gray = eallocimage(1, 1, screen->chan, 1, 0xefefefff);
bg = eallocimage(16, 16, screen->chan, 1, DWhite);
draw(bg, bg->r, display->white, nil, ZP);
draw(bg, Rect(0,0,8,8), gray, nil, ZP);
draw(bg, Rect(8,8,16,16), gray, nil, ZP);
}
void
redraw(void)
{
lockdisplay(display);
draw(screen, screen->r, bg, nil, ZP);
if(img != nil)
draw(screen, rectaddpt(img->r, addpt(pos, screen->r.min)), img, nil, img->r.min);
flushimage(display, 1);
unlockdisplay(display);
}
void
pan(Point Δ)
{
Rectangle r, ir;
if(img == nil)
return;
if(Δ.x == 0 && Δ.y == 0)
return;
r = rectaddpt(img->r, addpt(pos, screen->r.min));
pos = addpt(pos, Δ);
ir = rectaddpt(r, Δ);
lockdisplay(display);
draw(screen, screen->r, bg, nil, ZP);
draw(screen, ir, img, nil, img->r.min);
unlockdisplay(display);
}
void
plumbproc(void *v)
{
Plumbmsg *m;
Channel *c;
int fd;
c = v;
threadsetname("plumbproc");
fd = plumbopen("image", OREAD);
if(fd < 0)
sysfatal("plumbopen: %r");
for(;;){
m = plumbrecv(fd);
if(m == nil)
sysfatal("plumbrecv: %r");
sendp(c, m);
}
}
/* FIXME: the whole temp file logic is bad and we risk
never deleting the file */
void
evtplumb(Plumbmsg *m)
{
int rm;
char *a, *f;
rm = 0;
a = plumblookup(m->attr, "action");
if(a != nil && strncmp(a, "showdata", 8) == 0){
f = smprint("/tmp/view.%ld.%d", time(nil), getpid());
if(writefile(f, m->data, m->ndata) < 0){
fprint(2, "cannot write showdata: %r\n");
goto Err;
}
rm = 1;
}else{
f = strdup(m->data);
}
if(loadfromfile(f) < 0)
fprint(2, "cannot load plumbed image: %r"); /* XXX: visual report */
Err:
plumbfree(m);
if(rm)
remove(f);
free(f);
}
void
evtresize(int new)
{
if(new && getwindow(display, Refnone)<0)
sysfatal("getwindow: %r");
redraw();
}
Image*
rotate(int op)
{
const char *cmd;
switch(op){
case Mflop:
cmd = "rotate -l";
break;
case Mflip:
cmd = "rotate -u";
break;
case Mrotccw:
cmd = "rotate -r 270";
break;
case Mrotcw:
cmd = "rotate -r 90";
break;
default:
werrstr("invalid rotate op");
return nil;
}
return ipipeto(orig, cmd);
}
void
zoom(void)
{
Image *i;
char cmd[255];
if(zoomlevel == Defaultzoomlevel){
img = orig;
redraw();
return;
}
if(snprint(cmd, sizeof cmd, "resize -x %s", zoomlevels[zoomlevel]) <= 0){
fprint(2, "error creating zoom command: %r\n"); /* XXX */
return;
}
i = ipipeto(orig, cmd);
if(i == nil){
fprint(2, "unable to zoom image: %r\n"); /* XXX */
return;
}
img = i;
redraw();
}
void
dozoom(int zop)
{
switch(zop){
case Mzoomin:
if(zoomlevel + 1 >= Nzoomlevels)
return;
++zoomlevel;
break;
case Mzoomout:
if(zoomlevel == 0)
return;
--zoomlevel;
break;
case Morigsize:
if(zoomlevel == Defaultzoomlevel)
return;
zoomlevel = Defaultzoomlevel;
break;
}
if(img != nil && img != orig)
freeimage(img);
zoom();
}
void
menu2hit(void)
{
Image *i;
int n;
char buf[255] = {0};
n = sepmenuhit(2, mctl, &menu2, nil);
if(img == nil)
return;
switch(n){
case Mzoomin:
case Mzoomout:
case Morigsize:
dozoom(n);
return;
case Mflip:
case Mflop:
case Mrotccw:
case Mrotcw:
i = rotate(n);
if(i == nil){
fprint(2, "unable to rotate image: %r\n");
return;
}
setimage(i, 0);
break;
case Mpipeto:
if(enter("command:", buf, sizeof buf, mctl, kctl, nil) <= 0)
return;
i = ipipeto(orig, buf);
if(i == nil){
fprint(2, "unable to pipe image: %r\n");
return;
}
setimage(i, 0);
break;
default:
return;
}
}
void
menu3hit(void)
{
char buf[255] = {0};
int n;
n = menuhit(3, mctl, &menu3, nil);
switch(n){
case Mopen:
if(enter("open:", buf, sizeof buf, mctl, kctl, nil) > 0){
if(loadfromfile(buf) < 0)
fprint(2, "cannot open file '%s': %r\n", buf);
}
break;
case Msave:
if(enter("save:", buf, sizeof buf, mctl, kctl, nil) > 0){
if(save(orig, buf) < 0)
fprint(2, "cannot save file '%s': %r\n", buf);
}
break;
case Mexport:
if(enter("export:", buf, sizeof buf, mctl, kctl, nil) > 0){
if(export(orig, buf) < 0)
fprint(2, "cannot export file '%s': %r\n", buf);
}
break;
case Mexit:
threadexitsall(nil);
break;
}
}
void
evtmouse(Mouse m)
{
Point o;
if(m.buttons == 1){
setcursor(mctl, &cross);
for(;;){
o = mctl->xy;
if(readmouse(mctl) < 0)
break;
if((mctl->buttons & 1) == 0)
break;
pan(subpt(mctl->xy, o));
}
setcursor(mctl, nil);
}else if(m.buttons == 2){
menu2hit();
}else if(m.buttons == 4){
menu3hit();
}
}
void
evtkey(Rune k)
{
switch(k){
case 'q':
case Kdel:
threadexitsall(nil);
break;
}
}
void
usage(void)
{
fprint(2, "usage: %s [<filename>]\n", argv0);
threadexitsall("usage");
}
void
threadmain(int argc, char **argv)
{
Mouse m;
Rune k;
Plumbmsg *pm;
Channel *plumbc;
Alt alts[] = {
{ nil, &m, CHANRCV },
{ nil, nil, CHANRCV },
{ nil, &k, CHANRCV },
{ nil, &pm, CHANRCV },
{ nil, nil, CHANEND },
};
img = nil;
zoomlevel = Defaultzoomlevel;
lfd = open("/dev/label", OWRITE);
ARGBEGIN{
default:
usage();
break;
}ARGEND;
if(argc > 1)
usage();
if(initdraw(nil, nil, argv0) < 0)
sysfatal("initdraw: %r");
unlockdisplay(display);
display->locking = 1;
if((mctl=initmouse(nil, screen)) == nil)
sysfatal("initmouse: %r");
if((kctl=initkeyboard(nil)) == nil)
sysfatal("initkeyboard: %r");
if((plumbc = chancreate(sizeof(Plumbmsg*), 0)) == nil)
sysfatal("chancreate: %r");
alts[Emouse].c = mctl->c;
alts[Eresize].c = mctl->resizec;
alts[Ekeyboard].c = kctl->c;
alts[Eplumb].c = plumbc;
initbg();
proccreate(plumbproc, plumbc, 8192);
if(*argv != nil)
loadfromfile(*argv);
else
evtresize(0);
for(;;){
switch(alt(alts)){
case Emouse:
evtmouse(m);
break;
case Eresize:
evtresize(1);
break;
case Ekeyboard:
evtkey(k);
break;
case Eplumb:
evtplumb(pm);
break;
}
}
}