| 1 | /* |
|---|
| 2 | * neercs console-based window manager |
|---|
| 3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
|---|
| 4 | * 2008 Jean-Yves Lamoureux <jylam@lnxscene.org> |
|---|
| 5 | * 2008 Pascal Terjan <pterjan@linuxfr.org> |
|---|
| 6 | * All Rights Reserved |
|---|
| 7 | * |
|---|
| 8 | * $Id: client.c 3939 2009-11-18 14:40:39Z jylam $ |
|---|
| 9 | * |
|---|
| 10 | * This program is free software. It comes without any warranty, to |
|---|
| 11 | * the extent permitted by applicable law. You can redistribute it |
|---|
| 12 | * and/or modify it under the terms of the Do What The Fuck You Want |
|---|
| 13 | * To Public License, Version 2, as published by Sam Hocevar. See |
|---|
| 14 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | #include "config.h" |
|---|
| 18 | |
|---|
| 19 | #include <stdio.h> |
|---|
| 20 | #include <string.h> |
|---|
| 21 | #include <stdlib.h> |
|---|
| 22 | #include <unistd.h> |
|---|
| 23 | #include <fcntl.h> |
|---|
| 24 | #include <signal.h> |
|---|
| 25 | #include <sys/ioctl.h> |
|---|
| 26 | #include <sys/types.h> |
|---|
| 27 | #include <sys/wait.h> |
|---|
| 28 | #include <sys/time.h> |
|---|
| 29 | #include <time.h> |
|---|
| 30 | #include <pwd.h> |
|---|
| 31 | |
|---|
| 32 | #include <errno.h> |
|---|
| 33 | #include <caca.h> |
|---|
| 34 | #include <caca.h> |
|---|
| 35 | |
|---|
| 36 | #include "neercs.h" |
|---|
| 37 | |
|---|
| 38 | #define NEERCS_RECV_BUFSIZE 128*1024 |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | int start_client(struct screen_list * screen_list) |
|---|
| 42 | { |
|---|
| 43 | char *sess = NULL; |
|---|
| 44 | create_socket(screen_list, SOCK_CLIENT); |
|---|
| 45 | while ((sess = connect_socket(screen_list, SOCK_SERVER)) == NULL) |
|---|
| 46 | usleep(100); |
|---|
| 47 | free(sess); |
|---|
| 48 | |
|---|
| 49 | /* Create main canvas and associated caca window */ |
|---|
| 50 | screen_list->cv = caca_create_canvas(0, 0); |
|---|
| 51 | screen_list->dp = caca_create_display(screen_list->cv); |
|---|
| 52 | |
|---|
| 53 | if (!screen_list->dp) |
|---|
| 54 | return -3; |
|---|
| 55 | |
|---|
| 56 | caca_set_display_time(screen_list->dp, screen_list->delay * 1000); |
|---|
| 57 | caca_set_cursor(screen_list->dp, 1); |
|---|
| 58 | |
|---|
| 59 | request_attach(screen_list); |
|---|
| 60 | |
|---|
| 61 | return 0; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | int send_event(caca_event_t ev, struct screen_list *screen_list) |
|---|
| 65 | { |
|---|
| 66 | |
|---|
| 67 | enum caca_event_type t; |
|---|
| 68 | |
|---|
| 69 | t = caca_get_event_type(&ev); |
|---|
| 70 | |
|---|
| 71 | if (t & CACA_EVENT_KEY_PRESS) |
|---|
| 72 | { |
|---|
| 73 | char buf[16]; |
|---|
| 74 | int bytes; |
|---|
| 75 | bytes = |
|---|
| 76 | snprintf(buf, sizeof(buf) - 1, "KEY %d", |
|---|
| 77 | caca_get_event_key_ch(&ev)); |
|---|
| 78 | buf[bytes] = '\0'; |
|---|
| 79 | debug("Sending key press to server: %s", buf); |
|---|
| 80 | debug("Sending '%s', %d bytes\n", buf, strlen(buf)); |
|---|
| 81 | return write(screen_list->comm.socket[SOCK_SERVER], buf, strlen(buf)+1) <= 0; |
|---|
| 82 | } |
|---|
| 83 | else if (t & CACA_EVENT_RESIZE) |
|---|
| 84 | { |
|---|
| 85 | |
|---|
| 86 | char buf[32]; |
|---|
| 87 | int bytes; |
|---|
| 88 | bytes = snprintf(buf, sizeof(buf) - 1, "RESIZE %10d %10d", |
|---|
| 89 | caca_get_event_resize_width(&ev), |
|---|
| 90 | caca_get_event_resize_height(&ev)); |
|---|
| 91 | buf[bytes] = '\0'; |
|---|
| 92 | debug("Sending '%s', %d bytes\n", buf, strlen(buf)); |
|---|
| 93 | return write(screen_list->comm.socket[SOCK_SERVER], buf, strlen(buf)+1) <= 0; |
|---|
| 94 | } |
|---|
| 95 | else if (t & CACA_EVENT_MOUSE_PRESS) |
|---|
| 96 | { |
|---|
| 97 | char buf[52]; |
|---|
| 98 | int bytes; |
|---|
| 99 | bytes = snprintf(buf, sizeof(buf) - 1, "MOUSEP %10d %10d %10d", |
|---|
| 100 | caca_get_mouse_x (screen_list->dp), |
|---|
| 101 | caca_get_mouse_y (screen_list->dp), |
|---|
| 102 | caca_get_event_mouse_button(&ev)); |
|---|
| 103 | buf[bytes] = '\0'; |
|---|
| 104 | debug("Sending '%s', %d bytes\n", buf, strlen(buf)); |
|---|
| 105 | return write(screen_list->comm.socket[SOCK_SERVER], buf, strlen(buf)+1) <= 0; |
|---|
| 106 | } |
|---|
| 107 | else if (t & CACA_EVENT_QUIT) |
|---|
| 108 | return write(screen_list->comm.socket[SOCK_SERVER], "QUIT", |
|---|
| 109 | strlen("QUIT")) <= 0; |
|---|
| 110 | |
|---|
| 111 | return 0; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | int send_delay(struct screen_list *screen_list) |
|---|
| 115 | { |
|---|
| 116 | debug("Sending DELAY\n"); |
|---|
| 117 | char buf[18]; |
|---|
| 118 | int bytes; |
|---|
| 119 | bytes = snprintf(buf, sizeof(buf) - 1, "DELAY %10d", screen_list->delay); |
|---|
| 120 | buf[bytes] = '\0'; |
|---|
| 121 | return write(screen_list->comm.socket[SOCK_SERVER], buf, strlen(buf)) <= 0; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | void mainloop(struct screen_list *screen_list) |
|---|
| 126 | { |
|---|
| 127 | char *buf = NULL; |
|---|
| 128 | |
|---|
| 129 | screen_list->last_key_time = get_us(); |
|---|
| 130 | |
|---|
| 131 | for (;;) |
|---|
| 132 | { |
|---|
| 133 | caca_event_t ev; |
|---|
| 134 | int ret = 0; |
|---|
| 135 | ssize_t n; |
|---|
| 136 | if (!screen_list) |
|---|
| 137 | goto end; |
|---|
| 138 | if (!buf) |
|---|
| 139 | buf = malloc(NEERCS_RECV_BUFSIZE); |
|---|
| 140 | if (!buf) |
|---|
| 141 | { |
|---|
| 142 | debug("Failed to allocate memory"); |
|---|
| 143 | goto end; |
|---|
| 144 | } |
|---|
| 145 | if (screen_list->comm.socket[SOCK_CLIENT] |
|---|
| 146 | && (n = |
|---|
| 147 | read(screen_list->comm.socket[SOCK_CLIENT], buf, |
|---|
| 148 | NEERCS_RECV_BUFSIZE - 1)) > 0) |
|---|
| 149 | { |
|---|
| 150 | buf[n] = 0; |
|---|
| 151 | debug("Received from server: '%s' (%d bytes)", buf, n); |
|---|
| 152 | if (!strncmp("DETACH", buf, 6)) |
|---|
| 153 | { |
|---|
| 154 | /* ret = 1; Not used */ |
|---|
| 155 | break; |
|---|
| 156 | } |
|---|
| 157 | else if (!strncmp("UPDATE ", buf, 7)) |
|---|
| 158 | { |
|---|
| 159 | int x, y; |
|---|
| 160 | ssize_t l2 = 0, lb = 0; |
|---|
| 161 | char *buf2; |
|---|
| 162 | size_t l = 0; |
|---|
| 163 | /* FIXME check the length before calling atoi */ |
|---|
| 164 | x = atoi(buf + 8); |
|---|
| 165 | y = atoi(buf + 19); |
|---|
| 166 | |
|---|
| 167 | /* 0 means we have valid data but incomplete, so read the rest |
|---|
| 168 | */ |
|---|
| 169 | while (l == 0) |
|---|
| 170 | { |
|---|
| 171 | buf2 = realloc(buf, l2 + NEERCS_RECV_BUFSIZE); |
|---|
| 172 | if (!buf2) |
|---|
| 173 | { |
|---|
| 174 | debug("Failed to allocate memory"); |
|---|
| 175 | goto end; |
|---|
| 176 | } |
|---|
| 177 | buf = buf2; |
|---|
| 178 | fcntl(screen_list->comm.socket[SOCK_CLIENT], F_SETFL, 0); |
|---|
| 179 | lb = read(screen_list->comm.socket[SOCK_CLIENT], buf + l2, |
|---|
| 180 | NEERCS_RECV_BUFSIZE - 1); |
|---|
| 181 | if (lb < 0) |
|---|
| 182 | { |
|---|
| 183 | debug |
|---|
| 184 | ("Failed to read the end of the refresh message (%s)", |
|---|
| 185 | strerror(errno)); |
|---|
| 186 | l = -1; |
|---|
| 187 | } |
|---|
| 188 | else |
|---|
| 189 | { |
|---|
| 190 | l2 += lb; |
|---|
| 191 | #if defined HAVE_CACA_DIRTY_RECTANGLES |
|---|
| 192 | l = caca_import_area_from_memory(screen_list->cv, x, y, |
|---|
| 193 | buf, l2, "caca"); |
|---|
| 194 | #else |
|---|
| 195 | l = caca_import_from_memory(screen_list->cv, buf, l2, |
|---|
| 196 | "caca"); |
|---|
| 197 | #endif |
|---|
| 198 | } |
|---|
| 199 | } |
|---|
| 200 | fcntl(screen_list->comm.socket[SOCK_CLIENT], F_SETFL, O_NONBLOCK); |
|---|
| 201 | } |
|---|
| 202 | else if (!strncmp("REFRESH ", buf, 8)) |
|---|
| 203 | { |
|---|
| 204 | int dt, x, y; |
|---|
| 205 | /* FIXME check the length before calling atoi */ |
|---|
| 206 | x = atoi(buf + 8); |
|---|
| 207 | y = atoi(buf + 19); |
|---|
| 208 | caca_gotoxy(screen_list->cv, x, y); |
|---|
| 209 | caca_refresh_display(screen_list->dp); |
|---|
| 210 | dt = caca_get_display_time(screen_list->dp); |
|---|
| 211 | |
|---|
| 212 | /* Adjust refresh delay so that the server do not compute |
|---|
| 213 | useless things */ |
|---|
| 214 | if (dt > 2 * 1000 * screen_list->delay |
|---|
| 215 | && screen_list->delay <= 100) |
|---|
| 216 | { |
|---|
| 217 | screen_list->delay *= 2; |
|---|
| 218 | send_delay(screen_list); |
|---|
| 219 | } |
|---|
| 220 | else if (dt < screen_list->delay * 1000 * 1.2 && |
|---|
| 221 | screen_list->delay >= |
|---|
| 222 | 3 * screen_list->requested_delay / 2) |
|---|
| 223 | { |
|---|
| 224 | screen_list->delay = 2 * screen_list->delay / 3; |
|---|
| 225 | send_delay(screen_list); |
|---|
| 226 | } |
|---|
| 227 | } |
|---|
| 228 | else if (!strncmp("CURSOR ", buf, 7)) |
|---|
| 229 | { |
|---|
| 230 | caca_set_cursor(screen_list->dp, atoi(buf + 7)); |
|---|
| 231 | } |
|---|
| 232 | else if (!strncmp("TITLE ", buf, 6)) |
|---|
| 233 | { |
|---|
| 234 | caca_set_display_title(screen_list->dp, buf + 6); |
|---|
| 235 | caca_refresh_display(screen_list->dp); |
|---|
| 236 | } |
|---|
| 237 | else |
|---|
| 238 | { |
|---|
| 239 | debug("Unknown message received from server: %s", buf); |
|---|
| 240 | } |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | if (ret) |
|---|
| 244 | { |
|---|
| 245 | debug("ret1\n"); |
|---|
| 246 | break; |
|---|
| 247 | } |
|---|
| 248 | ret = caca_get_event(screen_list->dp, |
|---|
| 249 | CACA_EVENT_KEY_PRESS |
|---|
| 250 | | CACA_EVENT_MOUSE_PRESS |
|---|
| 251 | | CACA_EVENT_MOUSE_RELEASE |
|---|
| 252 | | CACA_EVENT_MOUSE_MOTION |
|---|
| 253 | | CACA_EVENT_RESIZE |
|---|
| 254 | | CACA_EVENT_QUIT, &ev, 10000); |
|---|
| 255 | if (ret) |
|---|
| 256 | ret = send_event(ev, screen_list); |
|---|
| 257 | |
|---|
| 258 | if (ret) |
|---|
| 259 | { |
|---|
| 260 | debug("ret2\n"); |
|---|
| 261 | break; |
|---|
| 262 | } |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | end: |
|---|
| 266 | if (buf) |
|---|
| 267 | free(buf); |
|---|
| 268 | |
|---|
| 269 | } |
|---|