Changeset 3969
- Timestamp:
- Nov 19, 2009, 5:26:53 PM (13 years ago)
- Location:
- neercs/trunk/src
- Files:
-
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
neercs/trunk/src/attach.c
r3968 r3969 1 /* 1 /* 2 2 * neercs console-based window manager 3 3 * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> … … 33 33 #include "neercs.h" 34 34 35 char * build_socket_path(char *socket_dir, char *session_name, enum socket_type socktype) 35 char *build_socket_path(char *socket_dir, char *session_name, 36 enum socket_type socktype) 36 37 { 37 38 char *path, *dir; 38 path = (char *)malloc(PATH_MAX +1);39 path = (char *)malloc(PATH_MAX + 1); 39 40 dir = socket_dir; 40 if (!dir)41 if (!dir) 41 42 dir = getenv("NEERCSDIR"); 42 if (!dir)43 if (!dir) 43 44 dir = getenv("TMPDIR"); 44 if (!dir)45 if (!dir) 45 46 dir = "/tmp"; 46 if(path) 47 snprintf(path, PATH_MAX+1, "%s/neercs.%s%s.sock", dir, session_name, socktype?"":".srv"); 47 if (path) 48 snprintf(path, PATH_MAX + 1, "%s/neercs.%s%s.sock", dir, session_name, 49 socktype ? "" : ".srv"); 48 50 return path; 49 51 } 50 52 51 static char * socket_to_session(char const *sockpath)53 static char *socket_to_session(char const *sockpath) 52 54 { 53 55 char *p, *s; 54 56 p = strrchr(sockpath, '/'); 55 if (!p)57 if (!p) 56 58 { 57 59 debug("Invalid socket path %s", sockpath); 58 60 return NULL; 59 61 } 60 p +=8;/* skip neercs. */62 p += 8; /* skip neercs. */ 61 63 s = strdup(p); 62 64 p = strrchr(s, '.'); 63 *p = '\0'; /* drop .sock */65 *p = '\0'; /* drop .sock */ 64 66 p = strrchr(s, '.'); 65 *p = '\0'; /* drop .srv */67 *p = '\0'; /* drop .srv */ 66 68 p = strdup(s); 67 69 free(s); … … 69 71 } 70 72 71 int create_socket(struct screen_list *screen_list, enum socket_type socktype)73 int create_socket(struct screen_list *screen_list, enum socket_type socktype) 72 74 { 73 75 int sock; … … 76 78 sock = socket(AF_UNIX, SOCK_DGRAM, 0); 77 79 78 if (sock < 0)80 if (sock < 0) 79 81 { 80 82 perror("create_socket:socket"); … … 85 87 86 88 myaddr.sun_family = AF_UNIX; 87 strncpy(myaddr.sun_path, screen_list->socket_path[socktype], sizeof(myaddr.sun_path) - 1); 89 strncpy(myaddr.sun_path, screen_list->socket_path[socktype], 90 sizeof(myaddr.sun_path) - 1); 88 91 89 92 unlink(screen_list->socket_path[socktype]); 90 93 91 if (bind(sock, (struct sockaddr *)&myaddr, sizeof(struct sockaddr_un)) < 0)94 if (bind(sock, (struct sockaddr *)&myaddr, sizeof(struct sockaddr_un)) < 0) 92 95 { 93 96 free(screen_list->socket_path[socktype]); … … 106 109 } 107 110 108 char ** 111 char **list_sockets(char *socket_dir, char *session_name) 109 112 { 110 113 char *pattern, *dir; … … 113 116 globbuf.gl_pathv = NULL; 114 117 115 pattern = (char *)malloc(PATH_MAX +1);118 pattern = (char *)malloc(PATH_MAX + 1); 116 119 117 120 dir = socket_dir; 118 if (!dir)121 if (!dir) 119 122 dir = getenv("NEERCSDIR"); 120 if (!dir)123 if (!dir) 121 124 dir = getenv("TMPDIR"); 122 if (!dir)125 if (!dir) 123 126 dir = "/tmp"; 124 127 125 if (!pattern)128 if (!pattern) 126 129 return globbuf.gl_pathv; 127 130 128 if (session_name && strlen(session_name)+strlen(dir)+13<PATH_MAX)131 if (session_name && strlen(session_name) + strlen(dir) + 13 < PATH_MAX) 129 132 sprintf(pattern, "%s/neercs.%s.srv.sock", dir, session_name); 130 133 else … … 141 144 } 142 145 143 char * connect_socket(struct screen_list* screen_list, enum socket_type socktype) 146 char *connect_socket(struct screen_list *screen_list, 147 enum socket_type socktype) 144 148 { 145 149 int sock; … … 149 153 150 154 /* Open the socket */ 151 if ((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) 155 if ((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) 152 156 { 153 157 perror("connect_server:socket"); … … 155 159 } 156 160 157 memset(&addr, 0,sizeof(addr));161 memset(&addr, 0, sizeof(addr)); 158 162 addr.sun_family = AF_UNIX; 159 strcpy(addr.sun_path, screen_list->socket_path[socktype]);163 strcpy(addr.sun_path, screen_list->socket_path[socktype]); 160 164 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) 161 165 { 162 debug("Failed to connect to %s: %s", screen_list->socket_path[socktype], strerror(errno)); 166 debug("Failed to connect to %s: %s", 167 screen_list->socket_path[socktype], strerror(errno)); 163 168 close(sock); 164 169 return NULL; … … 168 173 screen_list->socket[socktype] = sock; 169 174 170 if (socktype == SOCK_SERVER)175 if (socktype == SOCK_SERVER) 171 176 { 172 177 return socket_to_session(screen_list->socket_path[socktype]); … … 176 181 } 177 182 178 int request_attach(struct screen_list *screen_list)183 int request_attach(struct screen_list *screen_list) 179 184 { 180 185 char buf[41]; 181 186 int bytes; 182 187 183 bytes = snprintf(buf, sizeof(buf) -1, "ATTACH %10d %10d %10d",188 bytes = snprintf(buf, sizeof(buf) - 1, "ATTACH %10d %10d %10d", 184 189 caca_get_canvas_width(screen_list->cv), 185 190 caca_get_canvas_height(screen_list->cv), … … 190 195 } 191 196 192 static char * select_socket(struct screen_list*screen_list)197 static char *select_socket(struct screen_list *screen_list) 193 198 { 194 199 char **sockets = NULL, **usable_sockets = NULL; … … 197 202 198 203 sockets = list_sockets(screen_list->socket_dir, screen_list->session_name); 199 if (sockets)200 { 201 for (i=0; sockets[i]; i++);204 if (sockets) 205 { 206 for (i = 0; sockets[i]; i++); 202 207 203 208 /* Return the socket or NULL if there is not more than one match */ 204 if (i<=1)205 { 206 if (sockets[0])209 if (i <= 1) 210 { 211 if (sockets[0]) 207 212 ret = strdup(sockets[0]); 208 213 goto end; … … 210 215 211 216 /* Else ask the user to chose one */ 212 usable_sockets = malloc(i *sizeof(char*));213 for (i=0; sockets[i]; i++)217 usable_sockets = malloc(i * sizeof(char *)); 218 for (i = 0; sockets[i]; i++) 214 219 { 215 220 struct sockaddr_un addr; 216 if ((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) 221 if ((sock = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) 217 222 { 218 223 perror("select_socket:socket"); 219 224 goto end; 220 225 } 221 memset(&addr, 0,sizeof(addr));226 memset(&addr, 0, sizeof(addr)); 222 227 addr.sun_family = AF_UNIX; 223 228 strcpy(addr.sun_path, sockets[i]); 224 229 if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) 225 230 { 226 switch (errno)231 switch (errno) 227 232 { 228 233 case EACCES: … … 233 238 break; 234 239 default: 235 fprintf(stderr, "Unknown error on %s:%s\n", sockets[i], strerror(errno)); 240 fprintf(stderr, "Unknown error on %s:%s\n", sockets[i], 241 strerror(errno)); 236 242 } 237 243 } … … 244 250 } 245 251 } 246 if (!nb_usable_sockets)252 if (!nb_usable_sockets) 247 253 goto end; 248 if (nb_usable_sockets == 1)254 if (nb_usable_sockets == 1) 249 255 { 250 256 ret = strdup(usable_sockets[0]); … … 259 265 screen_list->cv = caca_create_canvas(0, 0); 260 266 screen_list->dp = caca_create_display(screen_list->cv); 261 if (!screen_list->dp)267 if (!screen_list->dp) 262 268 goto end; 263 269 caca_set_cursor(screen_list->dp, 0); 264 270 caca_set_display_title(screen_list->dp, PACKAGE_STRING); 265 while (1)271 while (1) 266 272 { 267 if (refresh)273 if (refresh) 268 274 { 269 275 caca_set_color_ansi(screen_list->cv, CACA_BLUE, CACA_BLUE); … … 273 279 caca_get_canvas_height(screen_list->cv), 274 280 '#'); 275 caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_BLUE); 276 caca_draw_cp437_box(screen_list->cv, 277 0, 0, 278 caca_get_canvas_width(screen_list->cv), 279 caca_get_canvas_height(screen_list->cv) 280 ); 281 caca_printf(screen_list->cv, 2, 2, "Please select a session to reconnect:"); 282 for(i=0; i<nb_usable_sockets; i++) 281 caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, 282 CACA_BLUE); 283 caca_draw_cp437_box(screen_list->cv, 0, 0, 284 caca_get_canvas_width(screen_list->cv), 285 caca_get_canvas_height(screen_list-> 286 cv)); 287 caca_printf(screen_list->cv, 2, 2, 288 "Please select a session to reconnect:"); 289 for (i = 0; i < nb_usable_sockets; i++) 283 290 { 284 if (i == current_line-1)291 if (i == current_line - 1) 285 292 { 286 293 caca_set_attr(screen_list->cv, CACA_BOLD); 287 caca_set_color_ansi(screen_list->cv, CACA_GREEN, CACA_BLUE); 288 caca_put_char(screen_list->cv, 1, i+3, '>'); 294 caca_set_color_ansi(screen_list->cv, CACA_GREEN, 295 CACA_BLUE); 296 caca_put_char(screen_list->cv, 1, i + 3, '>'); 289 297 } 290 298 else 291 299 { 292 300 caca_set_attr(screen_list->cv, 0); 293 caca_set_color_ansi(screen_list->cv, CACA_LIGHTGRAY, CACA_BLUE); 294 caca_put_char(screen_list->cv, 1, i+3, ' '); 301 caca_set_color_ansi(screen_list->cv, 302 CACA_LIGHTGRAY, CACA_BLUE); 303 caca_put_char(screen_list->cv, 1, i + 3, ' '); 295 304 } 296 305 caca_printf(screen_list->cv, 297 3, i +3,306 3, i + 3, 298 307 "%s", 299 308 socket_to_session(usable_sockets[i])); … … 303 312 } 304 313 305 if(!caca_get_event(screen_list->dp, 306 CACA_EVENT_KEY_PRESS 307 |CACA_EVENT_RESIZE 308 |CACA_EVENT_QUIT, 309 &ev, 10000)) 314 if (!caca_get_event(screen_list->dp, 315 CACA_EVENT_KEY_PRESS 316 | CACA_EVENT_RESIZE 317 | CACA_EVENT_QUIT, &ev, 10000)) 310 318 continue; 311 319 312 320 t = caca_get_event_type(&ev); 313 321 314 if (t & CACA_EVENT_KEY_PRESS)322 if (t & CACA_EVENT_KEY_PRESS) 315 323 { 316 324 unsigned int c = caca_get_event_key_ch(&ev); 317 switch (c)325 switch (c) 318 326 { 319 327 case CACA_KEY_UP: 320 if (current_line>1)328 if (current_line > 1) 321 329 current_line--; 322 330 break; 323 331 case CACA_KEY_DOWN: 324 if (current_line<nb_usable_sockets)332 if (current_line < nb_usable_sockets) 325 333 current_line++; 326 334 break; 327 335 case CACA_KEY_RETURN: 328 ret = strdup(usable_sockets[current_line -1]);336 ret = strdup(usable_sockets[current_line - 1]); 329 337 goto end; 330 338 break; … … 337 345 refresh = 1; 338 346 } 339 else if (t & CACA_EVENT_RESIZE)347 else if (t & CACA_EVENT_RESIZE) 340 348 { 341 349 refresh = 1; 342 350 } 343 else if (t & CACA_EVENT_QUIT)351 else if (t & CACA_EVENT_QUIT) 344 352 goto end; 345 353 } … … 347 355 } 348 356 349 end:350 if (sockets)351 { 352 for (i=0; sockets[i]; i++)357 end: 358 if (sockets) 359 { 360 for (i = 0; sockets[i]; i++) 353 361 free(sockets[i]); 354 362 free(sockets); 355 363 } 356 if (usable_sockets)357 { 358 for (i=0; i<nb_usable_sockets; i++)364 if (usable_sockets) 365 { 366 for (i = 0; i < nb_usable_sockets; i++) 359 367 free(usable_sockets[i]); 360 368 free(usable_sockets); 361 369 } 362 if (screen_list->dp)370 if (screen_list->dp) 363 371 { 364 372 caca_free_display(screen_list->dp); 365 373 screen_list->dp = NULL; 366 374 } 367 if (screen_list->cv)375 if (screen_list->cv) 368 376 { 369 377 caca_free_canvas(screen_list->cv); … … 373 381 } 374 382 375 void attach(struct screen_list *screen_list)383 void attach(struct screen_list *screen_list) 376 384 { 377 385 screen_list->socket_path[SOCK_SERVER] = select_socket(screen_list); 378 386 379 if (screen_list->socket_path[SOCK_SERVER])387 if (screen_list->socket_path[SOCK_SERVER]) 380 388 { 381 389 char *session; 382 390 session = connect_socket(screen_list, SOCK_SERVER); 383 if (session)391 if (session) 384 392 { 385 393 debug("Connected to session %s", session); … … 387 395 screen_list->cv = caca_create_canvas(0, 0); 388 396 screen_list->dp = caca_create_display(screen_list->cv); 389 if (!screen_list->dp)397 if (!screen_list->dp) 390 398 return; 391 caca_set_display_time(screen_list->dp, screen_list->delay *1000);399 caca_set_display_time(screen_list->dp, screen_list->delay * 1000); 392 400 caca_set_cursor(screen_list->dp, 1); 393 401 394 402 screen_list->socket_path[SOCK_CLIENT] = 395 build_socket_path(screen_list->socket_dir, session, SOCK_CLIENT); 403 build_socket_path(screen_list->socket_dir, session, 404 SOCK_CLIENT); 396 405 create_socket(screen_list, SOCK_CLIENT); 397 406 request_attach(screen_list); 398 if (screen_list->session_name)407 if (screen_list->session_name) 399 408 free(screen_list->session_name); 400 409 screen_list->session_name = session; … … 415 424 } 416 425 417 int send_event(caca_event_t ev, struct screen_list *screen_list)426 int send_event(caca_event_t ev, struct screen_list *screen_list) 418 427 { 419 428 enum caca_event_type t; … … 421 430 t = caca_get_event_type(&ev); 422 431 423 if (t & CACA_EVENT_KEY_PRESS)432 if (t & CACA_EVENT_KEY_PRESS) 424 433 { 425 434 char buf[16]; 426 435 int bytes; 427 bytes = snprintf(buf, sizeof(buf)-1, "KEY %d", caca_get_event_key_ch(&ev)); 436 bytes = 437 snprintf(buf, sizeof(buf) - 1, "KEY %d", 438 caca_get_event_key_ch(&ev)); 428 439 buf[bytes] = '\0'; 429 440 debug("Sending key press to server: %s", buf); 430 441 return write(screen_list->socket[SOCK_SERVER], buf, strlen(buf)) <= 0; 431 442 } 432 else if (t & CACA_EVENT_RESIZE)443 else if (t & CACA_EVENT_RESIZE) 433 444 { 434 445 char buf[32]; 435 446 int bytes; 436 bytes = snprintf(buf, sizeof(buf) -1, "RESIZE %10d %10d",447 bytes = snprintf(buf, sizeof(buf) - 1, "RESIZE %10d %10d", 437 448 caca_get_event_resize_width(&ev), 438 449 caca_get_event_resize_height(&ev)); … … 441 452 return write(screen_list->socket[SOCK_SERVER], buf, strlen(buf)) <= 0; 442 453 } 443 else if(t & CACA_EVENT_QUIT) 444 return write(screen_list->socket[SOCK_SERVER], "QUIT", strlen("QUIT")) <= 0; 454 else if (t & CACA_EVENT_QUIT) 455 return write(screen_list->socket[SOCK_SERVER], "QUIT", 456 strlen("QUIT")) <= 0; 445 457 446 458 return 0; 447 459 } 448 460 449 int send_delay(struct screen_list *screen_list)461 int send_delay(struct screen_list *screen_list) 450 462 { 451 463 char buf[18]; 452 464 int bytes; 453 bytes = snprintf(buf, sizeof(buf) -1, "DELAY %10d", screen_list->delay);465 bytes = snprintf(buf, sizeof(buf) - 1, "DELAY %10d", screen_list->delay); 454 466 buf[bytes] = '\0'; 455 467 return write(screen_list->socket[SOCK_SERVER], buf, strlen(buf)) <= 0; -
neercs/trunk/src/configuration.c
r3961 r3969 1 /* 1 /* 2 2 * neercs console-based window manager 3 3 * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> … … 36 36 37 37 /* Check if file exist */ 38 if (stat(filename, &st) < 0)39 { 40 return -1; 41 } 42 /* Get its size 38 if (stat(filename, &st) < 0) 39 { 40 return -1; 41 } 42 /* Get its size */ 43 43 size = st.st_size; 44 if (!size)44 if (!size) 45 45 { 46 46 fprintf(stderr, "File too short\n"); … … 50 50 /* Open it */ 51 51 fp = fopen(filename, "r"); 52 if (!fp)52 if (!fp) 53 53 { 54 54 return -1; … … 56 56 57 57 buffer = malloc(size + 1); 58 if (!buffer)58 if (!buffer) 59 59 { 60 60 fclose(fp); 61 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); 61 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, 62 __LINE__); 62 63 return -1; 63 64 } 64 65 /* Read it */ 65 while ((i = fread(buffer+total, 1, size, fp)) > 0)66 { 67 total +=i;66 while ((i = fread(buffer + total, 1, size, fp)) > 0) 67 { 68 total += i; 68 69 } 69 70 buffer[total] = '\n'; … … 71 72 fclose(fp); 72 73 73 i =0;74 i = 0; 74 75 75 76 /* Parse it */ 76 while((i = parse_conf_line(buffer + offset, total-offset, screen_list)) > 0) 77 { 78 offset+=i; 77 while ((i = 78 parse_conf_line(buffer + offset, total - offset, screen_list)) > 0) 79 { 80 offset += i; 79 81 l++; 80 82 } … … 97 99 static struct option *prev = NULL; 98 100 99 if(size <= 0) return -1; 100 101 /* Find EOL */ 102 for(i=0; i<size; i++) 103 { 104 if(buf[i] == '\n') 101 if (size <= 0) 102 return -1; 103 104 /* Find EOL */ 105 for (i = 0; i < size; i++) 106 { 107 if (buf[i] == '\n') 105 108 { 106 109 s = i + 1; … … 110 113 111 114 /* Strip comments and trailing spaces */ 112 for (i=0; i<s; i++)113 { 114 if (buf[i] == ';' && !in_quote)115 for (i = 0; i < s; i++) 116 { 117 if (buf[i] == ';' && !in_quote) 115 118 { 116 119 eol = i; 117 120 break; 118 121 } 119 else if (buf[i] == '\n')122 else if (buf[i] == '\n') 120 123 { 121 124 eol = i; 122 125 break; 123 126 } 124 else if(buf[i] == ' ' && !c) {} 127 else if (buf[i] == ' ' && !c) 128 { 129 } 125 130 else 126 131 { 127 if (line == NULL)132 if (line == NULL) 128 133 { 129 134 line = malloc(2); 130 if (!line)135 if (!line) 131 136 { 132 137 fprintf(stderr, "Can't allocate memory at %s:%d\n", 133 __FUNCTION__, __LINE__);138 __FUNCTION__, __LINE__); 134 139 return -1; 135 140 } … … 137 142 else 138 143 { 139 line = realloc(line, l +2);140 if (!line)144 line = realloc(line, l + 2); 145 if (!line) 141 146 { 142 147 fprintf(stderr, "Can't allocate memory at %s:%d\n", 143 __FUNCTION__, __LINE__);148 __FUNCTION__, __LINE__); 144 149 return -1; 145 150 } 146 151 } 147 if (buf[i] == '"')152 if (buf[i] == '"') 148 153 in_quote = !in_quote; 149 if (buf[i] == ' ')154 if (buf[i] == ' ') 150 155 end_spaces++; 151 156 else 152 157 end_spaces = 0; 153 158 154 line[l] 155 line[l +1] = 0;159 line[l] = buf[i]; 160 line[l + 1] = 0; 156 161 l++; 157 162 c = 1; … … 159 164 } 160 165 161 if (c==0)166 if (c == 0) 162 167 { 163 168 /* This line is empty, do nothing */ … … 166 171 { 167 172 struct option *option = malloc(sizeof(struct option)); 168 if (!option)173 if (!option) 169 174 { 170 175 fprintf(stderr, "Can't allocate memory at %s:%d\n", 171 __FUNCTION__, __LINE__);176 __FUNCTION__, __LINE__); 172 177 return -1; 173 178 } 174 179 option->next = NULL; 175 l -=end_spaces;176 line = realloc(line, l +1);180 l -= end_spaces; 181 line = realloc(line, l + 1); 177 182 line[l] = 0; 178 183 179 184 get_key_value(line, option); 180 185 181 if (!screen_list->config)186 if (!screen_list->config) 182 187 screen_list->config = option; 183 188 184 if (prev)189 if (prev) 185 190 prev->next = option; 186 191 … … 193 198 int get_key_value(char *line, struct option *option) 194 199 { 195 unsigned int i, o = 0, b =0, end_spaces = 0;200 unsigned int i, o = 0, b = 0, end_spaces = 0; 196 201 char *cur = NULL; 197 202 option->value = NULL; … … 199 204 200 205 /* Line is a section delimiter */ 201 if (line[0] == '[')206 if (line[0] == '[') 202 207 { 203 208 option->value = malloc(strlen(line) - 1); 204 if (!option->value)209 if (!option->value) 205 210 { 206 211 fprintf(stderr, "Can't allocate memory at %s:%d\n", … … 208 213 return -1; 209 214 } 210 memcpy(option->value, line +1, strlen(line) - 1);215 memcpy(option->value, line + 1, strlen(line) - 1); 211 216 option->value[strlen(line) - 2] = 0; 212 217 return 0; … … 214 219 215 220 cur = malloc(1); 216 if (!cur)221 if (!cur) 217 222 { 218 223 fprintf(stderr, "Can't allocate memory at %s:%d\n", … … 222 227 cur[0] = 0; 223 228 224 for(i=0; i<strlen(line); i++) 225 { 226 if(line[i] == ' ' && !b) continue; 227 228 229 if(line[i] == '=') 229 for (i = 0; i < strlen(line); i++) 230 { 231 if (line[i] == ' ' && !b) 232 continue; 233 234 235 if (line[i] == '=') 230 236 { 231 237 b = 0; 232 cur[o -end_spaces] = 0;233 cur = realloc(cur, (o -end_spaces)+1);234 if (!cur)238 cur[o - end_spaces] = 0; 239 cur = realloc(cur, (o - end_spaces) + 1); 240 if (!cur) 235 241 { 236 242 fprintf(stderr, "Can't allocate memory at %s:%d\n", 237 __FUNCTION__, __LINE__);243 __FUNCTION__, __LINE__); 238 244 return -1; 239 245 } … … 244 250 else 245 251 { 246 if(line[i] == ' ') end_spaces++; 247 else end_spaces = 0; 248 249 cur = realloc(cur, o+2); 250 if(!cur) 252 if (line[i] == ' ') 253 end_spaces++; 254 else 255 end_spaces = 0; 256 257 cur = realloc(cur, o + 2); 258 if (!cur) 251 259 { 252 260 fprintf(stderr, "Can't allocate memory at %s:%d\n", 253 __FUNCTION__, __LINE__);261 __FUNCTION__, __LINE__); 254 262 return -1; 255 263 } 256 cur[o] 264 cur[o] = line[i]; 257 265 o++; 258 266 b = 1; … … 276 284 char *section = NULL; 277 285 278 while (option)279 { 280 if (option->key == NULL)286 while (option) 287 { 288 if (option->key == NULL) 281 289 { 282 290 section = option->key; … … 285 293 } 286 294 287 if(IS_TOKEN("window_manager")) 288 { 289 if (IS_VALUE("full")) screen_list->wm_type = WM_FULL; 290 else if(IS_VALUE("hsplit")) screen_list->wm_type = WM_HSPLIT; 291 else if(IS_VALUE("vsplit")) screen_list->wm_type = WM_VSPLIT; 292 else if(IS_VALUE("card")) screen_list->wm_type = WM_CARD; 293 else if(IS_VALUE("cube")) screen_list->wm_type = WM_CUBE; 294 else fprintf(stderr, "Unknown window manager '%s'\n", option->key); 295 296 } else if(IS_TOKEN("cube_duration")) 297 { 298 screen_list->cube.duration = atoi(option->value) * 1000000; 299 } else if(IS_TOKEN("thumbnails")) 300 { 301 if (IS_VALUE("true") || IS_VALUE("1")) screen_list->mini = 1; 302 else screen_list->mini = 0; 303 304 } else if(IS_TOKEN("status_bar")) 305 { 306 if (IS_VALUE("true") || IS_VALUE("1")) screen_list->status = 1; 307 else screen_list->status = 0; 308 309 } else if(IS_TOKEN("screensaver_timeout")) 295 if (IS_TOKEN("window_manager")) 296 { 297 if (IS_VALUE("full")) 298 screen_list->wm_type = WM_FULL; 299 else if (IS_VALUE("hsplit")) 300 screen_list->wm_type = WM_HSPLIT; 301 else if (IS_VALUE("vsplit")) 302 screen_list->wm_type = WM_VSPLIT; 303 else if (IS_VALUE("card")) 304 screen_list->wm_type = WM_CARD; 305 else if (IS_VALUE("cube")) 306 screen_list->wm_type = WM_CUBE; 307 else 308 fprintf(stderr, "Unknown window manager '%s'\n", option->key); 309 310 } 311 else if (IS_TOKEN("cube_duration")) 312 { 313 screen_list->cube.duration = atoi(option->value) * 1000000; 314 } 315 else if (IS_TOKEN("thumbnails")) 316 { 317 if (IS_VALUE("true") || IS_VALUE("1")) 318 screen_list->mini = 1; 319 else 320 screen_list->mini = 0; 321 322 } 323 else if (IS_TOKEN("status_bar")) 324 { 325 if (IS_VALUE("true") || IS_VALUE("1")) 326 screen_list->status = 1; 327 else 328 screen_list->status = 0; 329 330 } 331 else if (IS_TOKEN("screensaver_timeout")) 310 332 { 311 333 screen_list->screensaver_timeout = atoi(option->value) * 1000000; 312 334 /* if timeout is 0, set it to 0xFFFFFFFFFFFFFFFF */ 313 if(!screen_list->screensaver_timeout) screen_list->screensaver_timeout-=1; 314 } else if(IS_TOKEN("autolock_timeout")) 335 if (!screen_list->screensaver_timeout) 336 screen_list->screensaver_timeout -= 1; 337 } 338 else if (IS_TOKEN("autolock_timeout")) 315 339 { 316 340 debug("Autolock is %d\n", screen_list->autolock_timeout); 317 if(screen_list->autolock_timeout == 0 || 318 screen_list->autolock_timeout == ((long long unsigned int)0) - 1) 319 { 320 screen_list->autolock_timeout = atoi(option->value) * 1000000; 321 /* if timeout is 0, set it to 0xFFFFFFFFFFFFFFFF */ 322 if(!screen_list->autolock_timeout) screen_list->autolock_timeout-=1; 323 } 324 } else if(IS_TOKEN("lock_on_detach")) 325 { 326 if (IS_VALUE("true") || IS_VALUE("1")) screen_list->lock_on_detach = 1; 327 else screen_list->lock_on_detach = 0; 328 } else if(IS_TOKEN("socket_dir")) 341 if (screen_list->autolock_timeout == 0 || 342 screen_list->autolock_timeout == 343 ((long long unsigned int)0) - 1) 344 { 345 screen_list->autolock_timeout = atoi(option->value) * 1000000; 346 /* if timeout is 0, set it to 0xFFFFFFFFFFFFFFFF */ 347 if (!screen_list->autolock_timeout) 348 screen_list->autolock_timeout -= 1; 349 } 350 } 351 else if (IS_TOKEN("lock_on_detach")) 352 { 353 if (IS_VALUE("true") || IS_VALUE("1")) 354 screen_list->lock_on_detach = 1; 355 else 356 screen_list->lock_on_detach = 0; 357 } 358 else if (IS_TOKEN("socket_dir")) 329 359 { 330 360 screen_list->socket_dir = option->value; 331 } else if(IS_TOKEN("delay")) 361 } 362 else if (IS_TOKEN("delay")) 332 363 { 333 364 screen_list->requested_delay = atoi(option->value); 334 365 screen_list->delay = atoi(option->value); 335 } else 366 } 367 else 336 368 { 337 369 fprintf(stderr, "Unknown option '%s'\n", option->key); -
neercs/trunk/src/effects.c
r3950 r3969 1 /* 1 /* 2 2 * neercs console-based window manager 3 3 * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> … … 29 29 void draw_thumbnails(struct screen_list *screen_list) 30 30 { 31 char const * 31 char const *const *fonts; 32 32 caca_dither_t *d; 33 33 caca_font_t *f; 34 34 uint8_t *buf; 35 int i, y = caca_get_canvas_height(screen_list->cv) - 6 - screen_list->status; 35 int i, y = 36 caca_get_canvas_height(screen_list->cv) - 6 - screen_list->status; 36 37 int miniw, minih; 37 38 38 if (screen_list->count)39 if (screen_list->count) 39 40 { 40 41 fonts = caca_get_font_list(); … … 48 49 49 50 #if defined(HAVE_ENDIAN_H) 50 if (__BYTE_ORDER == __BIG_ENDIAN)51 if (__BYTE_ORDER == __BIG_ENDIAN) 51 52 #else 52 53 54 if (*(uint8_t const *)&tmp == 0x12)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) 55 56 #endif 56 57 d = caca_create_dither(32, miniw, minih, 4 * miniw, 57 58 0xff0000, 0xff00, 0xff, 0x0); 58 59 else 59 60 d = caca_create_dither(32, miniw, minih, 4 * miniw, 60 61 62 for (i = 0; i < screen_list->count; i++)61 0xff00, 0xff0000, 0xff000000, 0x0); 62 63 for (i = 0; i < screen_list->count; i++) 63 64 { 64 if (!screen_list->screen[i]->changed && !screen_list->changed)65 if (!screen_list->screen[i]->changed && !screen_list->changed) 65 66 continue; 66 67 caca_render_canvas(screen_list->screen[i]->cv, f, buf, 67 68 miniw, minih, miniw * 4); 68 69 caca_dither_bitmap(screen_list->cv, 20 * i, y, 19, 6, d, buf); 69 70 caca_set_color_ansi(screen_list->cv, CACA_WHITE, CACA_BLUE); 70 71 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);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); 74 75 } 75 76 … … 88 89 89 90 90 /* caca_fill_box(screen_list->cv, 91 x, y, 92 caca_get_canvas_width(screen_list->cv), 1, '#');*/ 93 94 /* Hour */ 95 { 96 time_t now = time ((time_t *) 0); 91 /* caca_fill_box(screen_list->cv, x, y, 92 caca_get_canvas_width(screen_list->cv), 1, '#'); */ 93 94 /* Hour */ 95 { 96 time_t now = time((time_t *) 0); 97 97 struct tm *t = localtime(&now); 98 98 char hour[256]; … … 100 100 101 101 caca_set_color_ansi(screen_list->cv, CACA_LIGHTBLUE, CACA_BLUE); 102 caca_printf(screen_list->cv, x, y, 103 "["); 102 caca_printf(screen_list->cv, x, y, "["); 104 103 105 104 caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_BLUE); 106 caca_printf(screen_list->cv, x+1, y, 107 hour); 105 caca_printf(screen_list->cv, x + 1, y, hour); 108 106 caca_set_color_ansi(screen_list->cv, CACA_LIGHTBLUE, CACA_BLUE); 109 caca_printf(screen_list->cv, x + strlen(hour) + 1, y, 110 "]"); 107 caca_printf(screen_list->cv, x + strlen(hour) + 1, y, "]"); 111 108 x += 7; 112 109 113 110 } 114 111 115 /* Window */112 /* Window */ 116 113 { 117 114 char text[256]; 118 sprintf(text, "%d/%d", screen_list->pty +1, screen_list->count);115 sprintf(text, "%d/%d", screen_list->pty + 1, screen_list->count); 119 116 caca_set_color_ansi(screen_list->cv, CACA_BLUE, CACA_BLUE); 120 117 caca_put_char(screen_list->cv, x, y, '#'); … … 123 120 caca_printf(screen_list->cv, x, y, "Window:"); 124 121 caca_set_color_ansi(screen_list->cv, CACA_BLUE, CACA_BLUE); 125 caca_put_char(screen_list->cv, x +7, y, '#');122 caca_put_char(screen_list->cv, x + 7, y, '#'); 126 123 caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_BLUE); 127 caca_printf(screen_list->cv, x +8, y, text);128 x += 8+strlen(text);129 } 130 131 /* Window Manager */124 caca_printf(screen_list->cv, x + 8, y, text); 125 x += 8 + strlen(text); 126 } 127 128 /* Window Manager */ 132 129 { 133 130 char text[256]; 134 131 135 switch (screen_list->wm_type)132 switch (screen_list->wm_type) 136 133 { 137 134 case WM_CARD: … … 160 157 caca_printf(screen_list->cv, x, y, "WM:"); 161 158 caca_set_color_ansi(screen_list->cv, CACA_BLUE, CACA_BLUE); 162 caca_put_char(screen_list->cv, x +3, y, '#');159 caca_put_char(screen_list->cv, x + 3, y, '#'); 163 160 caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_BLUE); 164 caca_printf(screen_list->cv, x +4, y, text);165 x += 4+strlen(text);166 } 167 168 /* Help (must be the last one )*/161 caca_printf(screen_list->cv, x + 4, y, text); 162 x += 4 + strlen(text); 163 } 164 165 /* Help (must be the last one ) */ 169 166 { 170 167 char text[256]; 171 168 sprintf(text, "Help: ctrl-a-h"); 172 169 caca_set_color_ansi(screen_list->cv, CACA_BLUE, CACA_BLUE); 173 while(x < (int)(caca_get_canvas_width(screen_list->cv) - strlen(text))) 170 while (x < 171 (int)(caca_get_canvas_width(screen_list->cv) - strlen(text))) 174 172 { 175 173 caca_put_char(screen_list->cv, x, y, '#'); … … 192 190 193 191 caca_set_color_ansi(screen_list->cv, CACA_BLUE, CACA_BLUE); 194 caca_fill_box(screen_list->cv, 195 x, y, 196 w, h, '#'); 192 caca_fill_box(screen_list->cv, x, y, w, h, '#'); 197 193 caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_BLUE); 198 caca_draw_cp437_box(screen_list->cv, 199 x, y, 200 w, h); 201 202 x+=2; 194 caca_draw_cp437_box(screen_list->cv, x, y, w, h); 195 196 x += 2; 203 197 y++; 204 198 caca_printf(screen_list->cv, 205 (caca_get_canvas_width(screen_list->cv) - strlen(PACKAGE_STRING)) / 2, 206 y-1, 207 PACKAGE_STRING); 199 (caca_get_canvas_width(screen_list->cv) - 200 strlen(PACKAGE_STRING)) / 2, y - 1, PACKAGE_STRING); 208 201 caca_printf(screen_list->cv, x, y++, "Copyright (c) 2006-2009"); 209 caca_printf(screen_list->cv, x, y++, " Sam Hocevar <sam@zoy.org>"); 210 caca_printf(screen_list->cv, x, y++, " Jean-Yves Lamoureux <jylam@lnxscene.org>"); 211 caca_printf(screen_list->cv, x, y++, " Pascal Terjan <pterjan@linuxfr.org>"); 202 caca_printf(screen_list->cv, x, y++, 203 " Sam Hocevar <sam@zoy.org>"); 204 caca_printf(screen_list->cv, x, y++, 205 " Jean-Yves Lamoureux <jylam@lnxscene.org>"); 206 caca_printf(screen_list->cv, x, y++, 207 " Pascal Terjan <pterjan@linuxfr.org>"); 212 208 caca_printf(screen_list->cv, x, y++, ""); 213 209 caca_printf(screen_list->cv, x, y++, ""); 214 caca_printf(screen_list->cv, x, y++, "All shortcuts are in format 'ctrl-a-X' where X is :"); 210 caca_printf(screen_list->cv, x, y++, 211 "All shortcuts are in format 'ctrl-a-X' where X is :"); 215 212 caca_printf(screen_list->cv, x, y++, "n:\t Next window"); 216 213 caca_printf(screen_list->cv, x, y++, "p:\t Previous window"); … … 219 216 caca_printf(screen_list->cv, x, y++, "m:\t Thumbnails"); 220 217 caca_printf(screen_list->cv, x, y++, "d:\t Detach"); 221 caca_printf(screen_list->cv, x, y++, "k:\t Close window and kill associated process"); 218 caca_printf(screen_list->cv, x, y++, 219 "k:\t Close window and kill associated process"); 222 220 caca_printf(screen_list->cv, x, y++, "h:\t This help"); 223 221 caca_printf(screen_list->cv, x, y++, ""); 224 222 caca_printf(screen_list->cv, x, y++, ""); 225 caca_printf(screen_list->cv, x, y++, "See http://caca.zoy.org/wiki/neercs for more informations"); 223 caca_printf(screen_list->cv, x, y++, 224 "See http://caca.zoy.org/wiki/neercs for more informations"); 226 225 } 227 226 … … 230 229 debug("Got %x\n", c); 231 230 232 switch (c)231 switch (c) 233 232 { 234 233 case 0x111: 235 if (screen_list->cur_in_list>0)234 if (screen_list->cur_in_list > 0) 236 235 screen_list->cur_in_list--; 237 236 break; 238 237 case 0x112: 239 if (screen_list->cur_in_list<screen_list->count-1)238 if (screen_list->cur_in_list < screen_list->count - 1) 240 239 screen_list->cur_in_list++; 241 240 break; … … 243 242 screen_list->window_list = 0; 244 243 screen_list->prevpty = screen_list->pty; 245 screen_list->pty 244 screen_list->pty = screen_list->cur_in_list; 246 245 break; 247 246 case 0x22: … … 263 262 debug("Drawing list\n"); 264 263 caca_set_color_ansi(screen_list->cv, CACA_BLACK, CACA_BLACK); 265 caca_fill_box(screen_list->cv, 266 0, 0, 267 w, h, '#'); 264 caca_fill_box(screen_list->cv, 0, 0, w, h, '#'); 268 265 caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_DEFAULT); 269 caca_draw_cp437_box(screen_list->cv, 270 0, 0, 271 w, h); 266 caca_draw_cp437_box(screen_list->cv, 0, 0, w, h); 272 267 273 268 caca_printf(screen_list->cv, 2, 1, "Num \tName"); 274 for (i = 0; i < screen_list->count; i++)269 for (i = 0; i < screen_list->count; i++) 275 270 { 276 271 char line[1024]; 277 if (screen_list->cur_in_list == i)272 if (screen_list->cur_in_list == i) 278 273 caca_set_color_ansi(screen_list->cv, CACA_BLACK, CACA_WHITE); 279 274 else 280 275 caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_DEFAULT); 281 sprintf(line, "%d \t%s", i +1, screen_list->screen[i]->title);282 283 caca_printf(screen_list->cv, 2, i +3, line);276 sprintf(line, "%d \t%s", i + 1, screen_list->screen[i]->title); 277 278 caca_printf(screen_list->cv, 2, i + 3, line); 284 279 } 285 280 } … … 288 283 /* Total close time */ 289 284 #define DELAY 500000.0f 290 int close_screen_recurrent(struct screen_list* screen_list, struct recurrent* rec, void *user, long long unsigned int t) 285 int close_screen_recurrent(struct screen_list *screen_list, 286 struct recurrent *rec, void *user, 287 long long unsigned int t) 291 288 { 292 289 long long unsigned int delta = t - rec->start_time; … … 295 292 screen_list->delay = 0; 296 293 rec->kill_me = 0; 297 if (delta>=DELAY)294 if (delta >= DELAY) 298 295 { 299 296 rec->kill_me = 1; … … 311 308 int h = s->orig_h * r; 312 309 313 /* libcaca canvas resize function is bugged, do it by hand 310 /* libcaca canvas resize function is bugged, do it by hand */ 314 311 old = s->cv; 315 312 new = caca_create_canvas(w, h); … … 322 319 s->h = h; 323 320 324 s->x = 325 (s->orig_x * r) + 326 ((s->orig_w/2) - s->w/2); 327 s->y = 328 (s->orig_y * r) + 329 ((s->orig_h/2) - s->h/2); 321 s->x = (s->orig_x * r) + ((s->orig_w / 2) - s->w / 2); 322 s->y = (s->orig_y * r) + ((s->orig_h / 2) - s->h / 2); 330 323 } 331 324 screen_list->changed = 1; -
neercs/trunk/src/lock.c
r3551 r3969 1 /* 1 /* 2 2 * neercs console-based window manager 3 3 * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> … … 48 48 49 49 #if defined USE_LOCK 50 if(!screen_list->locked) return 0; 51 52 if(c==0x08) // BACKSPACE 53 { 54 if(screen_list->lock_offset) 55 { 56 screen_list->lockpass[screen_list->lock_offset-1] = 0; 50 if (!screen_list->locked) 51 return 0; 52 53 if (c == 0x08) // BACKSPACE 54 { 55 if (screen_list->lock_offset) 56 { 57 screen_list->lockpass[screen_list->lock_offset - 1] = 0; 57 58 screen_list->lock_offset--; 58 59 } 59 60 } 60 else if (c==0x0d)// RETURN61 else if (c == 0x0d) // RETURN 61 62 { 62 63 memset(screen_list->lockmsg, 0, 1024); 63 if (validate_lock(screen_list, getenv("USER"), screen_list->lockpass))64 if (validate_lock(screen_list, getenv("USER"), screen_list->lockpass)) 64 65 { 65 66 memset(screen_list->lockpass, 0, 1024); … … 77 78 else 78 79 { 79 if (screen_list->lock_offset < 1023)80 if (screen_list->lock_offset < 1023) 80 81 { 81 82 screen_list->lockpass[screen_list->lock_offset++] = c; 82 screen_list->lockpass[screen_list->lock_offset] 83 screen_list->lockpass[screen_list->lock_offset] = 0; 83 84 } 84 85 } … … 95 96 caca_canvas_t *cv = screen_list->cv; 96 97 97 gethostname(buffer, sizeof(buffer) -1);98 gethostname(buffer, sizeof(buffer) - 1); 98 99 99 100 int w = 65, h = 20; … … 103 104 104 105 caca_set_color_ansi(cv, CACA_BLUE, CACA_BLUE); 105 caca_fill_box(cv, 106 x, y, 107 w, h, '#'); 106 caca_fill_box(cv, x, y, w, h, '#'); 108 107 caca_set_color_ansi(cv, CACA_DEFAULT, CACA_BLUE); 109 caca_draw_cp437_box(cv, 110 x, y, 111 w, h); 112 113 x+=2; 108 caca_draw_cp437_box(cv, x, y, w, h); 109 110 x += 2; 114 111 y++; 115 112 caca_printf(cv, 116 (caca_get_canvas_width(cv) - strlen(PACKAGE_STRING " locked")) / 2, 117 y-1, 118 PACKAGE_STRING " locked"); 119 120 caca_printf(cv, x, y++, "Please type in your password for %s@%s :", getenv("USER"), buffer); 121 y+=2; 122 123 x = (caca_get_canvas_width(cv)/2) - ((strlen(screen_list->lockpass) / 2) + strlen("Password : ")); 113 (caca_get_canvas_width(cv) - 114 strlen(PACKAGE_STRING " locked")) / 2, y - 1, 115 PACKAGE_STRING " locked"); 116 117 caca_printf(cv, x, y++, "Please type in your password for %s@%s :", 118 getenv("USER"), buffer); 119 y += 2; 120 121 x = (caca_get_canvas_width(cv) / 2) - 122 ((strlen(screen_list->lockpass) / 2) + strlen("Password : ")); 124 123 caca_printf(cv, x, y, "Password : "); 125 x +=strlen("Password : ");126 for (i=0; i<strlen(screen_list->lockpass); i++)124 x += strlen("Password : "); 125 for (i = 0; i < strlen(screen_list->lockpass); i++) 127 126 { 128 127 caca_put_str(cv, x, y, "*"); … … 131 130 132 131 133 if(strlen(screen_list->lockmsg)) 134 { 135 x = ((caca_get_canvas_width(cv) - w) / 2) + (strlen(screen_list->lockmsg)); 136 y+=2; 132 if (strlen(screen_list->lockmsg)) 133 { 134 x = ((caca_get_canvas_width(cv) - w) / 2) + 135 (strlen(screen_list->lockmsg)); 136 y += 2; 137 137 caca_set_color_ansi(cv, CACA_RED, CACA_BLUE); 138 138 caca_printf(cv, x, y, "Error : %s", screen_list->lockmsg); … … 155 155 for (i = 0; i < num_msg; ++i) 156 156 { 157 switch (msg[i]->msg_style)157 switch (msg[i]->msg_style) 158 158 { 159 159 case PAM_PROMPT_ECHO_ON: … … 164 164 case PAM_ERROR_MSG: 165 165 break; 166 default 166 default: 167 167 printf("Unknow message type from PAM\n"); 168 168 break; … … 179 179 #if USE_LOCK 180 180 int ret; 181 pam_handle_t *pamh =NULL;181 pam_handle_t *pamh = NULL; 182 182 char buffer[100]; 183 const char *service ="neercs";183 const char *service = "neercs"; 184 184 struct pam_conv conv = { 185 185 convpam, … … 188 188 189 189 ret = pam_start(service, user, &conv, &pamh); 190 if (ret!=PAM_SUCCESS)190 if (ret != PAM_SUCCESS) 191 191 return 0; 192 192 pam_set_item(pamh, PAM_RUSER, user); 193 193 194 ret = gethostname(buffer, sizeof(buffer) -1);194 ret = gethostname(buffer, sizeof(buffer) - 1); 195 195 if (ret) 196 196 { … … 203 203 204 204 ret = pam_set_item(pamh, PAM_RHOST, buffer); 205 if (ret!=PAM_SUCCESS)205 if (ret != PAM_SUCCESS) 206 206 { 207 207 sprintf(screen_list->lockmsg, "Can't set hostname"); … … 211 211 212 212 ret = pam_authenticate(pamh, 0); 213 if (ret!=PAM_SUCCESS)213 if (ret != PAM_SUCCESS) 214 214 { 215 215 sprintf(screen_list->lockmsg, "Can't authenticate"); … … 223 223 return 1; 224 224 } 225 -
neercs/trunk/src/main.c
r3961 r3969 1 /* 1 /* 2 2 * neercs console-based window manager 3 3 * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> … … 53 53 printf("%s\n", PACKAGE_STRING); 54 54 printf("Copyright (C) 2006, 2008 Sam Hocevar <sam@zoy.org>\n"); 55 printf(" Jean-Yves Lamoureux <jylam@lnxscene.org>\n\n"); 56 printf("This is free software. You may redistribute copies of it under the\n"); 57 printf("terms of the Do What The Fuck You Want To Public License, Version 2\n"); 55 printf 56 (" Jean-Yves Lamoureux <jylam@lnxscene.org>\n\n"); 57 printf 58 ("This is free software. You may redistribute copies of it under the\n"); 59 printf 60 ("terms of the Do What The Fuck You Want To Public License, Version 2\n"); 58 61 printf("<http://sam.zoy.org/wtfpl/>.\n"); 59 62 printf("There is NO WARRANTY, to the extent permitted by law.\n"); 60 63 printf("\n"); 61 printf("For more informations, visit http://libcaca.zoy.org/wiki/neercs\n"); 64 printf 65 ("For more informations, visit http://libcaca.zoy.org/wiki/neercs\n"); 62 66 } 63 67 … … 71 75 printf("\t--pid\t\t-P [pid]\t\tgrab process\n"); 72 76 printf("\t\t\t-r [session]\t\treattach to a detached neercs\n"); 73 printf("\t\t\t-R [session]\t\treattach if possible, otherwise start a new session\n"); 77 printf 78 ("\t\t\t-R [session]\t\treattach if possible, otherwise start a new session\n"); 74 79 printf("\t\t\t-S <name>\t\tname this session <name> instead of <pid>\n"); 75 80 printf("\t--lock-after\t-l [n]\t\t\tlock screen after n seconds\n"); … … 88 93 screen_list->default_shell = getenv("SHELL"); 89 94 90 args = argc -1; 91 if(screen_list->default_shell == NULL && args <= 0) 92 { 93 fprintf(stderr, "Environment variable SHELL not set and no arguments given. kthxbye.\n"); 95 args = argc - 1; 96 if (screen_list->default_shell == NULL && args <= 0) 97 { 98 fprintf(stderr, 99 "Environment variable SHELL not set and no arguments given. kthxbye.\n"); 94 100 goto end; 95 101 } 96 102 97 if (handle_command_line(argc, argv, screen_list) < 0)103 if (handle_command_line(argc, argv, screen_list) < 0) 98 104 goto end; 99 105 … … 101 107 read_configuration_file("/etc/neercsrc", screen_list); 102 108 103 /* Then local one 104 if (screen_list->user_path)109 /* Then local one */ 110 if (screen_list->user_path) 105 111 { 106 112 read_configuration_file(screen_list->user_path, screen_list); … … 108 114 } 109 115 110 if(screen_list->attach) 111 { 112 if(screen_list->nb_to_grab || screen_list->to_start) 113 { 114 fprintf(stderr, "-R can not be associated with commands or pids!\n"); 116 if (screen_list->attach) 117 { 118 if (screen_list->nb_to_grab || screen_list->to_start) 119 { 120 fprintf(stderr, 121 "-R can not be associated with commands or pids!\n"); 115 122 goto end; 116 123 } … … 118 125 attach(screen_list); 119 126 120 if (screen_list->forceattach && !screen_list->attach)127 if (screen_list->forceattach && !screen_list->attach) 121 128 goto end; 122 129 } 123 130 124 131 /* Build default session name */ 125 if (!screen_list->session_name)126 { 127 char mypid[32]; /* FIXME Compute the length of PID_MAX ? */132 if (!screen_list->session_name) 133 { 134 char mypid[32]; /* FIXME Compute the length of PID_MAX ? */ 128 135 snprintf(mypid, 31, "%d", getpid()); 129 mypid[31] = '\0';136 mypid[31] = '\0'; 130 137 screen_list->session_name = strdup(mypid); 131 if(!screen_list->session_name) 132 { 133 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); 138 if (!screen_list->session_name) 139 { 140 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, 141 __LINE__); 134 142 goto end; 135 143 } 136 144 } 137 if (!screen_list->socket_path[SOCK_CLIENT])145 if (!screen_list->socket_path[SOCK_CLIENT]) 138 146 screen_list->socket_path[SOCK_CLIENT] = 139 build_socket_path(screen_list->socket_dir, screen_list->session_name, SOCK_CLIENT); 140 141 if(!screen_list->socket_path[SOCK_SERVER]) 147 build_socket_path(screen_list->socket_dir, 148 screen_list->session_name, SOCK_CLIENT); 149 150 if (!screen_list->socket_path[SOCK_SERVER]) 142 151 screen_list->socket_path[SOCK_SERVER] = 143 build_socket_path(screen_list->socket_dir, screen_list->session_name, SOCK_SERVER); 152 build_socket_path(screen_list->socket_dir, 153 screen_list->session_name, SOCK_SERVER); 144 154 145 155 /* Fork the server if needed */ 146 if (!screen_list->attach)156 if (!screen_list->attach) 147 157 { 148 158 debug("Spawning a new server"); 149 if (start_server(screen_list))159 if (start_server(screen_list)) 150 160 goto end; 151 161 } … … 156 166 /* Clean up */ 157 167 mainret = 0; 158 end:159 160 if (screen_list)168 end: 169 170 if (screen_list) 161 171 { 162 172 free_screen_list(screen_list); … … 171 181 struct option *option; 172 182 173 if (screen_list->dp)183 if (screen_list->dp) 174 184 caca_free_display(screen_list->dp); 175 185 176 if (screen_list->cv)186 if (screen_list->cv) 177 187 caca_free_canvas(screen_list->cv); 178 188 179 for (i = 0; i < screen_list->count; i++)189 for (i = 0; i < screen_list->count; i++) 180 190 { 181 191 destroy_screen(screen_list->screen[i]); 182 192 } 183 193 184 if (screen_list->socket_path[SOCK_SERVER])194 if (screen_list->socket_path[SOCK_SERVER]) 185 195 free(screen_list->socket_path[SOCK_SERVER]); 186 196 187 if (screen_list->socket_path[SOCK_CLIENT])197 if (screen_list->socket_path[SOCK_CLIENT]) 188 198 { 189 199 unlink(screen_list->socket_path[SOCK_CLIENT]); … … 191 201 } 192 202 193 if (screen_list->socket[SOCK_CLIENT])203 if (screen_list->socket[SOCK_CLIENT]) 194 204 close(screen_list->socket[SOCK_CLIENT]); 195 205 196 if (screen_list->socket[SOCK_SERVER])206 if (screen_list->socket[SOCK_SERVER]) 197 207 close(screen_list->socket[SOCK_SERVER]); 198 208 199 if (screen_list->screen)209 if (screen_list->screen) 200 210 free(screen_list->screen); 201 211 202 212 option = screen_list->config; 203 213 204 while (option)214 while (option) 205 215 { 206 216 struct option *kromeugnon = option; 207 217 option = option->next; 208 if(kromeugnon->key) free(kromeugnon->key); 209 if(kromeugnon->value) free(kromeugnon->value); 218 if (kromeugnon->key) 219 free(kromeugnon->key); 220 if (kromeugnon->value) 221 free(kromeugnon->value); 210 222 free(kromeugnon); 211 223 } 212 224 213 for (i=0; i<screen_list->recurrent_list->count; i++)225 for (i = 0; i < screen_list->recurrent_list->count; i++) 214 226 { 215 227 remove_recurrent(screen_list->recurrent_list, i); … … 217 229 } 218 230 219 if (screen_list->recurrent_list->recurrent)231 if (screen_list->recurrent_list->recurrent) 220 232 free(screen_list->recurrent_list->recurrent); 221 if (screen_list->recurrent_list)233 if (screen_list->recurrent_list) 222 234 free(screen_list->recurrent_list); 223 235 224 if (screen_list->session_name)236 if (screen_list->session_name) 225 237 free(screen_list->session_name); 226 238 227 if (screen_list->title)239 if (screen_list->title) 228 240 free(screen_list->title); 229 241 … … 239 251 240 252 /* Create screen list */ 241 screen_list = (struct screen_list*) malloc(sizeof(struct screen_list)); 242 if(!screen_list) 243 { 244 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); 253 screen_list = (struct screen_list *)malloc(sizeof(struct screen_list)); 254 if (!screen_list) 255 { 256 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, 257 __LINE__); 245 258 return NULL; 246 259 } 247 screen_list->screen = (struct screen**) malloc(sizeof(sizeof(struct screen*))); 248 if(!screen_list->screen) 249 { 250 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); 260 screen_list->screen = 261 (struct screen **)malloc(sizeof(sizeof(struct screen *))); 262 if (!screen_list->screen) 263 { 264 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, 265 __LINE__); 251 266 free(screen_list); 252 267 return NULL; … … 263 278 screen_list->changed = 0; 264 279 screen_list->requested_delay = 0; 265 screen_list->delay = 1000/60; /* Don't refresh more than 60 times per second */ 280 screen_list->delay = 1000 / 60; /* Don't refresh more than 60 times 281 per second */ 266 282 screen_list->pty = screen_list->prevpty = 0; 267 283 screen_list->dont_update_coords = 0; … … 275 291 screen_list->socket[SOCK_SERVER] = 0; 276 292 screen_list->socket[SOCK_CLIENT] = 0; 277 screen_list->socket_dir 293 screen_list->socket_dir = NULL; 278 294 screen_list->socket_path[SOCK_SERVER] = NULL; 279 295 screen_list->socket_path[SOCK_CLIENT] = NULL; 280 screen_list->session_name 296 screen_list->session_name = NULL; 281 297 screen_list->default_shell = NULL; 282 screen_list->user_path 298 screen_list->user_path = NULL; 283 299 screen_list->autolock_timeout = -1; 284 300 screen_list->to_grab = NULL; … … 304 320 /* Build local config file path */ 305 321 user_dir = getenv("HOME"); 306 if (!user_dir)322 if (!user_dir) 307 323 { 308 324 user_info = getpwuid(getuid()); 309 if (user_info)325 if (user_info) 310 326 { 311 327 user_dir = user_info->pw_dir; 312 328 } 313 329 } 314 if(user_dir) 315 { 316 screen_list->user_path = malloc(strlen(user_dir) + strlen("/.neercsrc") + 1); 330 if (user_dir) 331 { 332 screen_list->user_path = 333 malloc(strlen(user_dir) + strlen("/.neercsrc") + 1); 317 334 sprintf(screen_list->user_path, "%s/%s", user_dir, ".neercsrc"); 318 335 } 319 336 320 337 321 screen_list->recurrent_list = (struct recurrent_list*) malloc(sizeof(struct recurrent_list)); 322 screen_list->recurrent_list->recurrent = (struct recurrent**) malloc(sizeof(struct recurrent*)); 323 if(!screen_list->recurrent_list->recurrent) 324 { 325 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); 338 screen_list->recurrent_list = 339 (struct recurrent_list *)malloc(sizeof(struct recurrent_list)); 340 screen_list->recurrent_list->recurrent = 341 (struct recurrent **)malloc(sizeof(struct recurrent *)); 342 if (!screen_list->recurrent_list->recurrent) 343 { 344 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, 345 __LINE__); 326 346 free(screen_list); 327 347 free(screen_list->screen); … … 337 357 338 358 339 int handle_command_line(int argc, char *argv[], struct screen_list *screen_list) 359 int handle_command_line(int argc, char *argv[], 360 struct screen_list *screen_list) 340 361 { 341 362 int s = 0, i; 342 for (;;)363 for (;;) 343 364 { 344 365 int option_index = 0; 345 366 int pidopt; 346 static struct myoption long_options[] = 347 { 348 { "config", 1, NULL, 'c' }, 367 static struct myoption long_options[] = { 368 {"config", 1, NULL, 'c'}, 349 369 #if defined USE_GRAB 350 { "pid", 0, NULL, 'P'},370 {"pid", 0, NULL, 'P'}, 351 371 #endif 352 { "lock-after", 1, NULL, 'l'},353 { "help", 0, NULL, 'h'},354 { "version", 0, NULL, 'v'},355 { NULL, 0, NULL, 0},356 372 {"lock-after", 1, NULL, 'l'}, 373 {"help", 0, NULL, 'h'}, 374 {"version", 0, NULL, 'v'}, 375 {NULL, 0, NULL, 0}, 376 }; 357 377 #if defined USE_GRAB 358 int c = mygetopt(argc, argv, "c:S:R::l::r::P::hv", long_options, &option_index); 378 int c = 379 mygetopt(argc, argv, "c:S:R::l::r::P::hv", long_options, 380 &option_index); 359 381 #else 360 int c = mygetopt(argc, argv, "c:S:R::l::r::hv", long_options, &option_index); 382 int c = 383 mygetopt(argc, argv, "c:S:R::l::r::hv", long_options, 384 &option_index); 361 385 #endif 362 if (c == -1)363 break; 364 365 switch (c)366 { 367 case 'c': /* --config */368 if (screen_list->user_path)386 if (c == -1) 387 break; 388 389 switch (c) 390 { 391 case 'c': /* --config */ 392 if (screen_list->user_path) 369 393 free(screen_list->user_path); 370 394 screen_list->user_path = strdup(myoptarg); 371 s +=2;395 s += 2; 372 396 break; 373 397 case 'S': 374 if (!screen_list->session_name)398 if (!screen_list->session_name) 375 399 screen_list->session_name = strdup(myoptarg); 376 s +=2;377 break; 378 case 'P': /* --pid */379 if (myoptarg)400 s += 2; 401 break; 402 case 'P': /* --pid */ 403 if (myoptarg) 380 404 { 381 405 pidopt = atoi(myoptarg); 382 if (pidopt <= 0)406 if (pidopt <= 0) 383 407 { 384 408 fprintf(stderr, "Invalid pid %d\n", pidopt); 385 if (screen_list->to_grab)409 if (screen_list->to_grab) 386 410 free(screen_list->to_grab); 387 411 return -1; … … 390 414 else 391 415 pidopt = select_process(screen_list); 392 if (pidopt <= 0)416 if (pidopt <= 0) 393 417 { 394 s +=1;418 s += 1; 395 419 break; 396 420 } 397 if (!screen_list->to_grab)421 if (!screen_list->to_grab) 398 422 { 399 423 /* At most argc-1-s times -P <pid> + final 0 */ 400 screen_list->to_grab = (int *)malloc(((argc-1-s)/2+1)*sizeof(int)); 401 if(!screen_list->to_grab) 424 screen_list->to_grab = 425 (int *)malloc(((argc - 1 - s) / 2 + 1) * sizeof(int)); 426 if (!screen_list->to_grab) 402 427 { 403 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); 428 fprintf(stderr, "Can't allocate memory at %s:%d\n", 429 __FUNCTION__, __LINE__); 404 430 return -1; 405 431 } … … 407 433 screen_list->to_grab[screen_list->nb_to_grab++] = pidopt; 408 434 screen_list->to_grab[screen_list->nb_to_grab] = 0; 409 s +=2;435 s += 2; 410 436 break; 411 437 case 'l': 412 438 screen_list->autolock_timeout = atoi(myoptarg) * 1000000; 413 if(screen_list->autolock_timeout == 0)414 screen_list->autolock_timeout -=1;439 if (screen_list->autolock_timeout == 0) 440 screen_list->autolock_timeout -= 1; 415 441 break; 416 442 case 'r': 417 443 screen_list->forceattach = 1; 418 444 case 'R': 419 if (screen_list->attach)445 if (screen_list->attach) 420 446 { 421 447 fprintf(stderr, "Attaching can only be requested once\n"); 422 448 return -1; 423 449 } 424 if (myoptarg)450 if (myoptarg) 425 451 { 426 if (screen_list->session_name)452 if (screen_list->session_name) 427 453 free(screen_list->session_name); 428 454 screen_list->session_name = strdup(myoptarg); 429 s +=1;455 s += 1; 430 456 } 431 457 screen_list->attach = 1; 432 s +=1;433 break; 434 case 'h': /* --help */458 s += 1; 459 break; 460 case 'h': /* --help */ 435 461 usage(argc, argv); 436 462 return -1; 437 463 break; 438 case 'v': /* --version */464 case 'v': /* --version */ 439 465 version(); 440 466 return -1; … … 448 474 } 449 475 } 450 if(s >= 0 && s < argc - 1) 451 { 452 screen_list->to_start = (char**)malloc((argc-s)*sizeof(char*)); 453 if(!screen_list->to_start) 454 { 455 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); 476 if (s >= 0 && s < argc - 1) 477 { 478 screen_list->to_start = (char **)malloc((argc - s) * sizeof(char *)); 479 if (!screen_list->to_start) 480 { 481 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, 482 __LINE__); 456 483 return -1; 457 484 } 458 for (i=0; i<(argc-1) - s; i++)459 { 460 screen_list->to_start[i] = strdup(argv[i +s+1]);461 } 462 screen_list->to_start[argc -1-s] = NULL;485 for (i = 0; i < (argc - 1) - s; i++) 486 { 487 screen_list->to_start[i] = strdup(argv[i + s + 1]); 488 } 489 screen_list->to_start[argc - 1 - s] = NULL; 463 490 } 464 491 return s; -
neercs/trunk/src/mygetopt.c
r3551 r3969 1 /* 1 /* 2 2 * neercs console-based window manager 3 3 * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> … … 15 15 */ 16 16 17 /* 17 /* 18 18 * mygetopt.c: getopt_long reimplementation 19 19 */ … … 28 28 #include "mygetopt.h" 29 29 30 int 30 int myoptind = 1; 31 31 char *myoptarg = NULL; 32 32 33 33 /* XXX: this getopt_long implementation should not be trusted for other 34 *applications without any serious peer reviewing. It “just works” with35 *zzuf but may fail miserably in other programs. */36 int mygetopt(int argc, char * 34 applications without any serious peer reviewing. It “just works” with 35 zzuf but may fail miserably in other programs. */ 36 int mygetopt(int argc, char *const _argv[], const char *optstring, 37 37 const struct myoption *longopts, int *longindex) 38 38 { 39 char **argv = (char **)(uintptr_t) _argv;39 char **argv = (char **)(uintptr_t) _argv; 40 40 char *flag; 41 41 int i; 42 42 43 if (myoptind >= argc)43 if (myoptind >= argc) 44 44 return -1; 45 45 46 46 flag = argv[myoptind]; 47 47 48 if (flag[0] == '-' && flag[1] != '-')48 if (flag[0] == '-' && flag[1] != '-') 49 49 { 50 50 char *tmp; 51 51 int ret = flag[1]; 52 52 53 if (ret == '\0')53 if (ret == '\0') 54 54 return -1; 55 55 56 56 tmp = strchr(optstring, ret); 57 if (!tmp || ret == ':')57 if (!tmp || ret == ':') 58 58 return '?'; 59 59 60 60 myoptind++; 61 if (tmp[1] == ':')61 if (tmp[1] == ':') 62 62 { 63 if (flag[2] != '\0')63 if (flag[2] != '\0') 64 64 myoptarg = flag + 2; 65 else if (myoptind >= argc) 66 { 67 if (tmp[2] != ':') 68 { 69 fprintf(stderr, "%s: `%s' needs an argument\n", argv[0], 70 flag); 71 return -2; 72 } 73 } 65 74 else 66 if(myoptind >= argc) 67 { 68 if(tmp[2] != ':') 69 { 70 fprintf(stderr, "%s: `%s' needs an argument\n", argv[0], flag); 71 return -2; 72 } 73 } 74 else 75 myoptarg = argv[myoptind++]; 75 myoptarg = argv[myoptind++]; 76 76 return ret; 77 77 } 78 78 79 if (flag[2] != '\0')79 if (flag[2] != '\0') 80 80 { 81 81 flag[1] = '-'; … … 87 87 } 88 88 89 if (flag[0] == '-' && flag[1] == '-')89 if (flag[0] == '-' && flag[1] == '-') 90 90 { 91 if (flag[2] == '\0')91 if (flag[2] == '\0') 92 92 return -1; 93 93 94 for (i = 0; longopts[i].name; i++)94 for (i = 0; longopts[i].name; i++) 95 95 { 96 96 size_t l = strlen(longopts[i].name); 97 97 98 if (strncmp(flag + 2, longopts[i].name, l))98 if (strncmp(flag + 2, longopts[i].name, l)) 99 99 continue; 100 100 101 switch (flag[2 + l])101 switch (flag[2 + l]) 102 102 { 103 103 case '=': 104 if (!longopts[i].has_arg)104 if (!longopts[i].has_arg) 105 105 goto bad_opt; 106 if (longindex)106 if (longindex) 107 107 *longindex = i; 108 108 myoptind++; … … 110 110 return longopts[i].val; 111 111 case '\0': 112 if (longindex)112 if (longindex) 113 113 *longindex = i; 114 114 myoptind++; 115 if (longopts[i].has_arg)115 if (longopts[i].has_arg) 116 116 myoptarg = argv[myoptind++]; 117 117 return longopts[i].val; … … 120 120 } 121 121 } 122 bad_opt:122 bad_opt: 123 123 fprintf(stderr, "%s: unrecognized option `%s'\n", argv[0], flag); 124 124 return '?'; … … 127 127 return -1; 128 128 } 129 -
neercs/trunk/src/mytrace.c
r3942 r3969 1 /* 1 /* 2 2 * neercs console-based window manager 3 3 * Copyright (c) 2008 Pascal Terjan … … 37 37 #if defined USE_GRAB 38 38 static int memcpy_from_target(struct mytrace *t, 39 char *dest, long src, size_t n);39 char *dest, long src, size_t n); 40 40 static int memcpy_into_target(struct mytrace *t, 41 41 long dest, char const *src, size_t n); … … 95 95 /* from unistd_32.h on an amd64 system */ 96 96 int syscalls32[] = { 5, 6, 4, 63, 57, 66, 37, 2, 1, 11, 54 }; 97 97 98 int syscalls64[] = 98 99 #else 99 100 int syscalls32[] = 100 101 #endif 101 { SYS_open, SYS_close, SYS_write, SYS_dup2, SYS_setpgid, SYS_setsid, 102 SYS_kill, SYS_fork, SYS_exit, SYS_execve, SYS_ioctl }; 102 { SYS_open, SYS_close, SYS_write, SYS_dup2, SYS_setpgid, SYS_setsid, 103 SYS_kill, SYS_fork, SYS_exit, SYS_execve, SYS_ioctl 104 }; 103 105 104 106 char const *syscallnames[] = 105 107 { "open", "close", "write", "dup2", "setpgid", "setsid", "kill", "fork", 106 "exit", "execve", "ioctl" }; 108 "exit", "execve", "ioctl" 109 }; 107 110 108 111 #endif /* USE_GRAB */ … … 113 116 }; 114 117 115 struct mytrace *mytrace_attach(long int pid)118 struct mytrace *mytrace_attach(long int pid) 116 119 { 117 120 #if defined USE_GRAB … … 119 122 int status; 120 123 121 if (ptrace(PTRACE_ATTACH, pid, 0, 0) < 0)124 if (ptrace(PTRACE_ATTACH, pid, 0, 0) < 0) 122 125 { 123 126 perror("PTRACE_ATTACH (attach)"); 124 127 return NULL; 125 128 } 126 if (waitpid(pid, &status, 0) < 0)129 if (waitpid(pid, &status, 0) < 0) 127 130 { 128 131 perror("waitpid"); 129 132 return NULL; 130 133 } 131 if (!WIFSTOPPED(status))134 if (!WIFSTOPPED(status)) 132 135 { 133 136 fprintf(stderr, "traced process was not stopped\n"); … … 147 150 } 148 151 149 struct mytrace *mytrace_fork(struct mytrace *t)152 struct mytrace *mytrace_fork(struct mytrace *t) 150 153 { 151 154 #if defined USE_GRAB … … 198 201 int ret, err; 199 202 200 if (ptrace(PTRACE_GETREGS, t->pid, NULL, ®s) < 0)203 if (ptrace(PTRACE_GETREGS, t->pid, NULL, ®s) < 0) 201 204 { 202 205 perror("PTRACE_GETREGS (open)\n"); … … 205 208 206 209 /* Backup the data that we will use */ 207 if (memcpy_from_target(t, backup_data, regs.RSP, size) < 0)210 if (memcpy_from_target(t, backup_data, regs.RSP, size) < 0) 208 211 return -1; 209 212 … … 241 244 int ret, err; 242 245 243 if (ptrace(PTRACE_GETREGS, t->pid, NULL, ®s) < 0)246 if (ptrace(PTRACE_GETREGS, t->pid, NULL, ®s) < 0) 244 247 { 245 248 perror("PTRACE_GETREGS (write)\n"); … … 250 253 251 254 /* Backup the data that we will use */ 252 if (memcpy_from_target(t, backup_data, regs.RSP, len) < 0)255 if (memcpy_from_target(t, backup_data, regs.RSP, len) < 0) 253 256 return -1; 254 257 … … 326 329 char *env, *p; 327 330 long p2, envaddr, argvaddr, envptraddr; 328 char envpath[PATH_MAX +1];329 ssize_t envsize = 16 *1024;331 char envpath[PATH_MAX + 1]; 332 ssize_t envsize = 16 * 1024; 330 333 int ret, fd, l, l2; 331 334 char *nullp = NULL; … … 334 337 ptrace(PTRACE_SETOPTIONS, t->pid, NULL, PTRACE_O_TRACEEXEC); 335 338 336 if (ptrace(PTRACE_GETREGS, t->pid, NULL, ®s) < 0)339 if (ptrace(PTRACE_GETREGS, t->pid, NULL, ®s) < 0) 337 340 { 338 341 perror("PTRACE_GETREGS (exec)\n"); … … 367 370 } 368 371 envsize = r; 369 l2 = sizeof(char *); /* Size of a pointer */372 l2 = sizeof(char *); /* Size of a pointer */ 370 373 p2 = regs.RSP; 371 374 372 375 /* First argument is the command string */ 373 l = strlen(command) +1;376 l = strlen(command) + 1; 374 377 memcpy_into_target(t, p2, command, l); 375 378 p2 += l; … … 392 395 envptraddr = p2; 393 396 p = env; 394 while (p < env +envsize)397 while (p < env + envsize) 395 398 { 396 399 long diffp = p - env + envaddr; 397 400 memcpy_into_target(t, p2, (char *)&diffp, l2); 398 401 p2 += l2; 399 p += strlen(p)+1;402 p += strlen(p) + 1; 400 403 } 401 404 /* And have a NULL pointer at the end of the array */ … … 420 423 int ret, err; 421 424 422 if (ptrace(PTRACE_GETREGS, t->pid, NULL, ®s) < 0)425 if (ptrace(PTRACE_GETREGS, t->pid, NULL, ®s) < 0) 423 426 { 424 427 perror("PTRACE_GETREGS (tcgets)\n"); … … 427 430 428 431 /* Backup the data that we will use */ 429 if (memcpy_from_target(t, backup_data, regs.RSP, size) < 0)430 return -1; 431 432 ret = remote_syscall(t, MYCALL_IOCTL, fd, TCGETS, regs.RSP);432 if (memcpy_from_target(t, backup_data, regs.RSP, size) < 0) 433 return -1; 434 435 ret = remote_syscall(t, MYCALL_IOCTL, fd, TCGETS, regs.RSP); 433 436 err = errno; 434 437 … … 454 457 int ret, err; 455 458 456 if (ptrace(PTRACE_GETREGS, t->pid, NULL, ®s) < 0)459 if (ptrace(PTRACE_GETREGS, t->pid, NULL, ®s) < 0) 457 460 { 458 461 perror("PTRACE_GETREGS (tcsets)\n"); … … 461 464 462 465 /* Backup the data that we will use */ 463 if (memcpy_from_target(t, backup_data, regs.RSP, size) < 0)466 if (memcpy_from_target(t, backup_data, regs.RSP, size) < 0) 464 467 return -1; 465 468 … … 491 494 } 492 495 493 /* 496 /* 494 497 * XXX: the following functions are local 495 498 */ … … 497 500 #if defined USE_GRAB 498 501 static int memcpy_from_target(struct mytrace *t, 499 char *dest, long src, size_t n)502 char *dest, long src, size_t n) 500 503 { 501 504 static int const align = sizeof(long) - 1; 502 505 503 while (n)506 while (n) 504 507 { 505 508 long data; 506 509 size_t todo = sizeof(long) - (src & align); 507 510 508 if (n < todo)511 if (n < todo) 509 512 todo = n; 510 513 511 514 data = ptrace(PTRACE_PEEKTEXT, t->pid, src - (src & align), 0); 512 if (errno)515 if (errno) 513 516 { 514 517 perror("ptrace_peektext (memcpy_from_target)"); … … 530 533 static int const align = sizeof(long) - 1; 531 534 532 while (n)535 while (n) 533 536 { 534 537 long data; 535 538 size_t todo = sizeof(long) - (dest & align); 536 539 537 if (n < todo)540 if (n < todo) 538 541 todo = n; 539 if (todo != sizeof(long))542 if (todo != sizeof(long)) 540 543 { 541 544 data = ptrace(PTRACE_PEEKTEXT, t->pid, dest - (dest & align), 0); 542 if (errno)545 if (errno) 543 546 { 544 547 perror("ptrace_peektext (memcpy_into_target)"); … … 548 551 549 552 memcpy((char *)&data + (dest & align), src, todo); 550 if (ptrace(PTRACE_POKETEXT, t->pid, dest - (dest & align), data) < 0)553 if (ptrace(PTRACE_POKETEXT, t->pid, dest - (dest & align), data) < 0) 551 554 { 552 555 perror("ptrace_poketext (memcpy_into_target)"); … … 565 568 long arg1, long arg2, long arg3) 566 569 { 567 /* Method for remote syscall: 568 * - wait until the traced application exits from a syscall 569 * - save registers 570 * - rewind eip/rip to point on the syscall instruction 571 * - single step: execute syscall instruction 572 * - retrieve resulting registers 573 * - restore registers */ 570 /* Method for remote syscall: - wait until the traced application exits 571 from a syscall - save registers - rewind eip/rip to point on the 572 syscall instruction - single step: execute syscall instruction - 573 retrieve resulting registers - restore registers */ 574 574 struct user_regs_struct regs, oldregs; 575 575 long oinst; … … 577 577 int offset = 2; 578 578 579 if(call < 0 || call >= (long)(sizeof(syscallnames)/sizeof(*syscallnames))) 579 if (call < 0 580 || call >= (long)(sizeof(syscallnames) / sizeof(*syscallnames))) 580 581 { 581 582 fprintf(stderr, "unknown remote syscall %li\n", call); … … 592 593 #endif 593 594 594 for (;;)595 { 596 if (ptrace(PTRACE_GETREGS, t->pid, NULL, &oldregs) < 0)595 for (;;) 596 { 597 if (ptrace(PTRACE_GETREGS, t->pid, NULL, &oldregs) < 0) 597 598 { 598 599 perror("PTRACE_GETREGS (syscall 1)\n"); … … 603 604 604 605 #if defined __x86_64__ 605 if (oinst == SYSCALL_AMD64)606 if (oinst == SYSCALL_AMD64) 606 607 break; 607 608 #endif 608 if (oinst == SYSCALL_X86 || oinst == SYSCALL_X86_NEW)609 if (oinst == SYSCALL_X86 || oinst == SYSCALL_X86_NEW) 609 610 { 610 611 bits = 32; … … 612 613 } 613 614 614 if (ptrace(PTRACE_SYSCALL, t->pid, NULL, 0) < 0)615 if (ptrace(PTRACE_SYSCALL, t->pid, NULL, 0) < 0) 615 616 { 616 617 perror("ptrace_syscall (1)"); … … 618 619 } 619 620 waitpid(t->pid, NULL, 0); 620 if (ptrace(PTRACE_SYSCALL, t->pid, NULL, 0) < 0)621 if (ptrace(PTRACE_SYSCALL, t->pid, NULL, 0) < 0) 621 622 { 622 623 perror("ptrace_syscall (2)"); … … 628 629 print_registers(t->pid); 629 630 630 if(oinst == SYSCALL_X86_NEW) 631 { 632 /* Get back to sysenter */ 633 while((ptrace(PTRACE_PEEKTEXT, t->pid, oldregs.RIP - offset, 0) & 0xffff) != 0x340f) 631 if (oinst == SYSCALL_X86_NEW) 632 { 633 /* Get back to sysenter */ 634 while ((ptrace(PTRACE_PEEKTEXT, t->pid, oldregs.RIP - offset, 0) & 635 0xffff) != 0x340f) 634 636 offset++; 635 637 oldregs.RBP = oldregs.RSP; … … 639 641 regs.RIP = regs.RIP - offset; 640 642 #if defined __x86_64__ 641 if (bits == 64)643 if (bits == 64) 642 644 { 643 645 regs.RAX = syscalls64[call]; … … 655 657 } 656 658 657 if (ptrace(PTRACE_SETREGS, t->pid, NULL, ®s) < 0)659 if (ptrace(PTRACE_SETREGS, t->pid, NULL, ®s) < 0) 658 660 { 659 661 perror("PTRACE_SETREGS (syscall 1)\n"); … … 661 663 } 662 664 663 for (;;)665 for (;;) 664 666 { 665 667 int status; … … 667 669 print_registers(t->pid); 668 670 669 if (ptrace(PTRACE_SINGLESTEP, t->pid, NULL, NULL) < 0)671 if (ptrace(PTRACE_SINGLESTEP, t->pid, NULL, NULL) < 0) 670 672 { 671 673 perror("PTRACE_SINGLESTEP (syscall)\n"); … … 674 676 waitpid(t->pid, &status, 0); 675 677 676 if (WIFEXITED(status))678 if (WIFEXITED(status)) 677 679 return 0; 678 680 679 if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGTRAP)681 if (!WIFSTOPPED(status) || WSTOPSIG(status) != SIGTRAP) 680 682 continue; 681 683 682 684 /* Fuck Linux: there is no macro for this */ 683 switch ((status >> 16) & 0xffff)685 switch ((status >> 16) & 0xffff) 684 686 { 685 687 case PTRACE_EVENT_FORK: 686 if (ptrace(PTRACE_GETEVENTMSG, t->pid, 0, &t->child) < 0)688 if (ptrace(PTRACE_GETEVENTMSG, t->pid, 0, &t->child) < 0) 687 689 { 688 690 perror("PTRACE_GETEVENTMSG (syscall)\n"); … … 705 707 print_registers(t->pid); 706 708 707 if (ptrace(PTRACE_GETREGS, t->pid, NULL, ®s) < 0)709 if (ptrace(PTRACE_GETREGS, t->pid, NULL, ®s) < 0) 708 710 { 709 711 perror("PTRACE_GETREGS (syscall 2)\n"); … … 711 713 } 712 714 713 if (ptrace(PTRACE_SETREGS, t->pid, NULL, &oldregs) < 0)715 if (ptrace(PTRACE_SETREGS, t->pid, NULL, &oldregs) < 0) 714 716 { 715 717 perror("PTRACE_SETREGS (syscall 2)\n"); … … 720 722 debug("syscall %s returned %ld", syscallnames[call], regs.RAX); 721 723 722 if ((long)regs.RAX < 0)724 if ((long)regs.RAX < 0) 723 725 { 724 726 errno = -(long)regs.RAX; … … 734 736 static void print_registers(pid_t pid) 735 737 { 736 union { long int l; unsigned char data[sizeof(long int)]; } inst; 738 union 739 { 740 long int l; 741 unsigned char data[sizeof(long int)]; 742 } inst; 737 743 struct user_regs_struct regs; 738 744 int i; 739 745 740 if (ptrace(PTRACE_GETREGS, pid, NULL, ®s) < 0)746 if (ptrace(PTRACE_GETREGS, pid, NULL, ®s) < 0) 741 747 { 742 748 perror("PTRACE_GETREGS (syscall 2)"); … … 744 750 } 745 751 746 fprintf(stderr, " / %s: " FMT" ", STRINGIFY(RAX), regs.RAX);747 fprintf(stderr, "%s: " FMT"\n", STRINGIFY(RBX), regs.RBX);748 fprintf(stderr, " | %s: " FMT" ", STRINGIFY(RCX), regs.RCX);749 fprintf(stderr, "%s: " FMT"\n", STRINGIFY(RDX), regs.RDX);750 fprintf(stderr, " | %s: " FMT" ", STRINGIFY(RDI), regs.RDI);751 fprintf(stderr, "%s: " FMT"\n", STRINGIFY(RSI), regs.RSI);752 fprintf(stderr, " | %s: " FMT" ", STRINGIFY(RSP), regs.RSP);753 fprintf(stderr, "%s: " FMT"\n", STRINGIFY(RIP), regs.RIP);752 fprintf(stderr, " / %s: " FMT " ", STRINGIFY(RAX), regs.RAX); 753 fprintf(stderr, "%s: " FMT "\n", STRINGIFY(RBX), regs.RBX); 754 fprintf(stderr, " | %s: " FMT " ", STRINGIFY(RCX), regs.RCX); 755 fprintf(stderr, "%s: " FMT "\n", STRINGIFY(RDX), regs.RDX); 756 fprintf(stderr, " | %s: " FMT " ", STRINGIFY(RDI), regs.RDI); 757 fprintf(stderr, "%s: " FMT "\n", STRINGIFY(RSI), regs.RSI); 758 fprintf(stderr, " | %s: " FMT " ", STRINGIFY(RSP), regs.RSP); 759 fprintf(stderr, "%s: " FMT "\n", STRINGIFY(RIP), regs.RIP); 754 760 755 761 inst.l = ptrace(PTRACE_PEEKTEXT, pid, regs.RIP - 4, 0); … … 761 767 762 768 fprintf(stderr, " \\ stack: ... "); 763 for (i = -16; i < 24; i += sizeof(long))769 for (i = -16; i < 24; i += sizeof(long)) 764 770 { 765 771 inst.l = ptrace(PTRACE_PEEKDATA, pid, regs.RSP + i, 0); … … 772 778 inst.data[0], inst.data[1], inst.data[2], inst.data[3]); 773 779 #endif 774 if (i == 0)780 if (i == 0) 775 781 fprintf(stderr, "[%s] ", STRINGIFY(RSP)); 776 782 } -
neercs/trunk/src/recurrent.c
r3551 r3969 1 /* 1 /* 2 2 * neercs console-based window manager 3 3 * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> … … 31 31 32 32 33 int handle_recurrents(struct screen_list *screen_list)33 int handle_recurrents(struct screen_list *screen_list) 34 34 { 35 35 int refresh = 0, i; 36 36 /* Recurrent functions */ 37 for (i=0; i<screen_list->recurrent_list->count; i++)37 for (i = 0; i < screen_list->recurrent_list->count; i++) 38 38 { 39 if (screen_list->recurrent_list->recurrent[i]->function)39 if (screen_list->recurrent_list->recurrent[i]->function) 40 40 { 41 refresh |= screen_list->recurrent_list->recurrent[i]->function(screen_list, 42 screen_list->recurrent_list->recurrent[i], 43 screen_list->recurrent_list->recurrent[i]->user, 44 get_us()); 41 refresh |= 42 screen_list->recurrent_list->recurrent[i]-> 43 function(screen_list, 44 screen_list->recurrent_list->recurrent[i], 45 screen_list->recurrent_list->recurrent[i]->user, 46 get_us()); 45 47 } 46 48 } 47 49 /* Delete recurrent functions */ 48 for (i=0; i<screen_list->recurrent_list->count; i++)50 for (i = 0; i < screen_list->recurrent_list->count; i++) 49 51 { 50 if (screen_list->recurrent_list->recurrent[i]->kill_me)52 if (screen_list->recurrent_list->recurrent[i]->kill_me) 51 53 { 52 54 remove_recurrent(screen_list->recurrent_list, i); … … 58 60 59 61 60 /* Add recurrent function. It will be called at each main loop iteration, unless it is removed */ 62 /* Add recurrent function. It will be called at each main loop iteration, 63 unless it is removed */ 61 64 int add_recurrent(struct recurrent_list *list, 62 int (*func)(struct screen_list*, struct recurrent* rec, void *user, long long unsigned int t), 65 int (*func) (struct screen_list *, struct recurrent * rec, 66 void *user, long long unsigned int t), 63 67 void *user) 64 68 { 65 if(list == NULL || func == NULL) return -1; 69 if (list == NULL || func == NULL) 70 return -1; 66 71 67 list->recurrent = (struct recurrent **)realloc(list->recurrent,68 sizeof(struct recurrent *)69 * (list->count +1));72 list->recurrent = (struct recurrent **)realloc(list->recurrent, 73 sizeof(struct recurrent *) 74 * (list->count + 1)); 70 75 71 if(!list->recurrent) 72 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); 76 if (!list->recurrent) 77 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, 78 __LINE__); 73 79 74 80 list->recurrent[list->count] = malloc(sizeof(struct recurrent)); 75 81 76 list->recurrent[list->count]->kill_me 77 list->recurrent[list->count]->function 78 list->recurrent[list->count]->user 82 list->recurrent[list->count]->kill_me = 0; 83 list->recurrent[list->count]->function = func; 84 list->recurrent[list->count]->user = user; 79 85 list->recurrent[list->count]->start_time = get_us(); 80 86 list->count++; 81 87 82 return list->count -1;88 return list->count - 1; 83 89 } 84 90 … … 88 94 { 89 95 90 if(n>=list->count) return -1; 96 if (n >= list->count) 97 return -1; 91 98 92 99 memmove(&list->recurrent[n], 93 &list->recurrent[n +1],94 sizeof(struct recurrent *)*(list->count-(n+1)));100 &list->recurrent[n + 1], 101 sizeof(struct recurrent *) * (list->count - (n + 1))); 95 102 96 103 free(list->recurrent[n]); 97 list->recurrent = (struct recurrent**) realloc(list->recurrent, 98 sizeof(sizeof(struct recurrent*)) 99 * (list->count)); 100 if(!list->recurrent) 101 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); 104 list->recurrent = (struct recurrent **)realloc(list->recurrent, 105 sizeof(sizeof 106 (struct recurrent *)) 107 * (list->count)); 108 if (!list->recurrent) 109 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, 110 __LINE__); 102 111 103 112 -
neercs/trunk/src/screens.c
r3967 r3969 1 /* 1 /* 2 2 * neercs console-based window manager 3 3 * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> … … 30 30 #include "neercs.h" 31 31 32 struct screen *create_screen_grab(int w, int h, int pid)33 { 34 struct screen *s = (struct screen *)malloc(sizeof(struct screen));32 struct screen *create_screen_grab(int w, int h, int pid) 33 { 34 struct screen *s = (struct screen *)malloc(sizeof(struct screen)); 35 35 36 36 s->cv = caca_create_canvas(w, h); … … 48 48 s->fd = create_pty_grab(pid, w, h, &s->pid); 49 49 50 if (s->fd < 0)50 if (s->fd < 0) 51 51 { 52 52 caca_free_canvas(s->cv); … … 57 57 } 58 58 59 struct screen *create_screen(int w, int h, char *command)60 { 61 struct screen *s = (struct screen *)malloc(sizeof(struct screen));59 struct screen *create_screen(int w, int h, char *command) 60 { 61 struct screen *s = (struct screen *)malloc(sizeof(struct screen)); 62 62 63 63 s->cv = caca_create_canvas(w, h); … … 82 82 83 83 84 if (s->fd < 0)84 if (s->fd < 0) 85 85 { 86 86 caca_free_canvas(s->cv); … … 93 93 int destroy_screen(struct screen *s) 94 94 { 95 if (s->fd>=0)95 if (s->fd >= 0) 96 96 close(s->fd); 97 if (s->buf)97 if (s->buf) 98 98 free(s->buf); 99 if (s->title)99 if (s->title) 100 100 free(s->title); 101 101 s->buf = NULL; 102 if (s->cv)102 if (s->cv) 103 103 caca_free_canvas(s->cv); 104 104 s->cv = NULL; … … 109 109 int add_screen(struct screen_list *list, struct screen *s) 110 110 { 111 if(list == NULL || s == NULL) return -1; 111 if (list == NULL || s == NULL) 112 return -1; 112 113 113 114 else 114 115 { 115 list->screen = (struct screen**) realloc(list->screen, 116 sizeof(sizeof(struct screen*)) 117 * (list->count+1)); 118 if(!list->screen) 119 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); 116 list->screen = (struct screen **)realloc(list->screen, 117 sizeof(sizeof 118 (struct screen *)) * 119 (list->count + 1)); 120 if (!list->screen) 121 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, 122 __LINE__); 120 123 list->screen[list->count] = s; 121 124 list->count++; … … 124 127 list->changed = 1; 125 128 126 return list->count -1;129 return list->count - 1; 127 130 } 128 131 … … 130 133 { 131 134 132 if(n>=list->count) return -1; 133 134 if(please_kill) 135 if (n >= list->count) 136 return -1; 137 138 if (please_kill) 135 139 { 136 140 int status = 0; … … 141 145 kill(list->screen[n]->pid, SIGINT); 142 146 ret = waitpid(list->screen[n]->pid, &status, 143 WNOHANG |WUNTRACED|WCONTINUED);144 if (!ret)147 WNOHANG | WUNTRACED | WCONTINUED); 148 if (!ret) 145 149 kill(list->screen[n]->pid, SIGQUIT); 146 150 ret = waitpid(list->screen[n]->pid, &status, 147 WNOHANG |WUNTRACED|WCONTINUED);148 if (!ret)151 WNOHANG | WUNTRACED | WCONTINUED); 152 if (!ret) 149 153 kill(list->screen[n]->pid, SIGABRT); 150 154 ret = waitpid(list->screen[n]->pid, &status, 151 WNOHANG |WUNTRACED|WCONTINUED);152 if (!ret)155 WNOHANG | WUNTRACED | WCONTINUED); 156 if (!ret) 153 157 kill(list->screen[n]->pid, SIGKILL); 154 158 … … 157 161 158 162 memmove(&list->screen[n], 159 &list->screen[n +1],160 sizeof(struct screen *)*(list->count-(n+1)));161 162 list->screen = (struct screen **)realloc(list->screen,163 sizeof(sizeof(struct screen *))163 &list->screen[n + 1], 164 sizeof(struct screen *) * (list->count - (n + 1))); 165 166 list->screen = (struct screen **)realloc(list->screen, 167 sizeof(sizeof(struct screen *)) 164 168 * (list->count)); 165 if(!list->screen) 166 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); 169 if (!list->screen) 170 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, 171 __LINE__); 167 172 168 173 … … 179 184 int i; 180 185 181 if(!screen_list->count) return; 186 if (!screen_list->count) 187 return; 182 188 183 189 184 190 debug("Current pty is %d\n", screen_list->pty); 185 191 186 screen_list->width = caca_get_canvas_width(screen_list->cv); 187 screen_list->height = caca_get_canvas_height(screen_list->cv) - (screen_list->mini*6) - screen_list->status; 188 189 if(!screen_list->dont_update_coords) 192 screen_list->width = caca_get_canvas_width(screen_list->cv); 193 screen_list->height = 194 caca_get_canvas_height(screen_list->cv) - (screen_list->mini * 6) - 195 screen_list->status; 196 197 if (!screen_list->dont_update_coords) 190 198 { 191 199 update_windows_props(screen_list); 192 200 } 193 201 194 if (screen_list->changed)202 if (screen_list->changed) 195 203 { 196 204 caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_DEFAULT); … … 208 216 209 217 210 if (screen_list->mini)218 if (screen_list->mini) 211 219 { 212 220 draw_thumbnails(screen_list); 213 221 } 214 if (screen_list->status)222 if (screen_list->status) 215 223 { 216 224 draw_status(screen_list); 217 225 } 218 if (screen_list->help)226 if (screen_list->help) 219 227 { 220 228 draw_help(screen_list); 221 229 } 222 if (screen_list->window_list)230 if (screen_list->window_list) 223 231 { 224 232 draw_list(screen_list); 225 233 } 226 234 screen_list->changed = 0; 227 for (i = screen_list->count - 1; i >=0; i--)235 for (i = screen_list->count - 1; i >= 0; i--) 228 236 screen_list->screen[i]->changed = 0; 229 237 } 230 238 231 239 232 int update_screens_contents(struct screen_list *screen_list)240 int update_screens_contents(struct screen_list *screen_list) 233 241 { 234 242 int i, refresh = 0; … … 240 248 /* Read data, if any */ 241 249 FD_ZERO(&fdset); 242 for (i = 0; i < screen_list->count; i++)243 { 244 if (screen_list->screen[i]->fd >= 0)250 for (i = 0; i < screen_list->count; i++) 251 { 252 if (screen_list->screen[i]->fd >= 0) 245 253 FD_SET(screen_list->screen[i]->fd, &fdset); 246 if (screen_list->screen[i]->fd > maxfd)254 if (screen_list->screen[i]->fd > maxfd) 247 255 maxfd = screen_list->screen[i]->fd; 248 256 } … … 251 259 ret = select(maxfd + 1, &fdset, NULL, NULL, &tv); 252 260 253 if (ret < 0)254 { 255 if (errno == EINTR)256 ; /* We probably got a SIGWINCH, ignore it */261 if (ret < 0) 262 { 263 if (errno == EINTR) 264 ; /* We probably got a SIGWINCH, ignore it */ 257 265 else 258 266 { 259 /* FIXME: Useless since break will mean that we return 0 260 * But Idon't know what was the purpose of this code */261 for (i = 0; i < screen_list->count; i++)262 if (screen_list->screen[i]->total)267 /* FIXME: Useless since break will mean that we return 0 But I 268 don't know what was the purpose of this code */ 269 for (i = 0; i < screen_list->count; i++) 270 if (screen_list->screen[i]->total) 263 271 break; 264 if (i == screen_list->count)272 if (i == screen_list->count) 265 273 return 0; 266 274 } 267 275 } 268 else if(ret) 269 { 270 271 for(i = 0; i < screen_list->count; i++) 272 { 273 /* FIXME: try a new strategy: read all filedescriptors until 274 * each of them starved at least once. */ 275 276 if(screen_list->screen[i]->fd < 0 || 277 !FD_ISSET(screen_list->screen[i]->fd, &fdset)) 278 continue; 279 280 for(;;) 276 else if (ret) 277 { 278 279 for (i = 0; i < screen_list->count; i++) 281 280 { 282 ssize_t nr; 283 284 screen_list->screen[i]->buf = 285 realloc(screen_list->screen[i]->buf, 286 screen_list->screen[i]->total + 1024); 287 if(!screen_list->screen[i]->buf) 288 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); 289 290 nr = read(screen_list->screen[i]->fd, 291 screen_list->screen[i]->buf + 292 screen_list->screen[i]->total, 1024); 293 294 if(nr > 0) 281 /* FIXME: try a new strategy: read all filedescriptors until each 282 of them starved at least once. */ 283 284 if (screen_list->screen[i]->fd < 0 || 285 !FD_ISSET(screen_list->screen[i]->fd, &fdset)) 286 continue; 287 288 for (;;) 295 289 { 296 screen_list->screen[i]->total += nr; 297 continue; 290 ssize_t nr; 291 292 screen_list->screen[i]->buf = 293 realloc(screen_list->screen[i]->buf, 294 screen_list->screen[i]->total + 1024); 295 if (!screen_list->screen[i]->buf) 296 fprintf(stderr, "Can't allocate memory at %s:%d\n", 297 __FUNCTION__, __LINE__); 298 299 nr = read(screen_list->screen[i]->fd, 300 screen_list->screen[i]->buf + 301 screen_list->screen[i]->total, 1024); 302 303 if (nr > 0) 304 { 305 screen_list->screen[i]->total += nr; 306 continue; 307 } 308 309 if (nr == 0 || errno != EWOULDBLOCK) 310 { 311 remove_screen(screen_list, i, 0); 312 if (i < screen_list->prevpty) 313 screen_list->prevpty--; 314 if (i == screen_list->pty) 315 { 316 screen_list->pty = screen_list->prevpty; 317 screen_list->prevpty = 0; 318 } 319 if (i < (screen_list->pty)) 320 (screen_list->pty)--; 321 /* Don't skip the element which is now at position i */ 322 i--; 323 refresh = 1; 324 } 325 326 break; 298 327 } 299 300 if(nr == 0 || errno != EWOULDBLOCK)301 {302 remove_screen(screen_list, i, 0);303 if(i < screen_list->prevpty) screen_list->prevpty--;304 if(i == screen_list->pty)305 {306 screen_list->pty = screen_list->prevpty;307 screen_list->prevpty = 0;308 }309 if(i < (screen_list->pty)) (screen_list->pty)--;310 /* Don't skip the element which is now at position i */311 i--;312 refresh = 1;313 }314 315 break;316 328 } 317 329 } 318 }319 330 return refresh; 320 331 } 321 322 -
neercs/trunk/src/screensaver.c
r3954 r3969 1 /* 1 /* 2 2 * neercs console-based window manager 3 3 * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> … … 57 57 58 58 char toaster_text[3][99] = { { 59 " __._ \n" 60 " .-'== _',\n" 61 " <|_= .-' |\n" 62 " | --| \\'.-_ \n" 63 " | | \\ \" _.\n" 64 " `-_|.-\\_.-\n" 65 }, { 66 " __._ \n" 67 " .-'== _',\n" 68 " \\|_= .-' |\n" 69 " | --| __'-.\n" 70 " | | ___.-\n" 71 " `-_|.-\n" 72 }, { 73 " _- __._\n" 74 " /.-'== _',_.-.\n" 75 " \\|_= .-'/ _.'\n" 76 " | --| / .-\n" 77 " | | _.|\n" 78 " `-_|.-\n"} }; 59 " __._ \n" 60 " .-'== _',\n" 61 " <|_= .-' |\n" 62 " | --| \\'.-_ \n" 63 " | | \\ \" _.\n" " `-_|.-\\_.-\n"}, { 64 " __._ \n" 65 " .-'== _',\n" 66 " \\|_= .-' |\n" 67 " | --| __'-.\n" 68 " | | ___.-\n" 69 " `-_|.-\n"}, 70 { 71 " _- __._\n" " /.-'== _',_.-.\n" " \\|_= .-'/ _.'\n" " | --| / .-\n" 72 " | | _.|\n" " `-_|.-\n"} 73 }; 79 74 80 75 char toaster_mask[3][99] = { { 81 " __._ \n" 82 " .-'== _',\n" 83 " <|_=X.-'XX|\n" 84 " |X--|XXX\\'.-_ \n" 85 " |XXX|X\\XX\"X_.\n" 86 " `-_|.-\\_.-\n" 87 }, { 76 " __._ \n" 77 " .-'== _',\n" 78 " <|_=X.-'XX|\n" 79 " |X--|XXX\\'.-_ \n" 80 " |XXX|X\\XX\"X_.\n" " `-_|.-\\_.-\n"}, { 88 81 89 " __._ \n" 90 " .-'== _',\n" 91 " \\|_= .-'XX|\n" 92 " |X--|XX__'-.\n" 93 " |XXX|X___.-\n" 94 " `-_|.-\n" 95 }, { 96 " _- __._\n" 97 " /.-'== _',_.-.\n" 98 " \\|_= .-'/XX_.'\n" 99 " |X--|X/X.-\n" 100 " |XXX|XX_.|\n" 101 " `-_|.-\n"} }; 82 " __._ \n" 83 " .-'== _',\n" 84 " \\|_= .-'XX|\n" 85 " |X--|XX__'-.\n" 86 " |XXX|X___.-\n" 87 " `-_|.-\n"}, 88 { 89 " _- __._\n" " /.-'== _',_.-.\n" " \\|_= .-'/XX_.'\n" " |X--|X/X.-\n" 90 " |XXX|XX_.|\n" " `-_|.-\n"} 91 }; 102 92 103 93 struct flying_toaster … … 116 106 117 107 flying_toaster = malloc(sizeof(struct flying_toaster)); 118 flying_toaster->toaster = (caca_canvas_t **)malloc(sizeof(caca_canvas_t*)*3); 119 flying_toaster->mask = (caca_canvas_t **)malloc(sizeof(caca_canvas_t*)*3); 108 flying_toaster->toaster = 109 (caca_canvas_t **) malloc(sizeof(caca_canvas_t *) * 3); 110 flying_toaster->mask = 111 (caca_canvas_t **) malloc(sizeof(caca_canvas_t *) * 3); 120 112 121 for (i = 0; i < 3; i++)113 for (i = 0; i < 3; i++) 122 114 { 123 115 flying_toaster->toaster[i] = caca_create_canvas(0, 0); 124 116 flying_toaster->mask[i] = caca_create_canvas(0, 0); 125 117 caca_import_canvas_from_memory(flying_toaster->toaster[i], 126 toaster_text[i],127 strlen(toaster_text[i]), "utf8");118 toaster_text[i], 119 strlen(toaster_text[i]), "utf8"); 128 120 caca_import_canvas_from_memory(flying_toaster->mask[i], 129 toaster_mask[i],130 strlen(toaster_mask[i]), "utf8");121 toaster_mask[i], 122 strlen(toaster_mask[i]), "utf8"); 131 123 } 132 124 133 for (i = 0; i < COUNT; i++)125 for (i = 0; i < COUNT; i++) 134 126 { 135 flying_toaster->x[i] = (rand() %w) * PRECISION;136 flying_toaster->y[i] = (rand() %h) * PRECISION;137 flying_toaster->s[i] = (rand() %3) * PRECISION;127 flying_toaster->x[i] = (rand() % w) * PRECISION; 128 flying_toaster->y[i] = (rand() % h) * PRECISION; 129 flying_toaster->s[i] = (rand() % 3) * PRECISION; 138 130 } 139 131 … … 158 150 struct flying_toaster *d = screen_list->screensaver_data; 159 151 int i, w, h, x, y, s; 160 if(!d) return; 152 if (!d) 153 return; 161 154 162 155 w = caca_get_canvas_width(screen_list->cv); … … 166 159 caca_clear_canvas(screen_list->cv); 167 160 168 for (i = 0; i < COUNT; i++)161 for (i = 0; i < COUNT; i++) 169 162 { 170 x = d->x[i] /PRECISION;171 y = d->y[i] /PRECISION;172 s = d->s[i] /PRECISION;163 x = d->x[i] / PRECISION; 164 y = d->y[i] / PRECISION; 165 s = d->s[i] / PRECISION; 173 166 174 167 caca_blit(screen_list->cv, x, y, d->toaster[s], d->mask[s]); 175 168 176 d->x[i] -=40;177 d->y[i] +=10;169 d->x[i] -= 40; 170 d->y[i] += 10; 178 171 179 if (d->x[i]/PRECISION + caca_get_canvas_width(d->toaster[s])<=0)180 d->x[i] = ((rand() %w)/3+w) * PRECISION;181 if (d->y[i]/PRECISION>=h)182 d->y[i] = ((rand() %h)/2-h) * PRECISION;172 if (d->x[i] / PRECISION + caca_get_canvas_width(d->toaster[s]) <= 0) 173 d->x[i] = ((rand() % w) / 3 + w) * PRECISION; 174 if (d->y[i] / PRECISION >= h) 175 d->y[i] = ((rand() % h) / 2 - h) * PRECISION; 183 176 184 d->s[i] = ((d->s[i] +24)%(3*PRECISION));177 d->s[i] = ((d->s[i] + 24) % (3 * PRECISION)); 185 178 } 186 179 } -
neercs/trunk/src/server.c
r3964 r3969 455 455 if (screen_list->help) 456 456 { 457 if (c == CACA_KEY_ESCAPE || 458 c == 'h') 457 if (c == CACA_KEY_ESCAPE || c == 'h') 459 458 { 460 459 screen_list->help = 0; -
neercs/trunk/src/term.c
r3968 r3969 1 /* 1 /* 2 2 * neercs console-based window manager 3 3 * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> … … 20 20 #include <string.h> 21 21 #if defined HAVE_PTY_H 22 # include <pty.h> /* for openpty and forkpty */22 # include <pty.h> /* for openpty and forkpty */ 23 23 #else 24 # include <util.h> /* for OS X */24 # include <util.h> /* for OS X */ 25 25 #endif 26 26 #include <unistd.h> … … 37 37 switch (uc) 38 38 { 39 case '+': return 0x2192; /* RIGHTWARDS ARROW */ 40 case ',': return 0x2190; /* LEFTWARDS ARROW */ 41 case '-': return 0x2191; /* UPWARDS ARROW */ 42 case '.': return 0x2193; /* DOWNWARDS ARROW */ 43 case '0': return 0x25AE; /* BLACK VERTICAL RECTANGLE */ 44 case '_': return 0x25AE; /* BLACK VERTICAL RECTANGLE */ 45 case '`': return 0x25C6; /* BLACK DIAMOND */ 46 case 'a': return 0x2592; /* MEDIUM SHADE */ 47 case 'b': return 0x2409; /* SYMBOL FOR HORIZONTAL TABULATION */ 48 case 'c': return 0x240C; /* SYMBOL FOR FORM FEED */ 49 case 'd': return 0x240D; /* SYMBOL FOR CARRIAGE RETURN */ 50 case 'e': return 0x240A; /* SYMBOL FOR LINE FEED */ 51 case 'f': return 0x00B0; /* DEGREE SIGN */ 52 case 'g': return 0x00B1; /* PLUS-MINUS SIGN */ 53 case 'h': return 0x2424; /* SYMBOL FOR NEWLINE */ 54 case 'i': return 0x240B; /* SYMBOL FOR VERTICAL TABULATION */ 55 case 'j': return 0x2518; /* BOX DRAWINGS LIGHT UP AND LEFT */ 56 case 'k': return 0x2510; /* BOX DRAWINGS LIGHT DOWN AND LEFT */ 57 case 'l': return 0x250C; /* BOX DRAWINGS LIGHT DOWN AND RIGHT */ 58 case 'm': return 0x2514; /* BOX DRAWINGS LIGHT UP AND RIGHT */ 59 case 'n': return 0x253C; /* BOX DRAWINGS LIGHT VERTICAL AND HORIZONTAL */ 60 case 'o': return 0x23BA; /* HORIZONTAL SCAN LINE-1 */ 61 case 'p': return 0x23BB; /* HORIZONTAL SCAN LINE-3 */ 62 case 'q': return 0x2500; /* BOX DRAWINGS LIGHT HORIZONTAL */ 63 case 'r': return 0x23BC; /* HORIZONTAL SCAN LINE-7 */ 64 case 's': return 0x23BD; /* HORIZONTAL SCAN LINE-9 */ 65 case 't': return 0x251C; /* BOX DRAWINGS LIGHT VERTICAL AND RIGHT */ 66 case 'u': return 0x2524; /* BOX DRAWINGS LIGHT VERTICAL AND LEFT */ 67 case 'v': return 0x2534; /* BOX DRAWINGS LIGHT UP AND HORIZONTAL */ 68 case 'w': return 0x252C; /* BOX DRAWINGS LIGHT DOWN AND HORIZONTAL */ 69 case 'x': return 0x2502; /* BOX DRAWINGS LIGHT VERTICAL */ 70 case 'y': return 0x2264; /* LESS-THAN OR EQUAL TO */ 71 case 'z': return 0x2265; /* GREATER-THAN OR EQUAL TO */ 72 case '{': return 0x03C0; /* GREEK SMALL LETTER PI */ 73 case '|': return 0x2260; /* NOT EQUAL TO */ 74 case '}': return 0x00A3; /* POUND SIGN */ 75 case '~': return 0x00B7; /* MIDDLE DOT */ 39 case '+': 40 return 0x2192; /* RIGHTWARDS ARROW */ 41 case ',': 42 return 0x2190; /* LEFTWARDS ARROW */ 43 case '-': 44 return 0x2191; /* UPWARDS ARROW */ 45 case '.': 46 return 0x2193; /* DOWNWARDS ARROW */ 47 case '0': 48 return 0x25AE; /* BLACK VERTICAL RECTANGLE */ 49 case '_': 50 return 0x25AE; /* BLACK VERTICAL RECTANGLE */ 51 case '`': 52 return 0x25C6; /* BLACK DIAMOND */ 53 case 'a': 54 return 0x2592; /* MEDIUM SHADE */ 55 case 'b': 56 return 0x2409; /* SYMBOL FOR HORIZONTAL TABULATION */ 57 case 'c': 58 return 0x240C; /* SYMBOL FOR FORM FEED */ 59 case 'd': 60 return 0x240D; /* SYMBOL FOR CARRIAGE RETURN */ 61 case 'e': 62 return 0x240A; /* SYMBOL FOR LINE FEED */ 63 case 'f': 64 return 0x00B0; /* DEGREE SIGN */ 65 case 'g': 66 return 0x00B1; /* PLUS-MINUS SIGN */ 67 case 'h': 68 return 0x2424; /* SYMBOL FOR NEWLINE */ 69 case 'i': 70 return 0x240B; /* SYMBOL FOR VERTICAL TABULATION */ 71 case 'j': 72 return 0x2518; /* BOX DRAWINGS LIGHT UP AND LEFT */ 73 case 'k': 74 return 0x2510; /* BOX DRAWINGS LIGHT DOWN AND LEFT */ 75 case 'l': 76 return 0x250C; /* BOX DRAWINGS LIGHT DOWN AND RIGHT */ 77 case 'm': 78 return 0x2514; /* BOX DRAWINGS LIGHT UP AND RIGHT */ 79 case 'n': 80 return 0x253C; /* BOX DRAWINGS LIGHT VERTICAL AND HORIZONTAL */ 81 case 'o': 82 return 0x23BA; /* HORIZONTAL SCAN LINE-1 */ 83 case 'p': 84 return 0x23BB; /* HORIZONTAL SCAN LINE-3 */ 85 case 'q': 86 return 0x2500; /* BOX DRAWINGS LIGHT HORIZONTAL */ 87 case 'r': 88 return 0x23BC; /* HORIZONTAL SCAN LINE-7 */ 89 case 's': 90 return 0x23BD; /* HORIZONTAL SCAN LINE-9 */ 91 case 't': 92 return 0x251C; /* BOX DRAWINGS LIGHT VERTICAL AND RIGHT */ 93 case 'u': 94 return 0x2524; /* BOX DRAWINGS LIGHT VERTICAL AND LEFT */ 95 case 'v': 96 return 0x2534; /* BOX DRAWINGS LIGHT UP AND HORIZONTAL */ 97 case 'w': 98 return 0x252C; /* BOX DRAWINGS LIGHT DOWN AND HORIZONTAL */ 99 case 'x': 100 return 0x2502; /* BOX DRAWINGS LIGHT VERTICAL */ 101 case 'y': 102 return 0x2264; /* LESS-THAN OR EQUAL TO */ 103 case 'z': 104 return 0x2265; /* GREATER-THAN OR EQUAL TO */ 105 case '{': 106 return 0x03C0; /* GREEK SMALL LETTER PI */ 107 case '|': 108 return 0x2260; /* NOT EQUAL TO */ 109 case '}': 110 return 0x00A3; /* POUND SIGN */ 111 case '~': 112 return 0x00B7; /* MIDDLE DOT */ 76 113 default: 77 114 return uc; … … 88 125 unsigned int, unsigned int const *); 89 126 90 long int import_term(struct screen_list *screen_list, struct screen *sc, void const *data, unsigned int size) 127 long int import_term(struct screen_list *screen_list, struct screen *sc, 128 void const *data, unsigned int size) 91 129 { 92 unsigned char const *buffer = (unsigned char const *)data;93 unsigned int i, j, k, skip, dummy = 0;130 unsigned char const *buffer = (unsigned char const *)data; 131 unsigned int i, j, k, skip, dummy = 0; 94 132 unsigned int width, height, top, bottom; 95 133 uint32_t savedattr; … … 102 140 top = 1; 103 141 bottom = height; 104 105 if (!sc->init)142 143 if (!sc->init) 106 144 { 107 145 sc->dfg = CACA_LIGHTGRAY; … … 118 156 } 119 157 120 for (i = 0; i < size; i += skip)158 for (i = 0; i < size; i += skip) 121 159 { 122 160 uint32_t ch = 0; … … 125 163 skip = 1; 126 164 127 if (buffer[i] == '\r')165 if (buffer[i] == '\r') 128 166 { 129 167 x = 0; 130 168 } 131 169 132 else if (buffer[i] == '\n')170 else if (buffer[i] == '\n') 133 171 { 134 172 x = 0; 135 173 y++; 136 174 } 137 else if (buffer[i] == '\a')138 { 139 if (!sc->bell)175 else if (buffer[i] == '\a') 176 { 177 if (!sc->bell) 140 178 screen_list->in_bell++; 141 179 sc->bell = 1; 142 180 } 143 181 144 else if (buffer[i] == '\t')182 else if (buffer[i] == '\t') 145 183 { 146 184 x = (x + 7) & ~7; 147 185 } 148 186 149 else if (buffer[i] == '\x08')150 { 151 if (x > 0)187 else if (buffer[i] == '\x08') 188 { 189 if (x > 0) 152 190 x--; 153 191 } 154 192 155 else if(buffer[i] == '\x0e') 156 { 157 /* Shift Out (Ctrl-N) -> Switch to 158 * Alternate Character Set: invokes 159 * the G1 character set. */ 193 else if (buffer[i] == '\x0e') 194 { 195 /* Shift Out (Ctrl-N) -> Switch to Alternate Character Set: 196 invokes the G1 character set. */ 160 197 sc->conv_state.glr[0] = 1; 161 198 } 162 199 163 else if(buffer[i] == '\x0f') 164 { 165 /* Shift In (Ctrl-O) -> Switch to 166 * Standard Character Set: invokes 167 * the G0 character set. */ 200 else if (buffer[i] == '\x0f') 201 { 202 /* Shift In (Ctrl-O) -> Switch to Standard Character Set: invokes 203 the G0 character set. */ 168 204 sc->conv_state.glr[0] = 0; 169 205 } 170 206 171 207 /* If there are not enough characters to parse the escape sequence, 172 *wait until the next try. We require 3. */173 else if (buffer[i] == '\033' && i + 2 >= size)208 wait until the next try. We require 3. */ 209 else if (buffer[i] == '\033' && i + 2 >= size) 174 210 break; 175 211 176 /* Single Shift Select of G2 Character Set (SS2: 0x8e): 177 * affects nextcharacter only */178 else if (buffer[i] == '\033' && buffer[i + 1] == 'N')212 /* Single Shift Select of G2 Character Set (SS2: 0x8e): affects next 213 character only */ 214 else if (buffer[i] == '\033' && buffer[i + 1] == 'N') 179 215 { 180 216 sc->conv_state.ss = 2; … … 182 218 } 183 219 /* Reverse Index (RI) go up one line, reverse scroll if necessary */ 184 else if(buffer[i] == '\033' && buffer[i + 1] == 'M') 185 { 186 /* FIXME : not sure about the meaning of 'go up one line' and 187 * 'if necessary' words. Implemented as a scroller only. */ 188 for(j = height; j > 0; j--) 189 { 190 for(k = 0; k < width; k++) 191 { 192 caca_put_char(sc->cv, k, j, caca_get_char(sc->cv, k, j-1)); 193 caca_put_attr(sc->cv, k, j, caca_get_attr(sc->cv, k, j-1)); 194 } 220 else if (buffer[i] == '\033' && buffer[i + 1] == 'M') 221 { 222 /* FIXME : not sure about the meaning of 'go up one line' and 'if 223 necessary' words. Implemented as a scroller only. */ 224 for (j = height; j > 0; j--) 225 { 226 for (k = 0; k < width; k++) 227 { 228 caca_put_char(sc->cv, k, j, 229 caca_get_char(sc->cv, k, j - 1)); 230 caca_put_attr(sc->cv, k, j, 231 caca_get_attr(sc->cv, k, j - 1)); 232 } 195 233 } 196 234 caca_draw_line(sc->cv, 0, 0, width, 0, ' '); 197 235 skip += 1; 198 236 } 199 200 /* Single Shift Select of G3 Character Set (SS2: 0x8f): 201 * affects nextcharacter only */202 else if (buffer[i] == '\033' && buffer[i + 1] == 'O')237 238 /* Single Shift Select of G3 Character Set (SS2: 0x8f): affects next 239 character only */ 240 else if (buffer[i] == '\033' && buffer[i + 1] == 'O') 203 241 { 204 242 sc->conv_state.ss = 3; … … 207 245 208 246 /* LOCKING-SHIFT TWO (LS2), ISO 2022, ECMA-48 (1986), ISO 6429 : 1988 */ 209 else if (buffer[i] == '\033' && buffer[i + 1] == 'n')247 else if (buffer[i] == '\033' && buffer[i + 1] == 'n') 210 248 { 211 249 sc->conv_state.glr[0] = 2; … … 213 251 } 214 252 215 /* LOCKING-SHIFT THREE (LS3) ISO 2022, ECMA-48 (1986), ISO 6429 : 1988 */ 216 else if(buffer[i] == '\033' && buffer[i + 1] == 'o') 253 /* LOCKING-SHIFT THREE (LS3) ISO 2022, ECMA-48 (1986), ISO 6429 : 1988 254 */ 255 else if (buffer[i] == '\033' && buffer[i + 1] == 'o') 217 256 { 218 257 sc->conv_state.glr[0] = 3; … … 221 260 222 261 /* RESET TO INITIAL STATE (RIS), ECMA-48 (1986), ISO 6429 : 1988 */ 223 else if (buffer[i] == '\033' && buffer[i + 1] == 'c')262 else if (buffer[i] == '\033' && buffer[i + 1] == 'c') 224 263 { 225 264 sc->dfg = CACA_DEFAULT; … … 234 273 } 235 274 236 /* Coding Method Delimiter (CMD), ECMA-48 (1991), ISO/IEC 6429:1992 (ISO IR 189) */ 237 else if(buffer[i] == '\033' && buffer[i + 1] == 'd') 275 /* Coding Method Delimiter (CMD), ECMA-48 (1991), ISO/IEC 6429:1992 276 (ISO IR 189) */ 277 else if (buffer[i] == '\033' && buffer[i + 1] == 'd') 238 278 { 239 279 reset_conv_state(sc); … … 241 281 } 242 282 243 /* GZDM4, G0-Designators, multi, 94^n chars [grandfathered short form from ISO 2022:1986] */ 244 else if(buffer[i] == '\033' && buffer[i + 1] == '$' && (buffer[i + 2] >= '@') && (buffer[i + 2] <= 'C')) 283 /* GZDM4, G0-Designators, multi, 94^n chars [grandfathered short form 284 from ISO 2022:1986] */ 285 else if (buffer[i] == '\033' && buffer[i + 1] == '$' 286 && (buffer[i + 2] >= '@') && (buffer[i + 2] <= 'C')) 245 287 { 246 288 sc->conv_state.gn[0] = LITERAL2CHAR('$', buffer[i + 2]); … … 248 290 } 249 291 250 /* GnDMx Gn-Designators, 9x^n chars; need one more char to distinguish these */ 251 else if(buffer[i] == '\033' && buffer[i + 1] == '$' && (i + 3 >= size)) 292 /* GnDMx Gn-Designators, 9x^n chars; need one more char to distinguish 293 these */ 294 else if (buffer[i] == '\033' && buffer[i + 1] == '$' 295 && (i + 3 >= size)) 252 296 break; 253 297 254 298 /* GZD4 G0-Designator, 94 chars */ 255 else if (buffer[i] == '\033' && buffer[i + 1] == '(')299 else if (buffer[i] == '\033' && buffer[i + 1] == '(') 256 300 { 257 301 sc->conv_state.gn[0] = buffer[i + 2]; … … 260 304 261 305 /* G1D4 G1-Designator, 94 chars */ 262 else if (buffer[i] == '\033' && buffer[i + 1] == ')')306 else if (buffer[i] == '\033' && buffer[i + 1] == ')') 263 307 { 264 308 sc->conv_state.gn[1] = buffer[i + 2]; … … 267 311 268 312 /* G2D4 G2-Designator, 94 chars */ 269 else if (buffer[i] == '\033' && buffer[i + 1] == '*')313 else if (buffer[i] == '\033' && buffer[i + 1] == '*') 270 314 { 271 315 sc->conv_state.gn[2] = buffer[i + 2]; … … 274 318 275 319 /* G3D4 G3-Designator, 94 chars */ 276 else if (buffer[i] == '\033' && buffer[i + 1] == '+')320 else if (buffer[i] == '\033' && buffer[i + 1] == '+') 277 321 { 278 322 sc->conv_state.gn[3] = buffer[i + 2]; … … 281 325 282 326 /* G2D6 G2-Designator, 96 chars */ 283 else if (buffer[i] == '\033' && buffer[i + 1] == '.')327 else if (buffer[i] == '\033' && buffer[i + 1] == '.') 284 328 { 285 329 sc->conv_state.gn[2] = LITERAL2CHAR('.', buffer[i + 2]); … … 288 332 289 333 /* G3D6 G3-Designator, 96 chars */ 290 else if (buffer[i] == '\033' && buffer[i + 1] == '/')334 else if (buffer[i] == '\033' && buffer[i + 1] == '/') 291 335 { 292 336 sc->conv_state.gn[3] = LITERAL2CHAR('.', buffer[i + 2]); … … 295 339 296 340 /* GZDM4 G0-Designator, 94^n chars */ 297 else if(buffer[i] == '\033' && buffer[i + 1] == '$' && buffer[i + 2] == '(') 341 else if (buffer[i] == '\033' && buffer[i + 1] == '$' 342 && buffer[i + 2] == '(') 298 343 { 299 344 sc->conv_state.gn[0] = LITERAL2CHAR('$', buffer[i + 3]); … … 302 347 303 348 /* G1DM4 G1-Designator, 94^n chars */ 304 else if(buffer[i] == '\033' && buffer[i + 1] == '$' && buffer[i + 2] == ')') 349 else if (buffer[i] == '\033' && buffer[i + 1] == '$' 350 && buffer[i + 2] == ')') 305 351 { 306 352 sc->conv_state.gn[1] = LITERAL2CHAR('$', buffer[i + 3]); … … 309 355 310 356 /* G2DM4 G2-Designator, 94^n chars */ 311 else if(buffer[i] == '\033' && buffer[i + 1] == '$' && buffer[i + 2] == '*') 357 else if (buffer[i] == '\033' && buffer[i + 1] == '$' 358 && buffer[i + 2] == '*') 312 359 { 313 360 sc->conv_state.gn[2] = LITERAL2CHAR('$', buffer[i + 3]); … … 316 363 317 364 /* G3DM4 G3-Designator, 94^n chars */ 318 else if(buffer[i] == '\033' && buffer[i + 1] == '$' && buffer[i + 2] == '+') 365 else if (buffer[i] == '\033' && buffer[i + 1] == '$' 366 && buffer[i + 2] == '+') 319 367 { 320 368 sc->conv_state.gn[3] = LITERAL2CHAR('$', buffer[i + 3]); … … 323 371 324 372 /* G2DM6 G2-Designator, 96^n chars */ 325 else if(buffer[i] == '\033' && buffer[i + 1] == '$' && buffer[i + 2] == '.') 373 else if (buffer[i] == '\033' && buffer[i + 1] == '$' 374 && buffer[i + 2] == '.') 326 375 { 327 376 sc->conv_state.gn[2] = LITERAL3CHAR('$', '.', buffer[i + 3]); … … 330 379 331 380 /* G3DM6 G3-Designator, 96^n chars */ 332 else if(buffer[i] == '\033' && buffer[i + 1] == '$' && buffer[i + 2] == '/') 381 else if (buffer[i] == '\033' && buffer[i + 1] == '$' 382 && buffer[i + 2] == '/') 333 383 { 334 384 sc->conv_state.gn[3] = LITERAL3CHAR('$', '.', buffer[i + 3]); … … 337 387 338 388 /* Interpret escape commands, as per Standard ECMA-48 "Control 339 *Functions for Coded Character Sets", 5.4. Control sequences. */340 else if (buffer[i] == '\033' && buffer[i + 1] == '[')389 Functions for Coded Character Sets", 5.4. Control sequences. */ 390 else if (buffer[i] == '\033' && buffer[i + 1] == '[') 341 391 { 342 392 unsigned int argc = 0, argv[101]; … … 344 394 345 395 346 /* Compute offsets to parameter bytes, intermediate bytes and 347 * to the final byte. Only the final byte is mandatory, there 348 * can be zero of the others. 349 * 0 param=2 inter final final+1 350 * +-----+------------------+---------------------+-----------------+ 351 * | CSI | parameter bytes | intermediate bytes | final byte | 352 * | | 0x30 - 0x3f | 0x20 - 0x2f | 0x40 - 0x7e | 353 * | ^[[ | 0123456789:;<=>? | SPC !"#$%&'()*+,-./ | azAZ@[\]^_`{|}~ | 354 * +-----+------------------+---------------------+-----------------+ 355 */ 396 /* Compute offsets to parameter bytes, intermediate bytes and to 397 the final byte. Only the final byte is mandatory, there can be 398 zero of the others. 0 param=2 inter final final+1 399 +-----+------------------+---------------------+-----------------+ 400 | CSI | parameter bytes | intermediate bytes | final byte | | | 401 0x30 - 0x3f | 0x20 - 0x2f | 0x40 - 0x7e | | ^[[ | 0123456789:;<=>? 402 | SPC !"#$%&'()*+,-./ | azAZ@[\]^_`{|}~ | 403 +-----+------------------+---------------------+-----------------+ */ 356 404 param = 2; 357 405 358 for (inter = param; i + inter < size; inter++)359 if (buffer[i + inter] < 0x30 || buffer[i + inter] > 0x3f)406 for (inter = param; i + inter < size; inter++) 407 if (buffer[i + inter] < 0x30 || buffer[i + inter] > 0x3f) 360 408 break; 361 409 362 for (final = inter; i + final < size; final++)363 if (buffer[i + final] < 0x20 || buffer[i + final] > 0x2f)410 for (final = inter; i + final < size; final++) 411 if (buffer[i + final] < 0x20 || buffer[i + final] > 0x2f) 364 412 break; 365 413 366 if (i + final >= size414 if (i + final >= size 367 415 || buffer[i + final] < 0x40 || buffer[i + final] > 0x7e) 368 break; /* Invalid Final Byte */416 break; /* Invalid Final Byte */ 369 417 370 418 skip += final; 371 419 372 420 /* Sanity checks */ 373 if (param < inter && buffer[i + param] >= 0x3c)421 if (param < inter && buffer[i + param] >= 0x3c) 374 422 { 375 423 /* Private sequence, only parse what we know */ 376 424 debug("ansi import: private sequence \"^[[%.*s\"", 377 425 final - param + 1, buffer + i + param); 378 continue; /* Private sequence, skip it entirely */379 } 380 381 if (final - param > 100)382 continue; /* Suspiciously long sequence, skip it */426 continue; /* Private sequence, skip it entirely */ 427 } 428 429 if (final - param > 100) 430 continue; /* Suspiciously long sequence, skip it */ 383 431 384 432 /* Parse parameter bytes as per ECMA-48 5.4.2: Parameter string 385 *format */386 if (param < inter)433 format */ 434 if (param < inter) 387 435 { 388 436 argv[0] = 0; 389 for (j = param; j < inter; j++)390 { 391 if (buffer[i + j] == ';')437 for (j = param; j < inter; j++) 438 { 439 if (buffer[i + j] == ';') 392 440 argv[++argc] = 0; 393 else if (buffer[i + j] >= '0' && buffer[i + j] <= '9')441 else if (buffer[i + j] >= '0' && buffer[i + j] <= '9') 394 442 argv[argc] = 10 * argv[argc] + (buffer[i + j] - '0'); 395 443 } … … 398 446 399 447 /* Interpret final byte. The code representations are given in 400 *ECMA-48 5.4: Control sequences, and the code definitions are401 *given in ECMA-48 8.3: Definition of control functions. */448 ECMA-48 5.4: Control sequences, and the code definitions are 449 given in ECMA-48 8.3: Definition of control functions. */ 402 450 debug("ansi import: command '%c'", buffer[i + final]); 403 switch (buffer[i + final])404 { 405 case 'A': /* CUU (0x41) - Cursor Up */451 switch (buffer[i + final]) 452 { 453 case 'A': /* CUU (0x41) - Cursor Up */ 406 454 y -= argc ? argv[0] : 1; 407 if (y < 0)455 if (y < 0) 408 456 y = 0; 409 457 break; 410 case 'B': /* CUD (0x42) - Cursor Down */458 case 'B': /* CUD (0x42) - Cursor Down */ 411 459 y += argc ? argv[0] : 1; 412 460 break; 413 case 'C': /* CUF (0x43) - Cursor Right */461 case 'C': /* CUF (0x43) - Cursor Right */ 414 462 x += argc ? argv[0] : 1; 415 463 break; 416 case 'D': /* CUB (0x44) - Cursor Left */464 case 'D': /* CUB (0x44) - Cursor Left */ 417 465 x -= argc ? argv[0] : 1; 418 if (x < 0)466 if (x < 0) 419 467 x = 0; 420 468 break; 421 case 'G': /* CHA (0x47) - Cursor Character Absolute */469 case 'G': /* CHA (0x47) - Cursor Character Absolute */ 422 470 x = (argc && argv[0] > 0) ? argv[0] - 1 : 0; 423 471 break; 424 case 'H': /* CUP (0x48) - Cursor Position */472 case 'H': /* CUP (0x48) - Cursor Position */ 425 473 x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0; 426 474 y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0; 427 475 break; 428 case 'J': /* ED (0x4a) - Erase In Page */476 case 'J': /* ED (0x4a) - Erase In Page */ 429 477 savedattr = caca_get_attr(sc->cv, -1, -1); 430 478 caca_set_attr(sc->cv, sc->clearattr); 431 if (!argc || argv[0] == 0)479 if (!argc || argv[0] == 0) 432 480 { 433 481 caca_draw_line(sc->cv, x, y, width, y, ' '); 434 caca_fill_box(sc->cv, 0, y + 1, width - 1, height - 1, ' '); 435 } 436 else if(argv[0] == 1) 482 caca_fill_box(sc->cv, 0, y + 1, width - 1, height - 1, 483 ' '); 484 } 485 else if (argv[0] == 1) 437 486 { 438 487 caca_fill_box(sc->cv, 0, 0, width - 1, y - 1, ' '); 439 488 caca_draw_line(sc->cv, 0, y, x, y, ' '); 440 489 } 441 else if (argv[0] == 2)442 { 443 // x = y = 0;490 else if (argv[0] == 2) 491 { 492 // x = y = 0; 444 493 caca_fill_box(sc->cv, 0, 0, width, height, ' '); 445 494 } 446 495 caca_set_attr(sc->cv, savedattr); 447 496 break; 448 case 'K': /* EL (0x4b) - Erase In Line */449 if (!argc || argv[0] == 0)497 case 'K': /* EL (0x4b) - Erase In Line */ 498 if (!argc || argv[0] == 0) 450 499 { 451 500 caca_draw_line(sc->cv, x, y, width, y, ' '); 452 501 } 453 else if (argv[0] == 1)502 else if (argv[0] == 1) 454 503 { 455 504 caca_draw_line(sc->cv, 0, y, x, y, ' '); 456 505 } 457 else if (argv[0] == 2)458 { 459 if ((unsigned int)x < width)506 else if (argv[0] == 2) 507 { 508 if ((unsigned int)x < width) 460 509 caca_draw_line(sc->cv, x, y, width - 1, y, ' '); 461 510 } 462 // x = width;463 break; 464 case 'L': /* IL - Insert line */511 // x = width; 512 break; 513 case 'L': /* IL - Insert line */ 465 514 { 466 515 unsigned int nb_lines = argc ? argv[0] : 1; 467 516 debug("IL %d %d", argc, nb_lines); 468 for (j = bottom - 1; j >= (unsigned int)y + nb_lines; j--)517 for (j = bottom - 1; j >= (unsigned int)y + nb_lines; j--) 469 518 { 470 for (k = 0; k < width; k++)519 for (k = 0; k < width; k++) 471 520 { 472 caca_put_char(sc->cv, k, j, caca_get_char(sc->cv, k, j - nb_lines)); 473 caca_put_attr(sc->cv, k, j, caca_get_attr(sc->cv, k, j - nb_lines)); 474 } 475 caca_draw_line(sc->cv, 0, j - nb_lines, width, j - nb_lines, ' '); 521 caca_put_char(sc->cv, k, j, 522 caca_get_char(sc->cv, k, 523 j - nb_lines)); 524 caca_put_attr(sc->cv, k, j, 525 caca_get_attr(sc->cv, k, 526 j - nb_lines)); 527 } 528 caca_draw_line(sc->cv, 0, j - nb_lines, width, 529 j - nb_lines, ' '); 476 530 } 477 531 } 478 532 break; 479 case 'P': /* DCH (0x50) - Delete Character */480 if (!argc || argv[0] == 0)481 argv[0] = 1; /* echo -ne 'foobar\r\e[0P\n' */533 case 'P': /* DCH (0x50) - Delete Character */ 534 if (!argc || argv[0] == 0) 535 argv[0] = 1; /* echo -ne 'foobar\r\e[0P\n' */ 482 536 /* Jylam : Start from x, not 0 */ 483 for (j = x; (unsigned int)(j + argv[0]) < width; j++)537 for (j = x; (unsigned int)(j + argv[0]) < width; j++) 484 538 { 485 539 caca_put_char(sc->cv, j, y, 486 540 caca_get_char(sc->cv, j + argv[0], y)); 487 541 caca_put_attr(sc->cv, j, y, 488 489 } 490 break; /* Jylam: this one was missing I guess */542 caca_get_attr(sc->cv, j + argv[0], y)); 543 } 544 break; /* Jylam: this one was missing I guess */ 491 545 #if 0 492 546 savedattr = caca_get_attr(sc->cv, -1, -1); 493 547 caca_set_attr(sc->cv, sc->clearattr); 494 for (; (unsigned int)j < width; j++)548 for (; (unsigned int)j < width; j++) 495 549 caca_put_char(sc->cv, j, y, ' '); 496 550 caca_set_attr(sc->cv, savedattr); 497 551 #endif 498 case 'X': /* ECH (0x58) - Erase Character */499 if (argc && argv[0])552 case 'X': /* ECH (0x58) - Erase Character */ 553 if (argc && argv[0]) 500 554 { 501 555 savedattr = caca_get_attr(sc->cv, -1, -1); … … 504 558 caca_set_attr(sc->cv, savedattr); 505 559 } 506 case 'd': /* VPA (0x64) - Line Position Absolute */560 case 'd': /* VPA (0x64) - Line Position Absolute */ 507 561 y = (argc && argv[0] > 0) ? argv[0] - 1 : 0; 508 562 break; 509 case 'f': /* HVP (0x66) - Character And Line Position */563 case 'f': /* HVP (0x66) - Character And Line Position */ 510 564 x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0; 511 565 y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0; 512 566 break; 513 case 'r': /* FIXME */514 if (argc == 2)/* DCSTBM - Set top and bottom margin */567 case 'r': /* FIXME */ 568 if (argc == 2) /* DCSTBM - Set top and bottom margin */ 515 569 { 516 570 debug("DCSTBM %d %d", argv[0], argv[1]); … … 521 575 debug("ansi import: command r with %d params", argc); 522 576 break; 523 case 'h': /* SM (0x68) - FIXME */577 case 'h': /* SM (0x68) - FIXME */ 524 578 debug("ansi import: set mode %i", argc ? (int)argv[0] : -1); 525 579 break; 526 case 'l': /* RM (0x6c) - FIXME */580 case 'l': /* RM (0x6c) - FIXME */ 527 581 debug("ansi import: reset mode %i", argc ? (int)argv[0] : -1); 528 582 break; 529 case 'm': /* SGR (0x6d) - Select Graphic Rendition */530 if (argc)583 case 'm': /* SGR (0x6d) - Select Graphic Rendition */ 584 if (argc) 531 585 ansi_parse_grcm(sc, argc, argv); 532 586 else 533 587 ansi_parse_grcm(sc, 1, &dummy); 534 588 break; 535 case 's': /* Private (save cursor position) */589 case 's': /* Private (save cursor position) */ 536 590 save_x = x; 537 591 save_y = y; 538 592 break; 539 case 'u': /* Private (reload cursor position) */593 case 'u': /* Private (reload cursor position) */ 540 594 x = save_x; 541 595 y = save_y; … … 549 603 550 604 /* Parse OSC stuff. */ 551 else if (buffer[i] == '\033' && buffer[i + 1] == ']')605 else if (buffer[i] == '\033' && buffer[i + 1] == ']') 552 606 { 553 607 char *string; … … 555 609 unsigned int mode = 2, semicolon, final; 556 610 557 for (semicolon = mode; i + semicolon < size; semicolon++)558 { 559 if (buffer[i + semicolon] < '0' || buffer[i + semicolon] > '9')611 for (semicolon = mode; i + semicolon < size; semicolon++) 612 { 613 if (buffer[i + semicolon] < '0' || buffer[i + semicolon] > '9') 560 614 break; 561 615 command = 10 * command + (buffer[i + semicolon] - '0'); 562 616 } 563 617 564 if (i + semicolon >= size || buffer[i + semicolon] != ';')565 break; /* Invalid Mode */566 567 for (final = semicolon + 1; i + final < size; final++)568 if (buffer[i + final] < 0x20)618 if (i + semicolon >= size || buffer[i + semicolon] != ';') 619 break; /* Invalid Mode */ 620 621 for (final = semicolon + 1; i + final < size; final++) 622 if (buffer[i + final] < 0x20) 569 623 break; 570 624 571 if (i + final >= size || buffer[i + final] != '\a')572 break; /* Not enough data or no bell found */573 574 /* FIXME: differenciate between not enough data (try again)575 * andinvalid data (print shit) */625 if (i + final >= size || buffer[i + final] != '\a') 626 break; /* Not enough data or no bell found */ 627 /* FIXME: XTerm also reacts to <ESC><backslash> and <ST> */ 628 /* FIXME: differenciate between not enough data (try again) and 629 invalid data (print shit) */ 576 630 577 631 skip += final; 578 632 579 633 string = malloc(final - (semicolon + 1) + 1); 580 memcpy(string, buffer + i + (semicolon + 1), final - (semicolon + 1)); 634 memcpy(string, buffer + i + (semicolon + 1), 635 final - (semicolon + 1)); 581 636 string[final - (semicolon + 1)] = '\0'; 582 637 debug("ansi import: got OSC command %i string '%s'", command, 583 638 string); 584 if (command == 0 || command == 2)585 { 586 if (sc->title)639 if (command == 0 || command == 2) 640 { 641 if (sc->title) 587 642 free(sc->title); 588 643 sc->title = string; … … 597 652 size_t bytes; 598 653 599 if (i + 6 < size)654 if (i + 6 < size) 600 655 { 601 656 ch = caca_utf8_to_utf32((char const *)(buffer + i), &bytes); 602 } else 657 } 658 else 603 659 { 604 660 /* Add a trailing zero to what we're going to read */ … … 609 665 } 610 666 611 if (!bytes)667 if (!bytes) 612 668 { 613 669 /* If the Unicode is invalid, assume it was latin1. */ … … 617 673 618 674 /* very incomplete ISO-2022 implementation tailored to DEC ACS */ 619 if (sc->conv_state.cs == '@')675 if (sc->conv_state.cs == '@') 620 676 { 621 677 if (((ch > ' ') && (ch <= '~')) 622 678 && 623 (sc->conv_state.gn[sc->conv_state.ss ? sc->conv_state.gn[sc->conv_state.ss] : sc->conv_state.glr[0]] == '0')) 679 (sc->conv_state. 680 gn[sc->conv_state.ss ? sc->conv_state. 681 gn[sc->conv_state.ss] : sc->conv_state.glr[0]] == '0')) 624 682 { 625 683 ch = dec_acs(ch); 626 684 } 627 685 else if (((ch > 0x80) && (ch < 0xff)) 628 && 629 (sc->conv_state.gn[sc->conv_state.glr[1]] == '0')) 686 && (sc->conv_state.gn[sc->conv_state.glr[1]] == '0')) 630 687 { 631 688 ch = dec_acs(ch + ' ' - 0x80); 632 689 } 633 690 } 634 sc->conv_state.ss = 0; /* no single-shift (GL) */691 sc->conv_state.ss = 0; /* no single-shift (GL) */ 635 692 636 693 wch = caca_utf32_is_fullwidth(ch) ? 2 : 1; 637 694 638 695 skip += bytes - 1; 639 696 } 640 697 641 698 /* Wrap long lines or grow horizontally */ 642 while ((unsigned int)x + wch > width)699 while ((unsigned int)x + wch > width) 643 700 { 644 701 x -= width; … … 647 704 648 705 /* Scroll or grow vertically */ 649 if ((unsigned int)y >= bottom)706 if ((unsigned int)y >= bottom) 650 707 { 651 708 int lines = (y - bottom) + 1; … … 653 710 savedattr = caca_get_attr(sc->cv, -1, -1); 654 711 655 for(j = top - 1; j + lines < bottom; j++) 656 { 657 for(k = 0; k < width; k++) 658 { 659 caca_put_char(sc->cv, k, j, caca_get_char(sc->cv, k, j + lines)); 660 caca_put_attr(sc->cv, k, j, caca_get_attr(sc->cv, k, j + lines)); 712 for (j = top - 1; j + lines < bottom; j++) 713 { 714 for (k = 0; k < width; k++) 715 { 716 caca_put_char(sc->cv, k, j, 717 caca_get_char(sc->cv, k, j + lines)); 718 caca_put_attr(sc->cv, k, j, 719 caca_get_attr(sc->cv, k, j + lines)); 661 720 } 662 721 } 663 722 caca_set_attr(sc->cv, sc->clearattr); 664 caca_fill_box(sc->cv, 0, bottom - lines, 665 width, bottom - 1, ' '); 723 caca_fill_box(sc->cv, 0, bottom - lines, width, bottom - 1, ' '); 666 724 y -= lines; 667 725 caca_set_attr(sc->cv, savedattr); … … 669 727 670 728 /* Now paste our character, if any */ 671 if (wch)729 if (wch) 672 730 { 673 731 caca_put_char(sc->cv, x, y, ch); … … 678 736 caca_gotoxy(sc->cv, x, y); 679 737 680 if (i)738 if (i) 681 739 sc->changed = 1; 682 740 return i; 683 741 } 684 742 685 /* Coding Method Delimiter (CMD), ECMA-48 (1991), ISO/IEC 6429:1992 (ISO IR 189) */ 743 /* Coding Method Delimiter (CMD), ECMA-48 (1991), ISO/IEC 6429:1992 (ISO IR 744 189) */ 686 745 687 746 static void reset_conv_state(struct screen *sc) 688 747 { 689 sc->conv_state.cs = '@'; /* ISO-2022 coding system */748 sc->conv_state.cs = '@'; /* ISO-2022 coding system */ 690 749 sc->conv_state.cn[0] = '@'; /* ISO 646 C0 control charset */ 691 750 sc->conv_state.cn[1] = 'C'; /* ISO 6429-1983 C1 control charset */ 692 sc->conv_state.glr[0] = 0; /* G0 in GL */693 sc->conv_state.glr[1] = 2; /* G2 in GR */751 sc->conv_state.glr[0] = 0; /* G0 in GL */ 752 sc->conv_state.glr[1] = 2; /* G2 in GR */ 694 753 sc->conv_state.gn[0] = 'B'; /* US-ASCII G0 charset */ 695 754 sc->conv_state.gn[1] = '0'; /* DEC ACS G1 charset */ 696 sc->conv_state.gn[2] = LITERAL2CHAR('.', 'A'); /* ISO 8859-1 G2 charset */ 697 sc->conv_state.gn[3] = LITERAL2CHAR('.', 'A'); /* ISO 8859-1 G3 charset */ 698 sc->conv_state.ss = 0; /* no single-shift (GL) */ 755 sc->conv_state.gn[2] = LITERAL2CHAR('.', 'A'); /* ISO 8859-1 G2 756 charset */ 757 sc->conv_state.gn[3] = LITERAL2CHAR('.', 'A'); /* ISO 8859-1 G3 758 charset */ 759 sc->conv_state.ss = 0; /* no single-shift (GL) */ 699 760 sc->conv_state.ctrl8bit = 1; 700 761 } … … 705 766 unsigned int argc, unsigned int const *argv) 706 767 { 707 static uint8_t const ansi2caca[] = 708 { 768 static uint8_t const ansi2caca[] = { 709 769 CACA_BLACK, CACA_RED, CACA_GREEN, CACA_BROWN, 710 770 CACA_BLUE, CACA_MAGENTA, CACA_CYAN, CACA_LIGHTGRAY … … 712 772 713 773 unsigned int j; 714 uint8_t efg, ebg; /* Effective (libcaca) fg/bg */715 716 for (j = 0; j < argc; j++)774 uint8_t efg, ebg; /* Effective (libcaca) fg/bg */ 775 776 for (j = 0; j < argc; j++) 717 777 { 718 778 /* Defined in ECMA-48 8.3.117: SGR - SELECT GRAPHIC RENDITION */ 719 if (argv[j] >= 30 && argv[j] <= 37)779 if (argv[j] >= 30 && argv[j] <= 37) 720 780 sc->fg = ansi2caca[argv[j] - 30]; 721 else if (argv[j] >= 40 && argv[j] <= 47)781 else if (argv[j] >= 40 && argv[j] <= 47) 722 782 sc->bg = ansi2caca[argv[j] - 40]; 723 else if (argv[j] >= 90 && argv[j] <= 97)783 else if (argv[j] >= 90 && argv[j] <= 97) 724 784 sc->fg = ansi2caca[argv[j] - 90] + 8; 725 else if (argv[j] >= 100 && argv[j] <= 107)785 else if (argv[j] >= 100 && argv[j] <= 107) 726 786 sc->bg = ansi2caca[argv[j] - 100] + 8; 727 else switch(argv[j]) 728 { 729 case 0: /* default rendition */ 730 sc->fg = sc->dfg; 731 sc->bg = sc->dbg; 732 sc->bold = sc->blink = sc->italics = sc->negative 733 = sc->concealed = sc->underline = sc->faint = sc->strike 734 = sc->proportional = 0; 735 break; 736 case 1: /* bold or increased intensity */ 737 sc->bold = 1; 738 break; 739 case 2: /* faint, decreased intensity or second colour */ 740 sc->faint = 1; 741 break; 742 case 3: /* italicized */ 743 sc->italics = 1; 744 break; 745 case 4: /* singly underlined */ 746 sc->underline = 1; 747 break; 748 case 5: /* slowly blinking (less then 150 per minute) */ 749 case 6: /* rapidly blinking (150 per minute or more) */ 750 sc->blink = 1; 751 break; 752 case 7: /* negative image */ 753 sc->negative = 1; 754 break; 755 case 8: /* concealed characters */ 756 sc->concealed = 1; 757 break; 758 case 9: /* crossed-out (characters still legible but marked as to be 759 * deleted */ 760 sc->strike = 1; 761 break; 762 case 21: /* doubly underlined */ 763 sc->underline = 1; 764 break; 765 case 22: /* normal colour or normal intensity (neither bold nor 766 * faint) */ 767 sc->bold = sc->faint = 0; 768 break; 769 case 23: /* not italicized, not fraktur */ 770 sc->italics = 0; 771 break; 772 case 24: /* not underlined (neither singly nor doubly) */ 773 sc->underline = 0; 774 break; 775 case 25: /* steady (not blinking) */ 776 sc->blink = 0; 777 break; 778 case 26: /* (reserved for proportional spacing as specified in CCITT 779 * Recommendation T.61) */ 780 sc->proportional = 1; 781 break; 782 case 27: /* positive image */ 783 sc->negative = 0; 784 break; 785 case 28: /* revealed characters */ 786 sc->concealed = 0; 787 break; 788 case 29: /* not crossed out */ 789 sc->strike = 0; 790 break; 791 case 38: /* (reserved for future standardization, intended for setting 792 * character foreground colour as specified in ISO 8613-6 793 * [CCITT Recommendation T.416]) */ 794 break; 795 case 39: /* default display colour (implementation-defined) */ 796 sc->fg = sc->dfg; 797 break; 798 case 48: /* (reserved for future standardization, intended for setting 799 * character background colour as specified in ISO 8613-6 800 * [CCITT Recommendation T.416]) */ 801 break; 802 case 49: /* default background colour (implementation-defined) */ 803 sc->bg = sc->dbg; 804 break; 805 case 50: /* (reserved for cancelling the effect of the rendering 806 * aspect established by parameter value 26) */ 807 sc->proportional = 0; 808 break; 809 default: 810 debug("ansi import: unknown sgr %i", argv[j]); 811 break; 812 } 787 else 788 switch (argv[j]) 789 { 790 case 0: /* default rendition */ 791 sc->fg = sc->dfg; 792 sc->bg = sc->dbg; 793 sc->bold = sc->blink = sc->italics = sc->negative 794 = sc->concealed = sc->underline = sc->faint = sc->strike 795 = sc->proportional = 0; 796 break; 797 case 1: /* bold or increased intensity */ 798 sc->bold = 1; 799 break; 800 case 2: /* faint, decreased intensity or second colour 801 */ 802 sc->faint = 1; 803 break; 804 case 3: /* italicized */ 805 sc->italics = 1; 806 break; 807 case 4: /* singly underlined */ 808 sc->underline = 1; 809 break; 810 case 5: /* slowly blinking (less then 150 per minute) */ 811 case 6: /* rapidly blinking (150 per minute or more) */ 812 sc->blink = 1; 813 break; 814 case 7: /* negative image */ 815 sc->negative = 1; 816 break; 817 case 8: /* concealed characters */ 818 sc->concealed = 1; 819 break; 820 case 9: /* crossed-out (characters still legible but 821 marked as to be deleted */ 822 sc->strike = 1; 823 break; 824 case 21: /* doubly underlined */ 825 sc->underline = 1; 826 break; 827 case 22: /* normal colour or normal intensity (neither 828 bold nor faint) */ 829 sc->bold = sc->faint = 0; 830 break; 831 case 23: /* not italicized, not fraktur */ 832 sc->italics = 0; 833 break; 834 case 24: /* not underlined (neither singly nor doubly) */ 835 sc->underline = 0; 836 break; 837 case 25: /* steady (not blinking) */ 838 sc->blink = 0; 839 break; 840 case 26: /* (reserved for proportional spacing as 841 specified in CCITT Recommendation T.61) */ 842 sc->proportional = 1; 843 break; 844 case 27: /* positive image */ 845 sc->negative = 0; 846 break; 847 case 28: /* revealed characters */ 848 sc->concealed = 0; 849 break; 850 case 29: /* not crossed out */ 851 sc->strike = 0; 852 break; 853 case 38: /* (reserved for future standardization, 854 intended for setting character foreground 855 colour as specified in ISO 8613-6 [CCITT 856 Recommendation T.416]) */ 857 break; 858 case 39: /* default display colour 859 (implementation-defined) */ 860 sc->fg = sc->dfg; 861 break; 862 case 48: /* (reserved for future standardization, 863 intended for setting character background 864 colour as specified in ISO 8613-6 [CCITT 865 Recommendation T.416]) */ 866 break; 867 case 49: /* default background colour 868 (implementation-defined) */ 869 sc->bg = sc->dbg; 870 break; 871 case 50: /* (reserved for cancelling the effect of the 872 rendering aspect established by parameter 873 value 26) */ 874 sc->proportional = 0; 875 break; 876 default: 877 debug("ansi import: unknown sgr %i", argv[j]); 878 break; 879 } 813 880 } 814 881 815 if (sc->concealed)882 if (sc->concealed) 816 883 { 817 884 efg = ebg = CACA_TRANSPARENT; … … 822 889 ebg = sc->negative ? sc->fg : sc->bg; 823 890 824 if (sc->bold)825 { 826 if (efg < 8)891 if (sc->bold) 892 { 893 if (efg < 8) 827 894 efg += 8; 828 else if (efg == CACA_DEFAULT)895 else if (efg == CACA_DEFAULT) 829 896 efg = CACA_WHITE; 830 897 } … … 839 906 int fd; 840 907 pid_t pid; 841 908 842 909 pid = forkpty(&fd, NULL, NULL, NULL); 843 if (pid < 0)910 if (pid < 0) 844 911 { 845 912 fprintf(stderr, "forkpty() error\n"); 846 913 return -1; 847 914 } 848 else if (pid == 0)915 else if (pid == 0) 849 916 { 850 917 set_tty_size(0, w, h); … … 852 919 putenv("TERM=xterm"); 853 920 argv = malloc(2 * sizeof(char *)); 854 if(!argv) 855 { 856 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); 921 if (!argv) 922 { 923 fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, 924 __LINE__); 857 925 return -1; 858 926 } … … 876 944 int ret = openpty(&fdm, &fds, NULL, NULL, NULL); 877 945 878 if (ret < 0)946 if (ret < 0) 879 947 { 880 948 fprintf(stderr, "open() error\n"); … … 903 971 904 972 905 int update_terms(struct screen_list *screen_list)973 int update_terms(struct screen_list *screen_list) 906 974 { 907 975 int i, refresh = 0; 908 for (i = 0; i < screen_list->count; i++)976 for (i = 0; i < screen_list->count; i++) 909 977 { 910 if (screen_list->screen[i]->total && !screen_list->dont_update_coords)978 if (screen_list->screen[i]->total && !screen_list->dont_update_coords) 911 979 { 912 980 unsigned long int bytes; … … 917 985 screen_list->screen[i]->total); 918 986 919 if (bytes > 0)987 if (bytes > 0) 920 988 { 921 989 screen_list->screen[i]->total -= bytes; … … 923 991 screen_list->screen[i]->buf + bytes, 924 992 screen_list->screen[i]->total); 925 if (screen_list->screen[i]->visible || screen_list->mini)993 if (screen_list->screen[i]->visible || screen_list->mini) 926 994 refresh = 1; 927 995 } … … 930 998 return refresh; 931 999 } 932 -
neercs/trunk/src/wm.c
r3944 r3969 1 /* 1 /* 2 2 * neercs console-based window manager 3 3 * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> … … 39 39 s->h = h; 40 40 41 /* 41 /* 42 42 * caca_set_canvas_boundaries() is bugged as hell, so let's resize it by 43 43 * hands … … 419 419 float cube_projected[12][2]; 420 420 float fov = 0.5f; 421 float angle = 90.0f * ((float)cur_time / (float)screen_list->cube.duration); 421 float angle = 422 90.0f * ((float)cur_time / (float)screen_list->cube.duration); 422 423 423 424 angle *= (M_PI / 180.0f);
Note: See TracChangeset
for help on using the changeset viewer.