Debian Codegraph @

recorder/src/record.c

raw
1 /* X11::GUITest ($Id: record.c 253 2026-08-19 22:37:51Z ctrondlp $)
2 *
3 * Copyright (c) 2003-2026 Dennis K. Paulsen, All Rights Reserved.
4 * Email: [email protected]
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses>.
18 *
19 */
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <signal.h>
24 #include <unistd.h>
25 #include <sys/time.h>
26 #include <X11/X.h>
27 #include <X11/XKBlib.h>
28 #include <X11/Xproto.h>
29 #include <X11/extensions/record.h>
30 #include <X11/keysym.h>
31 #include <X11/Xutil.h>
32 #include <X11/Intrinsic.h>
33 #include <X11/StringDefs.h>
34 #include "record_event.h"
35 #include "record.h"
36 #include "Common.h"
37
38 static BOOL shouldExit = FALSE;
39 static struct timeval lastTime = {0};
40 static struct timeval currentTime = {0};
41 static void (*HandleEvent)(struct record_event ev);
42 static XRecordContext rcon;
43 static Display *otherDisp = NULL;
44 static Display *disp = NULL;
45
46
47 int RecordEvents(void (*handleEvent)(struct record_event ev))
48 {
49 XRecordClientSpec rcspec = {0};
50 XRecordRange *xrr = NULL;
51 int major = 0, minor = 0;
52
53 HandleEvent = handleEvent; // Register it
54
55 signal(SIGINT, sigint_handler);
56
57 SetLastTime();
58
59 disp = XOpenDisplay(NULL);
60 if (disp == NULL) {
61 fprintf(stderr, _("Unable to open display connection.\n"));
62 return 1;
63 }
64 XSynchronize(disp, False);
65
66 // Ensure extension available
67 if (!XRecordQueryVersion(disp, &major, &minor)) {
68 fprintf(stderr, _("The record extension is unavailable.\n"));
69 return 1;
70 }
71
72 xrr = XRecordAllocRange();
73 if (xrr == NULL) {
74 fprintf(stderr, _("Range allocation failed.\n"));
75 return 1;
76 }
77
78 xrr->device_events.first = KeyPress;
79 xrr->device_events.last = MotionNotify;
80 rcspec = XRecordAllClients;
81
82 rcon = XRecordCreateContext(disp, 0, &rcspec, 1, &xrr, 1);
83 if (!rcon) {
84 fprintf(stderr, _("Unable to create context.\n"));
85 return 1;
86 }
87
88 otherDisp = XOpenDisplay(NULL);
89 if (otherDisp == NULL) {
90 fprintf(stderr, _("Unable to open other display connection.\n"));
91 return 1;
92 }
93
94 // Clean out X events in progress
95 XFlush(disp);
96 XFlush(otherDisp);
97
98 // Record...
99 if (!XRecordEnableContext(otherDisp, rcon, EventCallback, (XPointer)disp)) {
100 fprintf(stderr, _("Enable context failed\n"));
101 return 1;
102 }
103 // ...until StopRecording() is called.
104
105 XFree(xrr);
106 return 0;
107 }
108
109 void StopRecording(void)
110 {
111 shouldExit = TRUE;
112 XRecordDisableContext(disp, rcon);
113 XRecordFreeContext(disp, rcon);
114 //XCloseDisplay(otherDisp); // Note: N/A, blocks indefinitely
115 XCloseDisplay(disp);
116 }
117
118 void SetLastTime(void)
119 {
120 if (gettimeofday(&lastTime, NULL) != 0) {
121 fprintf(stderr, _("unable to get time\n"));
122 }
123 }
124
125 void SetCurrentTime(void)
126 {
127 if (gettimeofday(&currentTime, NULL) != 0) {
128 fprintf(stderr, _("unable to get time\n"));
129 }
130 }
131
132 long GetDelay(void)
133 {
134 long secDiff = 0;
135 long usecDiff = 0;
136 long final = 0;
137
138 SetCurrentTime();
139 /* Get delay between the previous event and this one */
140 secDiff = (currentTime.tv_sec - lastTime.tv_sec);
141 usecDiff = ((currentTime.tv_usec - lastTime.tv_usec) / 1000);
142 final = ((secDiff * 1000) + usecDiff);
143 SetLastTime();
144
145 return final;
146 }
147
148 void EventCallback(XPointer p, XRecordInterceptData *idata)
149 {
150 if (shouldExit) {
151 return;
152 }
153
154 if (XRecordFromServer == idata->category) {
155 Display *disp = (Display *)p;
156 xEvent *xev = (xEvent *)idata->data;
157 int type = xev->u.u.type;
158 int keyPress = 0;
159 struct record_event re = {0};
160
161 re.delay = GetDelay();
162 re.type = NOTYPE;
163 re.state = NOSTATE;
164 re.dataname = NULL;
165 re.data = 0;
166
167 switch (type) {
168 case ButtonPress:
169 re.type = MOUSEBUTTON;
170 re.state = DOWN;
171 re.data = xev->u.u.detail;
172 break;
173 case ButtonRelease:
174 re.type = MOUSEBUTTON;
175 re.state = UP;
176 re.data = xev->u.u.detail;
177 break;
178 case KeyPress:
179 keyPress = 1;
180 case KeyRelease:
181 {
182 //printf("key code: %d\n", xev->u.u.detail);
183 KeyCode kc = xev->u.u.detail;
184 KeySym ks = XkbKeycodeToKeysym(disp, kc, 0, 0);
185 re.dataname = XKeysymToString(ks);
186 re.data = ks;
187 re.type = KEY;
188 re.state = (keyPress == 1) ? DOWN : UP;
189 }
190 break;
191 case MotionNotify:
192 {
193 re.type = MOUSEMOVE;
194 re.posX = xev->u.keyButtonPointer.rootX;
195 re.posY = xev->u.keyButtonPointer.rootY;
196 }
197 break;
198 case EnterNotify:
199 case LeaveNotify:
200 default:
201 break;
202 }
203 ////
204 HandleEvent(re);
205 ////
206 }
207 if (idata != NULL) {
208 XRecordFreeData(idata);
209 }
210 }
211
212 void sigint_handler(int sig)
213 {
214 StopRecording();
215 }
216