summaryrefslogtreecommitdiff
path: root/src/XIPassiveGrab.c
blob: ed79f1cdd24e877edbcd65ff35995142bfac0028 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * Copyright © 2009 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) 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 AUTHORS OR COPYRIGHT HOLDERS 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.
 *
 */

#include <stdint.h>
#include <X11/Xlibint.h>
#include <X11/extensions/XI2proto.h>
#include <X11/extensions/XInput2.h>
#include <X11/extensions/extutil.h>
#include "XIint.h"

static int
_XIPassiveGrabDevice(Display* dpy, int deviceid, int grabtype, int detail,
                     Window grab_window, Cursor cursor,
                     int grab_mode, int paired_device_mode,
                     Bool owner_events, XIEventMask *mask,
                     int num_modifiers, XIGrabModifiers *modifiers_inout)
{
    xXIPassiveGrabDeviceReq *req;
    xXIPassiveGrabDeviceReply reply;
    xXIGrabModifierInfo *failed_mods;
    int len = 0, i;
    char *buff;

    XExtDisplayInfo *extinfo = XInput_find_display(dpy);

    LockDisplay(dpy);
    if (_XiCheckExtInit(dpy, Dont_Check, extinfo) == -1)
	return -1;

    GetReq(XIPassiveGrabDevice, req);
    req->reqType = extinfo->codes->major_opcode;
    req->ReqType = X_XIPassiveGrabDevice;
    req->deviceid = deviceid;
    req->grab_mode = grab_mode;
    req->paired_device_mode = paired_device_mode;
    req->owner_events = owner_events;
    req->grab_window = grab_window;
    req->cursor = cursor;
    req->detail = detail;
    req->num_modifiers = num_modifiers;
    req->mask_len = (mask->mask_len + 3)/4;
    req->grab_type = grabtype;

    len = req->mask_len + num_modifiers;
    SetReqLen(req, len, len);

    buff = calloc(4, req->mask_len);
    memcpy(buff, mask->mask, mask->mask_len);
    Data32(dpy, buff, req->mask_len * 4);
    for (i = 0; i < num_modifiers; i++)
        Data32(dpy, &modifiers_inout[i].modifiers, 4);

    free(buff);

    if (_XReply(dpy, (xReply *)&reply, 0, xTrue))
    {
	UnlockDisplay(dpy);
	SyncHandle();
	return -1;
    }

    failed_mods = calloc(reply.num_modifiers, sizeof(xXIGrabModifierInfo));
    if (!failed_mods)
        return -1;
    _XRead(dpy, (char*)failed_mods, reply.num_modifiers * sizeof(xXIGrabModifierInfo));

    for (i = 0; i < reply.num_modifiers; i++)
    {
        modifiers_inout[i].status = failed_mods[i].status;
        modifiers_inout[i].modifiers = failed_mods[i].modifiers;
    }
    free(failed_mods);

    UnlockDisplay(dpy);
    SyncHandle();
    return reply.num_modifiers;
}

int
XIGrabButton(Display* dpy, int deviceid, int button,
             Window grab_window, Cursor cursor,
             int grab_mode, int paired_device_mode,
             Bool owner_events, XIEventMask *mask,
             int num_modifiers, XIGrabModifiers *modifiers_inout)
{
    return _XIPassiveGrabDevice(dpy, deviceid, XIGrabtypeButton, button,
                                grab_window, cursor, grab_mode,
                                paired_device_mode, owner_events, mask,
                                num_modifiers, modifiers_inout);
}

int
XIGrabKeysym(Display* dpy, int deviceid, int keysym,
             Window grab_window, int grab_mode, int paired_device_mode,
             Bool owner_events, XIEventMask *mask,
             int num_modifiers, XIGrabModifiers *modifiers_inout)
{
    return _XIPassiveGrabDevice(dpy, deviceid, XIGrabtypeKeysym, keysym,
                                grab_window, None, grab_mode, paired_device_mode,
                                owner_events, mask, num_modifiers,
                                modifiers_inout);
}

int
XIGrabEnter(Display *dpy, int deviceid, Window grab_window, Cursor cursor,
            int grab_mode, int paired_device_mode, Bool owner_events,
            XIEventMask *mask, int num_modifiers,
            XIGrabModifiers *modifiers_inout)
{
    return _XIPassiveGrabDevice(dpy, deviceid, XIGrabtypeEnter, 0,
                                grab_window, cursor, grab_mode, paired_device_mode,
                                owner_events, mask, num_modifiers,
                                modifiers_inout);
}

int
XIGrabFocusIn(Display *dpy, int deviceid, Window grab_window, int grab_mode,
            int paired_device_mode, Bool owner_events, XIEventMask *mask,
            int num_modifiers, XIGrabModifiers *modifiers_inout)
{
    return _XIPassiveGrabDevice(dpy, deviceid, XIGrabtypeFocusIn, 0,
                                grab_window, None, grab_mode, paired_device_mode,
                                owner_events, mask, num_modifiers,
                                modifiers_inout);
}

static int
_XIPassiveUngrabDevice(Display* dpy, int deviceid, int grabtype, int detail,
                       Window grab_window, int num_modifiers, XIGrabModifiers *modifiers)
{
    xXIPassiveUngrabDeviceReq *req;
    int i;

    XExtDisplayInfo *extinfo = XInput_find_display(dpy);

    LockDisplay(dpy);
    if (_XiCheckExtInit(dpy, Dont_Check, extinfo) == -1)
	return -1;

    GetReq(XIPassiveUngrabDevice, req);
    req->reqType = extinfo->codes->major_opcode;
    req->ReqType = X_XIPassiveUngrabDevice;
    req->deviceid = deviceid;
    req->grab_window = grab_window;
    req->detail = detail;
    req->num_modifiers = num_modifiers;
    req->grab_type = grabtype;

    SetReqLen(req, num_modifiers, num_modifiers);
    for (i = 0; i < num_modifiers; i++)
        Data32(dpy, &modifiers[i].modifiers, 4);

    UnlockDisplay(dpy);
    SyncHandle();
    return Success;
}

int
XIUngrabButton(Display* display, int deviceid, int button,Window grab_window,
               int num_modifiers, XIGrabModifiers *modifiers)
{
    return _XIPassiveUngrabDevice(display, deviceid, XIGrabtypeButton, button,
                                  grab_window, num_modifiers, modifiers);
}

int
XIUngrabKeysym(Display* display, int deviceid, int keysym, Window grab_window,
               int num_modifiers, XIGrabModifiers *modifiers)
{
    return _XIPassiveUngrabDevice(display, deviceid, XIGrabtypeKeysym, keysym,
                                  grab_window, num_modifiers, modifiers);
}


int
XIUngrabEnter(Display* display, int deviceid, Window grab_window,
               int num_modifiers, XIGrabModifiers *modifiers)
{
    return _XIPassiveUngrabDevice(display, deviceid, XIGrabtypeEnter, 0,
                                  grab_window, num_modifiers, modifiers);
}

int
XIUngrabFocusIn(Display* display, int deviceid, Window grab_window,
               int num_modifiers, XIGrabModifiers *modifiers)
{
    return _XIPassiveUngrabDevice(display, deviceid, XIGrabtypeFocusIn, 0,
                                  grab_window, num_modifiers, modifiers);
}