]> diplodocus.org Git - xorg-xinput/blob - src/hierarchy.c
XSync the display before jumping in the grab code.
[xorg-xinput] / src / hierarchy.c
1 /*
2 * Copyright 2007 Peter Hutterer <peter@cs.unisa.edu.au>
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation.
9 *
10 * The above copyright notice and this permission notice shall be included
11 * in all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR
17 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
19 * OTHER DEALINGS IN THE SOFTWARE.
20 *
21 * Except as contained in this notice, the name of the author shall
22 * not be used in advertising or otherwise to promote the sale, use or
23 * other dealings in this Software without prior written authorization
24 * from the author.
25 *
26 */
27
28 #include "xinput.h"
29 #include <string.h>
30
31 #define Error(error, ...) \
32 { \
33 fprintf(stderr, __VA_ARGS__); \
34 return error;\
35 }
36 /**
37 * Create a new master device. Name must be supplied, other values are
38 * optional.
39 */
40 int
41 create_master(Display* dpy, int argc, char** argv, char* name, char *desc)
42 {
43 XICreateMasterInfo c;
44
45 if (argc == 0)
46 {
47 fprintf(stderr, "Usage: xinput %s %s\n", name, desc);
48 return EXIT_FAILURE;
49 }
50
51 c.type = CH_CreateMasterDevice;
52 c.name = argv[0];
53 c.sendCore = (argc >= 2) ? atoi(argv[1]) : 1;
54 c.enable = (argc >= 3) ? atoi(argv[2]) : 1;
55
56 return XIChangeDeviceHierarchy(dpy, (XIAnyHierarchyChangeInfo*)&c, 1);
57 }
58
59 /**
60 * Remove a master device.
61 * By default, all attached devices are set to Floating, unless parameters are
62 * given.
63 */
64 int
65 remove_master(Display* dpy, int argc, char** argv, char *name, char *desc)
66 {
67 XIRemoveMasterInfo r;
68 int ret, id;
69
70 if (argc == 0)
71 {
72 fprintf(stderr, "usage: xinput %s %s\n", name, desc);
73 return EXIT_FAILURE;
74 }
75
76 id = xi2_find_device_id(dpy, argv[0]);
77
78 if (id == -1) {
79 fprintf(stderr, "unable to find device %s\n", argv[0]);
80 return EXIT_FAILURE;
81 }
82
83 r.type = CH_RemoveMasterDevice;
84 r.device = id;
85 if (argc >= 2)
86 {
87 if (!strcmp(argv[1], "Floating"))
88 r.returnMode = Floating;
89 else if (!strcmp(argv[1], "AttachToMaster"))
90 r.returnMode = AttachToMaster;
91 else
92 Error(BadValue, "Invalid returnMode.\n");
93 } else
94 r.returnMode = Floating;
95
96 if (r.returnMode == AttachToMaster)
97 {
98 r.returnPointer = atoi(argv[2]);
99 r.returnKeyboard = atoi(argv[3]);
100 }
101
102 ret = XIChangeDeviceHierarchy(dpy, (XIAnyHierarchyChangeInfo*)&r, 1);
103 return ret;
104 }
105
106 /**
107 * Swap a device from one master to another.
108 */
109 int
110 change_attachment(Display* dpy, int argc, char** argv, char *name, char* desc)
111 {
112 int sd_id, md_id;
113 XIAttachSlaveInfo c;
114 int ret;
115
116 if (argc < 2)
117 {
118 fprintf(stderr, "usage: xinput %s %s\n", name, desc);
119 return EXIT_FAILURE;
120 }
121
122 sd_id = xi2_find_device_id(dpy, argv[0]);
123 md_id = xi2_find_device_id(dpy, argv[1]);
124
125 if (sd_id == -1) {
126 fprintf(stderr, "unable to find device %s\n", argv[0]);
127 return EXIT_FAILURE;
128 }
129
130 if (md_id == -1) {
131 fprintf(stderr, "unable to find device %s\n", argv[1]);
132 return EXIT_FAILURE;
133 }
134
135 c.type = CH_AttachSlave;
136 c.device = sd_id;
137 c.newMaster = md_id;
138
139 ret = XIChangeDeviceHierarchy(dpy, (XIAnyHierarchyChangeInfo*)&c, 1);
140 return ret;
141 }
142
143 /**
144 * Set a device floating.
145 */
146 int
147 float_device(Display* dpy, int argc, char** argv, char* name, char* desc)
148 {
149 int id;
150 XIDetachSlaveInfo c;
151 int ret;
152
153 if (argc < 1)
154 {
155 fprintf(stderr, "usage: xinput %s %s\n", name, desc);
156 return EXIT_FAILURE;
157 }
158
159 id = xi2_find_device_id(dpy, argv[0]);
160
161 if (id == -1) {
162 fprintf(stderr, "unable to find device %s\n", argv[0]);
163 return EXIT_FAILURE;
164 }
165
166 c.type = CH_DetachSlave;
167 c.device = id;
168
169 ret = XIChangeDeviceHierarchy(dpy, (XIAnyHierarchyChangeInfo*)&c, 1);
170 return ret;
171 }
172
173