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
|
/**
* wormouse.cpp
*
* Copyright (c) 2010 Rodolfo Granata <warlock.cc@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* 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 General Public License for more details.
*
**/
#include <math.h>
#include <stdlib.h>
#include "wormouse.h"
COMPIZ_PLUGIN_20090315 (wormouse, WormousePluginVTable);
bool
WormouseScreen::updateConfig (CompOption *opt, Options num)
{
show_hotspot = optionGetShowHotspot ();
// Try absolute path
CompString path = optionGetCursorImage ();
if (loadCursor (path, optionGetHotspotX (), optionGetHotspotY (),
optionGetTailFactor (), optionGetImageAngle()))
return true;
// Try to load image from standard path
path = IMAGEDIR + ("/images/" + optionGetCursorImage ());
if (loadCursor (path, optionGetHotspotX (), optionGetHotspotY (),
optionGetTailFactor (), optionGetImageAngle()))
return true;
return false;
}
bool
WormouseScreen::loadCursor (CompString &path, int hotx, int hoty,
float tail_dist, float image_angle)
{
freeResources ();
#ifdef _CACHE_ROTATION_
if (!buildCache (path))
return false;
#else
img = cairo_image_surface_create_from_png (path.c_str ());
if (CAIRO_STATUS_SUCCESS != cairo_surface_status (img)) {
compLogMessage ("wormouse", CompLogLevelError,
"Failed to load cursor image [%s]\n", path.c_str ());
return false;
}
// calculate dimensions needed for rotate image
wrk_w = cairo_image_surface_get_width (img);
wrk_h = cairo_image_surface_get_height (img);
wrk_r = sqrt(wrk_w*wrk_w/4.0 + wrk_h*wrk_h/4.0);
// create a destination canvas
work = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 2*wrk_r, 2*wrk_r);
cr = cairo_create (work);
// clear canvas
cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
//cairo_set_source_rgba (cr, 0.2, 0.2, 0.2, 0.2);
cairo_paint(cr);
// center our image in the newly created context
cairo_translate (cr, (2.0*wrk_r-wrk_w)/2, (2.0*wrk_r-wrk_h)/2);
cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
cairo_set_source_surface (cr, img, 0, 0);
cairo_paint(cr);
#endif
// create a cursor image and set it
xci = XcursorImageCreate (2*wrk_r, 2*wrk_r);
xci->xhot = (2.0*wrk_r-wrk_w)/2 + hotx;
xci->yhot = (2.0*wrk_r-wrk_h)/2 + hoty;
#ifdef _CACHE_ROTATION_
xci->pixels = imageCache[0];
#else
xci->pixels = (XcursorPixel *)cairo_image_surface_get_data (work);
if (show_hotspot) {
cairo_identity_matrix (cr);
cairo_set_source_rgba (cr, 1, 0, 0, 1);
cairo_set_line_width (cr, 1.0);
cairo_arc (cr, xci->xhot, xci->yhot, 1.0, 0, 2 * M_PI);
cairo_stroke (cr);
}
#endif
Cursor original_cursor = screen->normalCursor ();
cursor = XcursorImageLoadCursor (screen->dpy(), xci);
XFixesChangeCursor (screen->dpy (), cursor, original_cursor);
// using cairo coordinates (inverted y axis)
hot_a = atan2f(xci->yhot - wrk_r, xci->xhot - wrk_r);
hot_d = sqrt((xci->xhot - wrk_r) * (xci->xhot - wrk_r) +
(xci->yhot - wrk_r) * (xci->yhot - wrk_r));
tail_d = tail_dist;
wrk_a = image_angle * M_PI / 180.0;
poller.start ();
return true;
}
void
WormouseScreen::freeResources ()
{
poller.stop ();
if (cursor) {
XFreeCursor (screen->dpy(), cursor);
cursor = 0;
}
if (xci) {
XcursorImageDestroy (xci);
xci = NULL;
}
#ifdef _CACHE_ROTATION_
int a;
for (a = 0; a < 360; a++)
if (imageCache[a]) {
free (imageCache[a]);
imageCache[a] = NULL;
}
#else
if (cr) {
cairo_destroy (cr);
cr = NULL;
}
if (img) {
cairo_surface_destroy (img);
img = NULL;
}
if (work) {
cairo_surface_destroy (work);
work = NULL;
}
#endif
}
#ifdef _CACHE_ROTATION_
void
WormouseScreen::mouseMove (const CompPoint &pos)
{
// get the new angle we're traveling
// needent invert y axis since cairo uses same coord system
float a = atan2f(pos.y() - piv_y, pos.x() - piv_x);
// update pivoting position
piv_x = pos.x() - tail_d * cosf(a);
piv_y = pos.y() - tail_d * sinf(a);
// compensate image orientation
a += wrk_a;
// create a cursor image and set it
xci->xhot = wrk_r + hot_d * cosf(hot_a + a);
xci->yhot = wrk_r + hot_d * sinf(hot_a + a);
xci->pixels = imageCache[ (int)(360 + a * 180.0 / M_PI) % 360 ];
Cursor newcur = XcursorImageLoadCursor (screen->dpy(), xci);
XFixesChangeCursor (screen->dpy (), newcur, cursor);
XFreeCursor (screen->dpy (), cursor);
cursor = newcur;
}
#else
void
WormouseScreen::mouseMove (const CompPoint &pos)
{
// get the new angle we're traveling
// needent invert y axis since cairo uses same coord system
float a = atan2f(pos.y() - piv_y, pos.x() - piv_x);
// update pivoting position
piv_x = pos.x() - tail_d * cosf(a);
piv_y = pos.y() - tail_d * sinf(a);
cairo_identity_matrix (cr);
cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
//cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
//cairo_set_source_rgba (cr, 0.2, 0.2, 0.2, 0.2);
cairo_paint(cr);
// compensate image orientation
a += wrk_a;
// move to the center, rotate, translate back an to the final place
cairo_translate (cr, wrk_r, wrk_r);
cairo_rotate (cr, a);
cairo_translate (cr, -wrk_w/2.0, -wrk_h/2.0);
cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
cairo_set_source_surface (cr, img, 0, 0);
cairo_paint(cr);
// create a cursor image and set it
xci->xhot = wrk_r + hot_d * cosf(hot_a + a);
xci->yhot = wrk_r + hot_d * sinf(hot_a + a);
xci->pixels = (XcursorPixel *)cairo_image_surface_get_data (work);
if (show_hotspot) {
cairo_identity_matrix (cr);
cairo_set_source_rgba (cr, 1, 0, 0, 1);
cairo_set_line_width (cr, 1.0);
cairo_arc (cr, xci->xhot, xci->yhot, 1.0, 0, 2 * M_PI);
cairo_stroke (cr);
}
Cursor newcur = XcursorImageLoadCursor (screen->dpy(), xci);
XFixesChangeCursor (screen->dpy (), newcur, cursor);
XFreeCursor (screen->dpy (), cursor);
cursor = newcur;
}
#endif
#ifdef _CACHE_ROTATION_
bool
WormouseScreen::buildCache (const CompString &path)
{
cairo_surface_t *img = cairo_image_surface_create_from_png (path.c_str ());
if (CAIRO_STATUS_SUCCESS != cairo_surface_status (img)) {
compLogMessage ("wormouse", CompLogLevelError,
"Failed to load cursor image [%s]\n", path.c_str ());
return false;
}
// calculate dimensions needed for rotate image
wrk_w = cairo_image_surface_get_width (img);
wrk_h = cairo_image_surface_get_height (img);
wrk_r = sqrt(wrk_w*wrk_w/4.0 + wrk_h*wrk_h/4.0);
// create a destination canvas
cairo_surface_t *work = cairo_image_surface_create (
CAIRO_FORMAT_ARGB32, 2*wrk_r, 2*wrk_r);
cairo_t *cr = cairo_create (work);
int a;
for (a = 0; a < 360; a++) {
// clear background
cairo_identity_matrix (cr);
cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
cairo_paint(cr);
// move to the center, rotate, translate back an to the final place
cairo_translate (cr, wrk_r, wrk_r);
cairo_rotate (cr, a * M_PI / 180.0);
cairo_translate (cr, -wrk_w/2.0, -wrk_h/2.0);
// paint rotate image
cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
cairo_set_source_surface (cr, img, 0, 0);
cairo_paint(cr);
if (imageCache[a]) free (imageCache[a]);
// cache the image
int stride = cairo_image_surface_get_stride (work);
void *data = cairo_image_surface_get_data (work);
imageCache[a] = (XcursorPixel *)malloc((int)(wrk_r * wrk_r * stride));
memcpy(imageCache[a], data, wrk_r * wrk_r * stride);
}
cairo_destroy (cr);
cairo_surface_destroy (img);
cairo_surface_destroy (work);
return true;
}
#endif
/*boilerplate */
WormouseScreen::WormouseScreen (CompScreen *screen):
PluginClassHandler <WormouseScreen, CompScreen> (screen),
#ifndef _CACHE_ROTATION_
img (NULL), work (NULL), cr (NULL),
#endif
cursor (0), xci (NULL)
{
poller.setCallback (boost::bind (&WormouseScreen::mouseMove, this, _1));
optionSetCursorImageNotify(boost::bind (&WormouseScreen::updateConfig, this, _1, _2));
optionSetShowHotspotNotify(boost::bind (&WormouseScreen::updateConfig, this, _1, _2));
optionSetHotspotXNotify(boost::bind (&WormouseScreen::updateConfig, this, _1, _2));
optionSetHotspotYNotify(boost::bind (&WormouseScreen::updateConfig, this, _1, _2));
optionSetTailFactorNotify(boost::bind (&WormouseScreen::updateConfig, this, _1, _2));
#ifdef _CACHE_ROTATION_
memset (imageCache, 0, sizeof(imageCache));
#endif
if (!updateConfig(NULL, Options()))
setFailed ();
}
WormouseScreen::~WormouseScreen ()
{
freeResources ();
}
bool
WormousePluginVTable::init ()
{
if (!CompPlugin::checkPluginABI ("core", CORE_ABIVERSION) ||
!CompPlugin::checkPluginABI ("mousepoll", COMPIZ_MOUSEPOLL_ABI))
return false;
int fixesEventBase, fixesErrorBase;
if (!XFixesQueryExtension(screen->dpy (), &fixesEventBase, &fixesErrorBase))
return false;
return true;
}
|