| 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 | |
|---|
| 65 | |
|---|
| 66 | void mainloop(struct screen_list *screen_list) |
|---|
| 67 | { |
|---|
| 68 | char *buf = NULL; |
|---|
| 69 | |
|---|
| 70 | screen_list->last_key_time = get_us(); |
|---|
| 71 | |
|---|
| 72 | for (;;) |
|---|
| 73 | { |
|---|
| 74 | caca_event_t ev; |
|---|
| 75 | int ret = 0; |
|---|
| 76 | ssize_t n; |
|---|
| 77 | if (!screen_list) |
|---|
| 78 | goto end; |
|---|
| 79 | if (!buf) |
|---|
| 80 | buf = malloc(NEERCS_RECV_BUFSIZE); |
|---|
| 81 | if (!buf) |
|---|
| 82 | { |
|---|
| 83 | debug("Failed to allocate memory"); |
|---|
| 84 | goto end; |
|---|
| 85 | } |
|---|
| 86 | if (screen_list->socket[SOCK_CLIENT] |
|---|
| 87 | && (n = |
|---|
| 88 | read(screen_list->socket[SOCK_CLIENT], buf, |
|---|
| 89 | NEERCS_RECV_BUFSIZE - 1)) > 0) |
|---|
| 90 | { |
|---|
| 91 | buf[n] = 0; |
|---|
| 92 | debug("Received from server: %s", buf); |
|---|
| 93 | if (!strncmp("DETACH", buf, 6)) |
|---|
| 94 | { |
|---|
| 95 | ret = 1; |
|---|
| 96 | break; |
|---|
| 97 | } |
|---|
| 98 | else if (!strncmp("UPDATE ", buf, 7)) |
|---|
| 99 | { |
|---|
| 100 | int x, y; |
|---|
| 101 | ssize_t l2 = 0, lb = 0; |
|---|
| 102 | char *buf2; |
|---|
| 103 | size_t l = 0; |
|---|
| 104 | /* FIXME check the length before calling atoi */ |
|---|
| 105 | x = atoi(buf + 8); |
|---|
| 106 | y = atoi(buf + 19); |
|---|
| 107 | /* 0 means we have valid data but incomplete, so read the rest |
|---|
| 108 | */ |
|---|
| 109 | while (l == 0) |
|---|
| 110 | { |
|---|
| 111 | buf2 = realloc(buf, l2 + NEERCS_RECV_BUFSIZE); |
|---|
| 112 | if (!buf2) |
|---|
| 113 | { |
|---|
| 114 | debug("Failed to allocate memory"); |
|---|
| 115 | goto end; |
|---|
| 116 | } |
|---|
| 117 | buf = buf2; |
|---|
| 118 | fcntl(screen_list->socket[SOCK_CLIENT], F_SETFL, 0); |
|---|
| 119 | lb = read(screen_list->socket[SOCK_CLIENT], buf + l2, |
|---|
| 120 | NEERCS_RECV_BUFSIZE - 1); |
|---|
| 121 | if (lb < 0) |
|---|
| 122 | { |
|---|
| 123 | debug |
|---|
| 124 | ("Failed to read the end of the refresh message (%s)", |
|---|
| 125 | strerror(errno)); |
|---|
| 126 | l = -1; |
|---|
| 127 | } |
|---|
| 128 | else |
|---|
| 129 | { |
|---|
| 130 | l2 += lb; |
|---|
| 131 | #if defined HAVE_CACA_DIRTY_RECTANGLES |
|---|
| 132 | l = caca_import_area_from_memory(screen_list->cv, x, y, |
|---|
| 133 | buf, l2, "caca"); |
|---|
| 134 | #else |
|---|
| 135 | l = caca_import_memory(screen_list->cv, buf, l2, |
|---|
| 136 | "caca"); |
|---|
| 137 | #endif |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | fcntl(screen_list->socket[SOCK_CLIENT], F_SETFL, O_NONBLOCK); |
|---|
| 141 | } |
|---|
| 142 | else if (!strncmp("REFRESH ", buf, 8)) |
|---|
| 143 | { |
|---|
| 144 | int dt, x, y; |
|---|
| 145 | /* FIXME check the length before calling atoi */ |
|---|
| 146 | x = atoi(buf + 8); |
|---|
| 147 | y = atoi(buf + 19); |
|---|
| 148 | caca_gotoxy(screen_list->cv, x, y); |
|---|
| 149 | caca_refresh_display(screen_list->dp); |
|---|
| 150 | dt = caca_get_display_time(screen_list->dp); |
|---|
| 151 | debug("refreshtime=%dms (limit %d, requested %d)", dt / 1000, |
|---|
| 152 | screen_list->delay, screen_list->requested_delay); |
|---|
| 153 | /* Adjust refresh delay so that the server do not compute |
|---|
| 154 | useless things */ |
|---|
| 155 | if (dt > 2 * 1000 * screen_list->delay |
|---|
| 156 | && screen_list->delay <= 100) |
|---|
| 157 | { |
|---|
| 158 | screen_list->delay *= 2; |
|---|
| 159 | send_delay(screen_list); |
|---|
| 160 | } |
|---|
| 161 | else if (dt < screen_list->delay * 1000 * 1.2 && |
|---|
| 162 | screen_list->delay >= |
|---|
| 163 | 3 * screen_list->requested_delay / 2) |
|---|
| 164 | { |
|---|
| 165 | screen_list->delay = 2 * screen_list->delay / 3; |
|---|
| 166 | send_delay(screen_list); |
|---|
| 167 | } |
|---|
| 168 | } |
|---|
| 169 | else if (!strncmp("CURSOR ", buf, 7)) |
|---|
| 170 | { |
|---|
| 171 | caca_set_cursor(screen_list->dp, atoi(buf + 7)); |
|---|
| 172 | } |
|---|
| 173 | else if (!strncmp("TITLE ", buf, 6)) |
|---|
| 174 | { |
|---|
| 175 | caca_set_display_title(screen_list->dp, buf + 6); |
|---|
| 176 | caca_refresh_display(screen_list->dp); |
|---|
| 177 | } |
|---|
| 178 | else |
|---|
| 179 | { |
|---|
| 180 | debug("Unknown message received from server: %s", buf); |
|---|
| 181 | } |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | if (ret) |
|---|
| 185 | break; |
|---|
| 186 | |
|---|
| 187 | ret = caca_get_event(screen_list->dp, |
|---|
| 188 | CACA_EVENT_KEY_PRESS |
|---|
| 189 | | CACA_EVENT_RESIZE |
|---|
| 190 | | CACA_EVENT_QUIT, &ev, 10000); |
|---|
| 191 | if (ret) |
|---|
| 192 | ret = send_event(ev, screen_list); |
|---|
| 193 | |
|---|
| 194 | if (ret) |
|---|
| 195 | break; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | end: |
|---|
| 199 | if (buf) |
|---|
| 200 | free(buf); |
|---|
| 201 | |
|---|
| 202 | } |
|---|