]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/rio/scrl.c
rio: /dev/kbd cleanup
[plan9front.git] / sys / src / cmd / rio / scrl.c
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <thread.h>
5 #include <cursor.h>
6 #include <mouse.h>
7 #include <keyboard.h>
8 #include <frame.h>
9 #include <fcall.h>
10 #include "dat.h"
11 #include "fns.h"
12
13 static Image *scrtmp;
14
15 static
16 Image*
17 scrtemps(void)
18 {
19         int h;
20
21         if(scrtmp == nil){
22                 h = BIG*Dy(screen->r);
23                 scrtmp = allocimage(display, Rect(0, 0, 32, h), screen->chan, 0, DNofill);
24         }
25         return scrtmp;
26 }
27
28 void
29 freescrtemps(void)
30 {
31         if(scrtmp){
32                 freeimage(scrtmp);
33                 scrtmp = nil;
34         }
35 }
36
37 static
38 Rectangle
39 scrpos(Rectangle r, uint p0, uint p1, uint tot)
40 {
41         Rectangle q;
42         int h;
43
44         q = r;
45         h = q.max.y-q.min.y;
46         if(tot == 0)
47                 return q;
48         if(tot > 1024*1024){
49                 tot>>=10;
50                 p0>>=10;
51                 p1>>=10;
52         }
53         if(p0 > 0)
54                 q.min.y += h*p0/tot;
55         if(p1 < tot)
56                 q.max.y -= h*(tot-p1)/tot;
57         if(q.max.y < q.min.y+2){
58                 if(q.min.y+2 <= r.max.y)
59                         q.max.y = q.min.y+2;
60                 else
61                         q.min.y = q.max.y-2;
62         }
63         return q;
64 }
65
66 void
67 wscrdraw(Window *w)
68 {
69         Rectangle r, r1, r2;
70         Image *b;
71
72         b = scrtemps();
73         if(b == nil || w->i == nil)
74                 return;
75         r = w->scrollr;
76         r1 = r;
77         r1.min.x = 0;
78         r1.max.x = Dx(r);
79         r2 = scrpos(r1, w->org, w->org+w->nchars, w->nr);
80         if(!eqrect(r2, w->lastsr)){
81                 w->lastsr = r2;
82                 /* move r1, r2 to (0,0) to avoid clipping */
83                 r2 = rectsubpt(r2, r1.min);
84                 r1 = rectsubpt(r1, r1.min);
85                 draw(b, r1, w->cols[BORD], nil, ZP);
86                 draw(b, r2, w->cols[BACK], nil, ZP);
87                 r2.min.x = r2.max.x-1;
88                 draw(b, r2, w->cols[BORD], nil, ZP);
89                 draw(w->i, r, b, nil, Pt(0, r1.min.y));
90         }
91 }
92
93 void
94 wscrsleep(Window *w, uint dt)
95 {
96         Timer   *timer;
97         int y, b;
98         static Alt alts[3];
99
100         if(display->bufp > display->buf)
101                 flushimage(display, 1);
102         timer = timerstart(dt);
103         y = w->mc.xy.y;
104         b = w->mc.buttons;
105         alts[0].c = timer->c;
106         alts[0].v = nil;
107         alts[0].op = CHANRCV;
108         alts[1].c = w->mc.c;
109         alts[1].v = &w->mc.Mouse;
110         alts[1].op = CHANRCV;
111         alts[2].op = CHANEND;
112         for(;;)
113                 switch(alt(alts)){
114                 case 0:
115                         timerstop(timer);
116                         return;
117                 case 1:
118                         if(abs(w->mc.xy.y-y)>2 || w->mc.buttons!=b){
119                                 timercancel(timer);
120                                 return;
121                         }
122                         break;
123                 }
124 }
125
126 void
127 wscroll(Window *w, int but)
128 {
129         uint p0, oldp0;
130         Rectangle s;
131         int y, my, h, first;
132
133         s = insetrect(w->scrollr, 1);
134         h = s.max.y-s.min.y;
135         oldp0 = ~0;
136         first = TRUE;
137         do{
138                 my = w->mc.xy.y;
139                 if(my < s.min.y)
140                         my = s.min.y;
141                 if(my >= s.max.y)
142                         my = s.max.y;
143                 if(but == 2){
144                         y = my;
145                         if(y > s.max.y-2)
146                                 y = s.max.y-2;
147                         if(w->nr > 1024*1024)
148                                 p0 = ((w->nr>>10)*(y-s.min.y)/h)<<10;
149                         else
150                                 p0 = w->nr*(y-s.min.y)/h;
151                         if(oldp0 != p0)
152                                 wsetorigin(w, p0, FALSE);
153                         oldp0 = p0;
154                         readmouse(&w->mc);
155                         continue;
156                 }
157                 if(but == 1 || but == 4){
158                         y = max(1, (my-s.min.y)/w->font->height);
159                         p0 = wbacknl(w, w->org, y);
160                 }else{
161                         y = max(my, s.min.y+w->font->height);
162                         p0 = w->org+frcharofpt(w, Pt(s.max.x, y));
163                 }
164                 if(oldp0 != p0)
165                         wsetorigin(w, p0, TRUE);
166                 oldp0 = p0;
167                 /* debounce */
168                 if(first){
169                         if(display->bufp > display->buf)
170                                 flushimage(display, 1);
171                         if(but > 3)
172                                 return;
173                         sleep(200);
174                         nbrecv(w->mc.c, &w->mc.Mouse);
175                         first = FALSE;
176                 }
177                 wscrsleep(w, 100);
178         }while(w->mc.buttons & (1<<(but-1)));
179         while(w->mc.buttons)
180                 readmouse(&w->mc);
181 }