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

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