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
|
#define _XOPEN_SOURCE 700
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <X11/extensions/Xrandr.h>
#define ACPID_SOCK_PATH "/var/run/acpid.socket"
/* once to go into tablet mode, twice to come out */
#define ACPI_ROTATE "ibm/hotkey LEN0068:00 00000080 000060c0\n"
/* once on keydown, once on keyup */
#define ACPI_ROTATELOCK "ibm/hotkey LEN0068:00 00000080 00006020\n"
#define FIXME_HARDCODED_DEV_NUMBER 4
#define TOUCH_DEVICE "SYNAPTICS Synaptics Touch Digitizer V04"
#define GRAVITY_CUTOFF (7.0)
#define DEV_PATH "/sys/bus/iio/devices/iio:device%d"
#define SCALE_FILE "in_accel_scale"
#define X_RAW_FILE "in_accel_x_raw"
#define Y_RAW_FILE "in_accel_y_raw"
int tabletmode = 0;
int rotatelock = 0;
enum rotation { NORMAL, INVERSE, LEFT, RIGHT };
void rotatescreen(enum rotation r)
{
Rotation xr[] = { RR_Rotate_0, RR_Rotate_180, RR_Rotate_90, RR_Rotate_270 };
static Display *dpy = NULL;
if (dpy == NULL) {
dpy = XOpenDisplay(NULL);
}
static Window root = 0;
if (root == 0) {
root = RootWindow(dpy, DefaultScreen(dpy));
}
XRRScreenConfiguration *xsc = XRRGetScreenInfo(dpy, root);
XRRSetScreenConfig(dpy, xsc, root, 0, xr[r], CurrentTime);
}
void rotatetouch(enum rotation r)
{
char *matrix[] = {
"1 0 0 0 1 0 0 0 1",
"-1 0 1 0 -1 1 0 0 1",
"0 -1 1 1 0 0 0 0 1",
"0 1 0 -1 0 1 0 0 1",
};
char cmd[256];
sprintf(cmd, "xinput set-float-prop '%s' 'Coordinate Transformation Matrix' %s", TOUCH_DEVICE, matrix[r]);
system(cmd);
}
void rotatewacom(enum rotation r)
{
char *rotations[] = { "none", "half", "ccw", "cw" };
char cmd[512];
sprintf(cmd, "for dev in $(xsetwacom --list devices | cut -b 38-40); do xsetwacom set \"$dev\" rotate %s; done", rotations[r]);
system(cmd);
}
enum rotation setrotation(enum rotation r)
{
static enum rotation prev = NORMAL;
if (r == prev) {
return r;
}
rotatescreen(r);
/* Allow xinput to relocate devices */
sleep(1);
rotatetouch(r);
rotatewacom(r);
prev = r;
return r;
}
int openacpi(const char *path)
{
struct sockaddr_un sun = {0};
int sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock == -1) {
return -1;
}
sun.sun_family = AF_UNIX;
strcpy(sun.sun_path, path ? path : ACPID_SOCK_PATH);
if (connect(sock, (const struct sockaddr*)&sun, sizeof(sun)) == -1) {
close(sock);
return -1;
}
return sock;
}
void checkacpi(int acpi)
{
static int nrotate = 0;
static int nlock = 0;
char buf[BUFSIZ];
ssize_t n = read(acpi, buf, sizeof(buf));
if (n <= 0) {
return;
}
if (!strcmp(buf, ACPI_ROTATE)) {
nrotate++;
if (nrotate == 1) {
tabletmode = 1;
/* disable touchpad */
/* disable trackpoint */
} else if (nrotate == 3) {
/* enable touchpad */
/* enable trackpoint */
setrotation(NORMAL);
tabletmode = 0;
nrotate = 0;
}
} else if (!strcmp(buf, ACPI_ROTATELOCK)) {
nlock++;
if (nlock == 2) {
nlock = 0;
rotatelock = ! rotatelock;
}
}
}
FILE *opendevfile(const char *file)
{
static char devdir[sizeof(DEV_PATH) + 5] = { '\0' };
char *path = NULL;
if (devdir[0] == '\0') {
sprintf(devdir, DEV_PATH, FIXME_HARDCODED_DEV_NUMBER);
}
path = malloc(strlen(devdir) + strlen(file) + 4);
sprintf(path, "%s/%s", devdir, file);
FILE *dev = fopen(path, "r");
free(path);
return dev;
}
double getgravity(double cutoff)
{
FILE *scale = opendevfile(SCALE_FILE);
if (scale == NULL) {
return cutoff;
}
double scalefactor;
fscanf(scale, "%lf", &scalefactor);
fclose(scale);
return cutoff / scalefactor;
}
double getraw(const char *file)
{
double value = 0.0;
FILE *raw = opendevfile(file);
if (raw) {
fscanf(raw, "%lg", &value);
fclose(raw);
}
return value;
}
int main(void)
{
double gravity = getgravity(GRAVITY_CUTOFF);
int acpi = openacpi(NULL);
if (acpi == -1) {
tabletmode = 1;
rotatelock = 0;
}
for (;;) {
int nfds = acpi + 1;
fd_set fds;
FD_ZERO(&fds);
struct timeval to = {0};
to.tv_usec = 5000;
int forever = 0;
if (acpi != -1) {
FD_SET(acpi, &fds);
if (tabletmode == 0 || rotatelock == 1) {
forever = 1;
}
}
select(nfds, &fds, NULL, NULL, forever ? NULL : &to);
if (acpi != -1 && FD_ISSET(acpi, &fds)) {
checkacpi(acpi);
}
if (tabletmode == 1 && rotatelock == 0) {
double x = getraw(X_RAW_FILE);
double y = getraw(Y_RAW_FILE);
if (y <= -gravity) {
setrotation(NORMAL);
} else if (y >= gravity) {
setrotation(INVERSE);
} else if (x >= gravity) {
setrotation(LEFT);
} else if (x <= -gravity) {
setrotation(RIGHT);
}
}
}
return 1;
}
|