| 1 | /* |
| 2 | * Copyright (C) 1998,1999,2000 Ross Combs ([email protected]) |
| 3 | * Copyright (C) 1999 Oleg Drokin ([email protected]) |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | #include "common/setup_before.h" |
| 20 | #include "client.h" |
| 21 | |
| 22 | #include <cstdlib> |
| 23 | #include <cstring> |
| 24 | #include <cstdio> |
| 25 | |
| 26 | #ifdef WIN32 |
| 27 | # include <conio.h> |
| 28 | #endif |
| 29 | #ifdef HAVE_UNISTD_H |
| 30 | # include <unistd.h> |
| 31 | #endif |
| 32 | |
| 33 | #include "common/network.h" |
| 34 | #include "common/setup_after.h" |
| 35 | |
| 36 | |
| 37 | namespace pvpgn |
| 38 | { |
| 39 | |
| 40 | namespace client |
| 41 | { |
| 42 | |
| 43 | extern int client_blocksend_packet(int sock, t_packet const * packet) |
| 44 | { |
| 45 | unsigned int size = 0; |
| 46 | |
| 47 | for (;;) |
| 48 | switch (net_send_packet(sock, packet, &size)) |
| 49 | { |
| 50 | case -1: |
| 51 | return -1; |
| 52 | |
| 53 | case 0: |
| 54 | continue; |
| 55 | |
| 56 | default: |
| 57 | return 0; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | |
| 62 | extern int client_blockrecv_packet(int sock, t_packet * packet) |
| 63 | { |
| 64 | unsigned int size = 0; |
| 65 | |
| 66 | for (;;) |
| 67 | switch (net_recv_packet(sock, packet, &size)) |
| 68 | { |
| 69 | case -1: |
| 70 | return -1; |
| 71 | |
| 72 | case 0: |
| 73 | continue; |
| 74 | |
| 75 | default: |
| 76 | /* std::printf("Got packet len %u/%u type 0x%04x\n",size,packet_get_size(packet),packet_get_type(packet)); */ |
| 77 | return 0; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | |
| 82 | extern int client_blockrecv_raw_packet(int sock, t_packet * packet, unsigned int len) |
| 83 | { |
| 84 | unsigned int size = 0; |
| 85 | |
| 86 | packet_set_size(packet, len); |
| 87 | for (;;) |
| 88 | switch (net_recv_packet(sock, packet, &size)) |
| 89 | { |
| 90 | case -1: |
| 91 | return -1; |
| 92 | case 0: |
| 93 | continue; |
| 94 | case 1: |
| 95 | default: |
| 96 | return 0; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | |
| 101 | extern int client_get_termsize(int fd, unsigned int * w, unsigned int * h) |
| 102 | { |
| 103 | if (fd < 0 || !w || !h) |
| 104 | return -1; |
| 105 | |
| 106 | *w = 0; |
| 107 | *h = 0; |
| 108 | |
| 109 | #ifdef HAVE_IOCTL |
| 110 | #ifdef TIOCGWINSZ |
| 111 | { |
| 112 | struct winsize ws; |
| 113 | |
| 114 | if (ioctl(fd, TIOCGWINSZ, &ws) >= 0) |
| 115 | { |
| 116 | *w = (unsigned int)ws.ws_col; |
| 117 | *h = (unsigned int)ws.ws_row; |
| 118 | } |
| 119 | } |
| 120 | #endif |
| 121 | #endif |
| 122 | |
| 123 | { |
| 124 | char const * str; |
| 125 | int val; |
| 126 | |
| 127 | if (!*w && (str = std::getenv("COLUMNS")) && (val = std::atoi(str)) > 0) |
| 128 | *w = val; |
| 129 | if (!*h && (str = std::getenv("LINES")) && (val = std::atoi(str)) > 0) |
| 130 | *h = val; |
| 131 | } |
| 132 | |
| 133 | if (!*w) |
| 134 | *w = DEF_SCREEN_WIDTH; |
| 135 | if (!*h) |
| 136 | *h = DEF_SCREEN_HEIGHT; |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | /* This routine std::gets keyboard input. It handles printing the prompt, cursor positioning, and |
| 143 | text scrolling. It unfortunatly assumes that the chars read from stdin are in ASCII. */ |
| 144 | /* visible: -1=nothing, 0=prompt only, 1=prompt and text */ |
| 145 | extern int client_get_comm(char const * prompt, char * text, unsigned int maxlen, unsigned int * curpos, int visible, int redraw, unsigned int width) |
| 146 | { |
| 147 | char temp; |
| 148 | unsigned int addlen; |
| 149 | unsigned int i; |
| 150 | unsigned int count; |
| 151 | int beg_pos; |
| 152 | |
| 153 | for (count = 0; count < 16; count++) |
| 154 | { |
| 155 | beg_pos = 0; |
| 156 | if (std::strlen(prompt) + 1 >= width) |
| 157 | visible = 0; /* no room to show any of the text */ |
| 158 | else if (*curpos + std::strlen(prompt) + 1 >= width) |
| 159 | beg_pos = (int)(*curpos + std::strlen(prompt) + 1 - width); |
| 160 | |
| 161 | if (redraw) |
| 162 | { |
| 163 | if (visible != -1) |
| 164 | std::printf("\r%s", prompt); |
| 165 | if (visible == 1) |
| 166 | std::printf("%s", text + beg_pos); |
| 167 | } |
| 168 | |
| 169 | std::fflush(stdout); |
| 170 | #ifndef WIN32 |
| 171 | addlen = read(fileno(stdin), &temp, 1); |
| 172 | #else |
| 173 | if (kbhit()) |
| 174 | { |
| 175 | temp = getch(); |
| 176 | addlen = 1; |
| 177 | } |
| 178 | else |
| 179 | { |
| 180 | temp = 0; |
| 181 | addlen = 0; |
| 182 | } |
| 183 | #endif |
| 184 | |
| 185 | if (addlen < 1) |
| 186 | return addlen; |
| 187 | |
| 188 | switch (temp) |
| 189 | { |
| 190 | case '\033': /* ESC */ |
| 191 | return -1; |
| 192 | case '\r': |
| 193 | case '\n': |
| 194 | return 1; |
| 195 | case '\b': /* BS */ |
| 196 | case '\177': /* DEL */ |
| 197 | if (*curpos<1) /* already empty */ |
| 198 | { |
| 199 | if (visible == 1) |
| 200 | std::printf("\a"); |
| 201 | continue; |
| 202 | } |
| 203 | (*curpos)--; |
| 204 | text[*curpos] = '\0'; |
| 205 | if (visible == 1) |
| 206 | { |
| 207 | if (beg_pos>0) |
| 208 | { |
| 209 | beg_pos--; |
| 210 | std::printf("\r%s%s", prompt, text + beg_pos); |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | std::printf("\b \b"); |
| 215 | } |
| 216 | } |
| 217 | continue; |
| 218 | case '\024': /* ^T */ |
| 219 | if (*curpos < 2) |
| 220 | { |
| 221 | if (visible == 1) |
| 222 | std::printf("\a"); |
| 223 | continue; |
| 224 | } |
| 225 | { |
| 226 | char swap; |
| 227 | |
| 228 | swap = text[*curpos - 1]; |
| 229 | text[*curpos - 1] = text[*curpos - 2]; |
| 230 | text[*curpos - 2] = swap; |
| 231 | |
| 232 | if (visible == 1) |
| 233 | { |
| 234 | std::printf("\b\b"); |
| 235 | std::printf("%s", &text[*curpos - 2]); |
| 236 | } |
| 237 | } |
| 238 | continue; |
| 239 | case '\027': /* ^W */ |
| 240 | if (*curpos < 1) |
| 241 | { |
| 242 | if (visible == 1) |
| 243 | std::printf("\a"); |
| 244 | continue; |
| 245 | } |
| 246 | { |
| 247 | char * t = std::strrchr(text, ' '); |
| 248 | unsigned int t1 = beg_pos, t2; |
| 249 | |
| 250 | addlen = t ? t - text : 0; |
| 251 | text[addlen] = '\0'; |
| 252 | beg_pos -= *curpos - addlen; |
| 253 | if (beg_pos < 0) |
| 254 | beg_pos = 0; |
| 255 | if (visible == 1) |
| 256 | { |
| 257 | if (t1 == 0) |
| 258 | for (i = 0; i < *curpos - addlen; i++) |
| 259 | std::printf("\b \b"); |
| 260 | else |
| 261 | { |
| 262 | /* the \r is counted in the return value, but that's ok |
| 263 | because the last column is always blank */ |
| 264 | t2 = std::printf("\r%s%s", prompt, text + beg_pos); |
| 265 | if (t1 > 0 && beg_pos == 0) |
| 266 | { |
| 267 | for (i = 0; i < width - t2; i++) |
| 268 | std::printf(" "); |
| 269 | for (i = 0; i < width - t2; i++) |
| 270 | std::printf("\b"); |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | *curpos = addlen; |
| 276 | continue; |
| 277 | case '\025': /* ^U */ |
| 278 | if (visible == 1) |
| 279 | { |
| 280 | unsigned int t2; |
| 281 | |
| 282 | t2 = std::printf("\r%s", prompt); |
| 283 | for (i = 0; i<width - t2; i++) |
| 284 | std::printf(" "); |
| 285 | std::printf("\r%s", prompt); |
| 286 | } |
| 287 | *curpos = 0; |
| 288 | text[0] = '\0'; |
| 289 | continue; |
| 290 | default: |
| 291 | if (temp>0 && temp < 32) /* unhandled control char */ |
| 292 | { |
| 293 | if (visible == 1) |
| 294 | std::printf("\a"); |
| 295 | continue; |
| 296 | } |
| 297 | if ((*curpos + 1) >= maxlen) /* too full */ |
| 298 | { |
| 299 | if (visible == 1) |
| 300 | std::printf("\a"); |
| 301 | continue; |
| 302 | } |
| 303 | if (visible == 1) |
| 304 | { |
| 305 | if (beg_pos > 0 || (*curpos + std::strlen(prompt) + 2 > width)) |
| 306 | { |
| 307 | beg_pos++; |
| 308 | std::printf("\r%s%s", prompt, text + beg_pos); |
| 309 | } |
| 310 | std::printf("%c", temp); |
| 311 | } |
| 312 | text[*curpos] = temp; |
| 313 | (*curpos)++; |
| 314 | text[*curpos] = '\0'; |
| 315 | continue; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | } |
| 323 | |
| 324 | } |