-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotate.cpp
377 lines (297 loc) · 8.23 KB
/
rotate.cpp
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xrandr.h>
#include <X11/extensions/XInput.h>
#include <gio/gio.h>
#include <gtk/gtk.h>
//typedef double TFLOAT;
union TFLOAT {
float scalar;
long padding;
};
namespace config {
// devices to do things with
static const char *XDevs [] = {
"MSFT0001:01 06CB:7F27 Touchpad",
"Wacom HID 517E Pen stylus",
"Wacom HID 517E Finger touch",
"Wacom HID 517E Pen eraser"
};
// https://wiki.ubuntu.com/X/InputCoordinateTransformation
// i have to admit, i did actually just transcribe these from the page
// i dont fully understand the scheme :(
static const TFLOAT Transform [4] [9] = {
{ // NORMAL
1, 0, 0,
0, 1, 0,
0, 0, 1
}, { // LEFT
0, -1, 1,
1, 0, 0,
0, 0, 1
}, { // DOWN
-1, 0, 1,
0, -1, 1,
0, 0, 1
}, { // RIGHT
0, 1, 0,
-1, 0, 1,
0, 0, 1
}
};
};
#define TDEVS (sizeof (config::XDevs) / sizeof (config::XDevs [0]))
namespace all {
static void setup (void);
static void enable (void);
static void disable (void);
static void rot (Rotation dir);
}
static bool on = false;
namespace x11 {
static Display *disp;
namespace scr {
int default_screen;
Window root;
static void setup (void) {
default_screen = DefaultScreen (disp);
root = RootWindow (disp, default_screen);
}
static void rot (Rotation dir) {
XSync (disp, false);
XRRScreenConfiguration *config = XRRGetScreenInfo (disp, root);
Rotation currot;
int sz = XRRConfigCurrentConfiguration (config, &currot);
XRRSetScreenConfig (disp, config, root, sz, dir, CurrentTime);
XRRFreeScreenConfigInfo (config);
}
}
namespace touch {
static XDevice *tdev [TDEVS] = { NULL };
static Atom transform_property;
static Atom float_type;
static void setup (void) {
int devices_n;
XDeviceInfo *devices = XListInputDevices (disp, &devices_n);
for (int i = 0; i != devices_n; i ++) {
for (int j = 0; j != TDEVS; j ++) {
if (!strcmp (devices [i].name, config::XDevs [j])) {
tdev [j] = XOpenDevice (disp, devices [i].id);
if (tdev [j] == NULL)
fprintf (stderr, "Can't open device %s\n", config::XDevs [j]);
else
fprintf (stderr, "Opened device %s\n", config::XDevs [j]);
}
}
}
float_type = XInternAtom (disp, "FLOAT", false);
transform_property = XInternAtom (disp, "Coordinate Transformation Matrix", false);
XFreeDeviceList (devices);
}
static void rot (Rotation dir) {
const TFLOAT *trans; // 🏳️⚧️
switch (dir) {
case RR_Rotate_0: trans = &config::Transform [0] [0];
break;
case RR_Rotate_90: trans = &config::Transform [1] [0];
break;
case RR_Rotate_180: trans = &config::Transform [2] [0];
break;
case RR_Rotate_270: trans = &config::Transform [3] [0];
break;
default: exit (127);
break;
}
for (int i = 0; i != TDEVS; i ++) {
if (tdev [i] != NULL) {
XChangeDeviceProperty (
disp, tdev [i],
transform_property, float_type, 32,
PropModeReplace,
(const unsigned char *) trans, 9
);
}
}
}
}
static void enable (void) { return; }
static void disable (void) { return; }
static void setup (void) {
disp = XOpenDisplay (getenv ("DISPLAY"));
scr::setup (); touch::setup ();
XSync (disp, false);
}
static void rot (Rotation dir) {
scr::rot (dir);
touch::rot (dir);
XSync (disp, false);
}
}
namespace motion {
static GDBusProxy *proxy;
namespace callback {
static void update (
GDBusProxy *proxy,
GVariant *changed, GStrv invalidated,
gpointer data
) {
GVariantDict dict;
g_variant_dict_init (&dict, changed);
if (g_variant_dict_contains (&dict, "AccelerometerOrientation")) {
GVariant *variant = g_dbus_proxy_get_cached_property (proxy, "AccelerometerOrientation");
const gchar *dir = g_variant_get_string (variant, NULL);
Rotation rot;
switch (dir[0]) {
case 'n': //normal
rot = RR_Rotate_0;
break;
case 'l': //left-up
rot = RR_Rotate_90;
break;
case 'b': //bottom-up
rot = RR_Rotate_0;
break;
case 'r': //right-up
rot = RR_Rotate_270;
break;
default:
exit (128);
break;
}
all::rot (rot);
g_variant_unref (variant);
}
}
}
static void enable (void) {
proxy = g_dbus_proxy_new_for_bus_sync (
G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE,
NULL,
"net.hadess.SensorProxy", "/net/hadess/SensorProxy",
"net.hadess.SensorProxy",
NULL, NULL
);
g_signal_connect (
G_OBJECT (proxy), "g-properties-changed",
G_CALLBACK (callback::update), NULL
);
g_dbus_proxy_call_sync (
proxy,
"ClaimAccelerometer",
NULL, G_DBUS_CALL_FLAGS_NONE,
-1, NULL, NULL
);
}
static void disable (void) {
g_object_unref (proxy); proxy = NULL;
}
static void setup (void) { return; }
}
namespace icon {
static GtkStatusIcon *icon;
static GtkMenu *menu;
namespace callback {
static void toggle (GtkMenuItem *self, gpointer dat) {
if (on)
all::disable ();
else
all::enable ();
}
static void popup (GtkStatusIcon *self, guint btn, guint time, gpointer dat) {
gtk_menu_popup (
menu,
NULL, NULL,
NULL, NULL,
btn, time
);
}
static void r_up (GtkMenuItem *self, gpointer dat) { all::rot (RR_Rotate_0); }
static void r_down (GtkMenuItem *self, gpointer dat) { all::rot (RR_Rotate_180); }
static void r_left (GtkMenuItem *self, gpointer dat) { all::rot (RR_Rotate_90); }
static void r_right (GtkMenuItem *self, gpointer dat) { all::rot (RR_Rotate_270); }
}
static void enable (void) {
gtk_status_icon_set_from_icon_name (icon, "changes-allow");
gtk_status_icon_set_tooltip_text (icon, "Rotation unlocked");
}
static void disable (void) {
gtk_status_icon_set_from_icon_name (icon, "changes-prevent");
gtk_status_icon_set_tooltip_text (icon, "Rotation locked");
}
static void append_radio_item (const gchar *text, GCallback callback) {\
static GSList *radio_group = NULL;
GtkWidget *item = gtk_radio_menu_item_new_with_label (radio_group, text);
radio_group = gtk_radio_menu_item_group (GTK_RADIO_MENU_ITEM (item));
g_signal_connect (
G_OBJECT (item), "activate",
callback, NULL
);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
gtk_widget_show_all (item);
}
static void setup (void) {
// set up icon
icon = gtk_status_icon_new ();
gtk_status_icon_set_title (icon, "Rotation control");
// set up popup menu
menu = GTK_MENU (gtk_menu_new ());
gtk_menu_set_title (menu, "Rotate");
gtk_menu_shell_set_take_focus (GTK_MENU_SHELL (menu), true);
// check box!
{ GtkWidget *item = gtk_check_menu_item_new_with_label ("Automatic rotation");
g_signal_connect (
G_OBJECT (item), "activate",
G_CALLBACK (callback::toggle), NULL
);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
// gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), true); // ???
gtk_widget_show_all (item);
}
// separator
{ GtkWidget *item = gtk_separator_menu_item_new (); // ???
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
gtk_widget_show_all (item);
}
// radio menu items
append_radio_item ("Normal", G_CALLBACK (callback::r_up));
append_radio_item ("Left", G_CALLBACK (callback::r_left));
append_radio_item ("Right", G_CALLBACK (callback::r_right));
append_radio_item ("Down", G_CALLBACK (callback::r_down));
// open popup menu on click
g_signal_connect (
G_OBJECT (icon), "button-press-event",
G_CALLBACK (callback::popup), NULL
);
gtk_status_icon_set_visible (icon, TRUE);
}
}
static void all::setup (void) {
icon::setup ();
motion::setup ();
x11::setup ();
}
static void all::enable (void) {
on = true;
icon::enable ();
motion::enable ();
x11::enable ();
}
static void all::disable (void) {
on = false;
icon::disable ();
motion::disable ();
x11::disable ();
}
static void all::rot (Rotation dir) {
x11::rot (dir);
}
int main (int argc, char *argv []) {
if (sizeof (float) != 4)
puts ("WARN: sizeof (float) != 4");
gtk_init (&argc, &argv);
all::setup ();
all::disable ();
GMainLoop *loop = g_main_loop_new (NULL, TRUE);
g_main_loop_run (loop);
return 0;
}