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