-
Notifications
You must be signed in to change notification settings - Fork 4
/
rio.c
1566 lines (1426 loc) · 29.4 KB
/
rio.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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <thread.h>
#include <cursor.h>
#include <mouse.h>
#include <keyboard.h>
#include <frame.h>
#include <fcall.h>
#include <plumb.h>
#include "dat.h"
#include "fns.h"
/*
* WASHINGTON (AP) - The Food and Drug Administration warned
* consumers Wednesday not to use ``Rio'' hair relaxer products
* because they may cause severe hair loss or turn hair green....
* The FDA urged consumers who have experienced problems with Rio
* to notify their local FDA office, local health department or the
* company at 1‑800‑543‑3002.
*/
void resize(void);
void move(void);
void delete(void);
void hide(void);
void tile(void);
void unhide(int);
void newtile(int);
Image *sweep(void);
Image *bandsize(Window*);
Image* drag(Window*, Rectangle*);
void refresh(Rectangle);
void resized(void);
Channel *exitchan; /* chan(int) */
Channel *winclosechan; /* chan(Window*); */
Channel *kbdchan; /* chan(char*); */
Rectangle viewr;
int threadrforkflag = 0; /* should be RFENVG but that hides rio from plumber */
void mousethread(void*);
void keyboardthread(void*);
void winclosethread(void*);
void deletethread(void*);
void initcmd(void*);
Channel* initkbd(void);
char *fontname;
enum
{
New,
Reshape,
Move,
Delete,
Hide,
Tile,
Exit,
};
enum
{
Cut,
Paste,
Snarf,
Plumb,
Send,
Scroll,
};
char *menu2str[] = {
[Cut] "cut",
[Paste] "paste",
[Snarf] "snarf",
[Plumb] "plumb",
[Send] "send",
[Scroll] "scroll",
nil
};
Menu menu2 =
{
menu2str
};
int Hidden = Exit+1;
char *menu3str[100] = {
[New] "New",
[Reshape] "Resize",
[Move] "Move",
[Delete] "Delete",
[Hide] "Hide",
[Tile] "Tile",
[Exit] "Exit",
nil
};
Menu menu3 =
{
menu3str
};
char *rcargv[] = { "rc", "-i", nil };
char *kbdargv[] = { "rc", "-c", nil, nil };
int errorshouldabort = 0;
void
derror(Display*, char *errorstr)
{
error(errorstr);
}
void
usage(void)
{
fprint(2, "usage: rio [-b] [-f font] [-i initcmd] [-k kbdcmd] [-s]\n");
exits("usage");
}
void
threadmain(int argc, char *argv[])
{
char *initstr, *kbdin, *s;
static void *arg[1];
char buf[256];
Image *i;
Rectangle r;
if(strstr(argv[0], ".out") == nil){
menu3str[Exit] = nil;
Hidden--;
}
initstr = nil;
kbdin = nil;
maxtab = 0;
ARGBEGIN{
case 'b':
reverse = ~0xFF;
break;
case 'f':
fontname = ARGF();
if(fontname == nil)
usage();
break;
case 'i':
initstr = ARGF();
if(initstr == nil)
usage();
break;
case 'k':
if(kbdin != nil)
usage();
kbdin = ARGF();
if(kbdin == nil)
usage();
break;
case 's':
scrolling = TRUE;
break;
}ARGEND
if(getwd(buf, sizeof buf) == nil)
startdir = estrdup(".");
else
startdir = estrdup(buf);
if(fontname == nil)
fontname = getenv("font");
s = getenv("tabstop");
if(s != nil)
maxtab = strtol(s, nil, 0);
if(maxtab == 0)
maxtab = 4;
free(s);
if(fontname){
/* check font before barging ahead */
if(access(fontname, 0) < 0){
fprint(2, "rio: can't access %s: %r\n", fontname);
exits("font open");
}
putenv("font", fontname);
}
snarffd = open("/dev/snarf", OREAD|OCEXEC);
gotscreen = access("/dev/screen", AEXIST)==0;
if(geninitdraw(nil, derror, nil, "rio", nil, Refnone) < 0){
fprint(2, "rio: can't open display: %r\n");
exits("display open");
}
iconinit();
view = screen;
viewr = view->r;
mousectl = initmouse(nil, screen);
if(mousectl == nil)
error("can't find mouse");
mouse = mousectl;
kbdchan = initkbd();
if(kbdchan == nil)
error("can't find keyboard");
wscreen = allocscreen(screen, background, 0);
if(wscreen == nil)
error("can't allocate screen");
draw(view, viewr, background, nil, ZP);
flushimage(display, 1);
exitchan = chancreate(sizeof(int), 0);
winclosechan = chancreate(sizeof(Window*), 0);
deletechan = chancreate(sizeof(char*), 0);
timerinit();
threadcreate(keyboardthread, nil, STACK);
threadcreate(mousethread, nil, STACK);
threadcreate(winclosethread, nil, STACK);
threadcreate(deletethread, nil, STACK);
filsys = filsysinit(xfidinit());
if(filsys == nil)
fprint(2, "rio: can't create file system server: %r\n");
else{
errorshouldabort = 1; /* suicide if there's trouble after this */
if(initstr)
proccreate(initcmd, initstr, STACK);
if(kbdin){
kbdargv[2] = kbdin;
r = screen->r;
r.max.x = r.min.x+300;
r.max.y = r.min.y+80;
i = allocwindow(wscreen, r, Refbackup, DNofill);
wkeyboard = new(i, FALSE, scrolling, 0, nil, "/bin/rc", kbdargv);
if(wkeyboard == nil)
error("can't create keyboard window");
}
threadnotify(shutdown, 1);
recv(exitchan, nil);
}
killprocs();
threadexitsall(nil);
}
/*
* /dev/snarf updates when the file is closed, so we must open our own
* fd here rather than use snarffd
*/
void
putsnarf(void)
{
int fd, i, n;
if(snarffd<0 || nsnarf==0)
return;
fd = open("/dev/snarf", OWRITE);
if(fd < 0)
return;
/* snarf buffer could be huge, so fprint will truncate; do it in blocks */
for(i=0; i<nsnarf; i+=n){
n = nsnarf-i;
if(n >= 256)
n = 256;
if(fprint(fd, "%.*S", n, snarf+i) < 0)
break;
}
close(fd);
}
void
getsnarf(void)
{
int i, n, nb, nulls;
char *sn, buf[1024];
if(snarffd < 0)
return;
sn = nil;
i = 0;
seek(snarffd, 0, 0);
while((n = read(snarffd, buf, sizeof buf)) > 0){
sn = erealloc(sn, i+n+1);
memmove(sn+i, buf, n);
i += n;
sn[i] = 0;
}
if(i > 0){
snarf = runerealloc(snarf, i+1);
cvttorunes(sn, i, snarf, &nb, &nsnarf, &nulls);
free(sn);
}
}
void
initcmd(void *arg)
{
char *cmd;
cmd = arg;
rfork(RFENVG|RFFDG|RFNOTEG|RFNAMEG);
procexecl(nil, "/bin/rc", "rc", "-c", cmd, nil);
fprint(2, "rio: exec failed: %r\n");
exits("exec");
}
char *oknotes[] =
{
"delete",
"hangup",
"kill",
"exit",
nil
};
int
shutdown(void *, char *msg)
{
int i;
static Lock shutdownlk;
killprocs();
for(i=0; oknotes[i]; i++)
if(strncmp(oknotes[i], msg, strlen(oknotes[i])) == 0){
lock(&shutdownlk); /* only one can threadexitsall */
threadexitsall(msg);
}
fprint(2, "rio %d: abort: %s\n", getpid(), msg);
abort();
exits(msg);
return 0;
}
void
killprocs(void)
{
int i;
for(i=0; i<nwindow; i++)
if(window[i]->notefd >= 0)
write(window[i]->notefd, "hangup", 6);
}
void movewindowforward(void) {
Window *w;
int i;
for(i = 0; i <= nwindow; i++) {
if(window[i] == input) {
if (i >= nwindow - 1) {
w = window[0];
incref(w);
wtopme(w);
wcurrent(w);
wclose(w);
} else {
w = window[i+1];
incref(w);
wtopme(w);
wcurrent(w);
wclose(w);
}
}
}
}
void movewindowbackwards(void) {
Window *w;
int i;
for(i = 0; i <= nwindow; i++) {
if(window[i] == input) {
if (i == 0 ) {
w = window[nwindow - 1];
incref(w);
wtopme(w);
wcurrent(w);
wclose(w);
} else {
w = window[i-1];
incref(w);
wtopme(w);
wcurrent(w);
wclose(w);
}
}
}
}
void
keyboardthread(void*)
{
char *s;
threadsetname("keyboardthread");
while(s = recvp(kbdchan)){
// print("s1: 0x%hhux, s2: 0x%hhux, s3: 0x%hhux, s4: 0x%hhux, s5: 0x%hhux\n", s[1], s[2], s[3], s[4], s[5]);
// print("%s\n", s);
if(*s == 'k' || *s == 'K')
shiftdown = utfrune(s+1, Kshift) != nil;
ctrldown = utfrune(s+1, Kctl) != nil;
// check if we've hit ctrl-shift-j
if(shiftdown && s[1] == (char)0x4a) {
movewindowforward();
} else if (shiftdown && s[1] == (char)0x4b) {
movewindowbackwards();
} else if (shiftdown && s[1] == (char)0x4c) {
Image *i;
Rectangle r;
r = screen->r;
r.max.x = r.min.x+300;
r.max.y = r.min.y+80;
i = allocwindow(wscreen, r, Refbackup, DNofill);
new(i, FALSE, scrolling, 0, nil, "/bin/rc", nil);
tile();
} else if (shiftdown && s[1] == (char)0x20 && s[2] == (char)0x0) {
tile();
} else {
if(input == nil || sendp(input->ck, s) <= 0)
free(s);
}
}
}
/*
* Used by /dev/kbdin
*/
void
keyboardsend(char *s, int cnt)
{
if(cnt <= 0)
return;
if(s[cnt-1] == 0)
chanprint(kbdchan, "%s", s);
else {
Rune *r;
int i, nb, nr;
r = runemalloc(cnt);
cvttorunes(s, cnt, r, &nb, &nr, nil);
for(i=0; i<nr; i++){
if(r[i])
chanprint(kbdchan, "c%C", r[i]);
}
free(r);
}
}
int
portion(int x, int lo, int hi)
{
x -= lo;
hi -= lo;
if(x < 20)
return 0;
if(x > hi-20)
return 2;
return 1;
}
int
whichcorner(Window *w, Point p)
{
int i, j;
i = portion(p.x, w->screenr.min.x, w->screenr.max.x);
j = portion(p.y, w->screenr.min.y, w->screenr.max.y);
return 3*j+i;
}
void
cornercursor(Window *w, Point p, int force)
{
if(w!=nil && winborder(w, p))
riosetcursor(corners[whichcorner(w, p)], force);
else
wsetcursor(w, force);
}
/* thread to allow fsysproc to synchronize window closing with main proc */
void
winclosethread(void*)
{
Window *w;
threadsetname("winclosethread");
for(;;){
w = recvp(winclosechan);
wclose(w);
}
}
/* thread to make Deleted windows that the client still holds disappear offscreen after an interval */
void
deletethread(void*)
{
char *s;
Image *i;
threadsetname("deletethread");
for(;;){
s = recvp(deletechan);
i = namedimage(display, s);
if(i != nil){
/* move it off-screen to hide it, since client is slow in letting it go */
originwindow(i, i->r.min, view->r.max);
}
freeimage(i);
free(s);
}
}
void
deletetimeoutproc(void *v)
{
char *s;
s = v;
sleep(750); /* remove window from screen after 3/4 of a second */
sendp(deletechan, s);
}
/*
* Button 6 - keyboard toggle - has been pressed.
* Send event to keyboard, wait for button up, send that.
* Note: there is no coordinate translation done here; this
* is just about getting button 6 to the keyboard simulator.
*/
void
keyboardhide(void)
{
send(wkeyboard->mc.c, mouse);
do
readmouse(mousectl);
while(mouse->buttons & (1<<5));
send(wkeyboard->mc.c, mouse);
}
void
mousethread(void*)
{
int sending, inside, scrolling, moving, band;
Window *w, *winput;
Image *i;
Rectangle r;
Point xy;
Mouse tmp;
enum {
MReshape,
MMouse,
NALT
};
static Alt alts[NALT+1];
threadsetname("mousethread");
sending = FALSE;
scrolling = FALSE;
moving = FALSE;
alts[MReshape].c = mousectl->resizec;
alts[MReshape].v = nil;
alts[MReshape].op = CHANRCV;
alts[MMouse].c = mousectl->c;
alts[MMouse].v = &mousectl->Mouse;
alts[MMouse].op = CHANRCV;
alts[NALT].op = CHANEND;
for(;;)
switch(alt(alts)){
case MReshape:
resized();
break;
case MMouse:
if(wkeyboard!=nil && (mouse->buttons & (1<<5))){
keyboardhide();
break;
}
Again:
winput = input;
/* override everything for the keyboard window */
if(wkeyboard!=nil && ptinrect(mouse->xy, wkeyboard->screenr)){
/* make sure it's on top; this call is free if it is */
wtopme(wkeyboard);
winput = wkeyboard;
}
if(winput!=nil && !winput->deleted && winput->i!=nil){
/* convert to logical coordinates */
xy.x = mouse->xy.x + (winput->i->r.min.x-winput->screenr.min.x);
xy.y = mouse->xy.y + (winput->i->r.min.y-winput->screenr.min.y);
/* the up and down scroll buttons are not subject to the usual rules */
if((mouse->buttons&(8|16)) && !winput->mouseopen)
goto Sending;
inside = ptinrect(mouse->xy, insetrect(winput->screenr, Selborder));
if(winput->mouseopen)
scrolling = FALSE;
else if(scrolling)
scrolling = mouse->buttons;
else
scrolling = mouse->buttons && ptinrect(xy, winput->scrollr);
/* topped will be zero or less if window has been bottomed */
if(sending == FALSE && !scrolling && winborder(winput, mouse->xy) && winput->topped>0){
moving = TRUE;
}else if(inside && (scrolling || winput->mouseopen || (mouse->buttons&1)))
sending = TRUE;
}else
sending = FALSE;
if(sending){
Sending:
if(mouse->buttons == 0){
cornercursor(winput, mouse->xy, 0);
sending = FALSE;
}else
wsetcursor(winput, 0);
tmp = mousectl->Mouse;
tmp.xy = xy;
send(winput->mc.c, &tmp);
continue;
}
w = wpointto(mouse->xy);
/* change cursor if over anyone's border */
if(w != nil)
cornercursor(w, mouse->xy, 0);
else
riosetcursor(nil, 0);
if(moving && (mouse->buttons&7)){
incref(winput);
band = mouse->buttons & 3;
sweeping = 1;
if(band)
i = bandsize(winput);
else
i = drag(winput, &r);
sweeping = 0;
if(i != nil){
if(band)
wsendctlmesg(winput, Reshaped, i->r, i);
else
wsendctlmesg(winput, Moved, r, i);
cornercursor(winput, mouse->xy, 1);
}
if(wclose(winput) == 0)
w = winput;
else {
riosetcursor(nil, 0);
w = nil;
}
}
/* we're not sending the event, but if button is down maybe we should */
if(mouse->buttons){
/* w->topped will be zero or less if window has been bottomed */
if(w==nil || (w==winput && w->topped>0)){
if(mouse->buttons & 1){
;
}else if(mouse->buttons & 2){
if(winput && !winput->deleted && !winput->mouseopen){
incref(winput);
button2menu(winput);
wclose(winput);
}
}else if(mouse->buttons & 4)
button3menu();
}else{
/* if button 1 event in the window, top the window and wait for button up. */
/* otherwise, top the window and pass the event on */
if(wtop(mouse->xy) && (mouse->buttons!=1 || winborder(w, mouse->xy)))
goto Again;
goto Drain;
}
}
moving = FALSE;
break;
Drain:
do
readmouse(mousectl);
while(mousectl->buttons);
moving = FALSE;
goto Again; /* recalculate mouse position, cursor */
}
}
int
wtopcmp(void *a, void *b)
{
return (*(Window**)a)->topped - (*(Window**)b)->topped;
}
void
resized(void)
{
Image *im;
int i, j;
Rectangle r;
Point o, n;
Window *w;
if(getwindow(display, Refnone) < 0)
error("failed to re-attach window");
freescrtemps();
view = screen;
freescreen(wscreen);
wscreen = allocscreen(screen, background, 0);
if(wscreen == nil)
error("can't re-allocate screen");
draw(view, view->r, background, nil, ZP);
o = subpt(viewr.max, viewr.min);
n = subpt(view->clipr.max, view->clipr.min);
qsort(window, nwindow, sizeof(window[0]), wtopcmp);
for(i=0; i<nwindow; i++){
w = window[i];
r = rectsubpt(w->i->r, viewr.min);
r.min.x = (r.min.x*n.x)/o.x;
r.min.y = (r.min.y*n.y)/o.y;
r.max.x = (r.max.x*n.x)/o.x;
r.max.y = (r.max.y*n.y)/o.y;
if(!goodrect(r))
r = rectsubpt(w->i->r, viewr.min);
r = rectaddpt(r, screen->clipr.min);
for(j=0; j<nhidden; j++)
if(w == hidden[j])
break;
incref(w);
if(j < nhidden){
im = allocimage(display, r, screen->chan, 0, DNofill);
r = ZR;
} else
im = allocwindow(wscreen, r, Refbackup, DNofill);
if(im)
wsendctlmesg(w, Reshaped, r, im);
wclose(w);
}
viewr = screen->r;
flushimage(display, 1);
}
int
obscured(Window *w, Rectangle r, int i)
{
Window *t;
if(Dx(r) < font->height || Dy(r) < font->height)
return 1;
if(!rectclip(&r, screen->r))
return 1;
for(; i<nwindow; i++){
t = window[i];
if(t == w || t->topped <= w->topped)
continue;
if(Dx(t->screenr) == 0 || Dy(t->screenr) == 0 || rectXrect(r, t->screenr) == 0)
continue;
if(r.min.y < t->screenr.min.y)
if(!obscured(w, Rect(r.min.x, r.min.y, r.max.x, t->screenr.min.y), i))
return 0;
if(r.min.x < t->screenr.min.x)
if(!obscured(w, Rect(r.min.x, r.min.y, t->screenr.min.x, r.max.y), i))
return 0;
if(r.max.y > t->screenr.max.y)
if(!obscured(w, Rect(r.min.x, t->screenr.max.y, r.max.x, r.max.y), i))
return 0;
if(r.max.x > t->screenr.max.x)
if(!obscured(w, Rect(t->screenr.max.x, r.min.y, r.max.x, r.max.y), i))
return 0;
return 1;
}
return 0;
}
void
button3menu(void)
{
int i, j, n;
n = nhidden;
for(i=0; i<nwindow; i++){
for(j=0; j<n; j++)
if(window[i] == hidden[j])
break;
if(j == n)
if(obscured(window[i], window[i]->screenr, 0)){
hidden[n++] = window[i];
if(n >= nelem(hidden))
break;
}
}
if(n >= nelem(menu3str)-Hidden)
n = nelem(menu3str)-Hidden-1;
for(i=0; i<n; i++){
free(menu3str[i+Hidden]);
menu3str[i+Hidden] = estrdup(hidden[i]->label);
}
for(i+=Hidden; menu3str[i]; i++){
free(menu3str[i]);
menu3str[i] = nil;
}
sweeping = 1;
switch(i = menuhit(3, mousectl, &menu3, wscreen)){
case -1:
break;
case New:
new(sweep(), FALSE, scrolling, 0, nil, "/bin/rc", nil);
break;
case Reshape:
resize();
break;
case Move:
move();
break;
case Delete:
delete();
break;
case Hide:
hide();
break;
case Tile:
tile();
break;
case Exit:
if(Hidden > Exit){
send(exitchan, nil);
break;
}
/* else fall through */
default:
unhide(i);
break;
}
sweeping = 0;
}
void
button2menu(Window *w)
{
if(w->scrolling)
menu2str[Scroll] = "noscroll";
else
menu2str[Scroll] = "scroll";
switch(menuhit(2, mousectl, &menu2, wscreen)){
case Cut:
wsnarf(w);
wcut(w);
wscrdraw(w);
break;
case Snarf:
wsnarf(w);
break;
case Paste:
getsnarf();
wpaste(w);
wscrdraw(w);
break;
case Plumb:
wplumb(w);
break;
case Send:
getsnarf();
wsnarf(w);
if(nsnarf == 0)
break;
if(w->rawing){
waddraw(w, snarf, nsnarf);
if(snarf[nsnarf-1]!='\n' && snarf[nsnarf-1]!='\004')
waddraw(w, L"\n", 1);
}else{
winsert(w, snarf, nsnarf, w->nr);
if(snarf[nsnarf-1]!='\n' && snarf[nsnarf-1]!='\004')
winsert(w, L"\n", 1, w->nr);
}
wsetselect(w, w->nr, w->nr);
wshow(w, w->nr);
break;
case Scroll:
if(w->scrolling ^= 1)
wshow(w, w->nr);
break;
}
wsendctlmesg(w, Wakeup, ZR, nil);
flushimage(display, 1);
}
Point
onscreen(Point p)
{
p.x = max(screen->clipr.min.x, p.x);
p.x = min(screen->clipr.max.x, p.x);
p.y = max(screen->clipr.min.y, p.y);
p.y = min(screen->clipr.max.y, p.y);
return p;
}
Image*
sweep(void)
{
Image *i, *oi;
Rectangle r;
Point p0, p;
i = nil;
menuing = TRUE;
riosetcursor(&crosscursor, 1);
while(mouse->buttons == 0)
readmouse(mousectl);
p0 = onscreen(mouse->xy);
p = p0;
r.min = p;
r.max = p;
oi = nil;
while(mouse->buttons == 4){
readmouse(mousectl);
if(mouse->buttons != 4 && mouse->buttons != 0)
break;
if(!eqpt(mouse->xy, p)){
p = onscreen(mouse->xy);
r = canonrect(Rpt(p0, p));
if(Dx(r)>5 && Dy(r)>5){
i = allocwindow(wscreen, r, Refnone, DNofill);
freeimage(oi);
if(i == nil)
goto Rescue;
oi = i;
border(i, r, Selborder, sizecol, ZP);
draw(i, insetrect(r, Selborder), cols[BACK], nil, ZP);
flushimage(display, 1);
}
}
}
if(mouse->buttons != 0)
goto Rescue;
if(i==nil || Dx(i->r)<100 || Dy(i->r)<3*font->height)
goto Rescue;
oi = i;
i = allocwindow(wscreen, oi->r, Refbackup, DNofill);
freeimage(oi);
if(i == nil)
goto Rescue;
cornercursor(input, mouse->xy, 1);
goto Return;
Rescue:
freeimage(i);
i = nil;
cornercursor(input, mouse->xy, 1);
while(mouse->buttons)
readmouse(mousectl);
Return:
moveto(mousectl, mouse->xy); /* force cursor update; ugly */
menuing = FALSE;
return i;
}
void
drawedge(Image **bp, Rectangle r)
{
Image *b = *bp;
if(b != nil && Dx(b->r) == Dx(r) && Dy(b->r) == Dy(r))
originwindow(b, r.min, r.min);
else{
freeimage(b);
b = allocwindow(wscreen, r, Refbackup, DNofill);
if(b != nil) draw(b, r, sizecol, nil, ZP);
*bp = b;
}
}
void
drawborder(Rectangle r, int show)
{
static Image *b[4];
int i;
if(show == 0){
for(i = 0; i < 4; i++){
freeimage(b[i]);
b[i] = nil;
}
}else{
r = canonrect(r);
drawedge(&b[0], Rect(r.min.x, r.min.y, r.min.x+Borderwidth, r.max.y));
drawedge(&b[1], Rect(r.min.x+Borderwidth, r.min.y, r.max.x-Borderwidth, r.min.y+Borderwidth));
drawedge(&b[2], Rect(r.max.x-Borderwidth, r.min.y, r.max.x, r.max.y));
drawedge(&b[3], Rect(r.min.x+Borderwidth, r.max.y-Borderwidth, r.max.x-Borderwidth, r.max.y));
}
}
Image*
drag(Window *w, Rectangle *rp)
{
Image *i, *ni;
Point p, op, d, dm, om;
Rectangle r;
i = w->i;
menuing = TRUE;
om = mouse->xy;
riosetcursor(&boxcursor, 1);
dm = subpt(mouse->xy, w->screenr.min);