This repository has been archived by the owner on Jun 12, 2018. It is now read-only.
/
main.c
489 lines (403 loc) · 15.1 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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
* display-rotation-menu-gtk
* Copyright (c) 2015 Charles Lehner
* Fair License (Fair)
*/
#define LICENSE "\
Fair License (Fair)\
\n\n\
Usage of the works is permitted provided that this instrument \
is retained with the works, so that any entity that uses the \
works is notified of this instrument.\
\n\n\
DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY."
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <stdlib.h>
#include <xcb/randr.h>
#include "xcbsource.h"
#define VERSION "0.1.0"
#define APPNAME "display-rotation-menu-gtk"
#define COPYRIGHT "Copyright (c) 2015 Charles Lehner"
#define COMMENTS "Display Rotation Icon"
#define WEBSITE "https://github.com/clehner/display-rotation-menu-gtk"
#define LOGO_ICON "display"
static const uint16_t
xcb_rotations_mask =
XCB_RANDR_ROTATION_ROTATE_0 | XCB_RANDR_ROTATION_ROTATE_90 |
XCB_RANDR_ROTATION_ROTATE_180 | XCB_RANDR_ROTATION_ROTATE_270,
xcb_reflections_mask =
XCB_RANDR_ROTATION_REFLECT_X |
XCB_RANDR_ROTATION_REFLECT_Y;
struct screen_info {
xcb_randr_rotation_t rotation;
xcb_timestamp_t config_timestamp;
uint16_t rate;
xcb_window_t root;
uint16_t sizeID;
GtkWidget *label_menu_item;
GtkWidget *rotation_menu_items[33];
GSList *rotation_menu_group;
};
uint8_t randr_base;
GtkStatusIcon *status_icon;
GtkWidget *app_menu;
GtkWidget *settings_menu;
GXCBSource *xcb_source;
xcb_connection_t *conn;
static GSList *screens_info = NULL;
/* static void menu_init_item(struct story *); */
static void init_xcb();
static gboolean on_xcb_event(GXCBSource *, xcb_generic_event_t *, gpointer);
static void on_screen_change(xcb_randr_screen_change_notify_event_t *);
static void on_set_screen(xcb_randr_set_screen_config_reply_t *);
static gchar *get_output_name(xcb_randr_get_output_info_reply_t *);
static void add_screen(xcb_randr_get_screen_info_reply_t *reply);
static void add_screen_rotation(struct screen_info *screen_info,
xcb_randr_rotation_t rotation, const gchar *label,
gboolean is_active);
static void menu_on_item(GtkMenuItem *, struct screen_info *);
static inline xcb_randr_rotation_t normalize_rotation(uint16_t r)
{
/* if both reflections are picked, turn it into a rotation */
if ((r & xcb_reflections_mask) == xcb_reflections_mask) {
switch (r ^ xcb_reflections_mask) {
case XCB_RANDR_ROTATION_ROTATE_0:
return XCB_RANDR_ROTATION_ROTATE_180;
case XCB_RANDR_ROTATION_ROTATE_90:
return XCB_RANDR_ROTATION_ROTATE_270;
case XCB_RANDR_ROTATION_ROTATE_180:
return XCB_RANDR_ROTATION_ROTATE_0;
case XCB_RANDR_ROTATION_ROTATE_270:
return XCB_RANDR_ROTATION_ROTATE_90;
}
}
return r;
}
static struct screen_info *get_screen_info(xcb_window_t window)
{
struct screen_info *screen_info;
GSList *screens;
for (screens = screens_info; screens; screens = g_slist_next(screens)) {
screen_info = screens->data;
if (screen_info->root == window)
return screen_info;
}
return NULL;
}
static void menu_on_about(GtkMenuItem *menuItem, gpointer userData)
{
gtk_show_about_dialog(NULL,
"program-name", APPNAME,
"version", VERSION,
"logo-icon-name", LOGO_ICON,
"copyright", COPYRIGHT,
"comments", COMMENTS,
"website", WEBSITE,
"license", LICENSE,
"wrap-license", TRUE,
NULL);
}
static void menu_on_quit(GtkMenuItem *item, gpointer userData)
{
xcb_disconnect(conn);
gtk_main_quit();
}
static gboolean status_icon_on_button_press(GtkStatusIcon *status_icon,
GdkEventButton *event, gpointer user_data)
{
/* Show the app menu on left click */
GtkMenu *menu = GTK_MENU(event->button == 1 ? app_menu : settings_menu);
gtk_menu_popup(menu, NULL, NULL, gtk_status_icon_position_menu,
status_icon, event->button, event->time);
return TRUE;
}
int main(int argc, char *argv[])
{
GtkWidget *item;
GtkMenuShell *menu;
GError *error = NULL;
if (!gtk_init_with_args(&argc, &argv, NULL, NULL, NULL, &error)) {
fprintf(stderr, "gtk init: %s\n", error->message);
g_error_free(error);
return 1;
}
/* Status icon */
status_icon = gtk_status_icon_new_from_icon_name(LOGO_ICON);
gtk_status_icon_set_visible(status_icon, TRUE);
g_signal_connect(G_OBJECT(status_icon), "button_press_event",
G_CALLBACK(status_icon_on_button_press), NULL);
/* App menu */
app_menu = gtk_menu_new();
menu = GTK_MENU_SHELL(app_menu);
/* Settings menu */
settings_menu = gtk_menu_new();
menu = GTK_MENU_SHELL(settings_menu);
/* About */
item = gtk_menu_item_new_with_mnemonic(_("_About"));
g_signal_connect(item, "activate", G_CALLBACK(menu_on_about), NULL);
gtk_menu_shell_append(menu, item);
/* Quit */
item = gtk_menu_item_new_with_mnemonic(_("_Quit"));
g_signal_connect(item, "activate", G_CALLBACK(menu_on_quit), NULL);
gtk_menu_shell_append(menu, item);
init_xcb();
gtk_widget_show_all(app_menu);
gtk_widget_show_all(settings_menu);
gtk_main();
return 0;
}
static void init_xcb()
{
const xcb_query_extension_reply_t *qer;
const xcb_setup_t *setup;
xcb_screen_t *screen;
xcb_screen_iterator_t screen_iter;
xcb_drawable_t win;
guint num_screens;
guint i;
xcb_generic_error_t *err = NULL;
/* Open xcb connection */
conn = xcb_connect(gdk_get_display_arg_name(), NULL);
if (xcb_connection_has_error(conn)) {
g_error("Failed to connect to display\n");
exit(EXIT_FAILURE);
}
/* query the version to prevent error 16 when setting config */
xcb_randr_query_version_unchecked(conn, 1, 5);
qer = xcb_get_extension_data(conn, &xcb_randr_id);
if (!qer || !qer->present) {
g_error("RandR extension missing\n");
exit(EXIT_FAILURE);
}
randr_base = qer->first_event;
xcb_source = g_xcb_source_new_for_connection(NULL, conn);
g_xcb_source_set_event_callback(xcb_source, on_xcb_event, NULL);
/* get the screens */
setup = xcb_get_setup(conn);
screen_iter = xcb_setup_roots_iterator(setup);
num_screens = setup->roots_len;
/* Set up space for cookies */
xcb_randr_get_screen_info_cookie_t get_screen_info_cookies[num_screens];
for (i = 0; i < num_screens; i++) {
/* Get root window */
screen = screen_iter.data;
win = screen->root;
/* Register for screen change events */
xcb_randr_select_input(conn, win, XCB_RANDR_NOTIFY_MASK_SCREEN_CHANGE);
/* Get screen info */
get_screen_info_cookies[i] =
xcb_randr_get_screen_info_unchecked(conn, win);
xcb_screen_next(&screen_iter);
}
/* TODO: detect adding and removal of screens */
xcb_flush(conn);
/* Get screen info replies */
for (i = 0; i < num_screens; i++) {
xcb_randr_get_screen_info_reply_t *reply =
xcb_randr_get_screen_info_reply(conn,
get_screen_info_cookies[i], &err);
if (err) {
g_warning("Error getting info for screen %u\n", i);
err = NULL;
continue;
}
add_screen(reply);
free(reply);
}
xcb_flush(conn);
}
static void add_screen(xcb_randr_get_screen_info_reply_t *reply)
{
const gchar *title = "Display";
GtkWidget *item = gtk_menu_item_new_with_label(title);
GtkMenuShell *menu = GTK_MENU_SHELL(app_menu);
struct screen_info *info = g_malloc(sizeof *info);
xcb_randr_rotation_t rotation = normalize_rotation(reply->rotation);
xcb_randr_rotation_t rotations = reply->rotations;
info->label_menu_item = item;
info->rotation_menu_group = NULL;
info->rotation = rotation;
info->config_timestamp = reply->config_timestamp;
info->root = reply->root;
info->sizeID = reply->sizeID;
info->rate = reply->rate;
screens_info = g_slist_append(screens_info, info);
gtk_widget_set_sensitive(item, FALSE);
gtk_menu_shell_append(menu, item);
#define R(rot, title) \
if (rotations & XCB_RANDR_ROTATION_##rot) \
add_screen_rotation(info, XCB_RANDR_ROTATION_##rot, title, \
XCB_RANDR_ROTATION_##rot & rotation)
R(ROTATE_0, "Landscape");
R(ROTATE_90, "Portrait");
R(ROTATE_180, "Landscape Flipped");
R(ROTATE_270, "Portrait Flipped");
if (rotations & xcb_rotations_mask && rotations & xcb_reflections_mask)
gtk_menu_shell_append(menu, gtk_separator_menu_item_new());
R(REFLECT_X, "Reflected X");
R(REFLECT_Y, "Reflected Y");
#undef R
gtk_menu_shell_append(menu, gtk_separator_menu_item_new());
gtk_widget_show_all(app_menu);
/* Get screen resources */
xcb_randr_get_screen_resources_current_cookie_t resources_cookie;
xcb_randr_get_screen_resources_current_reply_t *resources_reply;
xcb_generic_error_t *err = NULL;
resources_cookie = xcb_randr_get_screen_resources_current(conn,
info->root);
resources_reply = xcb_randr_get_screen_resources_current_reply(conn,
resources_cookie, &err);
if (err) {
g_warning("Get Screen Resources returned error %u\n", err->error_code);
return;
}
/* Get screen outputs */
xcb_randr_output_t *outputs;
guint i;
gchar *output_name;
outputs = xcb_randr_get_screen_resources_current_outputs(resources_reply);
for (i = 0; i < resources_reply->num_outputs; i++) {
xcb_randr_get_output_info_reply_t *output_info_reply;
xcb_randr_get_output_info_cookie_t output_info_cookie =
xcb_randr_get_output_info_unchecked(conn, outputs[i],
resources_reply->config_timestamp);
output_info_reply =
xcb_randr_get_output_info_reply(conn, output_info_cookie, NULL);
/* Show only if connected */
switch (output_info_reply->connection) {
case XCB_RANDR_CONNECTION_DISCONNECTED:
case XCB_RANDR_CONNECTION_UNKNOWN:
continue;
case XCB_RANDR_CONNECTION_CONNECTED:
break;
}
output_name = get_output_name(output_info_reply);
/* Put output names on the menu */
gtk_menu_item_set_label(GTK_MENU_ITEM(item), output_name);
g_free(output_name);
/* TODO: concatenate multiple names or pick them intelligently */
}
}
static void add_screen_rotation(struct screen_info *screen_info,
xcb_randr_rotation_t rotation, const gchar *label,
gboolean is_active)
{
GtkMenuShell *menu = GTK_MENU_SHELL(app_menu);
gboolean is_reflection = rotation &
(XCB_RANDR_ROTATION_REFLECT_X | XCB_RANDR_ROTATION_REFLECT_Y);
GtkWidget *item = is_reflection ?
gtk_check_menu_item_new_with_label(label) :
gtk_radio_menu_item_new_with_label(
screen_info->rotation_menu_group, label);
if (!is_reflection)
screen_info->rotation_menu_group =
gtk_radio_menu_item_get_group(GTK_RADIO_MENU_ITEM(item));
screen_info->rotation_menu_items[rotation] = item;
if (is_active)
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
g_object_set_data(G_OBJECT(item), "rotation", GUINT_TO_POINTER(rotation));
g_signal_connect(item, "activate", G_CALLBACK(menu_on_item), screen_info);
gtk_menu_shell_append(menu, item);
}
static void check_rotation_menu_item(struct screen_info *info,
xcb_randr_rotation_t rotation, gboolean is_active)
{
GtkWidget *item = info->rotation_menu_items[rotation];
if (!item) {
g_warning("Unknown rotation item\n");
return;
}
g_signal_handlers_block_by_func(G_OBJECT(item), G_CALLBACK(menu_on_item),
(gpointer)info);
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), is_active);
g_signal_handlers_unblock_by_func(G_OBJECT(item), G_CALLBACK(menu_on_item),
(gpointer)info);
}
static void on_screen_change(xcb_randr_screen_change_notify_event_t *ev)
{
struct screen_info *screen_info = get_screen_info(ev->root);
xcb_randr_rotation_t rotation = normalize_rotation(ev->rotation);
if (!screen_info) {
g_warning("Unable to find screen\n");
return;
}
/* Update menu items with new rotation setting */
screen_info->config_timestamp = ev->config_timestamp;
screen_info->rotation = rotation;
check_rotation_menu_item(screen_info,
rotation & xcb_rotations_mask, TRUE);
check_rotation_menu_item(screen_info, XCB_RANDR_ROTATION_REFLECT_X,
rotation & XCB_RANDR_ROTATION_REFLECT_X);
check_rotation_menu_item(screen_info, XCB_RANDR_ROTATION_REFLECT_Y,
rotation & XCB_RANDR_ROTATION_REFLECT_Y);
}
static void menu_on_item(GtkMenuItem *item, struct screen_info *screen_info)
{
xcb_randr_rotation_t old_config, option, rotation, reflection;
option = GPOINTER_TO_UINT(g_object_get_data(G_OBJECT(item), "rotation"));
if (option & xcb_rotations_mask &&
!gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(item))) {
/* let the activated item take care of the event */
return;
}
old_config = normalize_rotation(screen_info->rotation);
rotation = old_config & xcb_rotations_mask;
reflection = old_config & xcb_reflections_mask;
if (option & xcb_rotations_mask) {
rotation = option;
} else if (option & xcb_reflections_mask) {
reflection ^= option;
}
xcb_randr_set_screen_config_unchecked(conn, screen_info->root,
XCB_CURRENT_TIME, screen_info->config_timestamp,
screen_info->sizeID,
normalize_rotation(rotation | reflection),
screen_info->rate);
xcb_flush(conn);
}
static gboolean on_xcb_event(GXCBSource *source, xcb_generic_event_t *ev,
gpointer user_data) {
switch (ev->response_type) {
case 0: {
xcb_generic_error_t *err = (xcb_generic_error_t *)ev;
printf("Received X11 error %d\n", err->error_code);
free(err);
return G_SOURCE_CONTINUE;
}
}
switch (ev->response_type - randr_base) {
case XCB_RANDR_SELECT_INPUT:
break;
case XCB_RANDR_SCREEN_CHANGE_NOTIFY:
on_screen_change((xcb_randr_screen_change_notify_event_t *)ev);
break;
case XCB_RANDR_SET_SCREEN_CONFIG:
on_set_screen((xcb_randr_set_screen_config_reply_t *)ev);
break;
default:
printf("unknown event %u (%u)\n", ev->response_type, randr_base);
break;
}
free(ev);
return G_SOURCE_CONTINUE;
}
static void on_set_screen(xcb_randr_set_screen_config_reply_t *reply)
{
struct screen_info *screen_info = get_screen_info(reply->root);
if (!screen_info) {
g_warning("Unknown screen\n");
return;
}
screen_info->config_timestamp = reply->config_timestamp;
}
static gchar *get_output_name(xcb_randr_get_output_info_reply_t *reply)
{
gchar *name;
uint8_t *nbuf;
nbuf = xcb_randr_get_output_info_name(reply);
name = reply->name_len > 0 ?
g_strndup((gchar *)nbuf, reply->name_len) : NULL;
return name;
}
/* vim: set expandtab ts=4 sw=4: */