summaryrefslogtreecommitdiff
path: root/src/wormouse.cpp
blob: 1d41844b58ad6dd1719a197ebd7591c921b07680 (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
/**
 * 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 "wormouse.h"

COMPIZ_PLUGIN_20090315 (wormouse, WormousePluginVTable);

/*boilerplate */
WormouseScreen::WormouseScreen (CompScreen *screen):
    PluginClassHandler <WormouseScreen, CompScreen> (screen),
    cr (NULL), img (NULL), work (NULL), 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));
  
  if (!updateConfig(NULL, Options()))
    setFailed ();
}

WormouseScreen::~WormouseScreen ()
{
  freeResources ();
}

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 ();

  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);

  // 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;
  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 = XcursorImageLoadCursor (screen->dpy(), xci);
  XDefineCursor (screen->dpy (), screen->root (), cursor);
  XFlush (screen->dpy ());

  // 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 ();
  //XUndefineCursor(screen->dpy(), screen->root());
  if (cursor) {
    XFreeCursor (screen->dpy(), cursor);
    cursor = 0;
  }
  if (xci) {
    XcursorImageDestroy (xci);
    xci = NULL;
  }
  if (cr) {
    cairo_destroy (cr);
    cr = NULL;
  }
  if (img) {
    cairo_surface_destroy (img);
    img = NULL;
  }
  if (work) {
    cairo_surface_destroy (work);
    work = NULL;
  }
}

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;
}

bool
WormousePluginVTable::init ()
{
    if (!CompPlugin::checkPluginABI ("core", CORE_ABIVERSION))
	 return false;
    //TODO: query Xfixes / Xcursor extensions

    return true;
}