summaryrefslogtreecommitdiff
path: root/src/Overlay.cpp
blob: 4f10134187f004fb1277dd48252ea3f08bdaa689 (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
/* $Id: Overlay.cpp,v 1.10 2007/01/07 05:00:56 whot Exp $ */

/*--
  --*/

#include "Overlay.h"
#include <stdlib.h>
#include <string.h>

Overlay::Overlay(Manager* manager, WMWindow* client, XConn* x11)
{
    this->client = client;
    this->manager = manager;
    this->x11 = x11;
    this->width = client->width;
    this->height = client->height;

    layers.clear();
    initWindows();
}

Overlay::~Overlay()
{
    map<int, OverlayLayer*>::iterator it = layers.begin();
    while(it != layers.end())
    {
        delete it->second;
        it++;
    }
}

void Overlay::initWindows() 
{
    vector<PointerDevice*>* pointers = manager->getPointers();

    vector<PointerDevice*>::iterator it = pointers->begin();

    TRACE("creating with height %d\n", height);
    while(it != pointers->end())
    {
        OverlayLayer* layer = new OverlayLayer;

        layer->gcvals.line_width = Config::getInstance()->drawLineWidth;
        layer->gcvals.line_style = LineSolid;
        layer->gcvals.foreground = 0;

        layer->dev = *it;

        layer->win = XCreateSimpleWindow(x11->dpy, client->container, 0,
                Config::getInstance()->windowBarHeight, width, height, 0,
                CopyFromParent, layer->dev->getColor()); 
        layer->mask = XCreatePixmap(x11->dpy, client->container, width,
                height, 1);


        layer->canvas = XCreateGC(x11->dpy, layer->mask, 
                GCForeground | GCLineWidth | GCLineStyle, &layer->gcvals); 

        layer->lastX = layer->lastY = 0;
        layer->visible = false;
        XFillRectangle(x11->dpy, layer->mask, layer->canvas, 0, 0, width, height);
        XSetForeground(x11->dpy, layer->canvas, 1);
        XShapeCombineMask(x11->dpy, layer->win, 
                          ShapeBounding, 0, 0, 
                          layer->mask, ShapeSet); 
        layers[layer->dev->getID()] = layer;
        it++;
    }

    XFlush(x11->dpy);
}


/*
   toggle the layer state of the given layer.
 */
void Overlay::toggle(int id) 
{
    if (!layers[id]->visible)
    {
        TRACE("Overlay on\n");
        XMapRaised(x11->dpy, layers[id]->win);
        layers[id]->visible = !layers[id]->visible;
    } else
    {
        TRACE("Overlay off\n");
        XUnmapWindow(x11->dpy, layers[id]->win);
        layers[id]->visible = !layers[id]->visible;
    }
}

/*
   Returns true if the layer for the given device ID is visible.
 */
bool Overlay::isVisible(int deviceID)
{
    return layers[deviceID]->visible;
}

/* 
   returns true if any of the layers is activated
 */
bool Overlay::isVisible()
{
    map<int, OverlayLayer*>::iterator it = layers.begin();
    while(it != layers.end())
    {
        if (it->second->visible)
            return true;

        it++;
    }
    return false;
}

/*
   Handles the given event. 
   This method returns true if the event was handled properly. If false is
   returned, the event needs to be sent to the client.
 */
bool Overlay::handleMotionEvent(XDeviceMotionEvent* mev, PointerDevice* dev)
{
    OverlayLayer* layer = layers[mev->deviceid];
    if (!layer->visible)
        return false;

    if (!mev->device_state)
    {
        layer->lastX = mev->x;
        layer->lastY = mev->y;
        return true;
    }

    if (mev->device_state & Button1Mask)
    {
        if (!layer->gcvals.foreground)
        {
            layer->gcvals.foreground = 1;
            layer->gcvals.line_width = Config::getInstance()->drawLineWidth;
            XChangeGC(x11->dpy, layers[mev->deviceid]->canvas, 
                    GCForeground | GCLineWidth, &layer->gcvals); 
        }
    } else if (mev->device_state & Button3Mask)
    {
        if (layer->gcvals.foreground) 
        {
            layer->gcvals.foreground = 0;
            layer->gcvals.line_width = Config::getInstance()->eraseLineWidth;
            XChangeGC(x11->dpy, layers[mev->deviceid]->canvas, 
                    GCForeground | GCLineWidth, &layer->gcvals); 
        }
    }


    XDrawLine(x11->dpy, layer->mask, layer->canvas, layer->lastX, layer->lastY,
            mev->x, mev->y);
    layer->lastX = mev->x;
    layer->lastY = mev->y;
    XShapeCombineMask(x11->dpy, layer->win, ShapeBounding, 0, 0, layer->mask,
            ShapeSet);

    XFlush(x11->dpy);
    TRACE("Line to %d/%d\n", layer->lastX, layer->lastY);
    return true;
}

void Overlay::handlePressEvent(XDeviceButtonEvent* bev)
{

    XGCValues gcvals; 

    if (bev->state & Button1Mask)
    {
        gcvals.line_width = 4;
        gcvals.foreground = 1;
    }
    else if (bev->state & Button3Mask)
    {
        gcvals.line_width = 25;
        gcvals.foreground = 0;
    } else return;

    XChangeGC(x11->dpy, layers[bev->deviceid]->canvas, 
            GCForeground | GCLineWidth, &gcvals); 
    XFlush(x11->dpy);

    layers[bev->deviceid]->lastX = bev->x;
    layers[bev->deviceid]->lastY = bev->y;
}

/* 
   returns true if either the drawing window or the input window 
   is the given window.
*/

bool Overlay::hasWindow(Window win)
{
    map<int, OverlayLayer*>::iterator it = layers.begin();

    while(it != layers.end())
    {
        if (it->second->win == win || it->second->mask == win)
            return true;
        it++;
    }

    return false;
}

/*
  Resizes the overlay window. We need to first resize the input and the
  drawing window, then create a new pixmap and copy the contents over. 
  Pixmaps cannot be resized.
  */
void Overlay::resize(int width, int height)
{
    XWindowChanges wc;
    wc.width = width;
    wc.height = height;

    map<int, OverlayLayer*>::iterator it = layers.begin();

    XGCValues gcvals; 
    gcvals.line_width = 4;
    gcvals.line_style = LineSolid;
    gcvals.foreground = 0;

    while (it != layers.end())
    {
        OverlayLayer* layer = it->second;


        XConfigureWindow(x11->dpy, layer->win, CWWidth | CWHeight, &wc);
        Pixmap newmask = XCreatePixmap(x11->dpy, layer->win, width, height + 30, 1);
        GC newcanvas = XCreateGC(x11->dpy, newmask, 
                GCForeground | GCLineWidth | GCLineStyle, &gcvals);

        XFillRectangle(x11->dpy, newmask, newcanvas, 0, 0, width, height);
        XSetForeground(x11->dpy, newcanvas, 1);

        XCopyArea(x11->dpy, layer->mask, newmask, newcanvas, 0, 0, this->width, this->height, 0, 0);

        XFreePixmap(x11->dpy, layer->mask);
        XFreeGC(x11->dpy, layer->canvas);
        layer->mask = newmask;
        layer->canvas = newcanvas;

        it++;
    }

    XFlush(x11->dpy);
    this->width = width;
    this->height = height;
    TRACE("Overlay resized to %d/%d\n", width, height);
}

void Overlay::clean(int id)
{
    XSetForeground(x11->dpy, layers[id]->canvas, 0);
    XFillRectangle(x11->dpy, layers[id]->mask, layers[id]->canvas, 0, 0, width, height);
    XSetForeground(x11->dpy, layers[id]->canvas, 1);
    XShapeCombineMask(x11->dpy, layers[id]->win, ShapeBounding, 0, 0, layers[id]->mask,
            ShapeSet);
    XFlush(x11->dpy);
}