summaryrefslogtreecommitdiff
path: root/xcbviewfs.c
blob: 79386dde5cf213078a1274cb07c626ead81ef11e (plain)
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
/*
 * Attempt to implement picture viewer XCB with kbd and pointer grabbing, fullscreen
 *
 * Author: Peter Wu <lekensteyn@gmail.com>
 * Date: 2012-09-24
 * gcc -o xcbviewfs xcbviewfs.c -lxcb-image -lxcb-shm -lxcb
 */

#include <xcb/xcb.h>
//#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define die(msg) do { \
	fprintf(stderr, msg); \
	exit(EXIT_FAILURE); \
} while (0)

#define bail(lbl, msg) do { \
	fprintf(stderr, msg); \
	goto lbl; \
} while (0)

static const char empty_cursor_data[32];

int main(int argc, char **argv) {
	xcb_connection_t *c;
	xcb_screen_t *screen;
	xcb_window_t win;
	xcb_cursor_t cursor;
	if (argc < 2) {
		fprintf(stderr, "Usage: %s filename\n", *argv);
		return 1;
	}
	c = xcb_connect(NULL, NULL);
	if (!c) {
		fprintf(stderr, "Cannot open display\n");
		return 1;
	}
	screen = xcb_setup_roots_iterator(xcb_get_setup(c)).data;
	win = xcb_generate_id(c);
	xcb_create_window(c, 0, win, /* conn, depth, WinID */
		screen->root, /* parent */
		0, 0, /* x y */
		100, 100, /* width height */
		0, /* border_width */
		XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class */
		screen->root_visual,
		0, NULL);
	xcb_map_window(c, win);
	xcb_flush(c);

	/* options */
	int grab_pointer = 0;
	int hide_cursor  = 0;
	int grab_kbd     = 0;
	int fullscreen   = 0;
	grab_kbd = !(fullscreen = grab_pointer = hide_cursor = 0 );

	if (grab_pointer) {
		xcb_grab_pointer_cookie_t cookie;
		xcb_grab_pointer_reply_t *reply;
		cookie = xcb_grab_pointer(c, 1, screen->root, XCB_NONE,
			XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC,
			XCB_NONE, XCB_NONE,
			XCB_CURRENT_TIME);
		if (!(reply = xcb_grab_pointer_reply(c, cookie, NULL))) {
			bail(disconnect, "No pointer grab reply\n");
		}
		if (reply->status != XCB_GRAB_STATUS_SUCCESS) {
			bail(disconnect, "Grab pointer FAILED\n");
		}
		free(reply);
	}
	if (hide_cursor) {
		xcb_pixmap_t cp = xcb_create_pixmap_from_bitmap_data(c, win,
			empty_cursor_data, 16, 16, 1, 0, 0, 0);
		xcb_pixmap_t mp = xcb_create_pixmap_from_bitmap_data(c, win,
			empty_cursor_data, 16, 16, 1, 0, 0, 0);

		cursor = xcb_generate_id(c);
		xcb_create_cursor(c, cursor, cp, mp, 0, 0, 0, 0xFFFF, 0xFFFF, 0xFFFF, 8, 8);
		xcb_free_pixmap(c, cp);
		xcb_free_pixmap(c, mp);

		xcb_change_window_attributes(c, win, XCB_CW_CURSOR, &cursor);
		xcb_free_cursor(c, cursor);
		xcb_flush(c);
	}
	if (grab_kbd) {
		xcb_grab_keyboard_cookie_t cookie;
		xcb_grab_keyboard_reply_t *reply;
		cookie = xcb_grab_keyboard(c, 1, screen->root, XCB_CURRENT_TIME,
			XCB_GRAB_MODE_ASYNC, XCB_GRAB_MODE_ASYNC);
		if (!(reply = xcb_grab_keyboard_reply(c, cookie, NULL))) {
			bail(ungrab_pointer, "No kbd grab reply\n");
		}
		if (reply->status != XCB_GRAB_STATUS_SUCCESS) {
			bail(ungrab_pointer, "Grab kbd FAILED\n");
		}
		free(reply);
	}
	if (fullscreen) {
		xcb_intern_atom_cookie_t cookie;
		xcb_intern_atom_reply_t *reply;
		xcb_atom_t property, data;

		cookie = xcb_intern_atom(c, 0, 13, "_NET_WM_STATE");
		if (!(reply = xcb_intern_atom_reply(c, cookie, 0))) {
			bail(ungrab_kbd, "Failed to get _NET_WM_STATE atom\n");
		}
		property = reply->atom;
		free(reply);

		cookie = xcb_intern_atom(c, 0, 24, "_NET_WM_STATE_FULLSCREEN");
		if (!(reply = xcb_intern_atom_reply(c, cookie, 0))) {
			bail(ungrab_kbd, "Failed to get _NET_WM_STATE_FULLSCREEN atom\n");
		}
		data = reply->atom;
		free(reply);

		xcb_change_property(c, XCB_PROP_MODE_REPLACE, win,
			property, XCB_ATOM_ATOM, 32, 1, &data);
		xcb_flush(c);
	}
	xcb_generic_event_t *e;
	while ((e = xcb_wait_for_event(c))) {
		free(e);
	}
ungrab_kbd:
	xcb_ungrab_keyboard(c, XCB_CURRENT_TIME);
ungrab_pointer:
	xcb_ungrab_pointer(c, XCB_CURRENT_TIME);
disconnect:
	xcb_disconnect(c);
	return 0;
}