source: neercs/trunk/src/effects.c @ 3996

Last change on this file since 3996 was 3996, checked in by Jean-Yves Lamoureux, 11 years ago
  • Added preliminary OSX support (by support I mean it barely compiles)
  • Moved client's send commands to client.c
  • Property svn:keywords set to Id
File size: 10.3 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: effects.c 3996 2009-11-22 12:41:45Z 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 <stdlib.h>
20#include <string.h>
21#include <caca.h>
22#include <caca.h>
23#include <time.h>
24#include <sys/wait.h>
25#include <sys/types.h>
26
27#include "neercs.h"
28
29void draw_thumbnails(struct screen_list *screen_list)
30{
31    char const *const *fonts;
32    caca_dither_t *d;
33    caca_font_t *f;
34    uint8_t *buf;
35    int i, y =
36        caca_get_canvas_height(screen_list->cv) - 6 - screen_list->status;
37    int miniw, minih;
38
39    if (screen_list->count)
40    {
41        fonts = caca_get_font_list();
42        f = caca_load_font(fonts[0], 0);
43
44        miniw = caca_get_canvas_width(screen_list->screen[0]->cv)
45            * caca_get_font_width(f);
46        minih = caca_get_canvas_height(screen_list->screen[0]->cv)
47            * caca_get_font_height(f);
48        buf = malloc(4 * miniw * minih);
49
50#if defined(HAVE_ENDIAN_H)
51        if (__BYTE_ORDER == __BIG_ENDIAN)
52#else
53        /* This is compile-time optimised with at least -O1 or -Os */
54        uint32_t const tmp = 0x12345678;
55        if (*(uint8_t const *)&tmp == 0x12)
56#endif
57            d = caca_create_dither(32, miniw, minih, 4 * miniw,
58                                   0xff0000, 0xff00, 0xff, 0x0);
59        else
60            d = caca_create_dither(32, miniw, minih, 4 * miniw,
61                                   0xff00, 0xff0000, 0xff000000, 0x0);
62
63        for (i = 0; i < screen_list->count; i++)
64        {
65            if (!screen_list->screen[i]->changed && !screen_list->changed)
66                continue;
67            caca_render_canvas(screen_list->screen[i]->cv, f, buf,
68                               miniw, minih, miniw * 4);
69            caca_dither_bitmap(screen_list->cv, 20 * i, y, 19, 6, d, buf);
70            caca_set_color_ansi(screen_list->cv, CACA_WHITE, CACA_BLUE);
71
72            if (screen_list->pty == i)
73                caca_draw_cp437_box(screen_list->cv, 20 * i, y, 19, 6);
74            caca_printf(screen_list->cv, 20 * i, y, "(%i)", i + 1);
75        }
76
77        caca_free_dither(d);
78        caca_free_font(f);
79
80        free(buf);
81    }
82
83}
84
85/* FIXME, make this stuff more configurable */
86void draw_status(struct screen_list *screen_list)
87{
88    int x = 0, y = caca_get_canvas_height(screen_list->cv) - 1;
89
90
91    /* caca_fill_box(screen_list->cv, x, y,
92       caca_get_canvas_width(screen_list->cv), 1, '#'); */
93
94    /* Hour */
95    {
96        time_t now = time((time_t *) 0);
97        struct tm *t = localtime(&now);
98        char hour[256];
99        sprintf(hour, "%02d:%02d", t->tm_hour, t->tm_min);
100
101        caca_set_color_ansi(screen_list->cv, CACA_LIGHTBLUE, CACA_BLUE);
102        caca_printf(screen_list->cv, x, y, "[");
103
104        caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_BLUE);
105        caca_printf(screen_list->cv, x + 1, y, hour);
106        caca_set_color_ansi(screen_list->cv, CACA_LIGHTBLUE, CACA_BLUE);
107        caca_printf(screen_list->cv, x + strlen(hour) + 1, y, "]");
108        x += 7;
109
110    }
111
112    /* Window */
113    {
114        char text[256];
115        sprintf(text, "%d/%d", screen_list->pty + 1, screen_list->count);
116        caca_set_color_ansi(screen_list->cv, CACA_BLUE, CACA_BLUE);
117        caca_put_char(screen_list->cv, x, y, '#');
118        x++;
119        caca_set_color_ansi(screen_list->cv, CACA_LIGHTBLUE, CACA_BLUE);
120        caca_printf(screen_list->cv, x, y, "Window:");
121        caca_set_color_ansi(screen_list->cv, CACA_BLUE, CACA_BLUE);
122        caca_put_char(screen_list->cv, x + 7, y, '#');
123        caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_BLUE);
124        caca_printf(screen_list->cv, x + 8, y, text);
125        x += 8 + strlen(text);
126    }
127
128    /* Window Manager */
129    {
130        char text[256];
131
132        switch (screen_list->wm_type)
133        {
134        case WM_CARD:
135            strcpy(text, "card");
136            break;
137        case WM_HSPLIT:
138            strcpy(text, "hsplit");
139            break;
140        case WM_VSPLIT:
141            strcpy(text, "vsplit");
142            break;
143        case WM_CUBE:
144            strcpy(text, "cube");
145            break;
146        case WM_FULL:
147        default:
148            strcpy(text, "full");
149            break;
150
151        }
152
153        caca_set_color_ansi(screen_list->cv, CACA_BLUE, CACA_BLUE);
154        caca_put_char(screen_list->cv, x, y, '#');
155        x++;
156        caca_set_color_ansi(screen_list->cv, CACA_LIGHTBLUE, CACA_BLUE);
157        caca_printf(screen_list->cv, x, y, "WM:");
158        caca_set_color_ansi(screen_list->cv, CACA_BLUE, CACA_BLUE);
159        caca_put_char(screen_list->cv, x + 3, y, '#');
160        caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_BLUE);
161        caca_printf(screen_list->cv, x + 4, y, text);
162        x += 4 + strlen(text);
163    }
164
165    /* Help (must be the last one ) */
166    {
167        char text[256];
168        sprintf(text, "Help: ctrl-a-h");
169        caca_set_color_ansi(screen_list->cv, CACA_BLUE, CACA_BLUE);
170        while (x <
171               (int)(caca_get_canvas_width(screen_list->cv) - strlen(text)))
172        {
173            caca_put_char(screen_list->cv, x, y, '#');
174            x++;
175        }
176        caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_BLUE);
177        caca_printf(screen_list->cv, x, y, text);
178    }
179
180
181}
182
183
184void draw_help(struct screen_list *screen_list)
185{
186    int w = 65, h = 20;
187    int x = (caca_get_canvas_width(screen_list->cv) - w) / 2;
188    int y = (caca_get_canvas_height(screen_list->cv) - h) / 2;
189
190
191    caca_set_color_ansi(screen_list->cv, CACA_BLUE, CACA_BLUE);
192    caca_fill_box(screen_list->cv, x, y, w, h, '#');
193    caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_BLUE);
194    caca_draw_cp437_box(screen_list->cv, x, y, w, h);
195
196    x += 2;
197    y++;
198    caca_printf(screen_list->cv,
199                (caca_get_canvas_width(screen_list->cv) -
200                 strlen(PACKAGE_STRING)) / 2, y - 1, PACKAGE_STRING);
201    caca_printf(screen_list->cv, x, y++, "Copyright (c) 2006-2009");
202    caca_printf(screen_list->cv, x, y++, "              Sam Hocevar <sam@zoy.org>");
203    caca_printf(screen_list->cv, x, y++, "              Jean-Yves Lamoureux <jylam@lnxscene.org>");
204    caca_printf(screen_list->cv, x, y++, "              Pascal Terjan <pterjan@linuxfr.org>");
205    caca_printf(screen_list->cv, x, y++, "");
206    caca_printf(screen_list->cv, x, y++, "");
207    caca_printf(screen_list->cv, x, y++, "All shortcuts are in format 'ctrl-a-X' where X is :");
208    caca_printf(screen_list->cv, x, y++, "n:\t Next window");
209    caca_printf(screen_list->cv, x, y++, "p:\t Previous window");
210    caca_printf(screen_list->cv, x, y++, "w:\t Switch window manager");
211    caca_printf(screen_list->cv, x, y++, "c:\t Create new window");
212    caca_printf(screen_list->cv, x, y++, "m:\t Thumbnails");
213    caca_printf(screen_list->cv, x, y++, "d:\t Detach");
214    caca_printf(screen_list->cv, x, y++, "k:\t Close window and kill associated process");
215    caca_printf(screen_list->cv, x, y++, "h:\t This help");
216    caca_printf(screen_list->cv, x, y++, "");
217    caca_printf(screen_list->cv, x, y++, "");
218    caca_printf(screen_list->cv, x, y++, "See http://caca.zoy.org/wiki/neercs for more informations");
219}
220
221int update_window_list(int c, struct screen_list *screen_list)
222{
223    debug("Got %x\n", c);
224
225    switch (c)
226    {
227    case 0x111:
228        if (screen_list->cur_in_list > 0)
229            screen_list->cur_in_list--;
230        break;
231    case 0x112:
232        if (screen_list->cur_in_list < screen_list->count - 1)
233            screen_list->cur_in_list++;
234        break;
235    case 0xd:
236        screen_list->window_list = 0;
237        screen_list->prevpty = screen_list->pty;
238        screen_list->pty = screen_list->cur_in_list;
239        break;
240    case 0x22:
241        screen_list->window_list = 0;
242        break;
243    default:
244        break;
245    }
246
247    return 1;
248}
249
250void draw_list(struct screen_list *screen_list)
251{
252    int i;
253    int w = (caca_get_canvas_width(screen_list->cv));
254    int h = (caca_get_canvas_height(screen_list->cv));
255
256    debug("Drawing list\n");
257    caca_set_color_ansi(screen_list->cv, CACA_BLACK, CACA_BLACK);
258    caca_fill_box(screen_list->cv, 0, 0, w, h, '#');
259    caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_DEFAULT);
260    caca_draw_cp437_box(screen_list->cv, 0, 0, w, h);
261
262    caca_printf(screen_list->cv, 2, 1, "Num \tName");
263    for (i = 0; i < screen_list->count; i++)
264    {
265        char line[1024];
266        if (screen_list->cur_in_list == i)
267            caca_set_color_ansi(screen_list->cv, CACA_BLACK, CACA_WHITE);
268        else
269            caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_DEFAULT);
270        sprintf(line, "%d \t%s", i + 1, screen_list->screen[i]->title);
271
272        caca_printf(screen_list->cv, 2, i + 3, line);
273    }
274}
275
276/* Close a window by animating it collapsing */
277/* Total close time */
278#define DELAY 500000.0f
279int close_screen_recurrent(struct screen_list *screen_list,
280                           struct recurrent *rec, void *user,
281                           long long unsigned int t)
282{
283    long long unsigned int delta = t - rec->start_time;
284
285    screen_list->dont_update_coords = 1;
286    screen_list->delay = 0;
287    rec->kill_me = 0;
288    if (delta >= DELAY)
289    {
290        rec->kill_me = 1;
291        remove_screen(screen_list, screen_list->pty, 1);
292        screen_list->pty = screen_list->prevpty;
293        screen_list->prevpty = 0;
294        screen_list->dont_update_coords = 0;
295    }
296    else
297    {
298        float r = 1 - ((DELAY - (DELAY - delta)) / DELAY);
299        caca_canvas_t *old, *new;
300        struct screen *s = screen_list->screen[screen_list->pty];
301        int w = s->orig_w * r;
302        int h = s->orig_h * r;
303
304        /* libcaca canvas resize function is bugged, do it by hand */
305        old = s->cv;
306        new = caca_create_canvas(w, h);
307        caca_blit(new, 0, 0, old, NULL);
308        s->cv = new;
309        caca_free_canvas(old);
310        set_tty_size(s->fd, w, h);
311
312        s->w = w;
313        s->h = h;
314
315        s->x = (s->orig_x * r) + ((s->orig_w / 2) - s->w / 2);
316        s->y = (s->orig_y * r) + ((s->orig_h / 2) - s->h / 2);
317    }
318    screen_list->changed = 1;
319    return 1;
320}
Note: See TracBrowser for help on using the repository browser.