]> diplodocus.org Git - xorg-xinput/blob - src/hierarchy.c
Require inputproto 1.9.99.4
[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 XCreateMasterInfo 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 XChangeDeviceHierarchy(dpy, (XAnyHierarchyChangeInfo*)&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 XDeviceInfo *info;
68 XRemoveMasterInfo r;
69 XDevice* master = NULL, *ptr = NULL, *keybd = NULL;
70 int ret;
71
72 if (argc == 0)
73 {
74 fprintf(stderr, "usage: xinput %s %s\n", name, desc);
75 return EXIT_FAILURE;
76 }
77
78 info = find_device_info(dpy, argv[0], False);
79
80 if (!info) {
81 fprintf(stderr, "unable to find device %s\n", argv[0]);
82 return EXIT_FAILURE;
83 }
84
85 master = XOpenDevice(dpy, info->id);
86 if (!master)
87 Error(BadValue, "Unable to open device %s.\n", argv[0]);
88
89 r.type = CH_RemoveMasterDevice;
90 r.device = master;
91 if (argc >= 2)
92 {
93 if (!strcmp(argv[1], "Floating"))
94 r.returnMode = Floating;
95 else if (!strcmp(argv[1], "AttachToMaster"))
96 r.returnMode = AttachToMaster;
97 else
98 Error(BadValue, "Invalid returnMode.\n");
99 } else
100 r.returnMode = Floating;
101
102 if (r.returnMode == AttachToMaster)
103 {
104 ptr = XOpenDevice(dpy, atoi(argv[2]));
105 keybd = XOpenDevice(dpy, atoi(argv[3]));
106 if (!ptr || !keybd)
107 Error(BadValue, "Invalid fallback master.\n");
108 r.returnPointer = ptr;
109 r.returnKeyboard = keybd;
110 }
111
112 ret = XChangeDeviceHierarchy(dpy, (XAnyHierarchyChangeInfo*)&r, 1);
113 if (ptr)
114 XCloseDevice(dpy, ptr);
115 if (keybd)
116 XCloseDevice(dpy, keybd);
117 return ret;
118 }
119
120 /**
121 * Swap a device from one master to another.
122 */
123 int
124 change_attachment(Display* dpy, int argc, char** argv, char *name, char* desc)
125 {
126 XDeviceInfo *info_sd, *info_md;
127 XChangeAttachmentInfo c;
128 XDevice *slave, *master;
129 int ret;
130
131 if (argc < 2)
132 {
133 fprintf(stderr, "usage: xinput %s %s\n", name, desc);
134 return EXIT_FAILURE;
135 }
136
137 info_sd = find_device_info(dpy, argv[0], True);
138 info_md = find_device_info(dpy, argv[1], False);
139
140 if (!info_sd) {
141 fprintf(stderr, "unable to find device %s\n", argv[0]);
142 return EXIT_FAILURE;
143 }
144
145 if (!info_md) {
146 fprintf(stderr, "unable to find device %s\n", argv[1]);
147 return EXIT_FAILURE;
148 }
149
150 slave = XOpenDevice(dpy, info_sd->id);
151 master = XOpenDevice(dpy, info_md->id);
152
153 if (!slave)
154 Error(BadValue, "Invalid slave device given %d\n", atoi(argv[0]));
155
156 if (!master)
157 Error(BadValue, "Invalid master device given %d\n", atoi(argv[1]));
158
159 c.type = CH_ChangeAttachment;
160 c.changeMode = AttachToMaster;
161 c.device = slave;
162 c.newMaster = master;
163
164 ret = XChangeDeviceHierarchy(dpy, (XAnyHierarchyChangeInfo*)&c, 1);
165 XCloseDevice(dpy, slave);
166 XCloseDevice(dpy, master);
167 return ret;
168 }
169
170 /**
171 * Set a device floating.
172 */
173 int
174 float_device(Display* dpy, int argc, char** argv, char* name, char* desc)
175 {
176 XDeviceInfo *info;
177 XChangeAttachmentInfo c;
178 XDevice *slave;
179 int ret;
180
181 if (argc < 1)
182 {
183 fprintf(stderr, "usage: xinput %s %s\n", name, desc);
184 return EXIT_FAILURE;
185 }
186
187 info = find_device_info(dpy, argv[0], True);
188
189 if (!info) {
190 fprintf(stderr, "unable to find device %s\n", argv[0]);
191 return EXIT_FAILURE;
192 }
193
194 slave = XOpenDevice(dpy, info->id);
195
196 if (!slave)
197 return BadValue;
198
199 c.type = CH_ChangeAttachment;
200 c.changeMode = Floating;
201 c.device = slave;
202
203 ret = XChangeDeviceHierarchy(dpy, (XAnyHierarchyChangeInfo*)&c, 1);
204 XCloseDevice(dpy, slave);
205 return ret;
206 }
207
208