forked from okeri/iio-sway
-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.c
272 lines (242 loc) · 10.5 KB
/
main.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
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
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <dbus/dbus.h>
// corresponds to hyprctl orientation integers
enum Orientation { Normal, LeftUp, BottomUp, RightUp, Undefined};
DBusError error;
char* output = "eDP-1"; // Default output device
int rotate_master_layout = 0; // Default layout
void dbus_disconnect(DBusConnection* connection) {
dbus_connection_flush(connection);
dbus_connection_close(connection);
dbus_connection_unref(connection);
dbus_error_free(&error);
}
DBusConnection* dbus_connect(void) {
dbus_error_init(&error);
DBusConnection* connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
if (connection != NULL) {
DBusMessage* msg = dbus_message_new_method_call(
"net.hadess.SensorProxy", "/net/hadess/SensorProxy",
"net.hadess.SensorProxy", "ClaimAccelerometer");
char ok = dbus_connection_send_with_reply_and_block(
connection, msg, DBUS_TIMEOUT_INFINITE, &error) != NULL;
dbus_message_unref(msg);
if (ok) {
return connection;
}
}
return NULL;
}
enum Orientation property_to_enum(const char* orientation) {
if (!strcmp(orientation, "normal")) {
return Normal;
}
if (!strcmp(orientation, "bottom-up")) {
return BottomUp;
}
if (!strcmp(orientation, "left-up")) {
return LeftUp;
}
if (!strcmp(orientation, "right-up")) {
return RightUp;
}
return Undefined;
}
enum Orientation parse_orientation_signal(DBusMessage* msg) {
DBusMessageIter args, iter_array, iter_dict, iter_v;
const char *iface, *property, *orientation;
if (dbus_message_iter_init(msg, &args)) {
dbus_message_iter_get_basic(&args, &iface);
if (!strcmp("net.hadess.SensorProxy", iface)) {
dbus_message_iter_next(&args);
dbus_message_iter_recurse(&args, &iter_array);
dbus_message_iter_recurse(&iter_array, &iter_dict);
dbus_message_iter_get_basic(&iter_dict, &property);
if (!strcmp(property, "AccelerometerOrientation")) {
dbus_message_iter_next(&iter_dict);
dbus_message_iter_recurse(&iter_dict, &iter_v);
dbus_message_iter_get_basic(&iter_v, &orientation);
return property_to_enum(orientation);
}
}
}
return Undefined;
}
void system_fmt(char* format, ...) {
char command[420];
va_list args;
va_start(args, format);
vsnprintf(command, sizeof(command), format, args);
system(command);
va_end(args);
}
void handle_orientation(enum Orientation orientation, const char* monitor_id) {
if (orientation == Undefined)
return;
// Ran if the --either --left-master or --right-master is pass in
// (pray that our lord and savior vaxry won't change hyprctl output)
if (rotate_master_layout == 1) {
if (orientation == Normal) { // --left-master flag
system_fmt("hyprctl --batch \"keyword monitor %s,transform,%d ; keyword input:touchdevice:transform %d ; keyword input:tablet:transform %d ; keyword workspace m[%s], layoutopt:orientation:left\"", output, orientation, orientation, orientation, monitor_id);
}
else if (orientation == LeftUp) {
system_fmt("hyprctl --batch \"keyword monitor %s,transform,%d ; keyword input:touchdevice:transform %d ; keyword input:tablet:transform %d ; keyword workspace m[%s], layoutopt:orientation:top\"", output, orientation, orientation, orientation, monitor_id);
}
else if (orientation == BottomUp) {
system_fmt("hyprctl --batch \"keyword monitor %s,transform,%d ; keyword input:touchdevice:transform %d ; keyword input:tablet:transform %d ; keyword workspace m[%s], layoutopt:orientation:left\"", output, orientation, orientation, orientation, monitor_id);
}
else { // This covers RightUp orientation
system_fmt("hyprctl --batch \"keyword monitor %s,transform,%d ; keyword input:touchdevice:transform %d ; keyword input:tablet:transform %d ; keyword workspace m[%s], layoutopt:orientation:top\"", output, orientation, orientation, orientation, monitor_id);
}
}
else if (rotate_master_layout == 2) { // --right-master flag
if (orientation == Normal) {
system_fmt("hyprctl --batch \"keyword monitor %s,transform,%d ; keyword input:touchdevice:transform %d ; keyword input:tablet:transform %d ; keyword workspace m[%s], layoutopt:orientation:right\"", output, orientation, orientation, orientation, monitor_id);
}
else if (orientation == LeftUp) {
system_fmt("hyprctl --batch \"keyword monitor %s,transform,%d ; keyword input:touchdevice:transform %d ; keyword input:tablet:transform %d ; keyword workspace m[%s], layoutopt:orientation:bottom\"", output, orientation, orientation, orientation, monitor_id);
}
else if (orientation == BottomUp) {
system_fmt("hyprctl --batch \"keyword monitor %s,transform,%d ; keyword input:touchdevice:transform %d ; keyword input:tablet:transform %d ; keyword workspace m[%s], layoutopt:orientation:right\"", output, orientation, orientation, orientation, monitor_id);
}
else { // This covers RightUp orientation
system_fmt("hyprctl --batch \"keyword monitor %s,transform,%d ; keyword input:touchdevice:transform %d ; keyword input:tablet:transform %d ; keyword workspace m[%s], layoutopt:orientation:bottom\"", output, orientation, orientation, orientation, monitor_id);
}
}
else {
// Rotates monitor and touch device without changing layout if the --rotate-flag-layout flag is not passed
system_fmt("hyprctl --batch \"keyword monitor %s,transform,%d ; keyword input:touchdevice:transform %d ; keyword input:tablet:transform %d\"", output, orientation, orientation, orientation);
}
}
DBusMessage* request_orientation(DBusConnection* conn) {
// create request calling Get method
DBusMessage* req = dbus_message_new_method_call(
"net.hadess.SensorProxy", // destination
"/net/hadess/SensorProxy", // path
"org.freedesktop.DBus.Properties", // iface
"Get" // method
);
// append arguments of Get method to request
const char* interface_name = "net.hadess.SensorProxy";
const char* property_name = "AccelerometerOrientation";
dbus_message_append_args(req,
DBUS_TYPE_STRING, &interface_name,
DBUS_TYPE_STRING, &property_name,
DBUS_TYPE_INVALID
);
// send request and get reply
DBusMessage* reply = dbus_connection_send_with_reply_and_block(
conn,
req,
DBUS_TIMEOUT_INFINITE,
&error
);
// check reply
if (dbus_error_is_set(&error)) {
printf("Error receiving orientation request: %s: %s\n",
error.name, error.message);
}
// clean up and return
dbus_message_unref(req);
return reply;
}
enum Orientation parse_orientation_reply(DBusMessage* reply) {
DBusMessageIter iter, sub_iter;
const char* orientation;
dbus_message_iter_init(reply, &iter);
dbus_message_iter_recurse(&iter, &sub_iter); // DBUS_TYPE_VARIANT
dbus_message_iter_get_basic(&sub_iter, &orientation); // DBUS_TYPE_STRING
return property_to_enum(orientation);
}
void init_orientation(DBusConnection* conn, const char* monitor_id) {
// this function proactively requests current orientation, even if not changed
DBusMessage* reply = request_orientation(conn);
if (reply != NULL) {
handle_orientation(parse_orientation_reply(reply), monitor_id);
dbus_message_unref(reply);
}
}
void listen_orientation(DBusConnection* connection, const char* monitor_id) {
// this function passively listens for changes to orientation
DBusMessage* msg;
dbus_bus_add_match(connection,
"type='signal',interface='org.freedesktop.DBus.Properties'", &error);
dbus_bus_add_match(connection,
"type='signal',sender='org.freedesktop.DBus',interface='org."
"freedesktop.DBus',member='NameOwnerChanged',arg0='net.hadess."
"SensorProxy'",
&error);
dbus_connection_flush(connection);
while (dbus_connection_read_write_dispatch(connection, -1)) {
msg = dbus_connection_pop_message(connection);
if (msg != NULL) {
if (dbus_message_is_signal(msg, "org.freedesktop.DBus.Properties",
"PropertiesChanged")) {
handle_orientation(parse_orientation_signal(msg), monitor_id);
} else {
dbus_message_unref(msg);
break;
}
dbus_message_unref(msg);
}
}
}
char* get_monitor_id(const char* monitor_name) {
// Monitor prop for workspace layoutopt rule isn't accepting monitor name, for some reason
// This is a workaround to get monitor ID instead
char command[256];
snprintf(command, sizeof(command), "hyprctl monitors %s -j | jq -r '.[] | select(.name==\"%s\") | .id'", monitor_name, monitor_name);
FILE* fp = popen(command, "r");
if (fp == NULL) {
perror("popen");
return NULL;
}
static char monitor_id[16];
if (fgets(monitor_id, sizeof(monitor_id), fp) == NULL) {
perror("fgets");
pclose(fp);
return NULL;
}
pclose(fp);
// Remove newline character if present
monitor_id[strcspn(monitor_id, "\n")] = '\0';
return monitor_id;
}
int main(int argc, char* argv[]) {
DBusConnection* connection = dbus_connect();
if (connection == NULL) {
printf("error: cannot open dbus connection\n");
return 1;
}
// Parse command-line arguments
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--left-master") == 0) {
rotate_master_layout = 1; // Enable rotate-layout if flag is found
}
else if (strcmp(argv[i], "--right-master") == 0) {
rotate_master_layout = 2; // Enable rotate-layout if flag is found
}
else {
output = argv[i];
}
}
// Get monitor ID for the specified output
char* monitor_id = get_monitor_id(output);
if (monitor_id == NULL) {
printf("error: cannot get monitor ID for %s\n", output);
dbus_disconnect(connection);
return 1;
}
// if hyprland and iio-hyprland are restarted after display is already rotated,
// init_orientation ensures correct immediate orientation without
// waiting for display to move
init_orientation(connection, monitor_id);
listen_orientation(connection, monitor_id);
dbus_disconnect(connection);
return 0;
}