~ubuntu-branches/ubuntu/raring/recorditnow/raring

« back to all changes in this revision

Viewing changes to src/cursorwidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Felix Geyer
  • Date: 2011-01-09 14:54:01 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110109145401-gyckb4airz4fio50
Tags: 0.8.1-0ubuntu1
* New upstream release. (LP: #681270)
  - Update debian/copyright.
* Build-depend on recordmydesktop.
* Add a watch file.
* Drop 01_fix_ftbfs_kwarning_call.diff, fixed upstream.
* Add 01_joschy_install_to_usr_lib.diff.
* Add 02_fix_ftbfs_no-add-needed.diff.
* Add 03_dont_install_header_files.diff.
* Replace dependency on libpolkit-qt-1-0 with policykit-1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
// own
22
22
#include "cursorwidget.h"
23
 
#include "mousebutton.h"
24
 
#include "keymon/device.h"
25
 
#include "keymon/manager.h"
 
23
#include "config/mousebutton.h"
 
24
#include "keymonmanager.h"
26
25
 
27
26
// KDE
28
 
#include <kdeversion.h>
29
 
#if KDE_IS_VERSION(4,3,80)
30
 
    #include <kauth.h>
31
 
#endif
32
27
#include <kdebug.h>
33
28
#include <kapplication.h>
34
29
#include <kwindowsystem.h>
51
46
#include <X11/extensions/shape.h>
52
47
 
53
48
 
 
49
#define RESET_TIME 600
54
50
CursorWidget::CursorWidget(QWidget *parent)
55
51
    : QWidget(parent, Qt::X11BypassWindowManagerHint|Qt::FramelessWindowHint|Qt::Tool)
56
52
{
61
57
    // _UGLY_ HACK: Prevents KWin to draw a shadow
62
58
    setAttribute(Qt::WA_X11NetWmWindowTypeDock);
63
59
 
64
 
    m_useKeyMon = false;
65
60
    m_grab = false;
66
 
    m_device = 0;
67
61
    m_mode = LEDMode;
68
62
    m_opacity = 0.4;
 
63
    m_show = true;
69
64
 
70
65
    m_timer = new QTimer(this);
71
66
    connect(m_timer, SIGNAL(timeout()), this, SLOT(updatePos()));
81
76
    m_normalColor = Qt::black;
82
77
    m_currentColor = m_normalColor;
83
78
 
 
79
    connect(KeyMonManager::self(), SIGNAL(keyEvent(KeyMon::Event)), this,
 
80
            SLOT(buttonPressed(KeyMon::Event)));
 
81
 
84
82
}
85
83
 
86
84
 
87
85
CursorWidget::~CursorWidget()
88
86
{
89
87
 
90
 
    updateGrab(false);
91
 
 
92
88
    delete m_timer;
93
89
    delete m_resetTimer;
94
90
 
104
100
}
105
101
 
106
102
 
107
 
void CursorWidget::setNormalColor(const QColor &color)
108
 
{
109
 
 
110
 
    if (m_normalColor == color) {
111
 
        return;
112
 
    }
113
 
 
114
 
    m_normalColor = color;
115
 
    resetColor();
116
 
    update();
117
 
 
118
 
}
119
 
 
120
 
 
121
103
void CursorWidget::setButtons(const QHash<int, QColor> &buttons)
122
104
{
123
105
 
126
108
}
127
109
 
128
110
 
129
 
void CursorWidget::setUseKeyMon(const bool &use, const QString &deviceName)
 
111
void CursorWidget::setDevice(const QString &deviceName)
130
112
{
131
113
 
132
 
    m_useKeyMon = use;
133
114
    m_deviceName = deviceName;
134
115
 
135
116
}
166
147
        return;
167
148
    }
168
149
 
169
 
    m_opacity = opacity;
 
150
    const double max = 1.0;
 
151
    const double onePercent = max/100;
 
152
    const double value = opacity*onePercent;
 
153
 
 
154
    m_opacity = value;
170
155
    update();
171
156
 
172
157
}
173
158
 
174
159
 
 
160
void CursorWidget::setShowAlways(const bool &show)
 
161
{
 
162
 
 
163
    m_show = show;
 
164
 
 
165
}
 
166
 
 
167
 
