]> diplodocus.org Git - xorg-xinput/blob - src/hierarchy.c
Require inputproto 1.9.99.12
[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 XIAddMasterInfo 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 = XIAddMaster;
52 c.name = argv[0];
53 c.send_core = (argc >= 2) ? atoi(argv[1]) : 1;
54 c.enable = (argc >= 3) ? atoi(argv[2]) : 1;
55
56 return XIChangeHierarchy(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 XIDeviceInfo *info;
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 info = xi2_find_device_info(dpy, argv[0]);
78
79 if (!info) {
80 fprintf(stderr, "unable to find device %s\n", argv[0]);
81 return EXIT_FAILURE;
82 }
83
84 r.type = XIRemoveMaster;
85 r.deviceid = info->deviceid;
86 if (argc >= 2)
87 {
88 if (!strcmp(argv[1], "Floating"))
89 r.return_mode = XIFloating;
90 else if (!strcmp(argv[1], "AttachToMaster"))
91 r.return_mode = XIAttachToMaster;
92 else
93 Error(BadValue, "Invalid return_mode.\n");
94 } else
95 r.return_mode = XIFloating;
96
97 if (r.return_mode == XIAttachToMaster)
98 {
99 r.return_pointer = atoi(argv[2]);
100 r.return_keyboard = atoi(argv[3]);
101 }
102
103 ret = XIChangeHierarchy(dpy, (XIAnyHierarchyChangeInfo*)&r, 1);
104 return ret;
105 }
106
107 /**
108 * Swap a device from one master to another.
109 */
110 int
111 change_attachment(Display* dpy, int argc, char** argv, char *name, char* desc)
112 {
113 XIDeviceInfo *sd_info, *md_info;
114 XIAttachSlaveInfo c;
115 int ret;
116
117 if (argc < 2)
118 {
119 fprintf(stderr, "usage: xinput %s %s\n", name, desc);
120 return EXIT_FAILURE;
121 }
122
123 sd_info = xi2_find_device_info(dpy, argv[0]);
124 md_info= xi2_find_device_info(dpy, argv[1]);
125
126 if (!sd_info) {
127 fprintf(stderr, "unable to find device %s\n", argv[0]);
128 return EXIT_FAILURE;
129 }
130
131 if (!md_info) {
132 fprintf(stderr, "unable to find device %s\n", argv[1]);
133 return EXIT_FAILURE;
134 }
135
136 c.type = XIAttachSlave;
137 c.deviceid = sd_info->deviceid;
138 c.new_master = md_info->deviceid;
139
140 ret = XIChangeHierarchy(dpy, (XIAnyHierarchyChangeInfo*)&c, 1);
141 return ret;
142 }
143
144 /**
145 * Set a device floating.
146 */
147 int
148 float_device(Display* dpy, int argc, char** argv, char* name, char* desc)
149 {
150 XIDeviceInfo *info;
151 XIDetachSlaveInfo c;
152 int ret;
153
154 if (argc < 1)
155 {
156 fprintf(stderr, "usage: xinput %s %s\n", name, desc);
157 return EXIT_FAILURE;
158 }
159
160 info = xi2_find_device_info(dpy, argv[0]);
161
162 if (!info) {
163 fprintf(stderr, "unable to find device %s\n", argv[0]);
164 return EXIT_FAILURE;
165 }
166
167 c.type = XIDetachSlave;
168 c.deviceid = info->deviceid;
169
170 ret = XIChangeHierarchy(dpy, (XIAnyHierarchyChangeInfo*)&c, 1);
171 return ret;
172 }
173
174