summaryrefslogtreecommitdiff
path: root/src/hierarchy.c
blob: ed5e93035c8cea6692f233e67c08f8015c801108 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright 2007 Peter Hutterer <peter@cs.unisa.edu.au>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of the author shall
 * not be used in advertising or otherwise to promote the sale, use or
 * other dealings in this Software without prior written authorization
 * from the author.
 *
 */

#include "xinput.h"
#include <string.h>

#define Error(error, ...) \
{ \
    fprintf(stderr, __VA_ARGS__); \
    return error;\
}
/**
 * Create a new master device. Name must be supplied, other values are
 * optional.
 */
int
create_master(Display* dpy, int argc, char** argv, char* name, char *desc)
{
    XICreateMasterInfo c;

    if (argc == 0)
    {
        fprintf(stderr, "Usage: xinput %s %s\n", name, desc);
        return EXIT_FAILURE;
    }

    c.type = CH_CreateMasterDevice;
    c.name = argv[0];
    c.sendCore = (argc >= 2) ? atoi(argv[1]) : 1;
    c.enable = (argc >= 3) ? atoi(argv[2]) : 1;

    return XIChangeDeviceHierarchy(dpy, (XIAnyHierarchyChangeInfo*)&c, 1);
}

/**
 * Remove a master device.
 * By default, all attached devices are set to Floating, unless parameters are
 * given.
 */
int
remove_master(Display* dpy, int argc, char** argv, char *name, char *desc)
{
    XIRemoveMasterInfo r;
    XIDeviceInfo *info;
    int ret;

    if (argc == 0)
    {
        fprintf(stderr, "usage: xinput %s %s\n", name, desc);
        return EXIT_FAILURE;
    }

    info = xi2_find_device_info(dpy, argv[0]);

    if (!info) {
	fprintf(stderr, "unable to find device %s\n", argv[0]);
	return EXIT_FAILURE;
    }

    r.type = CH_RemoveMasterDevice;
    r.device = info->deviceid;
    if (argc >= 2)
    {
        if (!strcmp(argv[1], "Floating"))
            r.returnMode = Floating;
        else if (!strcmp(argv[1], "AttachToMaster"))
            r.returnMode = AttachToMaster;
        else
            Error(BadValue, "Invalid returnMode.\n");
    } else
        r.returnMode = Floating;

    if (r.returnMode == AttachToMaster)
    {
        r.returnPointer = atoi(argv[2]);
        r.returnKeyboard = atoi(argv[3]);
    }

    ret = XIChangeDeviceHierarchy(dpy, (XIAnyHierarchyChangeInfo*)&r, 1);
    return ret;
}

/**
 * Swap a device from one master to another.
 */
int
change_attachment(Display* dpy, int argc, char** argv, char *name, char* desc)
{
    XIDeviceInfo *sd_info, *md_info;
    XIAttachSlaveInfo c;
    int ret;

    if (argc < 2)
    {
        fprintf(stderr, "usage: xinput %s %s\n", name, desc);
        return EXIT_FAILURE;
    }

    sd_info = xi2_find_device_info(dpy, argv[0]);
    md_info= xi2_find_device_info(dpy, argv[1]);

    if (!sd_info) {
	fprintf(stderr, "unable to find device %s\n", argv[0]);
	return EXIT_FAILURE;
    }

    if (!md_info) {
	fprintf(stderr, "unable to find device %s\n", argv[1]);
	return EXIT_FAILURE;
    }

    c.type = CH_AttachSlave;
    c.device = sd_info->deviceid;
    c.newMaster = md_info->deviceid;

    ret = XIChangeDeviceHierarchy(dpy, (XIAnyHierarchyChangeInfo*)&c, 1);
    return ret;
}

/**
 * Set a device floating.
 */
int
float_device(Display* dpy, int argc, char** argv, char* name, char* desc)
{
    XIDeviceInfo *info;
    XIDetachSlaveInfo c;
    int ret;

    if (argc < 1)
    {
        fprintf(stderr, "usage: xinput %s %s\n", name, desc);
        return EXIT_FAILURE;
    }

    info = xi2_find_device_info(dpy, argv[0]);

    if (!info) {
	fprintf(stderr, "unable to find device %s\n", argv[0]);
	return EXIT_FAILURE;
    }

    c.type = CH_DetachSlave;
    c.device = info->deviceid;

    ret = XIChangeDeviceHierarchy(dpy, (XIAnyHierarchyChangeInfo*)&c, 1);
    return ret;
}