-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitch.c
153 lines (121 loc) · 3.61 KB
/
switch.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
/*
* Switch, switches the pointer and focus to the next X screen using xcb
* Copyright (C) 2011 Joško Nikolić
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <xcb/xcb.h>
#define PROGRAM_NAME "Switch"
#define PROGRAM_VERSION "1.1"
static struct option const long_options[] =
{
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{"screen", required_argument, NULL, 's'},
{NULL, 0, NULL, 0}
};
static void usage(char *argv[])
{
printf("Usage: %s\n"
"\t-v, --version Show version and exit\n"
"\t-h, --help Show this help message and exit\n", argv[0]);
}
int main(int argc, char *argv[])
{
/*
* variables
*/
char c;
int i, screen_num;
xcb_connection_t *connection;
xcb_screen_t *next_screen;
xcb_screen_t *current_screen;
xcb_screen_iterator_t iterator;
xcb_query_pointer_reply_t *pointer;
/*
* handle the arguments
*/
while (-1 != (c = getopt_long(argc, argv, "hv", long_options, NULL)))
{
switch (c)
{
case 'h':
usage(argv);
return 0;
case 'v':
printf("GPL %s: %s\n"
"Copyright (C) 2011 Joško Nikolić\n", PROGRAM_NAME,
PROGRAM_VERSION);
return 0;
default:
usage(argv);
break;
}
}
/*
* open the connection
*/
connection = xcb_connect(NULL, &screen_num);
if (xcb_connection_has_error(connection))
{
puts("ERROR: couldn't open display");
exit(EXIT_FAILURE);
}
const xcb_setup_t *setup = xcb_get_setup(connection);
iterator = xcb_setup_roots_iterator(setup);
/*
* find the current screen
*/
for (i = 0; i < screen_num; ++i)
xcb_screen_next(&iterator);
current_screen = iterator.data;
/*
* get the next screen to switch to
*/
xcb_screen_next(&iterator);
/*
* if the next screen doesn't exist move the iterator to start
*/
if (0 == (iterator.data)->width_in_pixels
&& 0 == (iterator.data)->height_in_pixels)
{
iterator = xcb_setup_roots_iterator(setup);
}
next_screen = iterator.data;
/*
* get pointer information
*/
pointer = xcb_query_pointer_reply(connection,
xcb_query_pointer_unchecked(connection, current_screen->root),
NULL);
if (NULL != pointer)
{
/*
* warp the pointer and focus the new screen
*/
xcb_warp_pointer(connection, XCB_NONE, next_screen->root, 0, 0, 0, 0,
pointer->win_x, pointer->win_y);
xcb_set_input_focus(connection, XCB_INPUT_FOCUS_PARENT,
next_screen->root, XCB_CURRENT_TIME);
xcb_flush(connection);
free(pointer);
}
else
puts("ERROR: couldn't query pointer");
xcb_disconnect(connection);
return EXIT_SUCCESS;
}