summaryrefslogtreecommitdiff
path: root/src/PointerDevice.cpp
blob: 603a7a252084815ef073ab74f4256a0ba15efcc6 (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
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
/* $Id: PointerDevice.cpp,v 1.17 2007/01/07 05:00:56 whot Exp $ */

/*--
 --*/

#include "PointerDevice.h"
#include <X11/Xcursor/Xcursor.h>

int PointerDevice::XI_MotionNotify;
int PointerDevice::XI_ButtonPress;
int PointerDevice::XI_ButtonRelease;

int PointerDevice::counter = 0;

PointerDevice::PointerDevice(XDeviceInfo* dev, XConn* x11, Manager* manager)
{
    XDevice* d = XOpenDevice(x11->dpy, dev->id);

    if (!d)
        throw new DeviceError(DeviceError::OPEN_FAILED);

    this->x11 = x11;
    this->pointerID = PointerDevice::counter++;
    TRACE("Creating pointer %d\n", pointerID);
    this->name = string(dev->name);
    this->id = dev->id;
    this->dev = d;

    this->dragWindow = NULL;
    this->resizeWindow = NULL;

    XEventClass motion, press, release;

    DeviceMotionNotify(d, XI_MotionNotify, motion);
    TRACE("XI_MotionNotify is %d\n", (int)XI_MotionNotify);
    TRACE("Motion class is %d\n", (int)motion);

    DeviceButtonPress(d, XI_ButtonPress, press);
    TRACE("XI_ButtonPress is %d\n", (int)XI_ButtonPress);
    TRACE("ButtonPress class is %d\n", (int)press);

    DeviceButtonRelease(d, XI_ButtonRelease, release);
    TRACE("XI_ButtonRelease is %d\n", (int)XI_ButtonRelease);
    TRACE("ButtonRelease class is %d\n", (int)release);

    evclasses[XI_MotionNotify] = motion;
    evclasses[XI_ButtonPress] = press;
    evclasses[XI_ButtonRelease] = release;

    XSelectExtensionEvent(x11->dpy, x11->root, &motion, 1);
    color = Config::getInstance()->cursorColor(pointerID);

    generatePointerImage(dev->id);

    Cursor cursor = XcursorFilenameLoadCursor(x11->dpy, "/tmp/.mpwm_pointer.cur");
    XDefineDeviceCursor(x11->dpy, d, x11->root, cursor);

    TRACE("Device %d (%s) initialised\n", 
            (unsigned int)dev->id, dev->name);
}

/**
 * Move the client's window to the given position.
 */
void PointerDevice::dragTo(int x, int y)
{
    if (dragWindow != NULL)
        dragWindow->move(x - dragOffset[0] , y - dragOffset[1]);
    else
        ERR("DragTo where? No window given.\n");
}

void PointerDevice::dragOff()
{
    if (!dragWindow)
    {
        ERR("Drag off when no window was dragged.\n");
        return;
    }

    dragWindow->releaseController();
    dragWindow = NULL;
    dragOffset[0] = dragOffset[1] = 0;

    XEventClass motion = evclasses[XI_MotionNotify];
    XSelectExtensionEvent(x11->dpy, x11->root, &motion, 1);
    XUngrabDevice(x11->dpy, this->dev, CurrentTime);
    TRACE("Ungrabbing device.\n");
    XFlush(x11->dpy);

}

bool PointerDevice::isDragging()
{
    return (dragWindow != NULL);
}

/**
 * Switches dragging on for the given window. 
 * The coordinates are used for the offset to maintain the window's relative
 * position to the mouse cursor.
 */
bool PointerDevice::dragOn(WMWindow* win, int x, int y)
{
    if (win == NULL)
    {
        ERR("NULL window given for drag\n");
        return false;
    }
        
    if (win->setController(this))
    {
        dragWindow = win;
        dragOffset[0] = x;
        dragOffset[1] = y;
        
        // in case we get out of the window we still need to catch the release
        XEventClass classes[2] = {evclasses[XI_MotionNotify], evclasses[XI_ButtonRelease]};
        XSelectExtensionEvent(x11->dpy, x11->root, classes, 2);
        XGrabDevice(x11->dpy, this->dev, win->getWindowBar(), True, 2, 
                classes, GrabModeAsync, GrabModeAsync, CurrentTime);
        return true;
    }

    return false;
}

bool PointerDevice::resizeOn(WMWindow* win, Window button, int x, int y)
{
    if (win == NULL)
    {
        ERR("NULL window given for resize\n");
        return false;
    }

    if (win->addResizer(this))
    {
        TRACE("Device %s is resizing\n", name.c_str());
        resizeWindow = win;
        resizeOffset[0] = x;
        resizeOffset[1] = y;
        resizeButton = button;

        XEventClass classes[3] = {evclasses[XI_MotionNotify], 
                                  evclasses[XI_ButtonPress], 
                                  evclasses[XI_ButtonRelease]};
        XGrabDevice(x11->dpy, this->dev, win->getWindowBar(), 
                True, 3, classes, GrabModeAsync, GrabModeAsync, CurrentTime);
        return true;
    }

    return false;
}

void PointerDevice::resizeOff()
{
    if (!resizeWindow)
    {
        ERR("Resize off when no window was being resized.\n");
        return;
    }
    
    resizeWindow->releaseResizer(this);
    resizeWindow = NULL;
    resizeOffset[0] = resizeOffset[1] = 0;

    XUngrabDevice(x11->dpy, this->dev, CurrentTime);
}

bool PointerDevice::isResizing()
{
    return resizeWindow != NULL;
}

void PointerDevice::resizeTo(int x, int y)
{
    if (resizeWindow != NULL)
    {
        resizeWindow->resizeDirected(resizeButton, 
                                     x - resizeOffset[0] , 
                                     y - resizeOffset[1]); 
        resizeOffset[0] = x;
        resizeOffset[1] = y;
    }
    else
        ERR("resizeTo where? No window given.\n");
}

/**
 * Enables device events on the given windows. 
 */
void PointerDevice::setWMEvents(WMWindow* window)
{
    XEventClass classes[3];

    classes[0] = evclasses[XI_MotionNotify];
    classes[1] = evclasses[XI_ButtonPress];
    classes[2] = evclasses[XI_ButtonRelease];

    XSelectExtensionEvent(x11->dpy, window->getWindowBar(), &classes[0], 3);
    //XSelectExtensionEvent(x11->dpy, window->getResizeBar(), &classes[0], 3);
    XSelectExtensionEvent(x11->dpy, window->getButtonClose(), &classes[1], 2);
    XSelectExtensionEvent(x11->dpy, window->getButtonFloor(), &classes[1], 2);
    XSelectExtensionEvent(x11->dpy, window->getButtonOverlay(), &classes[1], 1);
    XSelectExtensionEvent(x11->dpy, window->getButtonMinimize(), &classes[1], 1);

    XFlush(x11->dpy);
    TRACE("Events set on windows %x, %x, %x\n", (int)window->getWindowBar(),
            (int)window->getButtonClose(), (int)window->getButtonFloor());
    TRACE(" -- events are %d, %d, %d\n", (int)classes[0], (int)classes[1],
            (int)classes[2]);

    XSelectExtensionEvent(x11->dpy, window->getResizeBtNE(), &classes[1], 1);
    XSelectExtensionEvent(x11->dpy, window->getResizeBtNW(), &classes[1], 1);
    XSelectExtensionEvent(x11->dpy, window->getResizeBtSE(), &classes[1], 1);
    XSelectExtensionEvent(x11->dpy, window->getResizeBtSW(), &classes[1], 1);

    XGrabDeviceButton(x11->dpy, dev, Button1, AnyModifier, 
            NULL, window->getClientWindow(), False, 
            0, NULL, GrabModeSync, GrabModeSync);
}

void PointerDevice::setDockEvents(Window win)
{
    XEventClass evclass = evclasses[XI_ButtonPress];
    XSelectExtensionEvent(x11->dpy, win, &evclass, 1);
}

/**
 * Sets the ButtonPressEvent mask for the given window. 
 */
void PointerDevice::setButtonPressEventMask(Window win)
{
    XEventClass evclass = evclasses[XI_ButtonPress];
    XSelectExtensionEvent(x11->dpy, win, &evclass, 1);
}

void PointerDevice::generatePointerImage(int number){
    int bare_cursor_width,
        bare_cursor_height,
        total_width,
        total_height;
    stringstream s;
    s << number;
    std::string text = std::string(s.str());

    cairo_surface_t* dummy_surface;
    cairo_surface_t* main_surface;
    cairo_t* cr;

    TRACE("Generating cursor from %s\n",Config::getInstance()->crsImage);
    cairo_surface_t* png_cursor = 
	    cairo_image_surface_create_from_png(Config::getInstance()->crsImage);
    bare_cursor_width = cairo_image_surface_get_width(png_cursor);
    bare_cursor_height = cairo_image_surface_get_height(png_cursor);
    
    dummy_surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,10,10);
    cr = cairo_create(dummy_surface);
    cairo_text_extents_t est;
    cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_NORMAL, 
            CAIRO_FONT_WEIGHT_BOLD);
    cairo_set_font_size (cr, Config::getInstance()->idFontSize);

    cairo_text_extents(cr,text.c_str(),&est);
    TRACE("Generated ID pointer text (%2.0fx%2.0f)\n",est.width,est.height);

    total_width = (int)(Config::getInstance()->idXOffset + est.width + est.x_bearing);	
    total_height = (int)(Config::getInstance()->idYOffset + est.height + est.y_bearing);	

    main_surface = cairo_image_surface_create(
            CAIRO_FORMAT_ARGB32,
            total_width,
            total_height
            );

    cr = cairo_create(main_surface);
    cairo_set_source_surface(cr,png_cursor,0,0);
    cairo_paint (cr);

    cairo_select_font_face (cr, "Sans", CAIRO_FONT_SLANT_NORMAL, 
            CAIRO_FONT_WEIGHT_BOLD);
    cairo_set_font_size (cr, Config::getInstance()->idFontSize);

    cairo_set_source_rgb(cr,255,0,0);
    cairo_move_to(cr,Config::getInstance()->idXOffset,Config::getInstance()->idYOffset);
    cairo_show_text(cr,text.c_str());
    
    cairo_surface_write_to_png(main_surface,"/tmp/.mpwm_pointer.png");
    
    cairo_destroy(cr);
    cairo_surface_destroy(dummy_surface);
    cairo_surface_destroy(main_surface);
    cairo_surface_destroy(png_cursor);

    /*FIXME: I should use internal API*/
    /*/usr/include/X11/Xcursor/Xcursor.h*/
    /*XCursorImage* XcursorFileLoadImage(FILE* file,int size)*/
    /*Cursor XcursorImageLoadCursor(Display* dpy,const XcursorImage* image)*/
    system("echo \"24 0 0 /tmp/.mpwm_pointer.png \" > /tmp/.mpwm_pointer.cfg");
    system(XCURSORGEN" /tmp/.mpwm_pointer.cfg /tmp/.mpwm_pointer.cur");
}