1 | /* |
---|
2 | * neercs console-based window manager |
---|
3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
---|
4 | * 2008 Jean-Yves Lamoureux <jylam@lnxscene.org> |
---|
5 | * All Rights Reserved |
---|
6 | * |
---|
7 | * $Id: wm.c 4021 2009-11-23 12:52:07Z pterjan $ |
---|
8 | * |
---|
9 | * This program is free software. It comes without any warranty, to |
---|
10 | * the extent permitted by applicable law. You can redistribute it |
---|
11 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
12 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
13 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
14 | */ |
---|
15 | |
---|
16 | #include "config.h" |
---|
17 | |
---|
18 | #include <stdio.h> |
---|
19 | #include <caca.h> |
---|
20 | #include <caca.h> |
---|
21 | #include <stdlib.h> |
---|
22 | #include <math.h> |
---|
23 | |
---|
24 | #include "neercs.h" |
---|
25 | |
---|
26 | |
---|
27 | void resize_screen(struct screen *s, int w, int h) |
---|
28 | { |
---|
29 | caca_canvas_t *old, *new; |
---|
30 | |
---|
31 | if (w == s->w && h == s->h) |
---|
32 | return; |
---|
33 | if (w <= 0 || h <= 0) |
---|
34 | return; |
---|
35 | |
---|
36 | s->changed = 1; |
---|
37 | |
---|
38 | s->w = w; |
---|
39 | s->h = h; |
---|
40 | |
---|
41 | /* |
---|
42 | * caca_set_canvas_boundaries() is bugged as hell, so let's resize it by |
---|
43 | * hands |
---|
44 | */ |
---|
45 | old = s->cv; |
---|
46 | new = caca_create_canvas(w, h); |
---|
47 | caca_blit(new, 0, 0, old, NULL); |
---|
48 | s->cv = new; |
---|
49 | caca_gotoxy(new, caca_get_cursor_x(old), caca_get_cursor_y(old)); |
---|
50 | caca_free_canvas(old); |
---|
51 | set_tty_size(s->fd, w, h); |
---|
52 | |
---|
53 | s->orig_w = s->w; |
---|
54 | s->orig_h = s->h; |
---|
55 | s->orig_x = s->x; |
---|
56 | s->orig_y = s->y; |
---|
57 | } |
---|
58 | |
---|
59 | void update_windows_props(struct screen_list *screen_list) |
---|
60 | { |
---|
61 | debug("%s, %d screens, type %d\n", __FUNCTION__, screen_list->count, |
---|
62 | screen_list->wm_type); |
---|
63 | |
---|
64 | if (!screen_list->count) |
---|
65 | return; |
---|
66 | |
---|
67 | switch (screen_list->wm_type) |
---|
68 | { |
---|
69 | case WM_CARD: |
---|
70 | update_windows_props_cards(screen_list); |
---|
71 | break; |
---|
72 | case WM_HSPLIT: |
---|
73 | update_windows_props_hsplit(screen_list); |
---|
74 | break; |
---|
75 | case WM_VSPLIT: |
---|
76 | update_windows_props_vsplit(screen_list); |
---|
77 | break; |
---|
78 | case WM_FULL: |
---|
79 | default: |
---|
80 | update_windows_props_full(screen_list); |
---|
81 | break; |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | void update_windows_props_hsplit(struct screen_list *screen_list) |
---|
86 | { |
---|
87 | int i; |
---|
88 | int w = |
---|
89 | (screen_list->width / screen_list->count) - |
---|
90 | (screen_list->border_size * 2); |
---|
91 | int h = screen_list->height - (screen_list->border_size * 2); |
---|
92 | |
---|
93 | for (i = 0; i < screen_list->count; i++) |
---|
94 | { |
---|
95 | screen_list->screen[i]->x = (i * w) + screen_list->border_size; |
---|
96 | screen_list->screen[i]->y = screen_list->border_size; |
---|
97 | screen_list->screen[i]->visible = 1; |
---|
98 | if (i != screen_list->count - 1) |
---|
99 | { |
---|
100 | resize_screen(screen_list->screen[i], w - 1, h); |
---|
101 | } |
---|
102 | else |
---|
103 | { |
---|
104 | resize_screen(screen_list->screen[i], |
---|
105 | screen_list->width - i * w - 2, h); |
---|
106 | } |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | void update_windows_props_vsplit(struct screen_list *screen_list) |
---|
111 | { |
---|
112 | int i; |
---|
113 | int w = screen_list->width - (screen_list->border_size * 2); |
---|
114 | int h = (screen_list->height) / screen_list->count; |
---|
115 | |
---|
116 | for (i = 0; i < screen_list->count; i++) |
---|
117 | { |
---|
118 | screen_list->screen[i]->x = screen_list->border_size; |
---|
119 | screen_list->screen[i]->y = (i * h) + (screen_list->border_size); |
---|
120 | screen_list->screen[i]->visible = 1; |
---|
121 | if (i != screen_list->count - 1) |
---|
122 | { |
---|
123 | resize_screen(screen_list->screen[i], w, |
---|
124 | h - (screen_list->border_size * 2)); |
---|
125 | } |
---|
126 | else |
---|
127 | { |
---|
128 | resize_screen(screen_list->screen[i], |
---|
129 | w, |
---|
130 | screen_list->height - i * h - |
---|
131 | (screen_list->border_size * 2)); |
---|
132 | } |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | |
---|
137 | void update_windows_props_full(struct screen_list *screen_list) |
---|
138 | { |
---|
139 | int i; |
---|
140 | int w = screen_list->width - (screen_list->border_size * 2); |
---|
141 | int h = screen_list->height - (screen_list->border_size * 2); |
---|
142 | |
---|
143 | for (i = 0; i < screen_list->count; i++) |
---|
144 | { |
---|
145 | screen_list->screen[i]->visible = 0; |
---|
146 | screen_list->screen[i]->x = screen_list->border_size; |
---|
147 | screen_list->screen[i]->y = screen_list->border_size; |
---|
148 | |
---|
149 | resize_screen(screen_list->screen[i], w, h); |
---|
150 | } |
---|
151 | screen_list->screen[screen_list->pty]->visible = 1; |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | void update_windows_props_cards(struct screen_list *screen_list) |
---|
156 | { |
---|
157 | int i; |
---|
158 | int w = (screen_list->width - screen_list->count * 3) + 1; |
---|
159 | int h = (screen_list->height - screen_list->count) - 1; |
---|
160 | int x = 1; |
---|
161 | int y = screen_list->count; |
---|
162 | |
---|
163 | for (i = 0; i < screen_list->count; i++) |
---|
164 | { |
---|
165 | screen_list->screen[i]->visible = 1; |
---|
166 | screen_list->screen[i]->x = x; |
---|
167 | screen_list->screen[i]->y = y; |
---|
168 | |
---|
169 | resize_screen(screen_list->screen[i], w, h); |
---|
170 | x += 3; |
---|
171 | y--; |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | /* Window managers refresh */ |
---|
176 | |
---|
177 | void wm_refresh(struct screen_list *screen_list) |
---|
178 | { |
---|
179 | /* FIXME : move set_color to a relevant place */ |
---|
180 | caca_set_color_ansi(screen_list->cv, CACA_LIGHTRED, CACA_BLACK); |
---|
181 | |
---|
182 | switch (screen_list->wm_type) |
---|
183 | { |
---|
184 | case WM_CARD: |
---|
185 | wm_refresh_card(screen_list); |
---|
186 | break; |
---|
187 | case WM_HSPLIT: |
---|
188 | wm_refresh_hsplit(screen_list); |
---|
189 | break; |
---|
190 | case WM_VSPLIT: |
---|
191 | wm_refresh_hsplit(screen_list); |
---|
192 | break; |
---|
193 | case WM_FULL: |
---|
194 | default: |
---|
195 | wm_refresh_cube(screen_list); |
---|
196 | break; |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | static void wm_bell(struct screen_list *screen_list) |
---|
201 | { |
---|
202 | if (screen_list->screen[screen_list->pty]->bell) |
---|
203 | { |
---|
204 | caca_set_color_ansi(screen_list->cv, CACA_RED, CACA_BLACK); |
---|
205 | screen_list->screen[screen_list->pty]->bell = 0; |
---|
206 | screen_list->in_bell--; |
---|
207 | } |
---|
208 | else |
---|
209 | { |
---|
210 | caca_set_color_ansi(screen_list->cv, CACA_LIGHTGREEN, CACA_BLACK); |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | static void wm_box(struct screen_list *screen_list, int pty) |
---|
215 | { |
---|
216 | if (!screen_list->screen[pty]->changed |
---|
217 | && !screen_list->changed) |
---|
218 | return; |
---|
219 | |
---|
220 | if (!screen_list->border_size) |
---|
221 | return; |
---|
222 | |
---|
223 | caca_draw_cp437_box(screen_list->cv, |
---|
224 | screen_list->screen[pty]->x - 1, |
---|
225 | screen_list->screen[pty]->y - 1, |
---|
226 | screen_list->screen[pty]->w + 2, |
---|
227 | screen_list->screen[pty]->h + 2); |
---|
228 | |
---|
229 | if (screen_list->screen[pty]->title) |
---|
230 | { |
---|
231 | caca_printf(screen_list->cv, |
---|
232 | screen_list->screen[pty]->x, |
---|
233 | screen_list->screen[pty]->y - 1, |
---|
234 | " %.*s ", |
---|
235 | screen_list->screen[pty]->w - 3, |
---|
236 | screen_list->screen[pty]->title); |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | static void wm_blit_current_screen(struct screen_list *screen_list) |
---|
241 | { |
---|
242 | if (screen_list->screen[screen_list->pty]->changed || screen_list->changed) |
---|
243 | caca_blit(screen_list->cv, |
---|
244 | screen_list->screen[screen_list->pty]->x, |
---|
245 | screen_list->screen[screen_list->pty]->y, |
---|
246 | screen_list->screen[screen_list->pty]->cv, NULL); |
---|
247 | } |
---|
248 | |
---|
249 | void wm_refresh_card(struct screen_list *screen_list) |
---|
250 | { |
---|
251 | int i; |
---|
252 | |
---|
253 | for (i = screen_list->count - 1; i >= 0; i--) |
---|
254 | { |
---|
255 | if (i != screen_list->pty && screen_list->screen[i]->visible && |
---|
256 | (screen_list->screen[i]->changed || screen_list->changed)) |
---|
257 | { |
---|
258 | caca_blit(screen_list->cv, |
---|
259 | screen_list->screen[i]->x, |
---|
260 | screen_list->screen[i]->y, |
---|
261 | screen_list->screen[i]->cv, NULL); |
---|
262 | |
---|
263 | wm_box(screen_list, i); |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | /* Force 'changed' to force redraw */ |
---|
268 | screen_list->screen[screen_list->pty]->changed = 1; |
---|
269 | wm_blit_current_screen(screen_list); |
---|
270 | wm_bell(screen_list); |
---|
271 | wm_box(screen_list, screen_list->pty); |
---|
272 | } |
---|
273 | |
---|
274 | void wm_refresh_full(struct screen_list *screen_list) |
---|
275 | { |
---|
276 | wm_blit_current_screen(screen_list); |
---|
277 | wm_bell(screen_list); |
---|
278 | wm_box(screen_list, screen_list->pty); |
---|
279 | } |
---|
280 | |
---|
281 | void wm_refresh_vsplit(struct screen_list *screen_list) |
---|
282 | { |
---|
283 | int i; |
---|
284 | |
---|
285 | for (i = screen_list->count - 1; i >= 0; i--) |
---|
286 | { |
---|
287 | if (i != screen_list->pty && screen_list->screen[i]->visible && |
---|
288 | (screen_list->screen[i]->changed || screen_list->changed)) |
---|
289 | { |
---|
290 | caca_blit(screen_list->cv, |
---|
291 | screen_list->screen[i]->x, |
---|
292 | screen_list->screen[i]->y, |
---|
293 | screen_list->screen[i]->cv, NULL); |
---|
294 | |
---|
295 | wm_box(screen_list, i); |
---|
296 | } |
---|
297 | } |
---|
298 | |
---|
299 | wm_blit_current_screen(screen_list); |
---|
300 | wm_bell(screen_list); |
---|
301 | wm_box(screen_list, screen_list->pty); |
---|
302 | } |
---|
303 | |
---|
304 | void wm_refresh_hsplit(struct screen_list *screen_list) |
---|
305 | { |
---|
306 | int i; |
---|
307 | |
---|
308 | for (i = screen_list->count - 1; i >= 0; i--) |
---|
309 | { |
---|
310 | if (i != screen_list->pty && screen_list->screen[i]->visible && |
---|
311 | (screen_list->screen[i]->changed || screen_list->changed)) |
---|
312 | { |
---|
313 | caca_blit(screen_list->cv, |
---|
314 | screen_list->screen[i]->x, |
---|
315 | screen_list->screen[i]->y, |
---|
316 | screen_list->screen[i]->cv, NULL); |
---|
317 | |
---|
318 | wm_box(screen_list, i); |
---|
319 | } |
---|
320 | } |
---|
321 | |
---|
322 | wm_blit_current_screen(screen_list); |
---|
323 | wm_bell(screen_list); |
---|
324 | wm_box(screen_list, screen_list->pty); |
---|
325 | } |
---|
326 | |
---|
327 | |
---|
328 | static float |
---|
329 | get_direction(float p1x, float p1y, float p2x, float p2y, float p3x, float p3y) |
---|
330 | { |
---|
331 | float d1x, d1y, d2x, d2y; |
---|
332 | |
---|
333 | d1x = p3x - p1x; |
---|
334 | d1y = p3y - p1y; |
---|
335 | d2x = p3x - p2x; |
---|
336 | d2y = p3y - p2y; |
---|
337 | return (d1x * d2y) - (d1y * d2x); |
---|
338 | } |
---|
339 | |
---|
340 | |
---|
341 | /* 3D Cube. Yeah I know, it's a mess. Just look anywhere else. */ |
---|
342 | static void draw_face(caca_canvas_t * cv, |
---|
343 | int p1x, int p1y, |
---|
344 | int p2x, int p2y, |
---|
345 | int p3x, int p3y, |
---|
346 | int p4x, int p4y, caca_canvas_t * tex, int color) |
---|
347 | { |
---|
348 | if (get_direction(p1x, p1y, p2x, p2y, p3x, p3y) >= 0) |
---|
349 | { |
---|
350 | int coords[6]; |
---|
351 | float uv[6]; |
---|
352 | coords[0] = p1x; |
---|
353 | coords[1] = p1y; |
---|
354 | coords[2] = p2x; |
---|
355 | coords[3] = p2y; |
---|
356 | coords[4] = p3x; |
---|
357 | coords[5] = p3y; |
---|
358 | uv[0] = 1; |
---|
359 | uv[1] = 1; |
---|
360 | uv[2] = 0; |
---|
361 | uv[3] = 1; |
---|
362 | uv[4] = 0; |
---|
363 | uv[5] = 0; |
---|
364 | #if defined HAVE_CACA_TRIANGLE_TEXTURING |
---|
365 | caca_fill_triangle_textured(cv, coords, tex, uv); |
---|
366 | #endif |
---|
367 | coords[0] = p1x; |
---|
368 | coords[1] = p1y; |
---|
369 | coords[2] = p3x; |
---|
370 | coords[3] = p3y; |
---|
371 | coords[4] = p4x; |
---|
372 | coords[5] = p4y; |
---|
373 | uv[0] = 1; |
---|
374 | uv[1] = 1; |
---|
375 | uv[2] = 0; |
---|
376 | uv[3] = 0; |
---|
377 | uv[4] = 1; |
---|
378 | uv[5] = 0; |
---|
379 | #if defined HAVE_CACA_TRIANGLE_TEXTURING |
---|
380 | caca_fill_triangle_textured(cv, coords, tex, uv); |
---|
381 | #endif |
---|
382 | caca_set_color_ansi(cv, color, CACA_BLACK); |
---|
383 | caca_draw_thin_line(cv, p1x, p1y, p2x, p2y); |
---|
384 | caca_draw_thin_line(cv, p2x, p2y, p3x, p3y); |
---|
385 | caca_draw_thin_line(cv, p3x, p3y, p4x, p4y); |
---|
386 | caca_draw_thin_line(cv, p4x, p4y, p1x, p1y); |
---|
387 | } |
---|
388 | } |
---|
389 | |
---|
390 | |
---|
391 | void wm_refresh_cube(struct screen_list *screen_list) |
---|
392 | { |
---|
393 | int i; |
---|
394 | |
---|
395 | if (!screen_list->cube.in_switch || !screen_list->eyecandy) |
---|
396 | { |
---|
397 | wm_refresh_full(screen_list); |
---|
398 | screen_list->force_refresh = 0; |
---|
399 | } |
---|
400 | else |
---|
401 | { |
---|
402 | long long unsigned int cur_time = get_us() - screen_list->last_switch; |
---|
403 | |
---|
404 | if (cur_time >= screen_list->cube.duration || screen_list->count == 1) |
---|
405 | { |
---|
406 | screen_list->changed = 1; |
---|
407 | screen_list->force_refresh = 1; |
---|
408 | screen_list->cube.in_switch = 0; |
---|
409 | } |
---|
410 | else |
---|
411 | { |
---|
412 | float cube[12][3] = { |
---|
413 | {-1, -1, 1}, |
---|
414 | {1, -1, 1}, |
---|
415 | {1, 1, 1}, |
---|
416 | {-1, 1, 1}, |
---|
417 | |
---|
418 | {1, -1, 1}, |
---|
419 | {1, -1, -1}, |
---|
420 | {1, 1, -1}, |
---|
421 | {1, 1, 1}, |
---|
422 | |
---|
423 | {-1, -1, -1}, |
---|
424 | {-1, -1, 1}, |
---|
425 | {-1, 1, 1}, |
---|
426 | {-1, 1, -1}, |
---|
427 | }; |
---|
428 | |
---|
429 | float cube_transformed[12][3]; |
---|
430 | float cube_projected[12][2]; |
---|
431 | float fov = 0.5f; |
---|
432 | float angle = |
---|
433 | 90.0f * ((float)cur_time / (float)screen_list->cube.duration); |
---|
434 | |
---|
435 | angle *= (M_PI / 180.0f); |
---|
436 | |
---|
437 | if (screen_list->cube.side == 1) |
---|
438 | angle = -angle; |
---|
439 | |
---|
440 | float sina = sin(angle); |
---|
441 | float cosa = cos(angle); |
---|
442 | |
---|
443 | for (i = 0; i < 12; i++) |
---|
444 | { |
---|
445 | cube_transformed[i][2] = cube[i][2] * cosa - cube[i][0] * sina; |
---|
446 | cube_transformed[i][0] = cube[i][2] * sina + cube[i][0] * cosa; |
---|
447 | cube_transformed[i][1] = cube[i][1]; |
---|
448 | |
---|
449 | cube_transformed[i][2] -= 3; |
---|
450 | |
---|
451 | cube_projected[i][0] = |
---|
452 | cube_transformed[i][0] / (cube_transformed[i][2] * fov); |
---|
453 | cube_projected[i][1] = |
---|
454 | cube_transformed[i][1] / (cube_transformed[i][2] * fov); |
---|
455 | |
---|
456 | cube_projected[i][0] /= 2.0f; |
---|
457 | cube_projected[i][1] /= 2.0f; |
---|
458 | cube_projected[i][0] += 0.5f; |
---|
459 | cube_projected[i][1] += 0.5f; |
---|
460 | |
---|
461 | cube_projected[i][0] *= screen_list->width; |
---|
462 | cube_projected[i][1] *= screen_list->height; |
---|
463 | } |
---|
464 | |
---|
465 | caca_set_color_ansi(screen_list->cv, CACA_WHITE, CACA_BLACK); |
---|
466 | caca_clear_canvas(screen_list->cv); |
---|
467 | |
---|
468 | caca_canvas_t *first = |
---|
469 | screen_list->screen[screen_list->prevpty]->cv; |
---|
470 | caca_canvas_t *second = screen_list->screen[screen_list->pty]->cv; |
---|
471 | |
---|
472 | draw_face(screen_list->cv, |
---|
473 | cube_projected[0][0], cube_projected[0][1], |
---|
474 | cube_projected[1][0], cube_projected[1][1], |
---|
475 | cube_projected[2][0], cube_projected[2][1], |
---|
476 | cube_projected[3][0], cube_projected[3][1], |
---|
477 | first, CACA_LIGHTGREEN); |
---|
478 | |
---|
479 | |
---|
480 | if (screen_list->cube.side) |
---|
481 | { |
---|
482 | draw_face(screen_list->cv, |
---|
483 | cube_projected[4][0], cube_projected[4][1], |
---|
484 | cube_projected[5][0], cube_projected[5][1], |
---|
485 | cube_projected[6][0], cube_projected[6][1], |
---|
486 | cube_projected[7][0], cube_projected[7][1], |
---|
487 | second, CACA_RED); |
---|
488 | } |
---|
489 | else |
---|
490 | { |
---|
491 | draw_face(screen_list->cv, |
---|
492 | cube_projected[8][0], cube_projected[8][1], |
---|
493 | cube_projected[9][0], cube_projected[9][1], |
---|
494 | cube_projected[10][0], cube_projected[10][1], |
---|
495 | cube_projected[11][0], cube_projected[11][1], |
---|
496 | second, CACA_RED); |
---|
497 | } |
---|
498 | |
---|
499 | screen_list->changed = 1; |
---|
500 | screen_list->force_refresh = 1; |
---|
501 | screen_list->cube.in_switch = 1; |
---|
502 | } |
---|
503 | } |
---|
504 | } |
---|