-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointer.c
335 lines (295 loc) · 7.84 KB
/
pointer.c
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
///
/// @file pointer.c @brief Pointer/Cursor functions
///
/// Copyright (c) 2009 - 2011, 2021 by Lutz Sammer. All Rights Reserved.
///
/// Contributor(s):
///
/// License: AGPLv3
///
/// This program is free software: you can redistribute it and/or modify
/// it under the terms of the GNU Affero General Public License as
/// published by the Free Software Foundation, either version 3 of the
/// License.
///
/// This program is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/// GNU Affero General Public License for more details.
///
/// $Id$
//////////////////////////////////////////////////////////////////////////////
///
/// @defgroup pointer The pointer/cursor module.
///
/// This module handles pointers and cursors.
///
/// enable #USE_XCURSOR to use the xcursor-themes support:
/// @section Configuration
/// Edit the user's ~/.Xresources file:
/// @code{.unparsed}
/// ! Choose a cursor theme
/// Xcursor.theme: redglass
/// @endcode
/// Cursor size can be optionally chosen as well:
/// @code{.unparsed}
/// ! Choose the cursor size
/// Xcursor.size: 16
/// @endcode
/// Note:
/// `Xft.dpi: 192` is used to quess the cursor size, if not given.
///
///< @{
#include <xcb/xcb.h>
#include "uwm.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef USE_XCURSOR
#include <xcb/xcb_cursor.h>
#else
#include <X11/cursorfont.h> // cursor font XC_
#endif
#include "pointer.h"
//////////////////////////////////////////////////////////////////////////////
/// query pointer cookie for init
static xcb_query_pointer_cookie_t PointerCookie;
#ifdef USE_XCURSOR
static xcb_cursor_context_t *CursorCtx; ///< xcb cursor context
#endif
static int PointerX; ///< cached current pointer x position
static int PointerY; ///< cached current pointer y position
CursorTable Cursors; ///< all cursors
/**
** Set current pointer position.
**
** @param x current x-coordinate of pointer
** @param y current y-coordinate of pointer
*/
void PointerSetPosition(int x, int y)
{
PointerX = x;
PointerY = y;
}
/**
** Get current pointer position.
**
** @param[out] x set to current x-coordinate of pointer
** @param[out] y set to current y-coordinate of pointer
*/
void PointerGetPosition(int *x, int *y)
{
*x = PointerX;
*y = PointerY;
}
/**
** Grab pointer request.
**
** @param window specifies grab window
** @param cursor cursor to be displayed during grab
**
** @returns cookie to get grab reply.
*/
xcb_grab_pointer_cookie_t PointerGrabRequest(xcb_window_t window,
xcb_cursor_t cursor)
{
return xcb_grab_pointer_unchecked(Connection, 0, window,
XCB_EVENT_MASK_POINTER_MOTION | XCB_EVENT_MASK_BUTTON_PRESS |
XCB_EVENT_MASK_BUTTON_RELEASE, XCB_GRAB_MODE_ASYNC,
XCB_GRAB_MODE_ASYNC, XCB_NONE, cursor, XCB_CURRENT_TIME);
}
/**
** Grab pointer reply.
**
** @param cookie cookie of grab pointer request
**
** @returns true if grab successfull, false otherwise.
*/
int PointerGrabReply(xcb_grab_pointer_cookie_t cookie)
{
xcb_grab_pointer_reply_t *reply;
reply = xcb_grab_pointer_reply(Connection, cookie, NULL);
if (reply) {
int status;
Debug(3, " grab pointer %d\n", reply->status);
status = reply->status == XCB_GRAB_STATUS_SUCCESS;
free(reply);
return status;
}
return 0;
}
/**
** Request pointer grab with default cursor.
**
** @param window specifies grab window
**
** @returns cookie to get grab reply.
*/
xcb_grab_pointer_cookie_t PointerGrabDefaultRequest(xcb_window_t window)
{
return PointerGrabRequest(window, Cursors.Default);
}
/**
** Request pointer grab for moving a window.
**
** @returns cookie to get grab reply.
*/
xcb_grab_pointer_cookie_t PointerGrabForMoveRequest(void)
{
return PointerGrabRequest(XcbScreen->root, Cursors.Move);
}
/**
** Request pointer grab for choosing a window.
**
** @returns cookie to get grab reply.
*/
xcb_grab_pointer_cookie_t PointerGrabForChooseRequest(void)
{
return PointerGrabRequest(XcbScreen->root, Cursors.Choose);
}
/**
** Send query pointer request.
**
** @returns cookie of query pointer request.
*/
xcb_query_pointer_cookie_t PointerQueryRequest(void)
{
return xcb_query_pointer_unchecked(Connection, XcbScreen->root);
}
/**
** Get query pointer reply. Stores current pointer position in
** #PointerX and #PointerY.
**
** @param cookie cookie of query pointer request
**
** @returns current pointer button mask.
*/
xcb_button_mask_t PointerQueryReply(xcb_query_pointer_cookie_t cookie)
{
xcb_query_pointer_reply_t *reply;
xcb_button_mask_t mask;
mask = 0;
reply = xcb_query_pointer_reply(Connection, cookie, NULL);
if (reply) {
PointerX = reply->root_x;
PointerY = reply->root_y;
mask = reply->mask;
free(reply);
}
return mask;
}
/**
** Wrap pointer.
**
** Updates cached #PointerX and #PointerY.
**
** @param window wrap pointer relative to this window
** @param x destination x-coordinate of pointer
** @param y destination y-coordinate of pointer
**
** @todo FIXME: split into request / reply.
*/
void PointerWrap(xcb_window_t window, int x, int y)
{
xcb_warp_pointer(Connection, XCB_NONE, window, 0, 0, 0, 0, x, y);
PointerQueryReply(PointerQueryRequest());
}
/**
** Set default cursor for pointer.
**
** @param window specifies window to get default cursor
*/
void PointerSetDefaultCursor(xcb_window_t window)
{
xcb_change_window_attributes(Connection, window, XCB_CW_CURSOR,
&Cursors.Default);
}
// ---------------------------------------------------------------------------
#ifndef USE_XCURSOR
/**
** Create pointer
**
** @param font cursor font id
** @param index character index for cursor
**
** @returns cursor id of new created cursor
*/
static xcb_cursor_t CursorCreate(xcb_font_t font, int index)
{
xcb_cursor_t cursor;
cursor = xcb_generate_id(Connection);
xcb_create_glyph_cursor(Connection, cursor, font, font, index, index + 1,
0, 0, 0, 65535, 65535, 65535);
return cursor;
}
#endif
/**
** Prepare initialize pointers.
*/
void PointerPreInit(void)
{
PointerCookie = PointerQueryRequest();
}
/**
** Initialize pointers.
*/
void PointerInit(void)
{
unsigned u;
// check if the order in Cursors changed
IfDebug(if ((void *)&Cursors != &Cursors.Default
|| (&Cursors.Choose - &Cursors.Default) + 1 !=
sizeof(Cursors) / sizeof(Cursors.Choose)) {
FatalError("Order of elements in CursorTable changed\n");}
) ;
#ifdef USE_XCURSOR
if (xcb_cursor_context_new(Connection, XcbScreen, &CursorCtx) < 0) {
Error("Could not initialize xcb-cursor");
return;
}
for (u = 0; u < sizeof(Cursors) / sizeof(Cursors.Default); ++u) {
// must fit to CursorTable
static const char *table[] = { "left_ptr", "fleur", "top_side",
"bottom_side", "right_side", "left_side", "top_right_corner",
"top_left_corner", "bottom_right_corner", "bottom_left_corner",
"tcross"
};
((xcb_cursor_t *) & Cursors)[u] =
xcb_cursor_load_cursor(CursorCtx, table[u]);
}
#else
if (1) {
xcb_font_t font;
font = xcb_generate_id(Connection);
xcb_open_font(Connection, font, sizeof(CURSOR_FONT) - 1, CURSOR_FONT);
for (u = 0; u < sizeof(Cursors) / sizeof(Cursors.Default); ++u) {
// must fit to CursorTable
static const uint32_t table[] =
{ XC_left_ptr, XC_fleur, XC_top_side,
XC_bottom_side, XC_right_side, XC_left_side,
XC_top_right_corner,
XC_top_left_corner, XC_bottom_right_corner,
XC_bottom_left_corner,
XC_tcross
};
((xcb_cursor_t *) & Cursors)[u] = CursorCreate(font, table[u]);
}
xcb_close_font(Connection, font);
}
#endif
PointerQueryReply(PointerCookie);
}
/**
** Cleanup the pointer module.
*/
void PointerExit(void)
{
xcb_cursor_t *cursor;
for (cursor = &Cursors.Default; cursor <= &Cursors.Choose; ++cursor) {
xcb_free_cursor(Connection, *cursor);
*cursor = XCB_NONE;
}
#ifdef USE_XCURSOR
xcb_cursor_context_free(CursorCtx);
#endif
}
/// @}