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