]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/spred/spred.c
exec(2): fix prototypes
[plan9front.git] / sys / src / cmd / spred / spred.c
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <thread.h>
5 #include <draw.h>
6 #include <mouse.h>
7 #include <keyboard.h>
8 #include <frame.h>
9 #include <cursor.h>
10 #include "dat.h"
11 #include "fns.h"
12
13 Mousectl *mc;
14 Keyboardctl *kc;
15 int quitok;
16
17 enum {
18         ZEROX,
19         RESIZE,
20         CLOSE,
21         WRITE,
22         QUIT,
23         WIN
24 };
25
26 int 
27 quit(void)
28 {
29         File *f;
30         
31         if(!quitok)
32                 for(f = flist.next; f != &flist; f = f->next)
33                         if(f->change > 0){
34                                 cmdprint("?\n");
35                                 quitok = 1;
36                                 return 0;
37                         }
38         return -1;
39 }
40
41 static char *
42 menugen(int n)
43 {
44         File *f;
45         static int mw;
46         static char buf[512];
47         int rc;
48         char *p;
49
50         switch(n){
51         case ZEROX: return "zerox";
52         case CLOSE: return "close";
53         case RESIZE: return "resize";
54         case WRITE: return "write";
55         case QUIT: return "quit";
56         }
57         if(n < WIN)
58                 sysfatal("menugen: no string for n=%d", n);
59         n -= WIN;
60         if(n == 0){
61                 mw = 0;
62                 for(f = flist.next; f != &flist; f = f->next){
63                         rc = filtitlelen(f);
64                         if(rc > mw)
65                                 mw = rc;
66                 }
67                 return "~~spred~~";
68         }
69         for(f = flist.next; f != &flist; f = f->next)
70                 if(--n == 0){
71                         p = filtitle(f, buf, buf + sizeof(buf));
72                         rc = mw - utflen(buf);
73                         if(p + rc >= buf + sizeof(buf))
74                                 rc = buf + sizeof(buf) - p - 1;
75                         memset(p, ' ', rc);
76                         p[rc] = 0;
77                         return buf;
78                 }
79         return nil;
80         
81 }
82
83 static int
84 rmb(void)
85 {
86         static Menu menu = {nil, menugen};
87         int n;
88         Win *w;
89         File *f;
90
91         if(actw != nil && actw->tab->rmb != nil && actw->tab->rmb(actw, mc) >= 0)
92                 return 0;
93         n = menuhit(3, mc, &menu, nil);
94         if(n < 0)
95                 return 0;
96         switch(n){
97         case ZEROX:
98                 w = winsel(mc, 3);
99                 if(w != nil)
100                         winzerox(w, mc);
101                 return 0;
102         case CLOSE:
103                 w = winsel(mc, 3);
104                 if(w != nil)
105                         winclose(w);
106                 return 0;
107         case RESIZE:
108                 winresize(winsel(mc, 3), mc);
109                 return 0;
110         case WRITE:
111                 w = winsel(mc, 3);
112                 if(w != nil)
113                         winwrite(w, nil);
114                 return 0;
115         case QUIT:
116                 return quit();
117         }
118         if(n < WIN)
119                 sysfatal("rmb: no action for n=%d", n);
120         if(n == 0){
121                 setfocus(cmdw);
122                 return 0;
123         }
124         n -= WIN;
125         for(f = flist.next; f != &flist; f = f->next)
126                 if(--n == 0){
127                         if(f->wins.wnext == &f->wins){
128                                 newwinsel(f->type, mc, f);
129                                 return 0;
130                         }
131                         for(w = f->wins.wnext; w != &f->wins && w != actw; w = w->wnext)
132                                 ;
133                         if(w->wnext == &f->wins)
134                                 w = w->wnext;
135                         setfocus(w->wnext);
136                         return 0;
137                 }
138         return 0;
139 }
140
141 static void
142 loop(void)
143 {
144         Rune r;
145         int n;
146
147         Alt a[] = {
148                 {mc->c, &mc->Mouse, CHANRCV},
149                 {kc->c, &r, CHANRCV},
150                 {mc->resizec, &n, CHANRCV},
151                 {nil, nil, CHANEND}
152         };
153         
154         for(;;){
155                 flushimage(display, 1);
156                 switch(alt(a)){
157                 case 0:
158                         if((mc->buttons & 1) != 0)
159                                 winclick(mc);
160                         if((mc->buttons & 2) != 0)
161                                 if(actw != nil && actw->tab->menu != nil)
162                                         actw->tab->menu(actw, mc);
163                         if((mc->buttons & 4) != 0)
164                                 if(rmb() < 0)
165                                         return;
166                         break;
167                 case 1:
168                         if(actw != nil && actw->tab->key != nil)
169                                 actw->tab->key(actw, r);
170                         break;
171                 case 2:
172                         resize();
173                         break;
174                 }
175         }
176 }
177
178 void
179 threadmain(int argc, char **argv)
180 {
181         ARGBEGIN {
182         default:
183                 ;
184         } ARGEND;
185         
186         quotefmtinstall();
187         if(initdraw(nil, nil, nil) < 0)
188                 sysfatal("initdraw: %r");
189         initwin();
190         mc = initmouse(nil, screen);
191         if(mc == nil)
192                 sysfatal("initmouse: %r");
193         kc = initkeyboard(nil);
194         if(kc == nil)
195                 sysfatal("initkeyboard: %r");
196         loop();
197         threadexitsall(nil);
198 }
199
200 Cursor crosscursor = {
201         {-7, -7},
202         {0x03, 0xC0, 0x03, 0xC0, 0x03, 0xC0, 0x03, 0xC0,
203          0x03, 0xC0, 0x03, 0xC0, 0xFF, 0xFF, 0xFF, 0xFF,
204          0xFF, 0xFF, 0xFF, 0xFF, 0x03, 0xC0, 0x03, 0xC0,
205          0x03, 0xC0, 0x03, 0xC0, 0x03, 0xC0, 0x03, 0xC0, },
206         {0x00, 0x00, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80,
207          0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x7F, 0xFE,
208          0x7F, 0xFE, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80,
209          0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x00, 0x00, }
210 };