-
Notifications
You must be signed in to change notification settings - Fork 3
/
guiascii.c
179 lines (148 loc) · 4.85 KB
/
guiascii.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
/* xAutoClick -- ASCII GUI
*
* Copyright (C) 2006 Ivo van Poorten
*
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "gui.h"
#include "main.h"
#include "osdep.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
typedef struct ascii_gui_ctx {
int spins[SPINS_COUNT];
} ascii_gui_t;
static int sleeptime;
static void ascii_gui_main_loop(ascii_gui_t* ctx);
static int ascii_gui_get_spin_value(ascii_gui_t* ctx, spin_t spin) {
return ctx->spins[spin];
}
static void ascii_gui_set_spin_value(ascii_gui_t* ctx, spin_t spin, int value) {
ctx->spins[spin] = value;
}
static void ascii_gui_close(ascii_gui_t* ctx) {
free(ctx);
}
static void print_spin_command(char cmd, const spin_param_t* spin_param) {
if (spin_param->suffix && *spin_param->suffix != '\0')
printf("%c - set %s, %s\n", cmd, spin_param->descr, spin_param->suffix);
else
printf("%c - set %s\n", cmd, spin_param->descr);
}
void init_gui(gui_t* gui, const spin_param_t* spin_params, const char** button_names, int argc, char **argv) {
ascii_gui_t* ctx;
(void)argc;
(void)argv;
ctx = calloc(1, sizeof(ascii_gui_t));
if (!ctx) {
fprintf(stderr, "Can't allocate memory for ASCII GUI\n");
return;
}
printf("aautoclick\n\n");
print_spin_command('p', &spin_params[SPIN_PREDELAY]);
print_spin_command('i', &spin_params[SPIN_INTERVAL]);
print_spin_command('r', &spin_params[SPIN_RANDOM]);
print_spin_command('n', &spin_params[SPIN_NUMBER]);
printf("t - %s\n", button_names[BUTTON_TAP]);
printf("s - %s\n", button_names[BUTTON_START]);
printf("q - quit\n");
printf("\n");
gui->ctx = ctx;
gui->get_spin_value = (gui_get_spin_value_t)ascii_gui_get_spin_value;
gui->set_spin_value = (gui_set_spin_value_t)ascii_gui_set_spin_value;
gui->main_loop = (gui_main_loop_t)ascii_gui_main_loop;
gui->close = (gui_close_t)ascii_gui_close;
}
void set_alarm(int ms) {
sleeptime = ms;
}
static void print_variables(ascii_gui_t* ctx) {
printf("pre-delay: %i ms\n", ctx->spins[SPIN_PREDELAY]);
printf("interval: %i ms\n", ctx->spins[SPIN_INTERVAL]);
printf("random +/-: %i ms\n", ctx->spins[SPIN_RANDOM]);
printf("number of clicks: %i\n", ctx->spins[SPIN_NUMBER]);
printf("\n");
}
#define is_eol(x) ((x)=='\n')
static void flush_to_eol(int c) {
if (is_eol(c))
return;
while(!is_eol(c)) { c = fgetc(stdin); }
}
static void read_int(int *ptr) {
int result;
printf("new value: ");
result = fscanf(stdin, "%i", ptr);
if (result != 1)
{
fprintf(stderr, "Input value must be integer");
if (EOF == result)
{
int stdin_err = ferror(stdin);
if (stdin_err != 0)
{
fprintf(stderr, ", error: %d (%s)", errno, strerror(errno));
}
else
{
fprintf(stderr, ", result: %d (Reached end of input)", result);
}
}
fprintf(stderr, "\n");
}
flush_to_eol(0);
}
static void run(void) {
common_start_button();
while (sleeptime) {
usleep(sleeptime * 1000);
sleeptime = 0;
common_alarm_callback();
}
}
static void ascii_gui_main_loop(ascii_gui_t* ctx) {
int c;
print_variables(ctx);
printf("> ");
c = fgetc(stdin);
while (c) {
while(isblank(c)) { c = fgetc(stdin); }
if (is_eol(c)) {
printf("> ");
c = fgetc(stdin);
continue;
}
flush_to_eol(c);
switch (c) {
case 'p': read_int(&ctx->spins[SPIN_PREDELAY]); break;
case 'i': read_int(&ctx->spins[SPIN_INTERVAL]); break;
case 'r': read_int(&ctx->spins[SPIN_RANDOM]); break;
case 'n': read_int(&ctx->spins[SPIN_NUMBER]); break;
case 't': common_tap_button(); break;
case 's': run(); return;
case 'q': return;
default: printf("unknown command\n"); break;
}
print_variables(ctx);
usleep(100 * 1000);
printf("> ");
c = fgetc(stdin);
}
run();
}