| 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 <cucul.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 | |
|---|
| 29 | void draw_thumbnails(struct screen_list *screen_list) |
|---|
| 30 | { |
|---|
| 31 | char const * const *fonts; |
|---|
| 32 | cucul_dither_t *d; |
|---|
| 33 | cucul_font_t *f; |
|---|
| 34 | uint8_t *buf; |
|---|
| 35 | int i; |
|---|
| 36 | int miniw, minih; |
|---|
| 37 | |
|---|
| 38 | if(screen_list->count) |
|---|
| 39 | { |
|---|
| 40 | fonts = cucul_get_font_list(); |
|---|
| 41 | f = cucul_load_font(fonts[0], 0); |
|---|
| 42 | |
|---|
| 43 | miniw = cucul_get_canvas_width(screen_list->screen[0]->cv) |
|---|
| 44 | * cucul_get_font_width(f); |
|---|
| 45 | minih = cucul_get_canvas_height(screen_list->screen[0]->cv) |
|---|
| 46 | * cucul_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 = cucul_create_dither(32, miniw, minih, 4 * miniw, |
|---|
| 57 | 0xff0000, 0xff00, 0xff, 0x0); |
|---|
| 58 | else |
|---|
| 59 | d = cucul_create_dither(32, miniw, minih, 4 * miniw, |
|---|
| 60 | 0xff00, 0xff0000, 0xff000000, 0x0); |
|---|
| 61 | |
|---|
| 62 | for(i = 0; i < screen_list->count; i++) |
|---|
| 63 | { |
|---|
| 64 | cucul_render_canvas(screen_list->screen[i]->cv, f, buf, |
|---|
| 65 | miniw, minih, miniw * 4); |
|---|
| 66 | cucul_dither_bitmap(screen_list->cv, 20 * i, |
|---|
| 67 | cucul_get_canvas_height(screen_list->cv) - 6, 19, 6, d, buf); |
|---|
| 68 | cucul_set_color_ansi(screen_list->cv, CUCUL_WHITE, CUCUL_BLUE); |
|---|
| 69 | |
|---|
| 70 | if(screen_list->pty == i) |
|---|
| 71 | cucul_draw_cp437_box(screen_list->cv,20 * i, |
|---|
| 72 | cucul_get_canvas_height(screen_list->cv) - 6, 19, 6); |
|---|
| 73 | cucul_printf(screen_list->cv, 20 * i, |
|---|
| 74 | cucul_get_canvas_height(screen_list->cv) - 6, "(%i)", i+1); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | cucul_free_dither(d); |
|---|
| 78 | cucul_free_font(f); |
|---|
| 79 | |
|---|
| 80 | free(buf); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | /* FIXME, make this stuff more configurable */ |
|---|
| 86 | void draw_status(struct screen_list *screen_list) |
|---|
| 87 | { |
|---|
| 88 | int x = 0, y = cucul_get_canvas_height(screen_list->cv) - 1; |
|---|
| 89 | |
|---|
| 90 | |
|---|
| 91 | cucul_set_color_ansi(screen_list->cv, CUCUL_BLUE, CUCUL_BLUE); |
|---|
| 92 | cucul_fill_box(screen_list->cv, |
|---|
| 93 | x, y, |
|---|
| 94 | cucul_get_canvas_width(screen_list->cv), 1, '#'); |
|---|
| 95 | |
|---|
| 96 | /* Hour */ |
|---|
| 97 | { |
|---|
| 98 | time_t now = time ((time_t *) 0); |
|---|
| 99 | struct tm *t = localtime(&now); |
|---|
| 100 | char hour[256]; |
|---|
| 101 | sprintf(hour, "%02d:%02d", t->tm_hour, t->tm_min); |
|---|
| 102 | |
|---|
| 103 | cucul_set_color_ansi(screen_list->cv, CUCUL_LIGHTBLUE, CUCUL_BLUE); |
|---|
| 104 | cucul_printf(screen_list->cv, x, y, |
|---|
| 105 | "["); |
|---|
| 106 | |
|---|
| 107 | cucul_set_color_ansi(screen_list->cv, CUCUL_DEFAULT, CUCUL_BLUE); |
|---|
| 108 | cucul_printf(screen_list->cv, x+1, y, |
|---|
| 109 | hour); |
|---|
| 110 | cucul_set_color_ansi(screen_list->cv, CUCUL_LIGHTBLUE, CUCUL_BLUE); |
|---|
| 111 | cucul_printf(screen_list->cv, x + strlen(hour) + 1, y, |
|---|
| 112 | "]"); |
|---|
| 113 | x += 7; |
|---|
| 114 | |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | /* Window */ |
|---|
| 118 | { |
|---|
| 119 | char text[256]; |
|---|
| 120 | sprintf(text, "%d/%d", screen_list->pty+1, screen_list->count); |
|---|
| 121 | x++; |
|---|
| 122 | cucul_set_color_ansi(screen_list->cv, CUCUL_LIGHTBLUE, CUCUL_BLUE); |
|---|
| 123 | cucul_printf(screen_list->cv, x, y, "Window:"); |
|---|
| 124 | cucul_set_color_ansi(screen_list->cv, CUCUL_DEFAULT, CUCUL_BLUE); |
|---|
| 125 | cucul_printf(screen_list->cv, x+8, y, text); |
|---|
| 126 | x+= 8+strlen(text); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | /* Window Manager */ |
|---|
| 130 | { |
|---|
| 131 | char text[256]; |
|---|
| 132 | |
|---|
| 133 | switch(screen_list->wm_type) |
|---|
| 134 | { |
|---|
| 135 | case WM_CARD: |
|---|
| 136 | strcpy(text, "card"); |
|---|
| 137 | break; |
|---|
| 138 | case WM_HSPLIT: |
|---|
| 139 | strcpy(text, "hsplit"); |
|---|
| 140 | break; |
|---|
| 141 | case WM_VSPLIT: |
|---|
| 142 | strcpy(text, "vsplit"); |
|---|
| 143 | break; |
|---|
| 144 | case WM_FULL: |
|---|
| 145 | default: |
|---|
| 146 | strcpy(text, "full"); |
|---|
| 147 | break; |
|---|
| 148 | |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | x++; |
|---|
| 152 | cucul_set_color_ansi(screen_list->cv, CUCUL_LIGHTBLUE, CUCUL_BLUE); |
|---|
| 153 | cucul_printf(screen_list->cv, x, y, "WM:"); |
|---|
| 154 | cucul_set_color_ansi(screen_list->cv, CUCUL_DEFAULT, CUCUL_BLUE); |
|---|
| 155 | cucul_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 | x = cucul_get_canvas_width(screen_list->cv) - strlen(text); |
|---|
| 164 | |
|---|
| 165 | cucul_set_color_ansi(screen_list->cv, CUCUL_DEFAULT, CUCUL_BLUE); |
|---|
| 166 | cucul_printf(screen_list->cv, x, y, text); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | |
|---|
| 173 | void draw_help(struct screen_list *screen_list) |
|---|
| 174 | { |
|---|
| 175 | int w = 65, h = 20; |
|---|
| 176 | int x = (cucul_get_canvas_width(screen_list->cv) - w) / 2; |
|---|
| 177 | int y = (cucul_get_canvas_height(screen_list->cv) - h) / 2; |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | cucul_set_color_ansi(screen_list->cv, CUCUL_BLUE, CUCUL_BLUE); |
|---|
| 181 | cucul_fill_box(screen_list->cv, |
|---|
| 182 | x, y, |
|---|
| 183 | w, h, '#'); |
|---|
| 184 | cucul_set_color_ansi(screen_list->cv, CUCUL_DEFAULT, CUCUL_BLUE); |
|---|
| 185 | cucul_draw_cp437_box(screen_list->cv, |
|---|
| 186 | x, y, |
|---|
| 187 | w, h); |
|---|
| 188 | |
|---|
| 189 | x+=2; |
|---|
| 190 | y++; |
|---|
| 191 | cucul_printf(screen_list->cv, |
|---|
| 192 | (cucul_get_canvas_width(screen_list->cv) - strlen(PACKAGE_STRING)) / 2, |
|---|
| 193 | y-1, |
|---|
| 194 | PACKAGE_STRING); |
|---|
| 195 | cucul_printf(screen_list->cv, x, y++, "Copyright (c) 2006-2008 "); |
|---|
| 196 | cucul_printf(screen_list->cv, x, y++, " Sam Hocevar <sam@zoy.org>"); |
|---|
| 197 | cucul_printf(screen_list->cv, x, y++, " Jean-Yves Lamoureux <jylam@lnxscene.org>"); |
|---|
| 198 | cucul_printf(screen_list->cv, x, y++, " Pascal Terjan <pterjan@linuxfr.org>"); |
|---|
| 199 | cucul_printf(screen_list->cv, x, y++, ""); |
|---|
| 200 | cucul_printf(screen_list->cv, x, y++, ""); |
|---|
| 201 | cucul_printf(screen_list->cv, x, y++, "All shortcuts are in format 'ctrl-a-X' where X is :"); |
|---|
| 202 | cucul_printf(screen_list->cv, x, y++, "n:\t Next window"); |
|---|
| 203 | cucul_printf(screen_list->cv, x, y++, "p:\t Previous window"); |
|---|
| 204 | cucul_printf(screen_list->cv, x, y++, "w:\t Switch window manager"); |
|---|
| 205 | cucul_printf(screen_list->cv, x, y++, "c:\t Create new window"); |
|---|
| 206 | cucul_printf(screen_list->cv, x, y++, "m:\t Thumbnails"); |
|---|
| 207 | cucul_printf(screen_list->cv, x, y++, "d:\t Detach"); |
|---|
| 208 | cucul_printf(screen_list->cv, x, y++, "k:\t Close window and kill associated process"); |
|---|
| 209 | cucul_printf(screen_list->cv, x, y++, "h:\t This help"); |
|---|
| 210 | cucul_printf(screen_list->cv, x, y++, ""); |
|---|
| 211 | cucul_printf(screen_list->cv, x, y++, ""); |
|---|
| 212 | cucul_printf(screen_list->cv, x, y++, "See http://libcaca.zoy.org/wiki/neercs for more informations"); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | /* Close a window by animating it collapsing */ |
|---|
| 218 | /* Total close time */ |
|---|
| 219 | #define DELAY 500000.0f |
|---|
| 220 | int close_screen_recurrent(struct screen_list* screen_list, struct recurrent* rec, void *user, long long unsigned int t) |
|---|
| 221 | { |
|---|
| 222 | long long unsigned int delta = t - rec->start_time; |
|---|
| 223 | |
|---|
| 224 | screen_list->dont_update_coords = 1; |
|---|
| 225 | rec->kill_me = 0; |
|---|
| 226 | if(delta>=DELAY) |
|---|
| 227 | { |
|---|
| 228 | rec->kill_me = 1; |
|---|
| 229 | remove_screen(screen_list, screen_list->pty, 1); |
|---|
| 230 | screen_list->pty = screen_list->prevpty; |
|---|
| 231 | screen_list->prevpty = 0; |
|---|
| 232 | screen_list->dont_update_coords = 0; |
|---|
| 233 | } |
|---|
| 234 | else |
|---|
| 235 | { |
|---|
| 236 | float r = 1 - ((DELAY - (DELAY - delta)) / DELAY); |
|---|
| 237 | cucul_canvas_t *old, *new; |
|---|
| 238 | struct screen *s = screen_list->screen[screen_list->pty]; |
|---|
| 239 | int w = s->orig_w * r; |
|---|
| 240 | int h = s->orig_h * r; |
|---|
| 241 | |
|---|
| 242 | /* libcucul canvas resize function is bugged, do it by hand */ |
|---|
| 243 | old = s->cv; |
|---|
| 244 | new = cucul_create_canvas(w, h); |
|---|
| 245 | cucul_blit(new, 0, 0, old, NULL); |
|---|
| 246 | s->cv = new; |
|---|
| 247 | cucul_free_canvas(old); |
|---|
| 248 | set_tty_size(s->fd, w, h); |
|---|
| 249 | |
|---|
| 250 | s->w = w; |
|---|
| 251 | s->h = h; |
|---|
| 252 | |
|---|
| 253 | s->x = |
|---|
| 254 | (s->orig_x * r) + |
|---|
| 255 | ((s->orig_w/2) - s->w/2); |
|---|
| 256 | s->y = |
|---|
| 257 | (s->orig_y * r) + |
|---|
| 258 | ((s->orig_h/2) - s->h/2); |
|---|
| 259 | } |
|---|
| 260 | return 1; |
|---|
| 261 | } |
|---|