Repository navigation
Expand file tree
/
Copy pathmove_cursor_linux.c
More file actions
255 lines (217 loc) · 8.68 KB
/
Copy pathmove_cursor_linux.c
File metadata and controls
255 lines (217 loc) · 8.68 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/Xatom.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
int is_esc_pressed(Display *display, KeyCode esc_code) {
char keys[32];
XQueryKeymap(display, keys);
return (keys[esc_code / 8] & (1 << (esc_code % 8))) != 0;
}
int is_mouse_clicked(Display *display, Window root, int *x_out, int *y_out) {
Window root_ret, child_ret;
int root_x, root_y, win_x, win_y;
unsigned int mask;
if (XQueryPointer(display, root, &root_ret, &child_ret, &root_x, &root_y, &win_x, &win_y, &mask)) {
if (x_out) *x_out = root_x;
if (y_out) *y_out = root_y;
if (mask & (Button1Mask | Button2Mask | Button3Mask)) {
return 1;
}
}
return 0;
}
void get_cursor_pos(Display *display, Window root, int *x_out, int *y_out) {
Window root_ret, child_ret;
int root_x, root_y, win_x, win_y;
unsigned int mask;
if (XQueryPointer(display, root, &root_ret, &child_ret, &root_x, &root_y, &win_x, &win_y, &mask)) {
if (x_out) *x_out = root_x;
if (y_out) *y_out = root_y;
}
}
static void* raise_zenity_topmost(void *arg) {
usleep(150000); // Dar 150ms a Zenity para crear la ventana
Display *display = XOpenDisplay(NULL);
if (!display) return NULL;
Window root = DefaultRootWindow(display);
Atom net_client_list = XInternAtom(display, "_NET_CLIENT_LIST", True);
Atom net_wm_state = XInternAtom(display, "_NET_WM_STATE", False);
Atom net_wm_state_above = XInternAtom(display, "_NET_WM_STATE_ABOVE", False);
Atom net_wm_state_modal = XInternAtom(display, "_NET_WM_STATE_MODAL", False);
for (int retry = 0; retry < 15; retry++) {
Atom type;
int format;
unsigned long nitems, bytes_after;
unsigned char *prop = NULL;
if (XGetWindowProperty(display, root, net_client_list, 0, 1024, False, XA_WINDOW,
&type, &format, &nitems, &bytes_after, &prop) == Success && prop) {
Window *windows = (Window *)prop;
for (unsigned long i = 0; i < nitems; i++) {
char *name = NULL;
if (XFetchName(display, windows[i], &name) && name) {
if (strstr(name, "IdleGuard")) {
XEvent xev = {0};
xev.type = ClientMessage;
xev.xclient.window = windows[i];
xev.xclient.message_type = net_wm_state;
xev.xclient.format = 32;
xev.xclient.data.l[0] = 1; // _NET_WM_STATE_ADD
xev.xclient.data.l[1] = net_wm_state_above;
xev.xclient.data.l[2] = net_wm_state_modal;
xev.xclient.data.l[3] = 1;
XSendEvent(display, root, False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
XRaiseWindow(display, windows[i]);
XSetInputFocus(display, windows[i], RevertToParent, CurrentTime);
XFlush(display);
XFree(name);
XFree(prop);
XCloseDisplay(display);
return NULL;
}
XFree(name);
}
}
XFree(prop);
}
usleep(50000);
}
XCloseDisplay(display);
return NULL;
}
int select_mode_dialog() {
pthread_t thread;
pthread_create(&thread, NULL, raise_zenity_topmost, NULL);
pthread_detach(thread);
FILE *fp = popen("zenity --question --modal --title='IdleGuard - Merke Software' "
"--window-icon=/home/perry/Desarrollo/mouse/icon.png "
"--width=620 --height=350 "
"--text='<b>Selecciona el modo de funcionamiento de IdleGuard:</b>\n\n"
"<b>1. Modo Giratorio</b>\n"
" Movimiento circular continuo alrededor del clic.\n"
" <i>(Se detiene haciendo Clic o presionando la tecla ESC)</i>\n\n"
"<b>2. Modo Inactividad (Segundo Plano)</b>\n"
" Micro-movimiento sutil tras 15 segundos inactivo.\n"
" <i>(Se detiene únicamente presionando la tecla ESC)</i>' "
"--ok-label='1. Modo Giratorio' "
"--extra-button='2. Modo Inactividad' "
"--cancel-label='Cancelar' 2>/dev/null", "r");
if (!fp) return 0;
char buffer[64] = {0};
char *res = fgets(buffer, sizeof(buffer), fp);
int status = pclose(fp);
if (res && strstr(buffer, "2. Modo Inactividad")) {
return 2;
}
if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
return 1;
}
return 0;
}
int sleep_check_esc(Display *display, KeyCode esc_code, int ms) {
for (int i = 0; i < ms / 10; i++) {
if (is_esc_pressed(display, esc_code)) return 1;
usleep(10000);
}
return 0;
}
int main() {
// 1. Desvincular de la terminal como primera instrucción
if (daemon(1, 0) != 0) {
int devnull = open("/dev/null", O_RDWR);
if (devnull != -1) {
dup2(devnull, STDIN_FILENO);
dup2(devnull, STDOUT_FILENO);
dup2(devnull, STDERR_FILENO);
close(devnull);
}
}
// 2. Diálogo modal sin ventana de consola de fondo
int mode = select_mode_dialog();
if (mode == 0) {
return 0;
}
Display *display = XOpenDisplay(NULL);
if (!display) {
return 1;
}
Window root = DefaultRootWindow(display);
KeyCode esc_code = XKeysymToKeycode(display, XK_Escape);
if (mode == 1) {
// MODO 1: GIRATORIO
system("notify-send 'IdleGuard' 'Modo Giratorio: Haz clic en la pantalla para tomar la posición inicial.' -i /home/perry/Desarrollo/mouse/icon.png >/dev/null 2>&1 &");
usleep(250000);
int click_x = 0, click_y = 0;
while (1) {
if (is_esc_pressed(display, esc_code)) {
XCloseDisplay(display);
return 0;
}
if (is_mouse_clicked(display, root, &click_x, &click_y)) {
break;
}
usleep(15000);
}
while (is_mouse_clicked(display, root, NULL, NULL)) {
usleep(20000);
}
usleep(100000);
system("notify-send 'IdleGuard' 'Girando... Haz clic o presiona ESC para detener.' -i /home/perry/Desarrollo/mouse/icon.png >/dev/null 2>&1 &");
int radius = 100;
int steps = 120;
useconds_t interval = 25000;
while (1) {
for (int step = 0; step < steps; step++) {
if (is_esc_pressed(display, esc_code) || is_mouse_clicked(display, root, NULL, NULL)) {
goto cleanup;
}
double angle = step * 2.0 * M_PI / steps;
int x = (int)(click_x + radius * cos(angle));
int y = (int)(click_y + radius * sin(angle));
XWarpPointer(display, None, root, 0, 0, 0, 0, x, y);
XFlush(display);
usleep(interval);
}
}
} else if (mode == 2) {
// MODO 2: INACTIVIDAD (SEGUNDO PLANO)
system("notify-send 'IdleGuard' 'Modo Inactividad activo. Micro-movimiento en inactividad (Detener con ESC).' -i /home/perry/Desarrollo/mouse/icon.png >/dev/null 2>&1 &");
usleep(250000);
int last_x = -1, last_y = -1;
int idle_seconds = 0;
while (1) {
if (is_esc_pressed(display, esc_code)) goto cleanup;
int cur_x = 0, cur_y = 0;
get_cursor_pos(display, root, &cur_x, &cur_y);
if (cur_x != last_x || cur_y != last_y) {
last_x = cur_x;
last_y = cur_y;
idle_seconds = 0;
} else {
idle_seconds++;
}
if (idle_seconds >= 15) {
XWarpPointer(display, None, root, 0, 0, 0, 0, cur_x + 1, cur_y);
XFlush(display);
if (sleep_check_esc(display, esc_code, 50)) goto cleanup;
XWarpPointer(display, None, root, 0, 0, 0, 0, cur_x, cur_y);
XFlush(display);
get_cursor_pos(display, root, &last_x, &last_y);
idle_seconds = 0;
}
if (sleep_check_esc(display, esc_code, 1000)) goto cleanup;
}
}
cleanup:
system("notify-send 'IdleGuard' 'Movimiento detenido.' -i /home/perry/Desarrollo/mouse/icon.png >/dev/null 2>&1 &");
XCloseDisplay(display);
return 0;
}