-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen.c
220 lines (184 loc) · 4.98 KB
/
screen.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* screen.c
*
* 2007 Copyright (c)
* Robert Iakobashvili, <coroberti@gmail.com>
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// must be first include
#include "fdsetsize.h"
#include <errno.h>
#include <unistd.h>
#include <sys/select.h>
#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <time.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include "screen.h"
#include "batch.h"
#include "client.h"
#include "loader.h"
//static int test_fd_readable (int fd);
static int on_keybord_input (int key, batch_context* bctx);
static struct termios start_tty, run_tty;
void screen_init ()
{
/* Initializes curses library */
//initscr();
/* Improves performances */
//noecho();
//fprintf(stderr, "\033[2J");
/* disable echoing keyboard input */
tcgetattr(STDIN_FILENO, &start_tty);
run_tty = start_tty;
run_tty.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &run_tty);
}
void screen_release ()
{
/* re-enable echoing */
//tcsetattr(STDIN_FILENO, TCSANOW, &start_tty);
return;
}
/*
* getch() -- a non-blocking single character input from stdin
*
* Returns a character, or -1 if an input error occurs.
*
* Conditionals allow compiling with or without echoing of
* the input characters, and with or without flushing
* pre-existing existing buffered input before blocking.
*
*/
int
getch(void)
{
struct termios otty, ntty;
int ch = -1;
tcgetattr(STDIN_FILENO, &otty);
ntty = otty;
ntty.c_lflag &= ~ICANON; /* line settings */
#if 1
/* disable echoing the char as it is typed */
ntty.c_lflag &= ~ECHO; /* disable echo */
#else
/* enable echoing the char as it is typed */
ntty.c_lflag |= ECHO; /* enable echo */
#endif
ntty.c_cc[VMIN] = 0; /* non-block for input */
ntty.c_cc[VTIME] = 1; /* with timer*/
#if 0
/*
* use this to flush the input buffer before
* blocking for new input
*/
#define FLAG TCSAFLUSH
#else
/*
* use this to return a char from the current input
* buffer, or block if no input is waiting.
*/
#define FLAG TCSANOW
#endif
if (!tcsetattr(STDIN_FILENO, FLAG, &ntty))
{
/* get a single character from stdin */
if (read (STDIN_FILENO, &ch, 1 ) == -1)
{
if (!stop_loading)
{
fprintf(stderr, "%s - read() failed with errno %d.\n",
__func__, errno);
}
return -1;
}
/* restore old settings */
tcsetattr(STDIN_FILENO, FLAG, &otty);
}
return ch;
}
int screen_test_keyboard_input (batch_context* bctx)
{
/*
int rval_input = -1;
if ((rval_input = test_fd_readable (STDIN_FILENO)) == -1)
{
fprintf(stderr, "%s - select () failed with errno %d.\n",
__func__, errno);
return -1;
}
if (!rval_input)
return 0;
*/
int the_key;
if ((the_key = getch ())== -1)
{
//fprintf(stderr, "%s - getch () failed with errno %d.\n",
// __func__, errno);
return 0;
}
return on_keybord_input (the_key, bctx);
}
int on_keybord_input (int key, batch_context* bctx)
{
char ch = key;
switch (ch)
{
case 'm':
case 'M':
bctx->stop_client_num_gradual_increase = 1;
//fprintf(stderr, "%s - stop_inc %d\n",
// __func__, bctx->stop_client_num_gradual_increase);
break;
case 'a':
case 'A':
bctx->stop_client_num_gradual_increase = 0;
break;
case '+':
add_loading_clients_num (bctx, 1);
break;
//case '-':
//break;
case '*':
add_loading_clients_num (bctx, 10);
break;
//case '/':
//break;
}
fprintf(stderr, "%s - got %c\n", __func__, key);
return 0;
}
/****************************************************************************************
* Function name - test_fd_readable
*
* Description - Tests, whether the descriptor is readable
* Input - fd - file descriptor
*
* Return Code - When readable (1), when not readable (0), on error (-1)
****************************************************************************************/
/*
static int test_fd_readable (int fd)
{
fd_set fdset;
struct timeval timeout = {0,0};
FD_ZERO(&fdset);
FD_SET(fd, &fdset);
return select (fd + 1, &fdset, NULL, NULL, &timeout);
}
*/