175
168
void CursorWidget::switchToPreviewMode()
176
169
{
177
170
 
187
180
void CursorWidget::start()
188
181
{
189
182
 
190
 
    m_timer->start(1000/25);
191
 
    show();
192
 
    updateGrab(false); // ungrab old buttons
193
 
    updateGrab(true);
 
183
    m_timer->start(1000/50);
 
184
 
 
185
    if (m_show) {
 
186
        show();
 
187
    }
194
188
 
195
189
}
196
190
 
199
193
{
200
194
 
201
195
    hide();
202
 
    updateGrab(false);
203
196
    deleteLater();
204
197
 
205
198
}
206
199
 
207
200
 
208
 
 
209
 
void CursorWidget::click(const int &button)
210
 
{
211
 
 
212
 
    if (m_resetTimer->isActive()) {
213
 
        m_resetTimer->stop();
214
 
    }
215
 
 
216
 
    m_currentColor = m_buttons[button];
217
 
    XAllowEvents(x11Info().display(), ReplayPointer, CurrentTime);
218
 
    update();
219
 
 
220
 
    m_resetTimer->start(350);
221
 
 
222
 
}
223
 
 
224
 
 
225
 
 
226
 
WId CursorWidget::getWindow() const
227
 
{
228
 
 
229
 
    return static_cast<QWidget*>(parent())->winId();
230
 
 
231
 
}
232
 
 
233
 
 
234
201
void CursorWidget::updatePos()
235
202
{
236
203
 
269
236
{
270
237
 
271
238
    m_currentColor = m_normalColor;
272
 
    update();
273
 
 
274
 
}
275
 
 
276
 
 
277
 
void CursorWidget::updateGrab(const bool &grab)
278
 
{
279
 
 
280
 
    QHashIterator<int, QColor> it(m_buttons);
281
 
    const int screen = x11Info().appScreen();
282
 
    if (grab) {
283
 
        if (!m_useKeyMon) {
284
 
            while (it.hasNext()) {
285
 
                it.next();
286
 
                KXErrorHandler handler;
287
 
                XGrabButton(x11Info().display(),
288
 
                            it.key(),
289
 
                            AnyModifier,
290
 
                            x11Info().appRootWindow(screen),
291
 
                            True,
292
 
                            ButtonPressMask,
293
 
                            GrabModeSync,
294
 
                            GrabModeAsync,
295
 
                            x11Info().appRootWindow(screen),
296
 
                            None);
297
 
                if (handler.error(true)) {
298
 
                    XErrorEvent event = handler.errorEvent();
299
 
                    if (event.error_code == 10) { // BadAccess
300
 
                        MouseButton::Button button = MouseButton::getButtonFromXButton(it.key());
301
 
                        const QString buttonName = MouseButton::getName(button);
302
 
                        emit error(i18n("Grab failed!\n"
303
 
                                        "Perhaps the button \"%1\" is already grabbed by another"
304
 
                                        " application.", buttonName));
305
 
                    }
306
 
                }
307
 
            }
308
 
        } else {
309
 
#if KDE_IS_VERSION(4,3,80)
310
 
            m_grab = true;
311
 
            KAuth::Action action("org.kde.recorditnow.helper.watch");
312
 
            action.setHelperID("org.kde.recorditnow.helper");
313
 
            connect(action.watcher(), SIGNAL(progressStep(QVariantMap)), this,
314
 
                    SLOT(progressStep(QVariantMap)));
315
 
 
316
 
            QVariantMap args;
317
 
            args["Device"] = m_deviceName;
318
 
            action.setArguments(args);
319
 
            action.setExecutesAsync(true);
320
 
 
321
 
            KAuth::ActionReply reply = action.execute("org.kde.recorditnow.helper");
322
 
            if (reply.type() != 2) {
323
 
                hide();
324
 
                emit error(i18n("Grab failed!"));
325
 
            }
326
 
#else
327
 
            m_device = KeyMon::Manager::watch(m_deviceName, this);
328
 
            if (m_device) {
329
 
                connect(m_device, SIGNAL(buttonPressed(KeyMon::Event)), this,
330
 
                        SLOT(buttonPressed(KeyMon::Event)));
331
 
            } else {
332
 
                emit error(i18n("Grab failed!"));
333
 
            }
334
 
#endif
335
 
        }
 
239
 
 
240
    if (m_show) {
 
241
        update();
336
242
    } else {
337
 
        while (it.hasNext()) {
338
 
            it.next();
339
 
            XUngrabButton(x11Info().display(),
340
 
                          it.key(),
341
 
                          AnyModifier,
342
 
                          x11Info().appRootWindow(screen));
343
 
        }
344
 
#if KDE_IS_VERSION(4,3,80)
345
 
        if (m_grab) {
346
 
            KAuth::Action action;
347
 
            action.setName("watch");
348
 
            action.setHelperID("org.kde.recorditnow.helper");
349
 
            action.stop();
350
 
            m_grab = false;
351
 
        }
352
 
#else
353
 
        if (m_device) {
354
 
            m_device->disconnect(this);
355
 
            m_device->deleteLater();
356
 
            m_device = 0;
357
 
        }
358
 
#endif
 
243
        hide();
359
244
    }
360
245
 
361
246
}
364
249
void CursorWidget::buttonPressed(const KeyMon::Event &event)
365
250
{
366
251
 
 
252
    if (!event.mouseEvent) {
 
253
        return;
 
254
    }
 
255
 
367
256
    if (m_resetTimer->isActive()) {
368
257
        m_resetTimer->stop();
369
258
    }
370
259
 
 
260
    if (!isVisible()) {
 
261
        show();
 
262
    }
 
263
 
371
264
    switch (event.key) {
372
265
    case KeyMon::Event::WheelUp:
373
266
    case KeyMon::Event::WheelDown: {
374
267
            m_currentColor = m_buttons[event.keyToXButton(event.key)];
375
 
            m_resetTimer->start(350);
 
268
            m_resetTimer->start(RESET_TIME);
376
269
            break;
377
270
        }
378
271
    default: {
379
272
            if (event.pressed) {
380
273
                m_currentColor = m_buttons[event.keyToXButton(event.key)];
381
274
            } else {
382
 
                m_currentColor = m_normalColor;
 
275
                m_resetTimer->start(RESET_TIME);
383
276
            }
384
277
            break;
385
278
        }
389
282
}
390
283
 
391
284
 
392
 
void CursorWidget::progressStep(const QVariantMap &data)
393
 
{
394
 
 
395
 
#if KDE_IS_VERSION(4,3,80)
396
 
    KeyMon::Event event;
397
 
    event.key = static_cast<KeyMon::Event::Key>(data["Key"].toInt());
398
 
    event.pressed = data["Pressed"].toBool();
399
 
    buttonPressed(event);
400
 
#else
401
 
    Q_UNUSED(data);
402
 
#endif
403
 
 
404
 
}
405
 
 
406
285
 
407
286
void CursorWidget::previewColors()
408
287
{