Changeset 3954
- Timestamp:
- 11/19/09 11:46:15 (4 years ago)
- Location:
- neercs/trunk/src
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
neercs/trunk/src/client.c
r3942 r3954 1 /* 1 /* 2 2 * neercs console-based window manager 3 3 * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> … … 40 40 void mainloop(struct screen_list *screen_list) 41 41 { 42 char * buf = NULL;42 char *buf = NULL; 43 43 44 long long unsigned int last_key_time = 0; 45 last_key_time = get_us(); 44 screen_list->last_key_time = get_us(); 46 45 47 for (;;)46 for (;;) 48 47 { 49 48 caca_event_t ev; 50 49 int ret = 0; 51 50 ssize_t n; 52 if(!screen_list) goto end; 53 if(!buf) 51 if (!screen_list) 52 goto end; 53 if (!buf) 54 54 buf = malloc(NEERCS_RECV_BUFSIZE); 55 if (!buf)55 if (!buf) 56 56 { 57 57 debug("Failed to allocate memory"); 58 58 goto end; 59 59 } 60 if(screen_list->socket[SOCK_CLIENT] && (n = read(screen_list->socket[SOCK_CLIENT], buf, NEERCS_RECV_BUFSIZE-1)) > 0) 60 if (screen_list->socket[SOCK_CLIENT] 61 && (n = 62 read(screen_list->socket[SOCK_CLIENT], buf, 63 NEERCS_RECV_BUFSIZE - 1)) > 0) 61 64 { 62 65 buf[n] = 0; 63 66 debug("Received from server: %s", buf); 64 if (!strncmp("DETACH", buf, 6))67 if (!strncmp("DETACH", buf, 6)) 65 68 { 66 69 ret = 1; 67 70 break; 68 71 } 69 else if (!strncmp("UPDATE ", buf, 7))72 else if (!strncmp("UPDATE ", buf, 7)) 70 73 { 71 74 int x, y; 72 75 ssize_t l2 = 0, lb = 0; 73 char * buf2;76 char *buf2; 74 77 size_t l = 0; 75 /* FIXME check the length before calling atoi*/ 76 x = atoi(buf+8); 77 y = atoi(buf+19); 78 /* 0 means we have valid data but incomplete, so read the rest */ 79 while(l == 0) 78 /* FIXME check the length before calling atoi */ 79 x = atoi(buf + 8); 80 y = atoi(buf + 19); 81 /* 0 means we have valid data but incomplete, so read the rest 82 */ 83 while (l == 0) 80 84 { 81 85 buf2 = realloc(buf, l2 + NEERCS_RECV_BUFSIZE); 82 if (!buf2)86 if (!buf2) 83 87 { 84 88 debug("Failed to allocate memory"); … … 87 91 buf = buf2; 88 92 fcntl(screen_list->socket[SOCK_CLIENT], F_SETFL, 0); 89 lb = read(screen_list->socket[SOCK_CLIENT], buf+l2, NEERCS_RECV_BUFSIZE-1); 90 if(lb < 0) 93 lb = read(screen_list->socket[SOCK_CLIENT], buf + l2, 94 NEERCS_RECV_BUFSIZE - 1); 95 if (lb < 0) 91 96 { 92 debug("Failed to read the end of the refresh message (%s)", strerror(errno)); 97 debug 98 ("Failed to read the end of the refresh message (%s)", 99 strerror(errno)); 93 100 l = -1; 94 101 } … … 97 104 l2 += lb; 98 105 #if defined HAVE_CACA_DIRTY_RECTANGLES 99 l = caca_import_area_from_memory(screen_list->cv, x, y, buf, l2, "caca"); 106 l = caca_import_area_from_memory(screen_list->cv, x, y, 107 buf, l2, "caca"); 100 108 #else 101 l = caca_import_memory(screen_list->cv, buf, l2, "caca"); 109 l = caca_import_memory(screen_list->cv, buf, l2, 110 "caca"); 102 111 #endif 103 112 } … … 105 114 fcntl(screen_list->socket[SOCK_CLIENT], F_SETFL, O_NONBLOCK); 106 115 } 107 else if (!strncmp("REFRESH ", buf, 8))116 else if (!strncmp("REFRESH ", buf, 8)) 108 117 { 109 118 int dt, x, y; 110 /* FIXME check the length before calling atoi */111 x = atoi(buf +8);112 y = atoi(buf +19);119 /* FIXME check the length before calling atoi */ 120 x = atoi(buf + 8); 121 y = atoi(buf + 19); 113 122 caca_gotoxy(screen_list->cv, x, y); 114 123 caca_refresh_display(screen_list->dp); 115 124 dt = caca_get_display_time(screen_list->dp); 116 debug("refreshtime=%dms (limit %d, requested %d)", dt/1000, screen_list->delay, screen_list->requested_delay); 117 /* Adjust refresh delay so that the server do not compute useless things */ 118 if(dt > 2*1000*screen_list->delay && screen_list->delay <= 100) 125 debug("refreshtime=%dms (limit %d, requested %d)", dt / 1000, 126 screen_list->delay, screen_list->requested_delay); 127 /* Adjust refresh delay so that the server do not compute 128 useless things */ 129 if (dt > 2 * 1000 * screen_list->delay 130 && screen_list->delay <= 100) 119 131 { 120 screen_list->delay *=2;132 screen_list->delay *= 2; 121 133 send_delay(screen_list); 122 134 } 123 else if(dt < screen_list->delay*1000*1.2 && 124 screen_list->delay >= 3*screen_list->requested_delay/2) 135 else if (dt < screen_list->delay * 1000 * 1.2 && 136 screen_list->delay >= 137 3 * screen_list->requested_delay / 2) 125 138 { 126 screen_list->delay =2*screen_list->delay/3;139 screen_list->delay = 2 * screen_list->delay / 3; 127 140 send_delay(screen_list); 128 141 } 129 142 } 130 else if (!strncmp("CURSOR ", buf, 7))143 else if (!strncmp("CURSOR ", buf, 7)) 131 144 { 132 caca_set_cursor(screen_list->dp, atoi(buf +7));145 caca_set_cursor(screen_list->dp, atoi(buf + 7)); 133 146 } 134 else if (!strncmp("TITLE ", buf, 6))147 else if (!strncmp("TITLE ", buf, 6)) 135 148 { 136 caca_set_display_title(screen_list->dp, buf +6);149 caca_set_display_title(screen_list->dp, buf + 6); 137 150 caca_refresh_display(screen_list->dp); 138 151 } … … 143 156 } 144 157 145 if (ret)158 if (ret) 146 159 break; 147 160 148 161 ret = caca_get_event(screen_list->dp, 149 162 CACA_EVENT_KEY_PRESS 150 |CACA_EVENT_RESIZE 151 |CACA_EVENT_QUIT, 152 &ev, 10000); 153 if(ret) 163 | CACA_EVENT_RESIZE 164 | CACA_EVENT_QUIT, &ev, 10000); 165 if (ret) 154 166 ret = send_event(ev, screen_list); 155 167 156 if (ret)168 if (ret) 157 169 break; 158 170 } 159 171 160 end:161 if (buf)172 end: 173 if (buf) 162 174 free(buf); 163 175 -
neercs/trunk/src/grab.c
r3942 r3954 1 /* 1 /* 2 2 * neercs console-based window manager 3 3 * Copyright (c) 2008 Pascal Terjan … … 15 15 #include "config.h" 16 16 17 #define _XOPEN_SOURCE 500 /* getsid() */17 #define _XOPEN_SOURCE 500 /* getsid() */ 18 18 19 19 #include <dirent.h> … … 57 57 58 58 parent = mytrace_attach(pid); 59 if (!parent)59 if (!parent) 60 60 { 61 61 fprintf(stderr, "Cannot access process %ld\n", pid); … … 69 69 70 70 /* Look for file descriptors that are PTYs */ 71 while ((fddirent = readdir(fddir)) && i < (int)sizeof(to_open)-1)71 while ((fddirent = readdir(fddir)) && i < (int)sizeof(to_open) - 1) 72 72 { 73 73 fd = atoi(fddirent->d_name); … … 75 75 to_open[i] = 0; 76 76 lstat(fdstr, &stat_buf); 77 if ((stat_buf.st_mode & S_IRUSR) && (stat_buf.st_mode & S_IWUSR))77 if ((stat_buf.st_mode & S_IRUSR) && (stat_buf.st_mode & S_IWUSR)) 78 78 mode[i] = O_RDWR; 79 else if (stat_buf.st_mode & S_IWUSR)79 else if (stat_buf.st_mode & S_IWUSR) 80 80 mode[i] = O_WRONLY; 81 81 else 82 82 mode[i] = O_RDONLY; 83 83 84 snprintf(fdstr, sizeof(fdstr), "/proc/%ld/fd/%s", pid, fddirent->d_name); 85 86 if(stat(fdstr, &stat_buf) < 0) 87 continue; 88 89 if(!S_ISCHR(stat_buf.st_mode) 84 snprintf(fdstr, sizeof(fdstr), "/proc/%ld/fd/%s", pid, 85 fddirent->d_name); 86 87 if (stat(fdstr, &stat_buf) < 0) 88 continue; 89 90 if (!S_ISCHR(stat_buf.st_mode) 90 91 || MAJOR(stat_buf.st_rdev) != UNIX98_PTY_SLAVE_MAJOR) 91 92 continue; … … 93 94 debug("found pty %d for pid %d", fd, pid); 94 95 95 if (!validtos)96 if (!validtos) 96 97 { 97 98 ret = mytrace_tcgets(child, fd, &tos); 98 if (ret < 0)99 if (ret < 0) 99 100 { 100 101 perror("mytrace_tcgets"); … … 110 111 closedir(fddir); 111 112 112 if (i>=(int)sizeof(to_open)-1)113 if (i >= (int)sizeof(to_open) - 1) 113 114 { 114 115 fprintf(stderr, "too many open pty\n"); … … 118 119 119 120 ret = mytrace_exec(parent, "/usr/bin/reset"); 120 if (ret < 0)121 if (ret < 0) 121 122 mytrace_exit(parent, 0); 122 123 mytrace_detach(parent); 123 waitpid(pid, NULL, 0); /* Wait for reset to finish before displaying */124 waitpid(pid, NULL, 0); /* Wait for reset to finish before displaying */ 124 125 mytrace_write(child, 2, "\033[H\033[2J", 7); 125 126 mytrace_write(child, 2, "\n[Process stolen by neercs]\r\n\n", 30); … … 132 133 133 134 ret = mytrace_setpgid(child, 0, getsid(pid)); 134 if (ret < 0)135 if (ret < 0) 135 136 { 136 137 fprintf(stderr, "syscall setpgid failed\n"); … … 139 140 } 140 141 141 if (ret != 0)142 if (ret != 0) 142 143 { 143 144 fprintf(stderr, "setpgid returned %d\n", ret); … … 147 148 148 149 ret = mytrace_setsid(child); 149 if (ret < 0)150 if (ret < 0) 150 151 { 151 152 fprintf(stderr, "syscall setsid failed\n"); … … 157 158 158 159 /* Reopen PTY file descriptors */ 159 for (; i >= 0; i--)160 { 161 if (!to_open[i])160 for (; i >= 0; i--) 161 { 162 if (!to_open[i]) 162 163 continue; 163 164 ret = mytrace_close(child, fds[i]); 164 if (ret < 0)165 if (ret < 0) 165 166 { 166 167 perror("mytrace_close"); … … 168 169 } 169 170 fd = mytrace_open(child, ptyname, mode[i]); 170 if (fd < 0)171 if (fd < 0) 171 172 { 172 173 perror("mytrace_open"); … … 177 178 mytrace_sctty(child, fd); 178 179 179 if (validtos)180 if (validtos) 180 181 { 181 182 ret = mytrace_tcsets(child, fd, &tos); 182 if (ret < 0)183 if (ret < 0) 183 184 { 184 185 perror("mytrace_tcsets"); … … 187 188 } 188 189 ret = mytrace_dup2(child, fd, fds[i]); 189 if (ret < 0)190 if (ret < 0) 190 191 { 191 192 perror("mytrace_dup2"); … … 204 205 } 205 206 206 struct process { 207 struct process 208 { 207 209 long pid; 208 210 char *cmdline; 209 211 }; 210 212 211 static int list_process(struct process ** process_list)213 static int list_process(struct process **process_list) 212 214 { 213 215 glob_t pglob; 214 unsigned int i, n =0;216 unsigned int i, n = 0; 215 217 glob("/proc/[0-9]*", GLOB_NOSORT, NULL, &pglob); 216 *process_list = malloc(pglob.gl_pathc *sizeof(struct process));217 for (i=0; i<pglob.gl_pathc; i++)218 *process_list = malloc(pglob.gl_pathc * sizeof(struct process)); 219 for (i = 0; i < pglob.gl_pathc; i++) 218 220 { 219 221 glob_t pglob2; 220 222 unsigned int j; 221 char * fds;223 char *fds; 222 224 (*process_list)[n].pid = atoi(basename(pglob.gl_pathv[i])); 223 225 /* Don't allow grabbing ourselves */ 224 if((*process_list)[n].pid == getpid()) 225 continue; 226 asprintf(&fds, "%s/fd/*", pglob.gl_pathv[i]); 226 if ((*process_list)[n].pid == getpid()) 227 continue; 228 /* FIXME check value of r */ 229 int r = asprintf(&fds, "%s/fd/*", pglob.gl_pathv[i]); 227 230 glob(fds, GLOB_NOSORT, NULL, &pglob2); 228 231 free(fds); 229 for (j=0; j<pglob2.gl_pathc; j++)232 for (j = 0; j < pglob2.gl_pathc; j++) 230 233 { 231 234 char path[4096]; 232 235 ssize_t l = readlink(pglob2.gl_pathv[j], path, sizeof(path)); 233 if (l <= 0)236 if (l <= 0) 234 237 continue; 235 238 path[l] = '\0'; 236 if (strstr(path, "/dev/pt"))237 { 238 char * cmdfile;239 if (strstr(path, "/dev/pt")) 240 { 241 char *cmdfile; 239 242 int fd; 240 asprintf(&cmdfile, "%s/cmdline", pglob.gl_pathv[i]); 243 /* FIXME check value of r */ 244 r = asprintf(&cmdfile, "%s/cmdline", pglob.gl_pathv[i]); 241 245 fd = open(cmdfile, O_RDONLY); 242 246 free(cmdfile); 243 if (fd)247 if (fd) 244 248 { 245 read(fd, path, sizeof(path)); 249 /* FIXME check value of r */ 250 r = read(fd, path, sizeof(path)); 246 251 (*process_list)[n].cmdline = strdup(path); 247 252 close(fd); … … 257 262 } 258 263 259 long select_process(struct screen_list *screen_list)264 long select_process(struct screen_list *screen_list) 260 265 { 261 266 caca_event_t ev; … … 266 271 int ret = 0; 267 272 int start = 0; 268 struct process * process_list;273 struct process *process_list; 269 274 270 275 nb_process = list_process(&process_list); … … 272 277 screen_list->cv = caca_create_canvas(0, 0); 273 278 screen_list->dp = caca_create_display(screen_list->cv); 274 if (!screen_list->dp)279 if (!screen_list->dp) 275 280 goto end; 276 281 caca_set_cursor(screen_list->dp, 0); 277 282 caca_set_display_title(screen_list->dp, PACKAGE_STRING); 278 while (1)279 { 280 if (refresh)283 while (1) 284 { 285 if (refresh) 281 286 { 282 287 caca_set_color_ansi(screen_list->cv, CACA_BLUE, CACA_BLUE); … … 284 289 0, 0, 285 290 caca_get_canvas_width(screen_list->cv), 286 caca_get_canvas_height(screen_list->cv), 287 '#'); 291 caca_get_canvas_height(screen_list->cv), '#'); 288 292 caca_set_color_ansi(screen_list->cv, CACA_DEFAULT, CACA_BLUE); 289 293 caca_draw_cp437_box(screen_list->cv, 290 294 0, 0, 291 295 caca_get_canvas_width(screen_list->cv), 292 caca_get_canvas_height(screen_list->cv) 293 ); 294 caca_printf(screen_list->cv, 2, 2, "Please select a process to grab:"); 295 for(i=0; i<nb_process && i<caca_get_canvas_height(screen_list->cv)-4; i++) 296 { 297 if(i == current_line-1) 296 caca_get_canvas_height(screen_list->cv)); 297 caca_printf(screen_list->cv, 2, 2, 298 "Please select a process to grab:"); 299 for (i = 0; 300 i < nb_process 301 && i < caca_get_canvas_height(screen_list->cv) - 4; i++) 302 { 303 if (i == current_line - 1) 298 304 { 299 305 caca_set_attr(screen_list->cv, CACA_BOLD); 300 caca_set_color_ansi(screen_list->cv, CACA_GREEN, CACA_BLUE); 301 caca_put_char(screen_list->cv, 1, i+3, '>'); 306 caca_set_color_ansi(screen_list->cv, CACA_GREEN, 307 CACA_BLUE); 308 caca_put_char(screen_list->cv, 1, i + 3, '>'); 302 309 } 303 310 else 304 311 { 305 312 caca_set_attr(screen_list->cv, 0); 306 caca_set_color_ansi(screen_list->cv, CACA_LIGHTGRAY, CACA_BLUE); 307 caca_put_char(screen_list->cv, 1, i+3, ' '); 313 caca_set_color_ansi(screen_list->cv, CACA_LIGHTGRAY, 314 CACA_BLUE); 315 caca_put_char(screen_list->cv, 1, i + 3, ' '); 308 316 } 309 317 caca_printf(screen_list->cv, 310 3, i +3,318 3, i + 3, 311 319 "%5d %s", 312 process_list[i +start].pid,313 process_list[i +start].cmdline);320 process_list[i + start].pid, 321 process_list[i + start].cmdline); 314 322 } 315 323 caca_refresh_display(screen_list->dp); … … 317 325 } 318 326 319 if(!caca_get_event(screen_list->dp, 320 CACA_EVENT_KEY_PRESS 321 |CACA_EVENT_RESIZE 322 |CACA_EVENT_QUIT, 323 &ev, 10000)) 327 if (!caca_get_event(screen_list->dp, 328 CACA_EVENT_KEY_PRESS 329 | CACA_EVENT_RESIZE | CACA_EVENT_QUIT, &ev, 10000)) 324 330 continue; 325 331 326 332 t = caca_get_event_type(&ev); 327 333 328 if (t & CACA_EVENT_KEY_PRESS)334 if (t & CACA_EVENT_KEY_PRESS) 329 335 { 330 336 unsigned int c = caca_get_event_key_ch(&ev); 331 switch (c)337 switch (c) 332 338 { 333 339 case CACA_KEY_UP: 334 if (current_line>1)340 if (current_line > 1) 335 341 current_line--; 336 if (current_line < start && start > 0)342 if (current_line < start && start > 0) 337 343 start--; 338 344 break; 339 345 case CACA_KEY_DOWN: 340 if (current_line<nb_process)346 if (current_line < nb_process) 341 347 current_line++; 342 if(current_line > start + caca_get_canvas_height(screen_list->cv)-3) 348 if (current_line > 349 start + caca_get_canvas_height(screen_list->cv) - 3) 343 350 start++; 344 351 break; 345 352 case CACA_KEY_RETURN: 346 ret = process_list[current_line -1].pid;353 ret = process_list[current_line - 1].pid; 347 354 goto end; 348 355 break; … … 355 362 refresh = 1; 356 363 } 357 else if (t & CACA_EVENT_RESIZE)364 else if (t & CACA_EVENT_RESIZE) 358 365 { 359 366 refresh = 1; 360 367 } 361 else if (t & CACA_EVENT_QUIT)368 else if (t & CACA_EVENT_QUIT) 362 369 goto end; 363 370 } 364 371 365 end:366 if (screen_list->dp)372 end: 373 if (screen_list->dp) 367 374 { 368 375 caca_free_display(screen_list->dp); 369 376 screen_list->dp = NULL; 370 377 } 371 if (screen_list->cv)378 if (screen_list->cv) 372 379 { 373 380 caca_free_canvas(screen_list->cv); 374 381 screen_list->cv = NULL; 375 382 } 376 if (nb_process > 0)377 { 378 for (i=0; i<nb_process; i++)383 if (nb_process > 0) 384 { 385 for (i = 0; i < nb_process; i++) 379 386 { 380 387 free(process_list[i].cmdline); … … 384 391 return ret; 385 392 } 386 -
neercs/trunk/src/screensaver.c
r3942 r3954 123 123 flying_toaster->toaster[i] = caca_create_canvas(0, 0); 124 124 flying_toaster->mask[i] = caca_create_canvas(0, 0); 125 caca_import_ memory(flying_toaster->toaster[i],125 caca_import_canvas_from_memory(flying_toaster->toaster[i], 126 126 toaster_text[i], 127 127 strlen(toaster_text[i]), "utf8"); 128 caca_import_ memory(flying_toaster->mask[i],128 caca_import_canvas_from_memory(flying_toaster->mask[i], 129 129 toaster_mask[i], 130 130 strlen(toaster_mask[i]), "utf8"); -
neercs/trunk/src/server.c
r3948 r3954 181 181 sprintf(buf2, "REFRESH %10d %10d", caca_get_cursor_x(screen_list->cv), 182 182 caca_get_cursor_y(screen_list->cv)); 183 write(screen_list->socket[SOCK_CLIENT], buf2, strlen(buf2) + 1); 183 /* FIXME check value of r */ 184 int r = 185 write(screen_list->socket[SOCK_CLIENT], buf2, strlen(buf2) + 1); 186 (void)r; 184 187 #if defined HAVE_CACA_DIRTY_RECTANGLES 185 188 caca_clear_dirty_rect_list(screen_list->cv); … … 502 505 /* Normal key, convert it if needed */ 503 506 str = convert_input_ansi(&c, &size); 504 write(screen_list->screen[screen_list->pty]->fd, str, size); 507 /* FIXME check value of r */ 508 int r = write(screen_list->screen[screen_list->pty]->fd, str, 509 size); 510 (void)r; 505 511 break; 506 512 }
Note: See TracChangeset
for help on using the changeset viewer.
