source: neercs/trunk/src/wm.c @ 4008

Last change on this file since 4008 was 4008, checked in by Pascal Terjan, 11 years ago
  • Add a FIXME explaining why we always get refresh now even when nothing changes
  • Property svn:keywords set to Id
File size: 13.7 KB
Line 
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 4008 2009-11-23 00:10:57Z 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
27void 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
59void 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
85void update_windows_props_hsplit(struct screen_list *screen_list)
86{
87    int i;
88    int w = (screen_list->width / screen_list->count) - 1;
89    int h = screen_list->height - 2;
90
91    for (i = 0; i < screen_list->count; i++)
92    {
93        screen_list->screen[i]->x = (i * w) + 1;
94        screen_list->screen[i]->y = 1;
95        screen_list->screen[i]->visible = 1;
96        if (i != screen_list->count - 1)
97        {
98            resize_screen(screen_list->screen[i], w - 1, h);
99        }
100        else
101        {
102            resize_screen(screen_list->screen[i],
103                          screen_list->width - i * w - 2, h);
104        }
105    }
106}
107
108void update_windows_props_vsplit(struct screen_list *screen_list)
109{
110    int i;
111    int w = screen_list->width - 2;
112    int h = (screen_list->height) / screen_list->count;
113
114    for (i = 0; i < screen_list->count; i++)
115    {
116        screen_list->screen[i]->x = 1;
117        screen_list->screen[i]->y = (i * h) + 1;
118        screen_list->screen[i]->visible = 1;
119        if (i != screen_list->count - 1)
120        {
121            resize_screen(screen_list->screen[i], w, h - 2);
122        }
123        else
124        {
125            resize_screen(screen_list->screen[i],
126                          w, screen_list->height - i * h - 2);
127        }
128    }
129}
130
131
132void update_windows_props_full(struct screen_list *screen_list)
133{
134    int i;
135    int w = screen_list->width - 2;
136    int h = screen_list->height - 2;
137
138    for (i = 0; i < screen_list->count; i++)
139    {
140        screen_list->screen[i]->visible = 0;
141        screen_list->screen[i]->x = 1;
142        screen_list->screen[i]->y = 1;
143
144        resize_screen(screen_list->screen[i], w, h);
145    }
146    screen_list->screen[screen_list->pty]->visible = 1;
147}
148
149
150void update_windows_props_cards(struct screen_list *screen_list)
151{
152    int i;
153    int w = (screen_list->width - screen_list->count * 3) + 1;
154    int h = (screen_list->height - screen_list->count) - 1;
155    int x = 1;
156    int y = screen_list->count;
157
158    for (i = 0; i < screen_list->count; i++)
159    {
160        screen_list->screen[i]->visible = 1;
161        screen_list->screen[i]->x = x;
162        screen_list->screen[i]->y = y;
163
164        resize_screen(screen_list->screen[i], w, h);
165        x += 3;
166        y--;
167    }
168}
169
170/* Window managers refresh */
171
172void wm_refresh(struct screen_list *screen_list)
173{
174    switch (screen_list->wm_type)
175    {
176    case WM_CARD:
177        wm_refresh_card(screen_list);
178        break;
179    case WM_HSPLIT:
180        wm_refresh_hsplit(screen_list);
181        break;
182    case WM_VSPLIT:
183        wm_refresh_hsplit(screen_list);
184        break;
185    case WM_FULL:
186    default:
187        wm_refresh_cube(screen_list);
188        break;
189    }
190}
191
192static void wm_bell(struct screen_list *screen_list)
193{
194    if (screen_list->screen[screen_list->pty]->bell)
195    {
196        caca_set_color_ansi(screen_list->cv, CACA_RED, CACA_BLACK);
197        screen_list->screen[screen_list->pty]->bell = 0;
198        screen_list->in_bell--;
199    }
200    else
201    {
202        caca_set_color_ansi(screen_list->cv, CACA_LIGHTGREEN, CACA_BLACK);
203    }
204}
205
206static void wm_box(struct screen_list *screen_list, int pty)
207{
208    /* FIXME drawing the box and rewriting the title creates a dirty rectangle
209     * for the title, even when nothing changed, triggering useless traffic
210     * between server and client.
211     */
212    caca_draw_cp437_box(screen_list->cv,
213                        screen_list->screen[pty]->x - 1,
214                        screen_list->screen[pty]->y - 1,
215                        screen_list->screen[pty]->w + 2,
216                        screen_list->screen[pty]->h + 2);
217
218    if (screen_list->screen[screen_list->pty]->title)
219    {
220        caca_printf(screen_list->cv,
221                    screen_list->screen[pty]->x,
222                    screen_list->screen[pty]->y - 1,
223                    " %.*s ",
224                    screen_list->screen[pty]->w - 3,
225                    screen_list->screen[pty]->title);
226    }
227}
228
229static void wm_blit_current_screen(struct screen_list *screen_list)
230{
231    if (screen_list->screen[screen_list->pty]->changed || screen_list->changed)
232        caca_blit(screen_list->cv,
233                  screen_list->screen[screen_list->pty]->x,
234                  screen_list->screen[screen_list->pty]->y,
235                  screen_list->screen[screen_list->pty]->cv, NULL);
236}
237
238void wm_refresh_card(struct screen_list *screen_list)
239{
240    int i;
241
242    for (i = screen_list->count - 1; i >= 0; i--)
243    {
244        if (i != screen_list->pty && screen_list->screen[i]->visible &&
245            (screen_list->screen[i]->changed || screen_list->changed))
246        {
247            caca_blit(screen_list->cv,
248                      screen_list->screen[i]->x,
249                      screen_list->screen[i]->y,
250                      screen_list->screen[i]->cv, NULL);
251
252            wm_box(screen_list, i);
253        }
254    }
255       
256    /* Force 'changed' to force redraw */
257    screen_list->screen[screen_list->pty]->changed = 1;
258    wm_blit_current_screen(screen_list);
259    wm_bell(screen_list);
260    wm_box(screen_list, screen_list->pty);
261}
262
263void wm_refresh_full(struct screen_list *screen_list)
264{
265    wm_blit_current_screen(screen_list);
266    wm_bell(screen_list);
267    wm_box(screen_list, screen_list->pty);
268}
269
270void wm_refresh_vsplit(struct screen_list *screen_list)
271{
272    int i;
273
274    for (i = screen_list->count - 1; i >= 0; i--)
275    {
276        if (i != screen_list->pty && screen_list->screen[i]->visible &&
277            (screen_list->screen[i]->changed || screen_list->changed))
278        {
279            caca_blit(screen_list->cv,
280                      screen_list->screen[i]->x,
281                      screen_list->screen[i]->y,
282                      screen_list->screen[i]->cv, NULL);
283
284            wm_box(screen_list, i);
285        }
286    }
287
288    wm_blit_current_screen(screen_list);
289    wm_bell(screen_list);
290    wm_box(screen_list, screen_list->pty);
291}
292
293void wm_refresh_hsplit(struct screen_list *screen_list)
294{
295    int i;
296
297    for (i = screen_list->count - 1; i >= 0; i--)
298    {
299        if (i != screen_list->pty && screen_list->screen[i]->visible &&
300            (screen_list->screen[i]->changed || screen_list->changed))
301        {
302            caca_blit(screen_list->cv,
303                      screen_list->screen[i]->x,
304                      screen_list->screen[i]->y,
305                      screen_list->screen[i]->cv, NULL);
306
307            wm_box(screen_list, i);
308        }
309    }
310
311    wm_blit_current_screen(screen_list);
312    wm_bell(screen_list);
313    wm_box(screen_list, screen_list->pty);
314}
315
316
317static float
318get_direction(float p1x, float p1y, float p2x, float p2y, float p3x, float p3y)
319{
320    float d1x, d1y, d2x, d2y;
321
322    d1x = p3x - p1x;
323    d1y = p3y - p1y;
324    d2x = p3x - p2x;
325    d2y = p3y - p2y;
326    return (d1x * d2y) - (d1y * d2x);
327}
328
329
330/* 3D Cube. Yeah I know, it's a mess. Just look anywhere else. */
331static void draw_face(caca_canvas_t * cv,
332                      int p1x, int p1y,
333                      int p2x, int p2y,
334                      int p3x, int p3y,
335                      int p4x, int p4y, caca_canvas_t * tex, int color)
336{
337    if (get_direction(p1x, p1y, p2x, p2y, p3x, p3y) >= 0)
338    {
339        int coords[6];
340        float uv[6];
341        coords[0] = p1x;
342        coords[1] = p1y;
343        coords[2] = p2x;
344        coords[3] = p2y;
345        coords[4] = p3x;
346        coords[5] = p3y;
347        uv[0] = 1;
348        uv[1] = 1;
349        uv[2] = 0;
350        uv[3] = 1;
351        uv[4] = 0;
352        uv[5] = 0;
353#if defined HAVE_CACA_TRIANGLE_TEXTURING
354        caca_fill_triangle_textured(cv, coords, tex, uv);
355#endif
356        coords[0] = p1x;
357        coords[1] = p1y;
358        coords[2] = p3x;
359        coords[3] = p3y;
360        coords[4] = p4x;
361        coords[5] = p4y;
362        uv[0] = 1;
363        uv[1] = 1;
364        uv[2] = 0;
365        uv[3] = 0;
366        uv[4] = 1;
367        uv[5] = 0;
368#if defined HAVE_CACA_TRIANGLE_TEXTURING
369        caca_fill_triangle_textured(cv, coords, tex, uv);
370#endif
371        caca_set_color_ansi(cv, color, CACA_BLACK);
372        caca_draw_thin_line(cv, p1x, p1y, p2x, p2y);
373        caca_draw_thin_line(cv, p2x, p2y, p3x, p3y);
374        caca_draw_thin_line(cv, p3x, p3y, p4x, p4y);
375        caca_draw_thin_line(cv, p4x, p4y, p1x, p1y);
376    }
377}
378
379
380void wm_refresh_cube(struct screen_list *screen_list)
381{
382    int i;
383   
384    if (!screen_list->cube.in_switch || !screen_list->eyecandy)
385    {
386        wm_refresh_full(screen_list);
387        screen_list->force_refresh = 0;
388    }
389    else
390    {
391        long long unsigned int cur_time = get_us() - screen_list->last_switch;
392
393        if (cur_time >= screen_list->cube.duration || screen_list->count == 1)
394        {
395            screen_list->changed = 1;
396            screen_list->force_refresh = 1;
397            screen_list->cube.in_switch = 0;
398        }
399        else
400        {
401            float cube[12][3] = {
402                {-1, -1, 1},
403                {1, -1, 1},
404                {1, 1, 1},
405                {-1, 1, 1},
406
407                {1, -1, 1},
408                {1, -1, -1},
409                {1, 1, -1},
410                {1, 1, 1},
411
412                {-1, -1, -1},
413                {-1, -1, 1},
414                {-1, 1, 1},
415                {-1, 1, -1},
416            };
417
418            float cube_transformed[12][3];
419            float cube_projected[12][2];
420            float fov = 0.5f;
421            float angle =
422                90.0f * ((float)cur_time / (float)screen_list->cube.duration);
423
424            angle *= (M_PI / 180.0f);
425
426            if (screen_list->cube.side == 1)
427                angle = -angle;
428
429            float sina = sin(angle);
430            float cosa = cos(angle);
431
432            for (i = 0; i < 12; i++)
433            {
434                cube_transformed[i][2] = cube[i][2] * cosa - cube[i][0] * sina;
435                cube_transformed[i][0] = cube[i][2] * sina + cube[i][0] * cosa;
436                cube_transformed[i][1] = cube[i][1];
437
438                cube_transformed[i][2] -= 3;
439
440                cube_projected[i][0] =
441                    cube_transformed[i][0] / (cube_transformed[i][2] * fov);
442                cube_projected[i][1] =
443                    cube_transformed[i][1] / (cube_transformed[i][2] * fov);
444
445                cube_projected[i][0] /= 2.0f;
446                cube_projected[i][1] /= 2.0f;
447                cube_projected[i][0] += 0.5f;
448                cube_projected[i][1] += 0.5f;
449
450                cube_projected[i][0] *= screen_list->width;
451                cube_projected[i][1] *= screen_list->height;
452            }
453
454            caca_set_color_ansi(screen_list->cv, CACA_WHITE, CACA_BLACK);
455            caca_clear_canvas(screen_list->cv);
456
457            caca_canvas_t *first =
458                screen_list->screen[screen_list->prevpty]->cv;
459            caca_canvas_t *second = screen_list->screen[screen_list->pty]->cv;
460
461            draw_face(screen_list->cv,
462                      cube_projected[0][0], cube_projected[0][1],
463                      cube_projected[1][0], cube_projected[1][1],
464                      cube_projected[2][0], cube_projected[2][1],
465                      cube_projected[3][0], cube_projected[3][1],
466                      first, CACA_LIGHTGREEN);
467
468
469            if (screen_list->cube.side)
470            {
471                draw_face(screen_list->cv,
472                          cube_projected[4][0], cube_projected[4][1],
473                          cube_projected[5][0], cube_projected[5][1],
474                          cube_projected[6][0], cube_projected[6][1],
475                          cube_projected[7][0], cube_projected[7][1],
476                          second, CACA_RED);
477            }
478            else
479            {
480                draw_face(screen_list->cv,
481                          cube_projected[8][0], cube_projected[8][1],
482                          cube_projected[9][0], cube_projected[9][1],
483                          cube_projected[10][0], cube_projected[10][1],
484                          cube_projected[11][0], cube_projected[11][1],
485                          second, CACA_RED);
486            }
487
488            screen_list->changed = 1;
489            screen_list->force_refresh = 1;
490            screen_list->cube.in_switch = 1;
491        }
492    }
493}
Note: See TracBrowser for help on using the repository browser.