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

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