]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libdraw/window.c
?a: getc() needs to increment lineno if it gets \n from peekc
[plan9front.git] / sys / src / libdraw / window.c
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4
5 typedef struct Memimage Memimage;
6
7 static int      screenid;
8
9 Screen*
10 allocscreen(Image *image, Image *fill, int public)
11 {
12         uchar *a;
13         Screen *s;
14         int id, try;
15         Display *d;
16
17         d = image->display;
18         if(d != fill->display){
19                 werrstr("allocscreen: image and fill on different displays");
20                 return nil;
21         }
22         s = malloc(sizeof(Screen));
23         if(s == nil)
24                 return nil;
25         if(!screenid)
26                 screenid = getpid();
27         for(try=0; try<25; try++){
28                 /* loop until find a free id */
29                 a = bufimage(d, 1+4+4+4+1);
30                 if(a == nil)
31                         break;
32                 id = ++screenid & 0xffff;       /* old devdraw bug */
33                 a[0] = 'A';
34                 BPLONG(a+1, id);
35                 BPLONG(a+5, image->id);
36                 BPLONG(a+9, fill->id);
37                 a[13] = public;
38                 if(flushimage(d, 0) != -1)
39                         goto Found;
40         }
41         free(s);
42         return nil;
43
44     Found:
45         s->display = d;
46         s->id = id;
47         s->image = image;
48         assert(s->image != nil && s->image->chan != 0);
49
50         s->fill = fill;
51         return s;
52 }
53
54 Screen*
55 publicscreen(Display *d, int id, ulong chan)
56 {
57         uchar *a;
58         Screen *s;
59
60         s = malloc(sizeof(Screen));
61         if(s == nil)
62                 return nil;
63         a = bufimage(d, 1+4+4);
64         if(a == nil){
65 Error:
66                 free(s);
67                 return nil;
68         }
69         a[0] = 'S';
70         BPLONG(a+1, id);
71         BPLONG(a+5, chan);
72         if(flushimage(d, 0) < 0)
73                 goto Error;
74
75         s->display = d;
76         s->id = id;
77         s->image = nil;
78         s->fill = nil;
79         return s;
80 }
81
82 int
83 freescreen(Screen *s)
84 {
85         uchar *a;
86         Display *d;
87
88         if(s == nil)
89                 return 0;
90         d = s->display;
91         a = bufimage(d, 1+4);
92         if(a == nil){
93 Error:
94                 free(s);
95                 return -1;
96         }
97         a[0] = 'F';
98         BPLONG(a+1, s->id);
99         /*
100          * flush(1) because screen is likely holding last reference to
101          * window, and want it to disappear visually.
102          */
103         if(flushimage(d, 1) < 0)
104                 goto Error;
105         free(s);
106         return 1;
107 }
108
109 Image*
110 allocwindow(Screen *s, Rectangle r, int ref, ulong col)
111 {
112         return _allocwindow(nil, s, r, ref, col);
113 }
114
115 Image*
116 _allocwindow(Image *i, Screen *s, Rectangle r, int ref, ulong col)
117 {
118         Display *d;
119
120         d = s->display;
121         i = _allocimage(i, d, r, d->screenimage->chan, 0, col, s->id, ref);
122         if(i == nil)
123                 return nil;
124         i->screen = s;
125         i->next = s->display->windows;
126         s->display->windows = i;
127         return i;
128 }
129
130 static
131 void
132 topbottom(Image **w, int n, int top)
133 {
134         int i;
135         uchar *b;
136         Display *d;
137
138         if(n < 0){
139     Ridiculous:
140                 fprint(2, "top/bottom: ridiculous number of windows\n");
141                 return;
142         }
143         if(n == 0)
144                 return;
145         if(n > (w[0]->display->bufsize-100)/4)
146                 goto Ridiculous;
147         /*
148          * this used to check that all images were on the same screen.
149          * we don't know the screen associated with images we acquired
150          * by name.  instead, check that all images are on the same display.
151          * the display will check that they are all on the same screen.
152          */
153         d = w[0]->display;
154         for(i=1; i<n; i++)
155                 if(w[i]->display != d){
156                         fprint(2, "top/bottom: windows not on same screen\n");
157                         return;
158                 }
159
160         if(n==0)
161                 return;
162         b = bufimage(d, 1+1+2+4*n);
163         if(b == nil)
164                 return;
165         b[0] = 't';
166         b[1] = top;
167         BPSHORT(b+2, n);
168         for(i=0; i<n; i++)
169                 BPLONG(b+4+4*i, w[i]->id);
170 }
171
172 void
173 bottomwindow(Image *w)
174 {
175         if(w->screen != nil)
176                 topbottom(&w, 1, 0);
177 }
178
179 void
180 topwindow(Image *w)
181 {
182         if(w->screen != nil)
183                 topbottom(&w, 1, 1);
184 }
185
186 void
187 bottomnwindows(Image **w, int n)
188 {
189         topbottom(w, n, 0);
190 }
191
192 void
193 topnwindows(Image **w, int n)
194 {
195         topbottom(w, n, 1);
196 }
197
198 int
199 originwindow(Image *w, Point log, Point scr)
200 {
201         uchar *b;
202         Point delta;
203
204         b = bufimage(w->display, 1+4+2*4+2*4);
205         if(b == nil)
206                 return 0;
207         b[0] = 'o';
208         BPLONG(b+1, w->id);
209         BPLONG(b+5, log.x);
210         BPLONG(b+9, log.y);
211         BPLONG(b+13, scr.x);
212         BPLONG(b+17, scr.y);
213         delta = subpt(log, w->r.min);
214         w->r = rectaddpt(w->r, delta);
215         w->clipr = rectaddpt(w->clipr, delta);
216         return 1;
217 }