| 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$ |
|---|
| 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 <stdint.h> |
|---|
| 17 | |
|---|
| 18 | #include <caca.h> |
|---|
| 19 | |
|---|
| 20 | enum wm_types |
|---|
| 21 | { |
|---|
| 22 | WM_FULL, |
|---|
| 23 | WM_CARD, |
|---|
| 24 | WM_HSPLIT, |
|---|
| 25 | WM_VSPLIT, |
|---|
| 26 | WM_CUBE, |
|---|
| 27 | |
|---|
| 28 | WM_MAX, |
|---|
| 29 | }; |
|---|
| 30 | |
|---|
| 31 | struct option |
|---|
| 32 | { |
|---|
| 33 | char *key; |
|---|
| 34 | char *value; |
|---|
| 35 | |
|---|
| 36 | struct option *next; |
|---|
| 37 | }; |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | /* ISO-2022 Conversion State */ |
|---|
| 42 | struct iso2022_conv_state |
|---|
| 43 | { |
|---|
| 44 | /* cs = coding system/coding method: */ |
|---|
| 45 | /* (with standard return) */ |
|---|
| 46 | /* '@' = ISO-2022, */ |
|---|
| 47 | /* 'G' = UTF-8 without implementation level, */ |
|---|
| 48 | /* '8' = UTF-8 (Linux console and imitators), */ |
|---|
| 49 | /* and many others that are rarely used; */ |
|---|
| 50 | /* (without standard return) */ |
|---|
| 51 | /* '/G' = UTF-8 Level 1, */ |
|---|
| 52 | /* '/H' = UTF-8 Level 2, */ |
|---|
| 53 | /* '/I' = UTF-8 Level 3, */ |
|---|
| 54 | /* and many others that are rarely used */ |
|---|
| 55 | uint32_t cs; |
|---|
| 56 | /* ctrl8bit = allow 8-bit controls */ |
|---|
| 57 | uint8_t ctrl8bit; |
|---|
| 58 | /* cn[0] = C0 control charset (0x00 ... 0x1f): |
|---|
| 59 | * '@' = ISO 646, |
|---|
| 60 | * '~' = empty, |
|---|
| 61 | * and many others that are rarely used */ |
|---|
| 62 | /* cn[1] = C1 control charset (0x80 ... 0x9f): |
|---|
| 63 | * 'C' = ISO 6429-1983, |
|---|
| 64 | * '~' = empty, |
|---|
| 65 | * and many others that are rarely used */ |
|---|
| 66 | uint32_t cn[2]; |
|---|
| 67 | /* glr[0] = GL graphic charset (94-char. 0x21 ... 0x7e, |
|---|
| 68 | * 94x94-char. 0x21/0x21 ... 0x7e/0x7e), |
|---|
| 69 | * and |
|---|
| 70 | * glr[1] = GR graphic charset (94-char. 0xa1 ... 0xfe, |
|---|
| 71 | * 96-char. 0xa0 ... 0xff, |
|---|
| 72 | * 94x94-char. 0xa1/0xa1 ... 0xfe/0xfe, |
|---|
| 73 | * 96x96-char. 0xa0/0xa0 ... 0xff/0xff): |
|---|
| 74 | * 0 = G0, 1 = G1, 2 = G2, 3 = G3 */ |
|---|
| 75 | uint8_t glr[2]; |
|---|
| 76 | /* gn[i] = G0/G1/G2/G3 graphic charset state: |
|---|
| 77 | * (94-char. sets) |
|---|
| 78 | * '0' = DEC ACS (VT100 and imitators), |
|---|
| 79 | * 'B' = US-ASCII, |
|---|
| 80 | * and many others that are rarely used for e.g. various national ASCII variations; |
|---|
| 81 | * (96-char. sets) |
|---|
| 82 | * '.A' = ISO 8859-1 "Latin 1" GR, |
|---|
| 83 | * '.~' = empty 96-char. set, |
|---|
| 84 | * and many others that are rarely used for e.g. ISO 8859-n GR; |
|---|
| 85 | * (double-byte 94x94-charsets) |
|---|
| 86 | * '$@' = Japanese Character Set ("old JIS") (JIS C 6226:1978), |
|---|
| 87 | * '$A' = Chinese Character Set (GB 2312), |
|---|
| 88 | * '$B' = Japanese Character Set (JIS X0208/JIS C 6226:1983), |
|---|
| 89 | * '$C' = Korean Graphic Character Set (KSC 5601:1987), |
|---|
| 90 | * '$D' = Supplementary Japanese Graphic Character Set (JIS X0212), |
|---|
| 91 | * '$E' = CCITT Chinese Set (GB 2312 + GB 8565), |
|---|
| 92 | * '$G' = CNS 11643 plane 1, |
|---|
| 93 | * '$H' = CNS 11643 plane 2, |
|---|
| 94 | * '$I' = CNS 11643 plane 3, |
|---|
| 95 | * '$J' = CNS 11643 plane 4, |
|---|
| 96 | * '$K' = CNS 11643 plane 5, |
|---|
| 97 | * '$L' = CNS 11643 plane 6, |
|---|
| 98 | * '$M' = CNS 11643 plane 7, |
|---|
| 99 | * '$O' = JIS X 0213 plane 1, |
|---|
| 100 | * '$P' = JIS X 0213 plane 2, |
|---|
| 101 | * '$Q' = JIS X 0213-2004 Plane 1, |
|---|
| 102 | * and many others that are rarely used for e.g. traditional |
|---|
| 103 | * ideographic Vietnamese and BlissSymbolics; |
|---|
| 104 | * (double-byte 96x96-charsets) |
|---|
| 105 | * none standardized or in use on terminals AFAIK (Mule does use |
|---|
| 106 | * some internally) |
|---|
| 107 | */ |
|---|
| 108 | uint32_t gn[4]; |
|---|
| 109 | /* ss = single-shift state: 0 = GL, 2 = G2, 3 = G3 */ |
|---|
| 110 | uint8_t ss; |
|---|
| 111 | }; |
|---|
| 112 | |
|---|
| 113 | struct screen |
|---|
| 114 | { |
|---|
| 115 | /* Graphics stuff */ |
|---|
| 116 | int init; |
|---|
| 117 | caca_canvas_t *cv; |
|---|
| 118 | uint32_t clearattr; |
|---|
| 119 | uint8_t fg, bg; /* ANSI-context fg/bg */ |
|---|
| 120 | uint8_t dfg, dbg; /* Default fg/bg */ |
|---|
| 121 | uint8_t bold, blink, italics, negative, concealed, underline; |
|---|
| 122 | uint8_t faint, strike, proportional; /* unsupported */ |
|---|
| 123 | struct iso2022_conv_state conv_state; /* charset mess */ |
|---|
| 124 | |
|---|
| 125 | /* Other stuff */ |
|---|
| 126 | int visible; /* Draw canvas and border flag */ |
|---|
| 127 | int fd; /* pty fd */ |
|---|
| 128 | unsigned char *buf; /* text buffer */ |
|---|
| 129 | long int total; /* buffer length */ |
|---|
| 130 | char *title; /* tty title */ |
|---|
| 131 | int bell; /* bell occuring */ |
|---|
| 132 | unsigned int scroll, s1, s2; /* FIXME, ANSI scroll properties */ |
|---|
| 133 | int pid; /* running program pid */ |
|---|
| 134 | int changed; /* content was updated */ |
|---|
| 135 | |
|---|
| 136 | int x, y; /* Canvas position */ |
|---|
| 137 | int w, h; /* Canvas size */ |
|---|
| 138 | |
|---|
| 139 | int orig_x, orig_y; /* Used by recurrents */ |
|---|
| 140 | int orig_w, orig_h; /* Used by recurrents */ |
|---|
| 141 | }; |
|---|
| 142 | |
|---|
| 143 | enum socket_type |
|---|
| 144 | { |
|---|
| 145 | SOCK_SERVER=0, |
|---|
| 146 | SOCK_CLIENT=1 |
|---|
| 147 | }; |
|---|
| 148 | |
|---|
| 149 | struct cube_props |
|---|
| 150 | { |
|---|
| 151 | int in_switch; |
|---|
| 152 | int side; |
|---|
| 153 | long long unsigned int duration; |
|---|
| 154 | }; |
|---|
| 155 | |
|---|
| 156 | struct screen_list |
|---|
| 157 | { |
|---|
| 158 | int wm_type; /* Window manager type */ |
|---|
| 159 | int in_bell; /* Bell occuring in a window */ |
|---|
| 160 | int dont_update_coords; /* Used by recurrents */ |
|---|
| 161 | int changed; /* Global redraw needed (e.g. adding a screen) */ |
|---|
| 162 | int delay; /* Minimal time between two refresh (ms) */ |
|---|
| 163 | int force_refresh; |
|---|
| 164 | long long unsigned int last_key_time; |
|---|
| 165 | int command; |
|---|
| 166 | int was_in_bell; |
|---|
| 167 | long long unsigned int last_refresh_time; |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | /* Detaching */ |
|---|
| 171 | int attached; /* Are we attached to a terminal */ |
|---|
| 172 | int socket[2]; /* Sockets to write to the server / to the client */ |
|---|
| 173 | char *socket_path[2]; /* Sockets to write to the server / to the client */ |
|---|
| 174 | char *socket_dir; /* Where to create the socket */ |
|---|
| 175 | char *session_name; /* Name of the session */ |
|---|
| 176 | |
|---|
| 177 | /* Lock */ |
|---|
| 178 | int locked; |
|---|
| 179 | int lock_offset; |
|---|
| 180 | int lock_on_detach; |
|---|
| 181 | long long unsigned int autolock_timeout; |
|---|
| 182 | char lockpass[1024]; |
|---|
| 183 | char lockmsg[1024]; |
|---|
| 184 | |
|---|
| 185 | /* Add-ons*/ |
|---|
| 186 | int mini; /* Thumbnails */ |
|---|
| 187 | int status; /* Status bar */ |
|---|
| 188 | int help; /* Help */ |
|---|
| 189 | int window_list; /* Window list */ |
|---|
| 190 | int cur_in_list; /* Window list */ |
|---|
| 191 | char *title; /* Window title */ |
|---|
| 192 | struct cube_props cube; /* Cube */ |
|---|
| 193 | long long unsigned int last_switch; /* Cube */ |
|---|
| 194 | |
|---|
| 195 | /* ScreenSaver stuff */ |
|---|
| 196 | long long unsigned int screensaver_timeout; /* Screensaver timeout in us */ |
|---|
| 197 | int in_screensaver; |
|---|
| 198 | void *screensaver_data; |
|---|
| 199 | |
|---|
| 200 | int pty, prevpty; /* Current and previous window */ |
|---|
| 201 | int count; /* Window count */ |
|---|
| 202 | int width, height; /* caca window size */ |
|---|
| 203 | struct screen **screen; /* Windows */ |
|---|
| 204 | |
|---|
| 205 | /* Option parsing and configuration */ |
|---|
| 206 | struct option *config; |
|---|
| 207 | char *default_shell; |
|---|
| 208 | struct recurrent_list *recurrent_list; |
|---|
| 209 | char *user_path; |
|---|
| 210 | int *to_grab; |
|---|
| 211 | char **to_start; |
|---|
| 212 | int nb_to_grab; |
|---|
| 213 | int attach, forceattach; |
|---|
| 214 | int requested_delay; |
|---|
| 215 | |
|---|
| 216 | caca_canvas_t *cv; |
|---|
| 217 | caca_display_t *dp; |
|---|
| 218 | }; |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | struct recurrent |
|---|
| 222 | { |
|---|
| 223 | int (*function)(struct screen_list*, struct recurrent* rec, void *user, long long unsigned int t); |
|---|
| 224 | void *user; |
|---|
| 225 | long long unsigned int start_time; |
|---|
| 226 | int kill_me; |
|---|
| 227 | }; |
|---|
| 228 | |
|---|
| 229 | struct recurrent_list |
|---|
| 230 | { |
|---|
| 231 | int count; |
|---|
| 232 | struct recurrent **recurrent; |
|---|
| 233 | }; |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | void version(void); |
|---|
| 238 | void usage(int argc, char **argv); |
|---|
| 239 | |
|---|
| 240 | int handle_command_line(int argc, char *argv[], struct screen_list *screen_list); |
|---|
| 241 | |
|---|
| 242 | struct screen_list *create_screen_list(void); |
|---|
| 243 | void free_screen_list(struct screen_list *screen_list); |
|---|
| 244 | |
|---|
| 245 | int start_client(struct screen_list * screen_list); |
|---|
| 246 | void mainloop(struct screen_list *screen_list); |
|---|
| 247 | |
|---|
| 248 | int create_pty(char *cmd, unsigned int w, unsigned int h, int *cpid); |
|---|
| 249 | int create_pty_grab(long pid, unsigned int w, unsigned int h, int *cpid); |
|---|
| 250 | int grab_process(long pid, char *ptyname, int ptyfd, int *newpid); |
|---|
| 251 | long select_process(struct screen_list* screen_list); |
|---|
| 252 | |
|---|
| 253 | long int import_term(struct screen_list *screen_list, struct screen *sc, void const *data, unsigned int size); |
|---|
| 254 | int set_tty_size(int fd, unsigned int w, unsigned int h); |
|---|
| 255 | int update_terms(struct screen_list* screen_list); |
|---|
| 256 | void refresh_screens(struct screen_list *screen_list); |
|---|
| 257 | int update_screens_contents(struct screen_list* screen_list); |
|---|
| 258 | long long get_us(void); |
|---|
| 259 | |
|---|
| 260 | void attach(struct screen_list* screen_list); |
|---|
| 261 | int detach(struct screen_list* screen_list); |
|---|
| 262 | int request_attach(struct screen_list* screen_list); |
|---|
| 263 | char * build_socket_path(char *socket_dir, char *session_name, enum socket_type socktype); |
|---|
| 264 | int create_socket(struct screen_list* screen_list, enum socket_type socktype); |
|---|
| 265 | char * connect_socket(struct screen_list* screen_list, enum socket_type socktype); |
|---|
| 266 | char ** list_sockets(char *socket_dir, char *session_name); |
|---|
| 267 | int start_server(struct screen_list *screen_list); |
|---|
| 268 | int send_event(caca_event_t ev, struct screen_list* screen_list); |
|---|
| 269 | int send_delay(struct screen_list* screen_list); |
|---|
| 270 | |
|---|
| 271 | /* Screens management */ |
|---|
| 272 | struct screen* create_screen(int w, int h, char *command); |
|---|
| 273 | struct screen* create_screen_grab(int w, int h, int pid); |
|---|
| 274 | int destroy_screen(struct screen *s); |
|---|
| 275 | int add_screen(struct screen_list *list, struct screen *s); |
|---|
| 276 | int remove_screen(struct screen_list *list, int n, int please_kill); |
|---|
| 277 | void resize_screen(struct screen *s, int z, int h); |
|---|
| 278 | |
|---|
| 279 | /* Window managers */ |
|---|
| 280 | void update_windows_props(struct screen_list *screen_list); |
|---|
| 281 | void update_windows_props_cards(struct screen_list *screen_list); |
|---|
| 282 | void update_windows_props_hsplit(struct screen_list *screen_list); |
|---|
| 283 | void update_windows_props_full(struct screen_list *screen_list); |
|---|
| 284 | void update_windows_props_vsplit(struct screen_list *screen_list); |
|---|
| 285 | void update_windows_props_cube(struct screen_list *screen_list); |
|---|
| 286 | |
|---|
| 287 | void wm_refresh(struct screen_list *screen_list); |
|---|
| 288 | void wm_refresh_card(struct screen_list *screen_list); |
|---|
| 289 | void wm_refresh_cube(struct screen_list *screen_list); |
|---|
| 290 | void wm_refresh_full(struct screen_list *screen_list); |
|---|
| 291 | void wm_refresh_hsplit(struct screen_list *screen_list); |
|---|
| 292 | void wm_refresh_vsplit(struct screen_list *screen_list); |
|---|
| 293 | int switch_screen_recurrent(struct screen_list* screen_list, struct recurrent* rec, void *user, long long unsigned int t); |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | /* Effects and addons */ |
|---|
| 297 | void draw_thumbnails(struct screen_list *screen_list); |
|---|
| 298 | void draw_status(struct screen_list *screen_list); |
|---|
| 299 | void draw_help(struct screen_list *screen_list); |
|---|
| 300 | int help_handle_key(struct screen_list *screen_list, unsigned int c); |
|---|
| 301 | int update_window_list(int c, struct screen_list *screen_list); |
|---|
| 302 | void draw_list(struct screen_list *screen_list); |
|---|
| 303 | void draw_lock(struct screen_list *screen_list); |
|---|
| 304 | int update_lock(int c, struct screen_list *screen_list); |
|---|
| 305 | int validate_lock(struct screen_list *screen_list, char *user, char *pass); |
|---|
| 306 | |
|---|
| 307 | int close_screen_recurrent(struct screen_list*, struct recurrent* rec, void *user, long long unsigned int t); |
|---|
| 308 | |
|---|
| 309 | /* Input to ANSI */ |
|---|
| 310 | void *convert_input_ansi(unsigned int *c, int *size); |
|---|
| 311 | int handle_command_input(struct screen_list*screen_list, unsigned int c); |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | /* Screensavers */ |
|---|
| 315 | void screensaver_init(struct screen_list *screen_list); |
|---|
| 316 | void screensaver_kill(struct screen_list *screen_list); |
|---|
| 317 | |
|---|
| 318 | void draw_screensaver(struct screen_list *screen_list); |
|---|
| 319 | void screensaver_flying_toasters(struct screen_list *screen_list); |
|---|
| 320 | |
|---|
| 321 | void screensaver_flying_toasters_init(struct screen_list *screen_list); |
|---|
| 322 | |
|---|
| 323 | void screensaver_flying_toasters_kill(struct screen_list *screen_list); |
|---|
| 324 | |
|---|
| 325 | |
|---|
| 326 | |
|---|
| 327 | /* Recurrents */ |
|---|
| 328 | int handle_recurrents(struct screen_list* screen_list); |
|---|
| 329 | int add_recurrent(struct recurrent_list *list, |
|---|
| 330 | int (*function)(struct screen_list*, struct recurrent* rec, void *user, long long unsigned int t), |
|---|
| 331 | void *user); |
|---|
| 332 | int remove_recurrent(struct recurrent_list *list, int n); |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | |
|---|
| 336 | /* Configuration file */ |
|---|
| 337 | int read_configuration_file(char *filename, struct screen_list *screen_list); |
|---|
| 338 | int parse_conf_line(char *buf, int size, struct screen_list *screen_list); |
|---|
| 339 | int get_key_value(char *line, struct option *option); |
|---|
| 340 | int fill_config(struct screen_list *screen_list); |
|---|
| 341 | |
|---|
| 342 | #if defined DEBUG |
|---|
| 343 | # include <stdio.h> |
|---|
| 344 | # include <stdarg.h> |
|---|
| 345 | static inline void debug(const char *format, ...) |
|---|
| 346 | { |
|---|
| 347 | va_list args; |
|---|
| 348 | va_start(args, format); |
|---|
| 349 | fprintf(stderr, "** neercs debug ** "); |
|---|
| 350 | vfprintf(stderr, format, args); |
|---|
| 351 | fprintf(stderr, "\n"); |
|---|
| 352 | va_end(args); |
|---|
| 353 | } |
|---|
| 354 | #else |
|---|
| 355 | # define debug(format, ...) do {} while(0) |
|---|
| 356 | #endif |
|---|
| 357 | |
|---|