fbui | Framebuffer-based graphical environment |
| download: https://git.y1.nz/archives/fbui.tar.gz | |
| README | Files | Log | Refs |
libfbui/WindowManager/main.c
1
2 /*=========================================================================
3 *
4 * fbwm, a window manager for FBUI (in-kernel framebuffer UI)
5 * Copyright (C) 2004 Zachary T Smith, fbui@comcast.net
6 *
7 * This module is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This module is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this module; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * (See the file COPYING in the main directory of this archive for
22 * more details.)
23 *
24 *=======================================================================*/
25
26 /*
27 * Changes:
28 *
29 * Oct 19, 2004, fbui@comcast.net: got rectangle subtraction working
30 * Oct 19, 2004, fbui@comcast.net: got background image drawing working
31 * Dec 31, 2004, fbui@comcast.net: moved rectangle code to separate file
32 */
33
34
35
36 #include <stdio.h>
37 #include <time.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40 #include <math.h>
41 #include <jpeglib.h>
42 #include <signal.h>
43
44
45 #include "libfbui.h"
46
47
48 short win_w, win_h;
49 #define STEELBLUE 0x4682B4
50
51 extern JSAMPLE * image_buffer;
52 extern int image_height;
53 extern int image_width;
54 extern int image_ncomponents;
55
56 extern int read_JPEG_file (char*);
57
58 extern char grayscale; /* 1 forces bg image to grayscale */
59
60 struct fbui_wininfo info[200];
61 struct fbui_wininfo prev[200];
62 int window_count;
63
64
65 extern void
66 draw_image (Display *dpy, Window *win, short x0, short y0, short x1, short y1);
67
68 extern void
69 draw_background (Display *dpy, Window *win, struct fbui_wininfo *, int);
70
71
72 void
73 update_focus (Display *dpy, Window *self)
74 {
75 int i;
76 for (i=0; i<window_count; i++) {
77 if (info[i].program_type == FBUI_PROGTYPE_APP)
78 fbui_assign_keyfocus (dpy, self, info[i].id);
79 }
80 }
81
82
83 int
84 main(int argc, char** argv)
85 {
86 int mypid,i,j;
87 Display *dpy;
88 Window *self;
89 Font *pcf;
90 short line_height;
91
92 grayscale=0;
93
94 pcf = font_new ();
95 if (!pcf)
96 FATAL ("out o' memory");
97
98 if (!pcf_read (pcf, "timR12.pcf")) {
99 font_free (pcf);
100 FATAL ("cannot read Times 12");
101 }
102
103 line_height = pcf->ascent + pcf->descent;
104
105 long fg,bg=0x404040;
106
107 dpy = fbui_display_open ();
108 if (!dpy)
109 FATAL ("request for control denied");
110
111 self = fbui_window_open (dpy, 1,1, &win_w, &win_h, 9999,9999,
112 0, 0,
113 &fg, &bg,
114 "fbwm", "",
115 FBUI_PROGTYPE_WM,
116 true, // we are a window manager
117 false, // but we are not autopositioning anything
118 -1,
119 false, // we don't need keys
120 false, // don't need all motion
121 false, // not hidden
122 argc,argv);
123 if (!self)
124 FATAL ("cannot open manager window");
125
126 /* Check for a background image */
127 image_width=0;
128 image_height=0;
129 image_ncomponents=0;
130 image_buffer=NULL;
131 char *image_path=NULL;
132 i=1;
133 while (i<argc) {
134 char *s = argv[i];
135 int ch = s ? s[0] : 0;
136 if (ch && '-' != ch) {
137 image_path = argv[i];
138 break;
139 }
140 i++;
141 }
142 if (image_path) {
143 int result;
144 result = read_JPEG_file (image_path);
145 if (image_ncomponents != 3)
146 result=0;
147 if (!result)
148 FATAL ("cannot read background image");
149
150 }
151
152 if (FBUI_SUCCESS != fbui_accelerator (dpy, self, '1', 1))
153 FATAL ("cannot register accelerator");
154 if (FBUI_SUCCESS != fbui_accelerator (dpy, self, '2', 1))
155 FATAL ("cannot register accelerator");
156 if (FBUI_SUCCESS != fbui_accelerator (dpy, self, '3', 1))
157 FATAL ("cannot register accelerator");
158 if (FBUI_SUCCESS != fbui_accelerator (dpy, self, '4', 1))
159 FATAL ("cannot register accelerator");
160 if (FBUI_SUCCESS != fbui_accelerator (dpy, self, '5', 1))
161 FATAL ("cannot register accelerator");
162 if (FBUI_SUCCESS != fbui_accelerator (dpy, self, '6', 1))
163 FATAL ("cannot register accelerator");
164 if (FBUI_SUCCESS != fbui_accelerator (dpy, self, '7', 1))
165 FATAL ("cannot register accelerator");
166 if (FBUI_SUCCESS != fbui_accelerator (dpy, self, '8', 1))
167 FATAL ("cannot register accelerator");
168 if (FBUI_SUCCESS != fbui_accelerator (dpy, self, '9', 1))
169 FATAL ("cannot register accelerator");
170 if (FBUI_SUCCESS != fbui_accelerator (dpy, self, '0', 1))
171 FATAL ("cannot register accelerator");
172
173 /* Alt-tab switches between apps
174 */
175 if (FBUI_SUCCESS != fbui_accelerator (dpy, self, '\t', 1))
176 FATAL ("cannot register accelerator");
177
178 /* Alt-backsp shuts down FBUI on the current VC
179 */
180 if (FBUI_SUCCESS != fbui_accelerator (dpy, self, '\b', 1))
181 FATAL ("cannot register accelerator");
182
183 int need_list = 0;
184 int need_redraw = 0;
185
186 while(1) {
187 Event ev;
188 int err;
189 if (err = fbui_wait_event (dpy, &ev,
190 FBUI_EVENTMASK_ALL - FBUI_EVENTMASK_KEY))
191 {
192 fbui_print_error (err);
193 continue;
194 }
195 int num = ev.type;
196 printf ("%s got event %s\n", argv[0], fbui_get_event_name (ev.type));
197
198 if (ev.win != self) {
199 printf ("ev.win=%08x\n", (unsigned long) ev.win);
200 FATAL ("got event for another win");
201 }
202
203 if (num == FBUI_EVENT_EXPOSE) {
204 need_redraw = 1;
205 need_list = 1;
206 }
207 else if (num == FBUI_EVENT_WINCHANGE) {
208 need_list = 1;
209 }
210 else if (num == FBUI_EVENT_ACCEL) {
211 short key = ev.key;
212 printf ("FBWM accel is %d (0x%04x)\n", key,key);
213 if (key == '\b') {
214 printf ("FBWM got alt-backspace\n");
215 goto done;
216 }
217
218 if (key == '\t') {
219 continue;
220 }
221 }
222
223 if (need_list) {
224 int i;
225 int prev_count = window_count;
226
227 need_redraw = 1;
228
229 for (i=0; i<window_count; i++)
230 prev[i] = info[i];
231
232 window_count = fbui_window_info (dpy, self, &info[0], 200);
233
234 /* determine which windows have disappeared */
235 for (i=0; i<window_count; i++) {
236 int missing=1;
237 int j=0;
238 printf ("%d: window id %d, %s width %d height %d\n",
239 i, info[i].id, info[i].name, info[i].width, info[i].height);
240 while (j < window_count) {
241 if (prev[i].pid == info[j].pid) {
242 missing=0;
243 break;
244 }
245 j++;
246 }
247 if (missing) {
248 draw_image (dpy, self,
249 prev[i].x,
250 prev[i].y - line_height,
251 prev[i].x + prev[i].width - 1,
252 prev[i].y - 1);
253 fbui_flush(dpy, self);
254 }
255 }
256
257 update_focus (dpy, self);
258
259 need_list=0;
260 }
261
262 if (need_redraw) {
263 int i;
264
265 need_redraw = 0;
266
267 // Fill in the gaps
268 draw_background (dpy, self, info, window_count);
269
270 i=0;
271 while (i < window_count) {
272 char tmp[100];
273 struct fbui_wininfo *wi = &info[i];
274 int x,y;
275
276 // locate window
277 x = wi->x;
278 y = wi->y;
279
280 // draw gradient behind text
281 int j=0;
282 while (j < wi->width) {
283 unsigned long r,g,b;
284 int k = (255 - j/2) > 1 ? (255 - j/2) : 0;
285 r = 30;
286 g = 32 + (7*k)/8;
287 b = k/3;
288 r <<= 16;
289 b <<= 8;
290 fbui_draw_vline (dpy, self, x+j, y-1, y-line_height,
291 r|g|b);
292 j++;
293 }
294
295 // draw program name/pid
296 sprintf(tmp,"%s (pid=%d)", wi->name,wi->pid);
297 fbui_draw_string (dpy, self, pcf,x,y-line_height,tmp, RGB_YELLOW);
298
299 i++;
300 }
301 }
302 }
303
304 done:
305 mypid = getpid();
306 for (i=0; i<window_count; i++) {
307 if (info[i].pid != mypid)
308 kill (info[i].pid, SIGTERM);
309 }
310
311 fbui_display_close (dpy);
312 return 0;
313 }
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.