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

Last change on this file since 3551 was 3551, checked in by Pascal Terjan, 11 years ago
  • Add missing svn:keywords on some source files
  • Property svn:keywords set to Id
File size: 10.4 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 3551 2009-07-10 14:46:54Z 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 <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 = caca_get_canvas_height(screen_list->cv) - 6 - screen_list->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,
91                   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
105        caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_BLUE);
106        caca_printf(screen_list->cv, x+1, y,
107                     hour);
108        caca_set_color_ansi(screen_list->cv, CACA_LIGHTBLUE, CACA_BLUE);
109        caca_printf(screen_list->cv, x + strlen(hour) + 1, y,
110                     "]");
111        x += 7;
112
113    }
114
115/* Window */
116    {
117        char text[256];
118        sprintf(text, "%d/%d", screen_list->pty+1, screen_list->count);
119        caca_set_color_ansi(screen_list->cv, CACA_BLUE, CACA_BLUE);
120        caca_put_char(screen_list->cv, x, y, '#');
121        x++;
122        caca_set_color_ansi(screen_list->cv, CACA_LIGHTBLUE, CACA_BLUE);
123        caca_printf(screen_list->cv, x, y, "Window:");
124        caca_set_color_ansi(screen_list->cv, CACA_BLUE, CACA_BLUE);
125        caca_put_char(screen_list->cv, x+7, y, '#');
126        caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_BLUE);
127        caca_printf(screen_list->cv, x+8, y, text);
128        x+= 8+strlen(text);
129    }
130
131/* Window Manager */
132    {
133        char text[256];
134
135        switch(screen_list->wm_type)
136        {
137        case WM_CARD:
138            strcpy(text, "card");
139            break;
140        case WM_HSPLIT:
141            strcpy(text, "hsplit");
142            break;
143        case WM_VSPLIT:
144            strcpy(text, "vsplit");
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 < (int)(caca_get_canvas_width(screen_list->cv) - strlen(text)))
171        {
172            caca_put_char(screen_list->cv, x, y, '#');
173            x++;
174        }
175        caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_BLUE);
176        caca_printf(screen_list->cv, x, y, text);
177    }
178
179
180}
181
182
183void draw_help(struct screen_list *screen_list)
184{
185    int w = 65, h = 20;
186    int x = (caca_get_canvas_width(screen_list->cv) - w) / 2;
187    int y = (caca_get_canvas_height(screen_list->cv) - h) / 2;
188
189
190    caca_set_color_ansi(screen_list->cv, CACA_BLUE, CACA_BLUE);
191    caca_fill_box(screen_list->cv,
192                   x, y,
193                   w, h, '#');
194    caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_BLUE);
195    caca_draw_cp437_box(screen_list->cv,
196                         x, y,
197                         w, h);
198
199    x+=2;
200    y++;
201    caca_printf(screen_list->cv,
202                 (caca_get_canvas_width(screen_list->cv) - strlen(PACKAGE_STRING)) / 2,
203                 y-1,
204                 PACKAGE_STRING);
205    caca_printf(screen_list->cv, x, y++, "Copyright (c) 2006-2008 ");
206    caca_printf(screen_list->cv, x, y++, "              Sam Hocevar <sam@zoy.org>");
207    caca_printf(screen_list->cv, x, y++, "              Jean-Yves Lamoureux <jylam@lnxscene.org>");
208    caca_printf(screen_list->cv, x, y++, "              Pascal Terjan <pterjan@linuxfr.org>");
209    caca_printf(screen_list->cv, x, y++, "");
210    caca_printf(screen_list->cv, x, y++, "");
211    caca_printf(screen_list->cv, x, y++, "All shortcuts are in format 'ctrl-a-X' where X is :");
212    caca_printf(screen_list->cv, x, y++, "n:\t Next window");
213    caca_printf(screen_list->cv, x, y++, "p:\t Previous window");
214    caca_printf(screen_list->cv, x, y++, "w:\t Switch window manager");
215    caca_printf(screen_list->cv, x, y++, "c:\t Create new window");
216    caca_printf(screen_list->cv, x, y++, "m:\t Thumbnails");
217    caca_printf(screen_list->cv, x, y++, "d:\t Detach");
218    caca_printf(screen_list->cv, x, y++, "k:\t Close window and kill associated process");
219    caca_printf(screen_list->cv, x, y++, "h:\t This help");
220    caca_printf(screen_list->cv, x, y++, "");
221    caca_printf(screen_list->cv, x, y++, "");
222    caca_printf(screen_list->cv, x, y++, "See http://libcaca.zoy.org/wiki/neercs for more informations");
223}
224
225int update_window_list(int c, struct screen_list *screen_list)
226{
227    debug("Got %x\n", c);
228
229    switch(c)
230    {
231    case 0x111:
232        if(screen_list->cur_in_list>0)
233            screen_list->cur_in_list--;
234        break;
235    case 0x112:
236        if(screen_list->cur_in_list<screen_list->count-1)
237            screen_list->cur_in_list++;
238        break;
239    case 0xd:
240        screen_list->window_list = 0;
241        screen_list->prevpty = screen_list->pty;
242        screen_list->pty     = screen_list->cur_in_list;
243        break;
244    case 0x22:
245        screen_list->window_list = 0;
246        break;
247    default:
248        break;
249    }
250
251    return 1;
252}
253
254void draw_list(struct screen_list *screen_list)
255{
256    int i;
257    int w = (caca_get_canvas_width(screen_list->cv));
258    int h = (caca_get_canvas_height(screen_list->cv));
259
260    debug("Drawing list\n");
261    caca_set_color_ansi(screen_list->cv, CACA_BLACK, CACA_BLACK);
262    caca_fill_box(screen_list->cv,
263                   0, 0,
264                   w, h, '#');
265    caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_DEFAULT);
266    caca_draw_cp437_box(screen_list->cv,
267                         0, 0,
268                         w, h);
269
270    caca_printf(screen_list->cv, 2, 1, "Num \tName");
271    for(i = 0; i < screen_list->count; i++)
272    {
273        char line[1024];
274        if(screen_list->cur_in_list == i)
275            caca_set_color_ansi(screen_list->cv, CACA_BLACK, CACA_WHITE);
276        else
277            caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_DEFAULT);
278        sprintf(line, "%d \t%s", i+1, screen_list->screen[i]->title);
279
280        caca_printf(screen_list->cv, 2, i+3, line);
281    }
282}
283
284/* Close a window by animating it collapsing */
285/* Total close time */
286#define DELAY 500000.0f
287int close_screen_recurrent(struct screen_list* screen_list, struct recurrent* rec, void *user, long long unsigned int t)
288{
289    long long unsigned int delta = t - rec->start_time;
290
291    screen_list->dont_update_coords = 1;
292    screen_list->delay = 0;
293    rec->kill_me = 0;
294    if(delta>=DELAY)
295    {
296        rec->kill_me = 1;
297        remove_screen(screen_list, screen_list->pty, 1);
298        screen_list->pty = screen_list->prevpty;
299        screen_list->prevpty = 0;
300        screen_list->dont_update_coords = 0;
301    }
302    else
303    {
304        float r = 1 - ((DELAY - (DELAY - delta)) / DELAY);
305        caca_canvas_t *old, *new;
306        struct screen *s = screen_list->screen[screen_list->pty];
307        int w = s->orig_w * r;
308        int h = s->orig_h * r;
309
310        /* libcaca canvas resize function is bugged, do it by hand  */
311        old = s->cv;
312        new = caca_create_canvas(w, h);
313        caca_blit(new, 0, 0, old, NULL);
314        s->cv = new;
315        caca_free_canvas(old);
316        set_tty_size(s->fd, w, h);
317
318        s->w = w;
319        s->h = h;
320
321        s->x =
322            (s->orig_x * r) +
323            ((s->orig_w/2) - s->w/2);
324        s->y =
325            (s->orig_y * r) +
326            ((s->orig_h/2) - s->h/2);
327    }
328    return 1;
329}
Note: See TracBrowser for help on using the repository browser.