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

Last change on this file since 3506 was 3506, checked in by Pascal Terjan, 12 years ago

Don't overlap screens/thumbnails/status bar

File size: 9.8 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 2360 2008-06-11 16:23:46Z 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 = 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_set_color_ansi(screen_list->cv, CACA_BLUE, CACA_BLUE);
91    caca_fill_box(screen_list->cv,
92                   x, y,
93                   caca_get_canvas_width(screen_list->cv), 1, '#');
94
95/* Hour */
96    {
97        time_t now = time ((time_t *) 0);
98        struct tm *t = localtime(&now);
99        char hour[256];
100        sprintf(hour, "%02d:%02d", t->tm_hour, t->tm_min);
101
102        caca_set_color_ansi(screen_list->cv, CACA_LIGHTBLUE, CACA_BLUE);
103        caca_printf(screen_list->cv, x, y,
104                     "[");
105
106        caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_BLUE);
107        caca_printf(screen_list->cv, x+1, y,
108                     hour);
109        caca_set_color_ansi(screen_list->cv, CACA_LIGHTBLUE, CACA_BLUE);
110        caca_printf(screen_list->cv, x + strlen(hour) + 1, y,
111                     "]");
112        x += 7;
113
114    }
115
116/* Window */
117    {
118        char text[256];
119        sprintf(text, "%d/%d", screen_list->pty+1, screen_list->count);
120        x++;
121        caca_set_color_ansi(screen_list->cv, CACA_LIGHTBLUE, CACA_BLUE);
122        caca_printf(screen_list->cv, x, y, "Window:");
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_FULL:
144        default:
145            strcpy(text, "full");
146            break;
147
148        }
149
150        x++;
151        caca_set_color_ansi(screen_list->cv, CACA_LIGHTBLUE, CACA_BLUE);
152        caca_printf(screen_list->cv, x, y, "WM:");
153        caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_BLUE);
154        caca_printf(screen_list->cv, x+4, y, text);
155        x+= 4+strlen(text);
156    }
157
158/* Help (must be the last one )*/
159    {
160        char text[256];
161        sprintf(text, "Help: ctrl-a-h");
162        x = caca_get_canvas_width(screen_list->cv) - strlen(text);
163
164        caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_BLUE);
165        caca_printf(screen_list->cv, x, y, text);
166    }
167
168
169}
170
171
172void draw_help(struct screen_list *screen_list)
173{
174    int w = 65, h = 20;
175    int x = (caca_get_canvas_width(screen_list->cv) - w) / 2;
176    int y = (caca_get_canvas_height(screen_list->cv) - h) / 2;
177
178
179    caca_set_color_ansi(screen_list->cv, CACA_BLUE, CACA_BLUE);
180    caca_fill_box(screen_list->cv,
181                   x, y,
182                   w, h, '#');
183    caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_BLUE);
184    caca_draw_cp437_box(screen_list->cv,
185                         x, y,
186                         w, h);
187
188    x+=2;
189    y++;
190    caca_printf(screen_list->cv,
191                 (caca_get_canvas_width(screen_list->cv) - strlen(PACKAGE_STRING)) / 2,
192                 y-1,
193                 PACKAGE_STRING);
194    caca_printf(screen_list->cv, x, y++, "Copyright (c) 2006-2008 ");
195    caca_printf(screen_list->cv, x, y++, "              Sam Hocevar <sam@zoy.org>");
196    caca_printf(screen_list->cv, x, y++, "              Jean-Yves Lamoureux <jylam@lnxscene.org>");
197    caca_printf(screen_list->cv, x, y++, "              Pascal Terjan <pterjan@linuxfr.org>");
198    caca_printf(screen_list->cv, x, y++, "");
199    caca_printf(screen_list->cv, x, y++, "");
200    caca_printf(screen_list->cv, x, y++, "All shortcuts are in format 'ctrl-a-X' where X is :");
201    caca_printf(screen_list->cv, x, y++, "n:\t Next window");
202    caca_printf(screen_list->cv, x, y++, "p:\t Previous window");
203    caca_printf(screen_list->cv, x, y++, "w:\t Switch window manager");
204    caca_printf(screen_list->cv, x, y++, "c:\t Create new window");
205    caca_printf(screen_list->cv, x, y++, "m:\t Thumbnails");
206    caca_printf(screen_list->cv, x, y++, "d:\t Detach");
207    caca_printf(screen_list->cv, x, y++, "k:\t Close window and kill associated process");
208    caca_printf(screen_list->cv, x, y++, "h:\t This help");
209    caca_printf(screen_list->cv, x, y++, "");
210    caca_printf(screen_list->cv, x, y++, "");
211    caca_printf(screen_list->cv, x, y++, "See http://libcaca.zoy.org/wiki/neercs for more informations");
212}
213
214int update_window_list(int c, struct screen_list *screen_list)
215{
216    debug("Got %x\n", c);
217
218    switch(c)
219    {
220    case 0x111:
221        if(screen_list->cur_in_list>0)
222            screen_list->cur_in_list--;
223        break;
224    case 0x112:
225        if(screen_list->cur_in_list<screen_list->count-1)
226            screen_list->cur_in_list++;
227        break;
228    case 0xd:
229        screen_list->window_list = 0;
230        screen_list->prevpty = screen_list->pty;
231        screen_list->pty     = screen_list->cur_in_list;
232        break;
233    case 0x22:
234        screen_list->window_list = 0;
235        break;
236    default:
237        break;
238    }
239
240    return 1;
241}
242
243void draw_list(struct screen_list *screen_list)
244{
245    int i;
246    int w = (caca_get_canvas_width(screen_list->cv));
247    int h = (caca_get_canvas_height(screen_list->cv));
248
249    debug("Drawing list\n");
250    caca_set_color_ansi(screen_list->cv, CACA_BLACK, CACA_BLACK);
251    caca_fill_box(screen_list->cv,
252                   0, 0,
253                   w, h, '#');
254    caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_DEFAULT);
255    caca_draw_cp437_box(screen_list->cv,
256                         0, 0,
257                         w, h);
258
259    caca_printf(screen_list->cv, 2, 1, "Num \tName");
260    for(i = 0; i < screen_list->count; i++)
261    {
262        char line[1024];
263        if(screen_list->cur_in_list == i)
264            caca_set_color_ansi(screen_list->cv, CACA_BLACK, CACA_WHITE);
265        else
266            caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_DEFAULT);
267        sprintf(line, "%d \t%s", i+1, screen_list->screen[i]->title);
268
269        caca_printf(screen_list->cv, 2, i+3, line);
270    }
271}
272
273/* Close a window by animating it collapsing */
274/* Total close time */
275#define DELAY 500000.0f
276int close_screen_recurrent(struct screen_list* screen_list, struct recurrent* rec, void *user, long long unsigned int t)
277{
278    long long unsigned int delta = t - rec->start_time;
279
280    screen_list->dont_update_coords = 1;
281    screen_list->delay = 0;
282    rec->kill_me = 0;
283    if(delta>=DELAY)
284    {
285        rec->kill_me = 1;
286        remove_screen(screen_list, screen_list->pty, 1);
287        screen_list->pty = screen_list->prevpty;
288        screen_list->prevpty = 0;
289        screen_list->dont_update_coords = 0;
290    }
291    else
292    {
293        float r = 1 - ((DELAY - (DELAY - delta)) / DELAY);
294        caca_canvas_t *old, *new;
295        struct screen *s = screen_list->screen[screen_list->pty];
296        int w = s->orig_w * r;
297        int h = s->orig_h * r;
298
299        /* libcaca canvas resize function is bugged, do it by hand  */
300        old = s->cv;
301        new = caca_create_canvas(w, h);
302        caca_blit(new, 0, 0, old, NULL);
303        s->cv = new;
304        caca_free_canvas(old);
305        set_tty_size(s->fd, w, h);
306
307        s->w = w;
308        s->h = h;
309
310        s->x =
311            (s->orig_x * r) +
312            ((s->orig_w/2) - s->w/2);
313        s->y =
314            (s->orig_y * r) +
315            ((s->orig_h/2) - s->h/2);
316    }
317    return 1;
318}
Note: See TracBrowser for help on using the repository browser.