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

Last change on this file since 3938 was 3938, checked in by Jean-Yves Lamoureux, 11 years ago
  • J'ai un chien qui s'apelle Moooooose (fixed an indentation problem)
  • Property svn:keywords set to Id
File size: 13.5 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 3938 2009-11-18 14:23:54Z jylam $
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    case WM_CUBE:
80    default:
81        update_windows_props_full(screen_list);
82        break;
83    }
84}
85
86void update_windows_props_hsplit(struct screen_list *screen_list)
87{
88    int i;
89    int w = (screen_list->width / screen_list->count) - 1;
90    int h = screen_list->height - 2;
91
92    for (i = 0; i < screen_list->count; i++)
93    {
94        screen_list->screen[i]->x = (i * w) + 1;
95        screen_list->screen[i]->y = 1;
96        screen_list->screen[i]->visible = 1;
97        if (i != screen_list->count - 1)
98        {
99            resize_screen(screen_list->screen[i], w - 1, h);
100        }
101        else
102        {
103            resize_screen(screen_list->screen[i],
104                          screen_list->width - i * w - 2, h);
105        }
106    }
107}
108
109void update_windows_props_vsplit(struct screen_list *screen_list)
110{
111    int i;
112    int w = screen_list->width - 2;
113    int h = (screen_list->height) / screen_list->count;
114
115    for (i = 0; i < screen_list->count; i++)
116    {
117        screen_list->screen[i]->x = 1;
118        screen_list->screen[i]->y = (i * h) + 1;
119        screen_list->screen[i]->visible = 1;
120        if (i != screen_list->count - 1)
121        {
122            resize_screen(screen_list->screen[i], w, h - 2);
123        }
124        else
125        {
126            resize_screen(screen_list->screen[i],
127                          w, screen_list->height - i * h - 2);
128        }
129    }
130}
131
132
133void update_windows_props_full(struct screen_list *screen_list)
134{
135    int i;
136    int w = screen_list->width - 2;
137    int h = screen_list->height - 2;
138
139    for (i = 0; i < screen_list->count; i++)
140    {
141        screen_list->screen[i]->visible = 0;
142        screen_list->screen[i]->x = 1;
143        screen_list->screen[i]->y = 1;
144
145        resize_screen(screen_list->screen[i], w, h);
146    }
147    screen_list->screen[screen_list->pty]->visible = 1;
148}
149
150
151void update_windows_props_cards(struct screen_list *screen_list)
152{
153    int i;
154    int w = (screen_list->width - screen_list->count * 3) + 1;
155    int h = (screen_list->height - screen_list->count) - 1;
156    int x = 1;
157    int y = screen_list->count;
158
159    for (i = 0; i < screen_list->count; i++)
160    {
161        screen_list->screen[i]->visible = 1;
162        screen_list->screen[i]->x = x;
163        screen_list->screen[i]->y = y;
164
165        resize_screen(screen_list->screen[i], w, h);
166        x += 3;
167        y--;
168    }
169}
170
171/* Window managers refresh */
172
173void wm_refresh(struct screen_list *screen_list)
174{
175    switch (screen_list->wm_type)
176    {
177    case WM_CARD:
178        wm_refresh_card(screen_list);
179        break;
180    case WM_FULL:
181        wm_refresh_full(screen_list);
182        break;
183    case WM_HSPLIT:
184        wm_refresh_hsplit(screen_list);
185        break;
186    case WM_VSPLIT:
187        wm_refresh_hsplit(screen_list);
188        break;
189    case WM_CUBE:
190        wm_refresh_cube(screen_list);
191        break;
192    default:
193        wm_refresh_full(screen_list);
194        break;
195    }
196}
197
198static void wm_bell(struct screen_list *screen_list)
199{
200    if (screen_list->screen[screen_list->pty]->bell)
201    {
202        caca_set_color_ansi(screen_list->cv, CACA_RED, CACA_BLACK);
203        screen_list->screen[screen_list->pty]->bell = 0;
204        screen_list->in_bell--;
205    }
206    else
207    {
208        caca_set_color_ansi(screen_list->cv, CACA_LIGHTGREEN, CACA_BLACK);
209    }
210}
211
212static void wm_box(struct screen_list *screen_list, int pty)
213{
214    caca_draw_cp437_box(screen_list->cv,
215                        screen_list->screen[pty]->x - 1,
216                        screen_list->screen[pty]->y - 1,
217                        screen_list->screen[pty]->w + 2,
218                        screen_list->screen[pty]->h + 2);
219
220    if (screen_list->screen[screen_list->pty]->title)
221    {
222        caca_printf(screen_list->cv,
223                    screen_list->screen[pty]->x,
224                    screen_list->screen[pty]->y - 1,
225                    " %.*s ",
226                    screen_list->screen[pty]->w - 3,
227                    screen_list->screen[pty]->title);
228    }
229}
230
231static void wm_blit_current_screen(struct screen_list *screen_list)
232{
233    if (screen_list->screen[screen_list->pty]->changed || screen_list->changed)
234        caca_blit(screen_list->cv,
235                  screen_list->screen[screen_list->pty]->x,
236                  screen_list->screen[screen_list->pty]->y,
237                  screen_list->screen[screen_list->pty]->cv, NULL);
238}
239
240void wm_refresh_card(struct screen_list *screen_list)
241{
242    int i;
243
244    for (i = screen_list->count - 1; i >= 0; i--)
245    {
246        if (i != screen_list->pty && screen_list->screen[i]->visible &&
247            (screen_list->screen[i]->changed || screen_list->changed))
248        {
249            caca_blit(screen_list->cv,
250                      screen_list->screen[i]->x,
251                      screen_list->screen[i]->y,
252                      screen_list->screen[i]->cv, NULL);
253
254            wm_box(screen_list, i);
255        }
256    }
257
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
357        coords[0] = p1x;
358        coords[1] = p1y;
359        coords[2] = p3x;
360        coords[3] = p3y;
361        coords[4] = p4x;
362        coords[5] = p4y;
363        uv[0] = 1;
364        uv[1] = 1;
365        uv[2] = 0;
366        uv[3] = 0;
367        uv[4] = 1;
368        uv[5] = 0;
369#if defined HAVE_CACA_TRIANGLE_TEXTURING
370        caca_fill_triangle_textured(cv, coords, tex, uv);
371#endif
372        caca_set_color_ansi(cv, color, CACA_BLACK);
373        caca_draw_thin_line(cv, p1x, p1y, p2x, p2y);
374        caca_draw_thin_line(cv, p2x, p2y, p3x, p3y);
375        caca_draw_thin_line(cv, p3x, p3y, p4x, p4y);
376        caca_draw_thin_line(cv, p4x, p4y, p1x, p1y);
377    }
378}
379
380#define CUBE_TIME 1000000
381void wm_refresh_cube(struct screen_list *screen_list)
382{
383    int i;
384
385    if (!screen_list->cube.in_switch)
386    {
387        wm_refresh_full(screen_list);
388        screen_list->force_refresh = 0;
389    }
390    else
391    {
392        long long unsigned int cur_time = get_us() - screen_list->last_switch;
393
394        if (cur_time >= CUBE_TIME || screen_list->count == 1)
395        {
396            screen_list->changed = 1;
397            screen_list->force_refresh = 1;
398            screen_list->cube.in_switch = 0;
399        }
400        else
401        {
402            float cube[12][3] = {
403                {-1, -1, 1},
404                {1, -1, 1},
405                {1, 1, 1},
406                {-1, 1, 1},
407
408                {1, -1, 1},
409                {1, -1, -1},
410                {1, 1, -1},
411                {1, 1, 1},
412
413                {-1, -1, -1},
414                {-1, -1, 1},
415                {-1, 1, 1},
416                {-1, 1, -1},
417            };
418
419            float cube_transformed[12][3];
420            float cube_projected[12][2];
421            float fov = 0.5f;
422            float angle = 90.0f * ((float)cur_time / (float)CUBE_TIME);
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    }
494}
Note: See TracBrowser for help on using the repository browser.