]> diplodocus.org Git - xorg-xinput/blob - src/hierarchy.c
edb66a1970f99173f35fb07a8577c474350f6825
[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 XCreateMasterInfo* pc = &c;
45
46 if (argc == 0)
47 {
48 fprintf(stderr, "Usage: xinput %s %s\n", name, desc);
49 return EXIT_FAILURE;
50 }
51
52 c.type = CH_CreateMasterDevice;
53 c.name = argv[0];
54 c.sendCore = (argc >= 2) ? atoi(argv[1]) : 1;
55 c.enable = (argc >= 3) ? atoi(argv[2]) : 1;
56
57 return XChangeDeviceHierarchy(dpy, 1, (XAnyHierarchyChangeInfo**)&pc);
58 }
59
60 /**
61 * Remove a master device.
62 * By default, all attached devices are set to Floating, unless parameters are
63 * given.
64 */
65 int
66 remove_master(Display* dpy, int argc, char** argv, char *name, char *desc)
67 {
68 XRemoveMasterInfo r;
69 XRemoveMasterInfo* pr = &r;
70 XDevice* master = NULL, *ptr = NULL, *keybd = NULL;
71 int ret;
72
73 if (argc == 0)
74 {
75 fprintf(stderr, "usage: xinput %s %s\n", name, desc);
76 return EXIT_FAILURE;
77 }
78
79 master = XOpenDevice(dpy, atoi(argv[0]));
80 if (!master)
81 Error(BadValue, "Invalid master device.\n");
82
83 r.type = CH_RemoveMasterDevice;
84 r.device = master;
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 ptr = XOpenDevice(dpy, atoi(argv[2]));
99 keybd = XOpenDevice(dpy, atoi(argv[3]));
100 if (!ptr || !keybd)
101 Error(BadValue, "Invalid fallback master.\n");
102 r.returnPointer = ptr;
103 r.returnKeyboard = keybd;
104 }
105
106 ret = XChangeDeviceHierarchy(dpy, 1, (XAnyHierarchyChangeInfo**)&pr);
107 if (ptr)
108 XCloseDevice(dpy, ptr);
109 if (keybd)
110 XCloseDevice(dpy, keybd);
111 return ret;
112 }
113
114 /**
115 * Swap a device from one master to another.
116 */
117 int
118 change_attachment(Display* dpy, int argc, char** argv, char *name, char* desc)
119 {
120 XChangeAttachmentInfo c;
121 XChangeAttachmentInfo* pc = &c;
122 XDevice *slave, *master;
123 int ret;
124
125 if (argc < 2)
126 {
127 fprintf(stderr, "usage: xinput %s %s\n", name, desc);
128 return EXIT_FAILURE;
129 }
130
131 slave = XOpenDevice(dpy, atoi(argv[0]));
132 master = XOpenDevice(dpy, atoi(argv[1]));
133
134 if (!slave)
135 Error(BadValue, "Invalid slave device given %d\n", atoi(argv[0]));
136
137 if (!master)
138 Error(BadValue, "Invalid master device given %d\n", atoi(argv[1]));
139
140 c.type = CH_ChangeAttachment;
141 c.changeMode = AttachToMaster;
142 c.device = slave;
143 c.newMaster = master;
144
145 ret = XChangeDeviceHierarchy(dpy, 1, (XAnyHierarchyChangeInfo**)&pc);
146 XCloseDevice(dpy, slave);
147 XCloseDevice(dpy, master);
148 return ret;
149 }
150
151 /**
152 * Set a device floating.
153 */
154 int
155 float_device(Display* dpy, int argc, char** argv, char* name, char* desc)
156 {
157 XChangeAttachmentInfo c;
158 XChangeAttachmentInfo* pc = &c;
159 XDevice *slave;
160 int ret;
161
162 if (argc < 1)
163 {
164 fprintf(stderr, "usage: xinput %s %s\n", name, desc);
165 return EXIT_FAILURE;
166 }
167
168
169 slave = XOpenDevice(dpy, atoi(argv[0]));
170
171 if (!slave)
172 return BadValue;
173
174 c.type = CH_ChangeAttachment;
175 c.changeMode = Floating;
176 c.device = slave;
177
178 ret = XChangeDeviceHierarchy(dpy, 1, (XAnyHierarchyChangeInfo**)&pc);
179 XCloseDevice(dpy, slave);
180 return ret;
181 }
182
183