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

Last change on this file since 4366 was 4366, checked in by Sam Hocevar, 11 years ago

Clean up source code, copyright information, author names, SVN keywords...

  • Property svn:keywords set to Id
File size: 8.5 KB
Line 
1/*
2 *  neercs        console-based window manager
3 *  Copyright (c) 2006-2010 Sam Hocevar <sam@hocevar.net>
4 *                2008-2010 Jean-Yves Lamoureux <jylam@lnxscene.org>
5 *                All Rights Reserved
6 *
7 *  This program is free software. It comes without any warranty, to
8 *  the extent permitted by applicable law. You can redistribute it
9 *  and/or modify it under the terms of the Do What The Fuck You Want
10 *  To Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14#include "config.h"
15
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <time.h>
20#include <sys/wait.h>
21#include <sys/types.h>
22#include <caca.h>
23
24#include "neercs.h"
25
26void draw_thumbnails(struct screen_list *screen_list)
27{
28    char const *const *fonts;
29    caca_dither_t *d;
30    caca_font_t *f;
31    uint8_t *buf;
32    int i, y =
33        caca_get_canvas_height(screen_list->cv) - 6 - screen_list->modals.status;
34    int miniw, minih;
35
36    if (screen_list->count)
37    {
38        fonts = caca_get_font_list();
39        f = caca_load_font(fonts[0], 0);
40
41        miniw = caca_get_canvas_width(screen_list->screen[0]->cv)
42            * caca_get_font_width(f);
43        minih = caca_get_canvas_height(screen_list->screen[0]->cv)
44            * caca_get_font_height(f);
45        buf = malloc(4 * miniw * minih);
46
47#if defined(HAVE_ENDIAN_H)
48        if (__BYTE_ORDER == __BIG_ENDIAN)
49#else
50        /* This is compile-time optimised with at least -O1 or -Os */
51        uint32_t const tmp = 0x12345678;
52        if (*(uint8_t const *)&tmp == 0x12)
53#endif
54            d = caca_create_dither(32, miniw, minih, 4 * miniw,
55                                   0xff0000, 0xff00, 0xff, 0x0);
56        else
57            d = caca_create_dither(32, miniw, minih, 4 * miniw,
58                                   0xff00, 0xff0000, 0xff000000, 0x0);
59
60        for (i = 0; i < screen_list->count; i++)
61        {
62            if (!screen_list->screen[i]->changed && !screen_list->changed)
63                continue;
64            caca_render_canvas(screen_list->screen[i]->cv, f, buf,
65                               miniw, minih, miniw * 4);
66            caca_dither_bitmap(screen_list->cv, 20 * i, y, 19, 6, d, buf);
67            caca_set_color_ansi(screen_list->cv, CACA_WHITE, CACA_BLUE);
68
69            if (screen_list->pty == i)
70                caca_draw_cp437_box(screen_list->cv, 20 * i, y, 19, 6);
71            caca_printf(screen_list->cv, 20 * i, y, "(%i)", i + 1);
72        }
73
74        caca_free_dither(d);
75        caca_free_font(f);
76
77        free(buf);
78    }
79
80}
81
82/* FIXME, make this stuff more configurable */
83void draw_status(struct screen_list *screen_list)
84{
85    int x = 0, y = caca_get_canvas_height(screen_list->cv) - 1;
86
87
88    /* caca_fill_box(screen_list->cv, x, y,
89       caca_get_canvas_width(screen_list->cv), 1, '#'); */
90
91    /* Hour */
92    {
93        time_t now = time((time_t *) 0);
94        struct tm *t = localtime(&now);
95        char hour[256];
96        sprintf(hour, "%02d:%02d", t->tm_hour, t->tm_min);
97
98        caca_set_color_ansi(screen_list->cv, CACA_LIGHTBLUE, CACA_BLUE);
99        caca_printf(screen_list->cv, x, y, "[");
100
101        caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_BLUE);
102        caca_printf(screen_list->cv, x + 1, y, hour);
103        caca_set_color_ansi(screen_list->cv, CACA_LIGHTBLUE, CACA_BLUE);
104        caca_printf(screen_list->cv, x + strlen(hour) + 1, y, "]");
105        x += 7;
106
107    }
108
109    /* Window */
110    {
111        char text[256];
112        sprintf(text, "%d/%d", screen_list->pty + 1, screen_list->count);
113        caca_set_color_ansi(screen_list->cv, CACA_BLUE, CACA_BLUE);
114        caca_put_char(screen_list->cv, x, y, '#');
115        x++;
116        caca_set_color_ansi(screen_list->cv, CACA_LIGHTBLUE, CACA_BLUE);
117        caca_printf(screen_list->cv, x, y, "Window:");
118        caca_set_color_ansi(screen_list->cv, CACA_BLUE, CACA_BLUE);
119        caca_put_char(screen_list->cv, x + 7, y, '#');
120        caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_BLUE);
121        caca_printf(screen_list->cv, x + 8, y, text);
122        x += 8 + strlen(text);
123    }
124
125    /* Window Manager */
126    {
127        char text[256];
128
129        switch (screen_list->wm_type)
130        {
131        case WM_CARD:
132            strcpy(text, "card");
133            break;
134        case WM_HSPLIT:
135            strcpy(text, "hsplit");
136            break;
137        case WM_VSPLIT:
138            strcpy(text, "vsplit");
139            break;
140        case WM_FULL:
141        default:
142            strcpy(text, "full");
143            break;
144
145        }
146
147        caca_set_color_ansi(screen_list->cv, CACA_BLUE, CACA_BLUE);
148        caca_put_char(screen_list->cv, x, y, '#');
149        x++;
150        caca_set_color_ansi(screen_list->cv, CACA_LIGHTBLUE, CACA_BLUE);
151        caca_printf(screen_list->cv, x, y, "WM:");
152        caca_set_color_ansi(screen_list->cv, CACA_BLUE, CACA_BLUE);
153        caca_put_char(screen_list->cv, x + 3, y, '#');
154        caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_BLUE);
155        caca_printf(screen_list->cv, x + 4, y, text);
156        x += 4 + strlen(text);
157    }
158
159    /* Help (must be the last one ) */
160    {
161        char text[256];
162        sprintf(text, "Help: ctrl-a-h");
163        caca_set_color_ansi(screen_list->cv, CACA_BLUE, CACA_BLUE);
164        while (x <
165               (int)(caca_get_canvas_width(screen_list->cv) - strlen(text)))
166        {
167            caca_put_char(screen_list->cv, x, y, '#');
168            x++;
169        }
170        caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_BLUE);
171        caca_printf(screen_list->cv, x, y, text);
172    }
173
174
175}
176
177
178
179int update_window_list(int c, struct screen_list *screen_list)
180{
181    debug("Got %x\n", c);
182
183    switch (c)
184    {
185    case 0x111:
186        if (screen_list->modals.cur_in_list > 0)
187            screen_list->modals.cur_in_list--;
188        break;
189    case 0x112:
190        if (screen_list->modals.cur_in_list < screen_list->count - 1)
191            screen_list->modals.cur_in_list++;
192        break;
193    case 0xd:
194        screen_list->modals.window_list = 0;
195        screen_list->prevpty = screen_list->pty;
196        screen_list->pty = screen_list->modals.cur_in_list;
197        break;
198    case 0x22:
199        screen_list->modals.window_list = 0;
200        break;
201    default:
202        break;
203    }
204
205    return 1;
206}
207
208void draw_list(struct screen_list *screen_list)
209{
210    int i;
211    int w = (caca_get_canvas_width(screen_list->cv));
212    int h = (caca_get_canvas_height(screen_list->cv));
213
214    debug("Drawing list\n");
215    caca_set_color_ansi(screen_list->cv, CACA_BLACK, CACA_BLACK);
216    caca_fill_box(screen_list->cv, 0, 0, w, h, '#');
217    caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_DEFAULT);
218    caca_draw_cp437_box(screen_list->cv, 0, 0, w, h);
219
220    caca_printf(screen_list->cv, 2, 1, "Num         Name");
221    for (i = 0; i < screen_list->count; i++)
222    {
223        char line[1024];
224        if (screen_list->modals.cur_in_list == i)
225            caca_set_color_ansi(screen_list->cv, CACA_BLACK, CACA_WHITE);
226        else
227            caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_DEFAULT);
228        sprintf(line, "%d           %s", i + 1, screen_list->screen[i]->title);
229
230        caca_printf(screen_list->cv, 2, i + 3, line);
231    }
232}
233
234/* Close a window by animating it collapsing */
235/* Total close time */
236#define DELAY 500000.0f
237int close_screen_recurrent(struct screen_list *screen_list,
238                           struct recurrent *rec, void *user,
239                           long long unsigned int t)
240{
241    long long unsigned int delta = t - rec->start_time;
242
243    screen_list->dont_update_coords = 1;
244    screen_list->delay = 0;
245    rec->kill_me = 0;
246    if (delta >= DELAY || (!screen_list->eyecandy))
247    {
248        rec->kill_me = 1;
249        remove_screen(screen_list, screen_list->pty, 1);
250        screen_list->pty = screen_list->prevpty>screen_list->count?screen_list->count:screen_list->prevpty;
251        screen_list->prevpty = 0;
252        screen_list->dont_update_coords = 0;
253    }
254    else
255    {
256        float r = 1 - ((DELAY - (DELAY - delta)) / DELAY);
257        caca_canvas_t *old, *new;
258        struct screen *s = screen_list->screen[screen_list->pty];
259        int w = s->orig_w * r;
260        int h = s->orig_h * r;
261
262        /* libcaca canvas resize function is bugged, do it by hand */
263        old = s->cv;
264        new = caca_create_canvas(w, h);
265        caca_blit(new, 0, 0, old, NULL);
266        s->cv = new;
267        caca_free_canvas(old);
268        set_tty_size(s->fd, w, h);
269
270        s->w = w;
271        s->h = h;
272
273        s->x = (s->orig_x * r) + ((s->orig_w / 2) - s->w / 2);
274        s->y = (s->orig_y * r) + ((s->orig_h / 2) - s->h / 2);
275    }
276    screen_list->changed = 1;
277    return 1;
278}
Note: See TracBrowser for help on using the repository browser.