]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/paint.c
webfs: close idle connections after 5 seconds
[plan9front.git] / sys / src / cmd / paint.c
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <event.h>
5
6 #define NCOLORS 6
7
8 Image   *colors[NCOLORS];
9
10 void
11 eresized(int)
12 {
13         if(getwindow(display, Refnone) < 0)
14                 sysfatal("resize failed");
15 }
16
17 int
18 loadimg(char *name)
19 {
20         Image *b;
21         int fd;
22
23         if((fd = open(name, OREAD)) < 0)
24                 return -1;
25         if((b = readimage(display, fd, 0)) == nil){
26                 close(fd);
27                 return -1;
28         }
29         draw(screen, screen->r, b, 0, b->r.min);
30         flushimage(display, 1);
31         freeimage(b);
32         close(fd);
33         return 0;
34 }
35
36 int
37 saveimg(char *name)
38 {
39         int fd;
40
41         if((fd = create(name, OWRITE|OTRUNC, 0666)) < 0)
42                 return -1;
43         writeimage(fd, screen, 0);
44         close(fd);
45         return 0;
46 }
47
48 void
49 main(int argc, char *argv[])
50 {
51         Event e;
52         Point last;
53         int b = 1;
54         int c = 0;
55         int cn, f;
56         int haslast = 0;
57         char brush[128];
58         char color[NCOLORS];
59         char file[128];
60         char fill[NCOLORS];
61         
62         if(initdraw(0, 0, "paint") < 0){
63                 fprint(2, "paint: initdraw failed: %r\n");
64                 exits("initdraw");
65         }
66         einit(Emouse | Ekeyboard);
67         draw(screen, screen->r, display->white, 0, ZP);
68         flushimage(display, 1);
69
70         colors[0] = display->black;
71         colors[1] = display->white;
72         colors[2] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DRed);
73         colors[3] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DGreen);
74         colors[4] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DBlue);
75         colors[5] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DYellow);
76
77         ARGBEGIN{
78         default:
79                 goto Usage;
80         }ARGEND
81         switch(argc){
82         default:
83         Usage:
84                 fprint(2, "Usage: [file]\n");
85                 exits("usage");
86         case 0:
87                 break;
88         case 1:
89                 strncpy(file, argv[0], sizeof(argv[0]));
90                 if(loadimg(file) < 0)
91                         sysfatal("%r");
92                 break;
93         }
94
95         while(1){
96                 switch(event(&e)){
97                 case Emouse:
98                         if(e.mouse.buttons & 1){
99                                 if(haslast)
100                                         line(screen, last, e.mouse.xy, Enddisc, Enddisc, b, colors[c], ZP);
101                                 else
102                                         fillellipse(screen, e.mouse.xy, b, b, colors[c], ZP);
103                                 last = e.mouse.xy;
104                                 haslast = 1;
105                                 flushimage(display, 1);
106                         } else
107                                 haslast = 0;
108                         break;
109                 case Ekeyboard:
110                         if(e.kbdc == 'b'){
111                                 if(eenter("Brush", brush, sizeof(brush), &e.mouse) <= 0)
112                                         break;
113                                 b = atoi(brush);
114                         }
115                         if(e.kbdc == 'c'){
116                                 if(eenter("Color", color, sizeof(color), &e.mouse) <= 0)
117                                         break;
118                                 cn = atoi(color);
119                                 if(cn >= 0 && cn < NCOLORS)
120                                         c = cn;
121                         }
122                         if(e.kbdc == 'f'){
123                                 if(eenter("Fill", fill, sizeof(fill), &e.mouse) <= 0)
124                                         break;
125                                 f = atoi(fill);
126                                 if(f >= 0 && f < NCOLORS)
127                                         draw(screen, screen->r, colors[f], 0, ZP);
128                         }
129                         if(e.kbdc == 'o'){
130                                 if(eenter("Open file", file, sizeof(file), &e.mouse) <= 0)
131                                         break;
132                                 if(loadimg(file) < 0){
133                                         rerrstr(file, sizeof(file));
134                                         eenter(file, 0, 0, &e.mouse);
135                                 }
136                         }
137                         if(e.kbdc == 'q')
138                                 exits(nil);
139                         if(e.kbdc == 's'){
140                                 if(eenter("Save to", file, sizeof(file), &e.mouse) <= 0)
141                                         break;
142                                 if(saveimg(file) < 0){
143                                         rerrstr(file, sizeof(file));
144                                         eenter(file, 0, 0, &e.mouse);
145                                 }
146                         }
147                         break;
148                 }
149         }
150 }