| 1 |
/*************************************************************************** |
/*************************************************************************** |
| 2 |
io.c - description |
io.c - description |
| 3 |
------------------- |
------------------- |
| 4 |
begin : Mon Oct 18 2004 |
Copyright : (C) 2004-2025 by Leaflet |
| 5 |
copyright : (C) 2004 by Leaflet |
Email : leaflet@leafok.com |
|
email : leaflet@leafok.com |
|
| 6 |
***************************************************************************/ |
***************************************************************************/ |
| 7 |
|
|
| 8 |
/*************************************************************************** |
/*************************************************************************** |
| 9 |
* * |
* * |
| 10 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
| 11 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
| 12 |
* the Free Software Foundation; either version 2 of the License, or * |
* the Free Software Foundation; either version 3 of the License, or * |
| 13 |
* (at your option) any later version. * |
* (at your option) any later version. * |
| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
| 17 |
#include "io.h" |
#include "io.h" |
| 18 |
|
#include "log.h" |
| 19 |
#include "common.h" |
#include "common.h" |
| 20 |
|
#include <errno.h> |
| 21 |
#include <stdio.h> |
#include <stdio.h> |
| 22 |
#include <stdarg.h> |
#include <stdarg.h> |
| 23 |
|
#include <time.h> |
| 24 |
|
#include <fcntl.h> |
| 25 |
|
#include <unistd.h> |
| 26 |
|
#include <sys/select.h> |
| 27 |
|
#include <sys/ioctl.h> |
| 28 |
|
|
| 29 |
int |
int outc(char c) |
|
outc(char c) |
|
| 30 |
{ |
{ |
| 31 |
int retval; |
int retval; |
| 32 |
|
|
| 33 |
retval = fprintf(stdout, "%c", c); |
retval = fprintf(stdout, "%c", c); |
| 34 |
|
|
| 35 |
return retval; |
return retval; |
| 36 |
} |
} |
| 37 |
|
|
| 38 |
int |
int prints(const char *format, ...) |
|
prints(const char * format, ...) |
|
| 39 |
{ |
{ |
| 40 |
va_list args; |
va_list args; |
| 41 |
int retval; |
int retval; |
| 42 |
|
|
| 43 |
va_start (args, format); |
va_start(args, format); |
| 44 |
retval = vfprintf (stdout, format, args); |
retval = vfprintf(stdout, format, args); |
| 45 |
va_end (args); |
va_end(args); |
| 46 |
|
|
| 47 |
return retval; |
return retval; |
| 48 |
} |
} |
| 49 |
|
|
| 50 |
int |
int iflush() |
|
iflush() |
|
| 51 |
{ |
{ |
| 52 |
int retval; |
int retval; |
| 53 |
|
|
| 54 |
retval = fflush (stdout); |
retval = fflush(stdout); |
| 55 |
|
|
| 56 |
return retval; |
return retval; |
| 57 |
} |
} |
| 58 |
|
|
| 59 |
int |
int igetch(int clear_buf) |
|
igetch () |
|
| 60 |
{ |
{ |
| 61 |
static char buf[256]; |
// static input buffer |
| 62 |
unsigned char c, tmp[256]; |
static unsigned char buf[LINE_BUFFER_LEN]; |
| 63 |
int out = 0, loop = 1, in_esc = 0, in_ascii = 0, in_control = 0, i = 0, j; |
static ssize_t len = 0; |
| 64 |
static int len = 0 , pos = 0; |
static int pos = 0; |
| 65 |
|
|
| 66 |
if (pos >= len) |
fd_set testfds; |
| 67 |
{ |
struct timeval timeout; |
| 68 |
len = s_receive (socket_client, buf, 255, ""); |
|
| 69 |
pos = 0; |
unsigned char tmp[LINE_BUFFER_LEN]; |
| 70 |
|
int ret; |
| 71 |
//For debug |
int out = KEY_NULL; |
| 72 |
for (j = 0; j < len; j++) |
int in_esc = 0; |
| 73 |
log_std ("<--[%u]\n", (buf[j] + 256) % 256); |
int in_ascii = 0; |
| 74 |
} |
int in_control = 0; |
| 75 |
|
int i = 0; |
|
while (pos < len) |
|
|
{ |
|
|
c = buf[pos++]; |
|
|
|
|
|
if (c == '\0') |
|
|
{ |
|
|
out = c; |
|
|
break; |
|
|
} |
|
|
|
|
|
if (c == KEY_CONTROL) |
|
|
{ |
|
|
if (in_control == 0) |
|
|
{ |
|
|
in_control = 1; |
|
|
i = 0; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
|
|
|
if (in_control) |
|
|
{ |
|
|
tmp[i++] = c; |
|
|
if (i >= 2) |
|
|
{ |
|
|
out = (int)tmp[0] * 256 + tmp[1]; |
|
|
in_control = 0; |
|
|
break; |
|
|
} |
|
|
continue; |
|
|
} |
|
| 76 |
|
|
| 77 |
if (c == ESC_KEY) |
if (clear_buf) |
| 78 |
{ |
{ |
| 79 |
if (in_esc == 0) |
pos = 0; |
| 80 |
{ |
len = 0; |
| 81 |
in_esc = 1; |
|
| 82 |
in_ascii = 1; |
return 0; |
|
i = 0; |
|
|
continue; |
|
|
} |
|
|
else |
|
|
{ |
|
|
out = ESC_KEY; |
|
|
in_esc = 0; |
|
|
break; |
|
|
} |
|
| 83 |
} |
} |
| 84 |
|
|
| 85 |
in_esc = 0; |
while (!SYS_server_exit && pos >= len) |
| 86 |
|
{ |
| 87 |
|
FD_ZERO(&testfds); |
| 88 |
|
FD_SET(STDIN_FILENO, &testfds); |
| 89 |
|
|
| 90 |
|
timeout.tv_sec = 0; |
| 91 |
|
timeout.tv_usec = 100 * 1000; // 0.1 second |
| 92 |
|
|
| 93 |
if (in_ascii) |
ret = select(FD_SETSIZE, &testfds, NULL, NULL, &timeout); |
| 94 |
|
|
| 95 |
|
if (ret < 0) |
| 96 |
|
{ |
| 97 |
|
if (errno != EINTR) |
| 98 |
|
{ |
| 99 |
|
log_error("Select error in igetch: !\n", errno); |
| 100 |
|
return KEY_NULL; |
| 101 |
|
} |
| 102 |
|
continue; |
| 103 |
|
} |
| 104 |
|
else if (ret == 0) |
| 105 |
|
{ |
| 106 |
|
return KEY_TIMEOUT; |
| 107 |
|
} |
| 108 |
|
|
| 109 |
|
if (FD_ISSET(STDIN_FILENO, &testfds)) |
| 110 |
|
{ |
| 111 |
|
len = read(STDIN_FILENO, buf, sizeof(buf)); |
| 112 |
|
pos = 0; |
| 113 |
|
break; |
| 114 |
|
} |
| 115 |
|
|
| 116 |
|
// For debug |
| 117 |
|
// for (j = 0; j < len; j++) |
| 118 |
|
// log_std ("<--[%u]\n", (buf[j] + 256) % 256); |
| 119 |
|
} |
| 120 |
|
|
| 121 |
|
while (pos < len) |
| 122 |
{ |
{ |
| 123 |
tmp[i++] = c; |
unsigned char c = buf[pos++]; |
| 124 |
if (c == 'm') |
|
| 125 |
{ |
if (c == '\0') |
| 126 |
in_ascii = 0; |
{ |
| 127 |
continue; |
out = c; |
| 128 |
} |
break; |
| 129 |
if (i == 2 && c >= 'A' && c <= 'D') |
} |
| 130 |
{ |
|
| 131 |
in_ascii = 0; |
if (c == KEY_CONTROL) |
| 132 |
switch (c) |
{ |
| 133 |
{ |
if (in_control == 0) |
| 134 |
case 'A': |
{ |
| 135 |
out = KEY_UP; |
in_control = 1; |
| 136 |
break; |
i = 0; |
| 137 |
case 'B': |
continue; |
| 138 |
out = KEY_DOWN; |
} |
| 139 |
break; |
} |
| 140 |
case 'C': |
|
| 141 |
out = KEY_RIGHT; |
if (in_control) |
| 142 |
break; |
{ |
| 143 |
case 'D': |
tmp[i++] = c; |
| 144 |
out = KEY_LEFT; |
if (i >= 2) |
| 145 |
break; |
{ |
| 146 |
} |
out = (int)tmp[0] * 256 + tmp[1]; |
| 147 |
break; |
in_control = 0; |
| 148 |
} |
break; |
| 149 |
if (i == 3 && tmp[0] == 91 && tmp[2] == 126) |
} |
| 150 |
{ |
continue; |
| 151 |
in_ascii = 0; |
} |
| 152 |
switch (tmp[1]) |
|
| 153 |
{ |
if (c == ESC_KEY) |
| 154 |
case 49: |
{ |
| 155 |
out = KEY_HOME; |
if (in_esc == 0) |
| 156 |
break; |
{ |
| 157 |
case 51: |
in_esc = 1; |
| 158 |
out = KEY_DEL; |
in_ascii = 1; |
| 159 |
break; |
i = 0; |
| 160 |
case 52: |
continue; |
| 161 |
out = KEY_END; |
} |
| 162 |
break; |
else |
| 163 |
case 53: |
{ |
| 164 |
out = KEY_PGUP; |
out = ESC_KEY; |
| 165 |
break; |
in_esc = 0; |
| 166 |
case 54: |
break; |
| 167 |
out = KEY_PGDOWN; |
} |
| 168 |
break; |
} |
| 169 |
} |
|
| 170 |
break; |
in_esc = 0; |
| 171 |
} |
|
| 172 |
continue; |
if (in_ascii) |
| 173 |
|
{ |
| 174 |
|
tmp[i++] = c; |
| 175 |
|
if (c == 'm') |
| 176 |
|
{ |
| 177 |
|
in_ascii = 0; |
| 178 |
|
continue; |
| 179 |
|
} |
| 180 |
|
if (i == 2 && c >= 'A' && c <= 'D') |
| 181 |
|
{ |
| 182 |
|
in_ascii = 0; |
| 183 |
|
switch (c) |
| 184 |
|
{ |
| 185 |
|
case 'A': |
| 186 |
|
out = KEY_UP; |
| 187 |
|
break; |
| 188 |
|
case 'B': |
| 189 |
|
out = KEY_DOWN; |
| 190 |
|
break; |
| 191 |
|
case 'C': |
| 192 |
|
out = KEY_RIGHT; |
| 193 |
|
break; |
| 194 |
|
case 'D': |
| 195 |
|
out = KEY_LEFT; |
| 196 |
|
break; |
| 197 |
|
} |
| 198 |
|
break; |
| 199 |
|
} |
| 200 |
|
if (i == 3 && tmp[0] == 91 && tmp[2] == 126) |
| 201 |
|
{ |
| 202 |
|
in_ascii = 0; |
| 203 |
|
switch (tmp[1]) |
| 204 |
|
{ |
| 205 |
|
case 49: |
| 206 |
|
out = KEY_HOME; |
| 207 |
|
break; |
| 208 |
|
case 51: |
| 209 |
|
out = KEY_DEL; |
| 210 |
|
break; |
| 211 |
|
case 52: |
| 212 |
|
out = KEY_END; |
| 213 |
|
break; |
| 214 |
|
case 53: |
| 215 |
|
out = KEY_PGUP; |
| 216 |
|
break; |
| 217 |
|
case 54: |
| 218 |
|
out = KEY_PGDN; |
| 219 |
|
break; |
| 220 |
|
} |
| 221 |
|
break; |
| 222 |
|
} |
| 223 |
|
continue; |
| 224 |
|
} |
| 225 |
|
|
| 226 |
|
out = ((int)c + 256) % 256; |
| 227 |
|
break; |
| 228 |
} |
} |
| 229 |
|
|
| 230 |
out = ((int)c + 256) % 256; |
// for debug |
| 231 |
break; |
// log_std ("-->[%u]\n", out); |
|
} |
|
| 232 |
|
|
| 233 |
//for debug |
return out; |
| 234 |
log_std ("-->[%u]\n", out); |
} |
| 235 |
|
|
| 236 |
|
int igetch_t(long int sec) |
| 237 |
|
{ |
| 238 |
|
int ch; |
| 239 |
|
time_t t_begin = time(0); |
| 240 |
|
|
| 241 |
|
do |
| 242 |
|
{ |
| 243 |
|
ch = igetch(0); |
| 244 |
|
} while (ch == KEY_TIMEOUT && (time(0) - t_begin < sec)); |
| 245 |
|
|
| 246 |
return out; |
return ch; |
| 247 |
} |
} |