forked from akissu/2in1screen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2in1screen.c
executable file
·234 lines (202 loc) · 5.12 KB
/
2in1screen.c
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
/*
mkdir -p ./bin \
&& gcc -O2 -o ./bin/2in1screen 2in1screen.c \
&& chown root:root ./bin/2in1screen \
&& chmod 0755 ./bin/2in1screen \
&& sudo mv ./bin/2in1screen /usr/bin/
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define DATA_SIZE 256
#define N_STATE 4
char basedir[DATA_SIZE];
char *basedir_end = NULL;
char content[DATA_SIZE];
char command[DATA_SIZE*4];
char output[DATA_SIZE] = "DSI1";
char *ROT[] =
{
"normal",
"inverted",
"right",
"left"
};
// 0
// 1
// 2
// 3
char *COOR[] =
{
"1 0 0 0 1 0 0 0 1",
"-1 0 1 0 -1 1 0 0 1",
"0 1 0 -1 0 1 0 0 1",
"0 -1 1 1 0 0 0 0 1"
};
char *ROT_WACOM[] =
{
"none",
"half",
"cw",
"ccw"
};
char *SCROLL[] =
{
"4 5 6 7",
"5 4 7 6",
"6 7 5 4",
"7 6 4 5"
};
// 3
// 2
// 0
// 1
char *COOR_TOUCHPAD[] =
{
"0 -1 1 1 0 0 0 0 1",
"0 1 0 -1 0 1 0 0 1",
"1 0 0 0 1 0 0 0 1",
"-1 0 1 0 -1 1 0 0 1"
};
//char *SCROLL_TOUCHPAD[] =
// {
// "7 6 4 5"
// "6 7 5 4",
// "4 5 6 7",
// "5 4 7 6",
// };
double accel_y = 0.0;
//
#if N_STATE == 4
double accel_x = 0.0;
#endif
double accel_gx = 2.0;
double accel_gy = 6.0;
double thrshld = 8.0;
int current_state = 0;
int rotation_changed(){
int state = 0;
fprintf(stdout, "x %f ", accel_x);
fprintf(stdout, "gx %f ", accel_gx);
fprintf(stdout, "y %f ", accel_y);
fprintf(stdout, "gy %f ", accel_gy);
if(-1*thrshld < accel_x && accel_x < thrshld && -1*thrshld < accel_y && accel_y < thrshld)
{
// change direction only if i want to
fprintf(stdout, "not changed\n");
return 0;
}
if(accel_y < -accel_gy)
{
state = 0;
}
else if(accel_y > accel_gy)
{
state = 1;
}
#if N_STATE == 4
else if(accel_x > accel_gx)
{
state = 2;
}
else if(accel_x < -accel_gx)
{
state = 3;
}
fprintf(stdout, "state %i\n", state);
#endif
if(current_state!=state)
{
current_state = state;
return 1;
}
else return 0;
}
FILE* bdopen(char const *fname, char leave_open){
*basedir_end = '/';
strcpy(basedir_end+1, fname);
FILE *fin = fopen(basedir, "r");
setvbuf(fin, NULL, _IONBF, 0);
fgets(content, DATA_SIZE, fin);
*basedir_end = '\0';
if(leave_open==0)
{
fclose(fin);
return NULL;
}
else return fin;
}
void rotate_screen(){
// ROTATE DISPLAY
sprintf(command, "xrandr --output %s --rotate %s", output, ROT[current_state]);
system(command);
// ADJUST SCROLL TOUCH SCREEN
// for libinput and mtrack
// sprintf(command, "xinput set-prop \"%s\" --type=int \"Trackpad Scroll Buttons\" %s", "GXTP7380:00 27C6:0113", SCROLL[current_state]);
// system(command);
// ADJUST TRANSFORM MATRICES/ROTATIONS
// // libinput
// sprintf(command, "xinput set-prop \"%s\" --type=float \"Coordinate Transformation Matrix\" %s", "GXTP7380:00 27C6:0113 touch", COOR[current_state]);
// sprintf(command, "xinput set-prop \"%s\" --type=float \"Coordinate Transformation Matrix\" %s", "GXTP7380:00 27C6:0113 Stylus stylus", COOR[current_state]);
// sprintf(command, "xinput set-prop \"%s\" --type=float \"Coordinate Transformation Matrix\" %s", "GXTP7380:00 27C6:0113 Stylus eraser", COOR[current_state]);
// libwacom
sprintf(command, "xsetwacom set \"%s\" \"Rotate\" %s", "GXTP7380:00 27C6:0113 touch", ROT_WACOM[current_state]);
system(command);
sprintf(command, "xsetwacom set \"%s\" \"Rotate\" %s", "GXTP7380:00 27C6:0113 Stylus stylus", ROT_WACOM[current_state]);
system(command);
sprintf(command, "xsetwacom set \"%s\" \"Rotate\" %s", "GXTP7380:00 27C6:0113 Stylus eraser", ROT_WACOM[current_state]);
system(command);
sleep(1.0); // needed, otherwise the mapping happens too early, resulting in a wrong one
sprintf(command, "xsetwacom set \"%s\" \"MapToOutput\" %s", "GXTP7380:00 27C6:0113 touch", output);
system(command);
sprintf(command, "xsetwacom set \"%s\" \"MapToOutput\" %s", "GXTP7380:00 27C6:0113 Stylus stylus", output);
system(command);
sprintf(command, "xsetwacom set \"%s\" \"MapToOutput\" %s", "GXTP7380:00 27C6:0113 Stylus eraser", output);
system(command);
// ADJUST TRANSFORM MATRIX TOUCHPAD
// libinput (only?)
sprintf(command, "xinput set-prop \"%s\" --type=float \"Coordinate Transformation Matrix\" %s", "HAILUCK CO.,LTD USB KEYBOARD Mouse", COOR_TOUCHPAD[current_state]);
system(command);
}
int main(int argc, char const *argv[]) {
FILE *pf = popen("ls /sys/bus/iio/devices/iio:device*/in_accel*", "r");
if(!pf)
{
fprintf(stderr, "IO Error.\n");
return 2;
}
if(fgets(basedir, DATA_SIZE , pf)!=NULL)
{
basedir_end = strrchr(basedir, '/');
if(basedir_end) *basedir_end = '\0';
fprintf(stderr, "Accelerometer: %s\n", basedir);
}
else
{
fprintf(stderr, "Unable to find any accelerometer.\n");
return 1;
}
pclose(pf);
bdopen("in_accel_scale", 0);
double scale = atof(content);
FILE *dev_accel_y = bdopen("in_accel_y_raw", 1);
#if N_STATE == 4
FILE *dev_accel_x = bdopen("in_accel_x_raw", 1);
#endif
while(1)
{
fseek(dev_accel_y, 0, SEEK_SET);
fgets(content, DATA_SIZE, dev_accel_y);
accel_y = atof(content) * scale;
#if N_STATE == 4
fseek(dev_accel_x, 0, SEEK_SET);
fgets(content, DATA_SIZE, dev_accel_x);
accel_x = atof(content) * scale;
#endif
if(rotation_changed())
rotate_screen();
sleep(2);
}
return 0;
}