-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.c
More file actions
223 lines (193 loc) · 6.7 KB
/
Copy pathmenu.c
File metadata and controls
223 lines (193 loc) · 6.7 KB
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
220
221
222
223
/** @file */
#include <stdio.h>
#include <key_codes.h>
#include <windows.h>
#include <f_queue.h>
#include <recording.h>
#include <files.h>
#include <replay.h>
#include <functions_queue.h>
#include <pressed_key.h>
#include <smooth_cursor.h>
#define error(msg) fprintf(stderr, msg)
void print_help()
{
printf("Welcome to WinAuto\n");
printf("You chose -h flag\n\n");
printf("Option (1): choose the hotkey. This hotkey will be used in options (2), (3), (4)\n\n");
printf("Option (2): start the recording. Save as <file-name>.txt, for example test.txt. The file has to end with .txt\n");
printf("Use hotkey to stop the recording.\n\n");
printf("Option (3): start the playback of the recording created in (2). Load the recording with appropriate file name.\n");
printf("Choose number of cycles (repetitions) of the replay. If you wish to terminate the playback, hold hotkey.\n\n");
printf("Option (4): extra feature. Choose arbitrary speed and minfps to start the screensaver.\nYou have to HOLD hotkey to terminate\n\n\n");
printf("Option (5): to exit program\n\n\n");
}
bool h_switch_invoked(int argc, char **argv)
{
if (argc > 1)
if (0 == strcmp(argv[1], "-h"))
return true;
return false;
}
/** Enum containing various menu flags used to determine which <b>printf</b> should be displayed to the user, based on earlier program behaviour. */
enum menu_flags { ///< start of definition
NO_ERRORS, ///< default
ERROR_NO_TXT_SUFFIX, ///< when user forgot to input the .txt postfix
ERROR_READING_FILE, ///< when file was corrupted, does not exist or cannot be opened
SAVED_HOTKEY, ///< when the hotkey has been successfully saved
SAVED_FILE, ///< when the file saved successfully
STOPPED_PLAYBACK, ///< when the recording playback successfully ended
STOPPED_SCREENSAVER, ///< when the screensaver has been successfully stopped
HELP_SWITCH ///< when program was ran with '-h' switch
};
void draw_menu(const int flag_id)
{
system("cls");
switch (flag_id) {
case 0:
printf("WinAuto\n");
break;
case 1:
error("ERROR: File name must end with .txt suffix\n\n");
break;
case 2:
error("ERROR: No such file or file is corrupted\n\n");
break;
case 3:
printf("Hotkey set successfully\n\n");
break;
case 4:
printf("Recording saved successfully\n\n");
break;
case 5:
printf("Playback finished or interrupted\n\n");
break;
case 6:
printf("Welcome back\n\n");
break;
case 7:
print_help();
break;
default: // do nothing
break;
}
printf("Press 1 to set global hotkey (DEFAULT HOTKEY: F5)\n");
printf("Press 2 to create new recording\n");
printf("Press 3 to play recording\n");
printf("Press 4 to start screensaver\n");
printf("Press 5 to exit\n");
}
int get_menu_choice(void)
{
int choice = 0;
while (choice < 1 || choice > 5)
if (1 != scanf("%d", &choice))
fseek(stdin, 0, SEEK_END);
return choice;
}
int get_hotkey(void)
{
printf("Set hotkey: \n");
int hotkey = 0;
while (hotkey == 0 ||
hotkey == KEY_RETURN ||
hotkey == KEY_LMB ||
hotkey == KEY_RMB ||
hotkey == KEY_F5) {
hotkey = get_keystroke();
}
FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
return hotkey;
}
bool str_ends_with(const char *source, const char *suffix)
{
int source_len = strlen(source);
int suffix_len = strlen(suffix);
return (source_len >= suffix_len) && (0 == strcmp(source + (source_len - suffix_len), suffix));
}
int get_cycles_num(void)
{
printf("How many playing cycles? (>5 to play infinitely, default 1):\n");
int cycles_num = 1;
if (1 != scanf("%d", &cycles_num) || cycles_num <= 0) {
fseek(stdin, 0, SEEK_END);
get_cycles_num();
}
return cycles_num;
}
void exec_play_recording(struct f_queue *head, struct f_queue *tail, const int cycles_num, const int hotkey_id)
{
printf("Playing recording...\n");
printf("Press your hotkey to stop\n");
if (cycles_num > 5) {
make_queue_cyclic(head, tail);
play_recording(tail, hotkey_id);
unmake_queue_cyclic(head, tail);
}
else {
for (int i = 0; i < cycles_num; i++)
play_recording(tail, hotkey_id);
}
}
void init_menu(struct f_queue *head, struct f_queue *tail, const int flag_id, const int hotkey_id);
void chosen_recording(struct f_queue *head, struct f_queue *tail, const int hotkey_id)
{
printf("Save recording as (i.e: myrecording.txt):\n");
char file_name[64];
scanf("%s", file_name);
if (str_ends_with(file_name, ".txt")) {
record(&head, &tail, 10, hotkey_id);
FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
trim_list(&head);
save_recording(tail, file_name);
free_recording(&head, &tail);
init_menu(head, tail, SAVED_FILE, hotkey_id);
}
else {
init_menu(head, tail, ERROR_NO_TXT_SUFFIX, hotkey_id);
}
}
void chosen_playback(struct f_queue *head, struct f_queue *tail, const int hotkey_id)
{
printf("Type in file name of your recording (i.e: myfile.txt):\n");
char file_name[64];
scanf("%s", file_name);
if (load_recording(&head, &tail, file_name)) {
int cycles_num = get_cycles_num();
exec_play_recording(head, tail, cycles_num, hotkey_id);
FlushConsoleInputBuffer(GetStdHandle(STD_INPUT_HANDLE));
free_recording(&head, &tail);
init_menu(head, tail, STOPPED_PLAYBACK, hotkey_id);
}
else { // error when reading file
if (tail)
free_recording(&head, &tail);
init_menu(head, tail, ERROR_READING_FILE, hotkey_id);
}
}
void init_menu(struct f_queue *head, struct f_queue *tail, const int flag_id, const int hotkey_id)
{
draw_menu(flag_id);
int choice = get_menu_choice();
static int hotkey = KEY_F5; /// default hotkey
switch(choice) {
case 1:
hotkey = get_hotkey();
init_menu(head, tail, SAVED_HOTKEY, hotkey);
break;
case 2:
chosen_recording(head, tail, hotkey);
break;
case 3:
chosen_playback(head, tail, hotkey);
break;
case 4:
exec_screen_saver(hotkey);
init_menu(head, tail, STOPPED_SCREENSAVER, hotkey);
break;
case 5:
return;
default: // do nothing
break;
}
}