summaryrefslogtreecommitdiffstats
path: root/src/gui/kernel/qeventdispatcher_pepper.cpp
blob: e0f4dce068d9a1c6ab6bd68cd6bf3f821baaeedc (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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qeventdispatcher_pepper_p.h"

#include "qplatformdefs.h"
#include "qapplication.h"
#include "qwaitcondition.h"
#include "qevent.h"
#include "qmutex.h"
#include "qdebug.h"
#include "qobject.h"
#include <errno.h>
#include <stdio.h>

#include <private/qapplication_p.h>
#include <time.h>

#include <nacl/nacl_npapi.h>
#include <nacl/npruntime.h>
#include <nacl/npupp.h>

#include "event_handler.h"

QT_BEGIN_NAMESPACE

QT_USE_NAMESPACE

QEventDispatcherPepper *qt_pepper_envent_dispatcher = 0;

class QEventDispatcherPepperPrivate : public QEventDispatcherUNIXPrivate
{
    Q_DECLARE_PUBLIC(QEventDispatcherPepper)
public:
    inline QEventDispatcherPepperPrivate()
    { }

};

bool QEventDispatcherPepper::hasPepperSupport()
{
    bool hasSupport = false;
    fprintf(stderr, "hasPepperSupport %d\n", hasSupport);
    return hasSupport;
}

extern "C" {
NPError NP_Initialize(NPNetscapeFuncs* browser_funcs,
                            NPPluginFuncs* plugin_funcs);
NPError NP_GetEntryPoints(NPPluginFuncs* plugin_funcs);
NPError NP_Shutdown();
NPError NP_GetValue(void* instance, NPPVariable variable, void* value);
char* NP_GetMIMEDescription();
}

QEventDispatcherPepper::QEventDispatcherPepper(QObject *parent)
    : QEventDispatcherUNIX(*new QEventDispatcherPepperPrivate, parent)
{ 
    // Reference the NP entry points without actually calling
    // them at run-time. This is neccasary to make sure they
    // are linked into the executable.
    // ### must be a better way...    
    volatile bool val = false;
    if (val) {    
        NP_Initialize(0,0);
        NP_GetEntryPoints(0);
        NP_Shutdown();
        NP_GetValue(0, NPPVariable(), 0);
        NP_GetMIMEDescription();
    }
    qt_pepper_envent_dispatcher = this;
    mainThreadWaiting = false;
}

QEventDispatcherPepper::~QEventDispatcherPepper()
{ }


bool QEventDispatcherPepper::processEvents(QEventLoop::ProcessEventsFlags flags)
{
    int eventCount = 0;

    QMutexLocker lock(&mutex);
    
    forever {
        // Possibly send timer events. The plugin thread sets
        // this when it recieves a timer callback. Do this
        // (once) before processing input events to ensure timely
        // timer delivery.
        if (callActivateTimers) {
            callActivateTimers = false;
            lock.unlock();
            activateTimers();
            lock.relock();
        }

        // Process all events in the queue. Release the lock while
        // processing - the plugin thread can add more events.
        while (eventQueue.isEmpty() == false) {
            NPPepperEvent event = eventQueue.dequeue();
            
            lock.unlock();
            ++eventCount;        
            processPepperEvent(&event);
 
           // Possibly send more timer events.        
           if (callActivateTimers) {
               callActivateTimers = false;
               activateTimers();
           }

           lock.relock();
        }

        if (!(flags & QEventLoop::WaitForMoreEvents)) {
            break;
        }

        // Check the queue again while holding the lock to
        // prevent sleeping after the plugin thread has added
        // an event and decided that the qt thread is active.
        if (callActivateTimers || eventQueue.isEmpty() == false)
            continue;

        // Wait until the plugin thread adds more events
        // to the queue and signals eventReady.        
        mainThreadWaiting = true;
        eventReady.wait(&mutex);
        mainThreadWaiting = false;
    }
    return (eventCount > 0);
}

bool QEventDispatcherPepper::hasPendingEvents()
{
    qDebug() << "QEventDispatcherPepper::hasPendingEvents";      
    extern uint qGlobalPostedEventsCount(); // from qapplication.cpp
    return qGlobalPostedEventsCount();
}

void QEventDispatcherPepper::registerSocketNotifier(QSocketNotifier *notifier)
{
    Q_UNUSED(notifier);
}

void QEventDispatcherPepper::unregisterSocketNotifier(QSocketNotifier *notifier)
{
    Q_UNUSED(notifier);
}

/*
    Timer support. Do the simplest thing possible for now,
    re-use the QTimerInfoList from the unix dispatcher.
    Call NPN_ScheduleTimer for each Qt timer, but
    simply call activateTimers() in the callback and let
    the timer list class figure out which timer to fire.

    Some complexity is added because of the dual thread
    system (Qt main thread and the plugin thread).
*/

/*
    Timer callback. Called in the the plugin thread.
*/
void qt_pepper_timer_callback(NPP npp, uint32 timerID)
{
    Q_UNUSED(npp);
    Q_UNUSED(timerID);
    qDebug() << "Pepper timer callback" << timerID;
    qt_pepper_envent_dispatcher->queueActivateTimers();
}

struct QtPepperTimerInfo
{
    int interval;
};

/*
    Timer registration function. Called in the plugin thread.
*/
void qt_pepper_registerTimer(void *data)
{
    QtPepperTimerInfo *timerInfo = static_cast<QtPepperTimerInfo *>(data);

 // uint32 NPN_ScheduleTimer(NPP npp, uint32 interval, NPBool repeat, void (*timerFunc)(NPP npp, uint32 timerID));
    extern NPNetscapeFuncs* browser;
    extern EventHandler* event_handler;
    // ### disabled (no NPN_ScheduleTimer call in nacl)
    // uint32 pepperId = browser->scheduletimer(event_handler->npp_, timerInfo->interval, true, qt_pepper_timer_callback);
    qDebug() << "qt_pepper_registerTimer" << timerInfo->interval; // << "pepper id" << pepperId;
    delete timerInfo;
}

void QEventDispatcherPepper::registerTimer(int timerId, int interval, QObject *object)
{
    qDebug() << "register timer" << timerId << interval << object;
    QtPepperTimerInfo *timerInfo = new QtPepperTimerInfo();
    timerInfo->interval = interval;    

    // register the timer in the plugin thread.    
    NPN_PluginThreadAsyncCall(event_handler->npp_, qt_pepper_registerTimer, timerInfo);
    return QEventDispatcherUNIX::registerTimer(timerId, interval, object);
}

bool QEventDispatcherPepper::unregisterTimer(int timerId)
{
    qDebug() << "unregister timer" << timerId;
    return QEventDispatcherUNIX::unregisterTimer(timerId);
}

bool QEventDispatcherPepper::unregisterTimers(QObject *object)
{
    qDebug() << "unregister timers" << object;
    return QEventDispatcherUNIX::unregisterTimers(object);
}

void QEventDispatcherPepper::startingUp()
{

}

void QEventDispatcherPepper::closingDown()
{

}

void QEventDispatcherPepper::wakeUp()
{

}

void QEventDispatcherPepper::interrupt()
{

}

void QEventDispatcherPepper::flush()
{
    if(qApp)
        qApp->sendPostedEvents();
}
/*
    Add an event to the queue and wake the main thread.
    The event is copied.
*/
void QEventDispatcherPepper::queueEvent(NPPepperEvent *event)
{
    QMutexLocker lock(&mutex);
    eventQueue.enqueue(*event); // queue a copy.
    if (mainThreadWaiting) {
        eventReady.wakeOne();
    }
}
/*
    Set a flag indicating that timers should fire and
    wake the main thread.
*/
void QEventDispatcherPepper::queueActivateTimers()
{
    callActivateTimers = true;
    QMutexLocker lock(&mutex);
    if (mainThreadWaiting) {
        eventReady.wakeOne();
    }
}

void QEventDispatcherPepper::processPepperEvent(NPPepperEvent *event)
{
    switch (event->type) {
        case NPEventType_MouseDown:
        case NPEventType_MouseUp:
        case NPEventType_MouseMove:
            processMouseEvent(&event->u.mouse, event->type);
        break;
        case NPEventType_KeyDown:
        case NPEventType_KeyUp:
            processKeyEvent(&event->u.key, event->type);
        break;
        case NPEventType_Char:
            processCharacterEvent(&event->u.character);
        break;
        case NPEventType_Minimize:
            processMinimizeEvent(&event->u.minimize);
        break;
        case NPEventType_Focus:
            processFocusEvent(&event->u.focus);
        break;
        case NPEventType_Device:
            processDeviceEvent(&event->u.device);
        break;
        case NPEventType_MouseEnter:
        case NPEventType_MouseLeave:
        case NPEventType_MouseWheel:
        default:
        break;
    }
}

Qt::KeyboardModifiers pepperKeyModifiersToQt(int32 modifier)
{
    // translate supported modifiers (first five pepper modifers).
    // pepper starts at 0x1, Qt starts at 0x02000000. (same order)
    return Qt::KeyboardModifiers((modifier & 0x1F)<< 25); 
}

void QEventDispatcherPepper::processKeyEvent(NPKeyEvent *key, uint32 eventType)
{
/*
  uint32 modifier;
  uint32 normalizedKeyCode;
*/    
    Qt::KeyboardModifiers modifiers = pepperKeyModifiersToQt(key->modifier);
    
    if (eventType == NPEventType_KeyDown) {
        qDebug() << "Key Down" << key->normalizedKeyCode << hex << modifiers;
        QKeyEvent keyEvent(QEvent::KeyPress, key->normalizedKeyCode, modifiers,
                           QString());
        QApplicationPrivate::handleKeyEvent(0, &keyEvent);
        
    }
    
    if (eventType == NPEventType_KeyUp) {
        qDebug() << "Key Up" << key->normalizedKeyCode << hex << modifiers;
        QKeyEvent keyEvent(QEvent::KeyRelease, key->normalizedKeyCode, modifiers,
                           QString());
        QApplicationPrivate::handleKeyEvent(0, &keyEvent);
    }
}

void QEventDispatcherPepper::processCharacterEvent(NPCharacterEvent *character)
{
/*
  uint32 modifier;
  uint16 text[4];
  uint16 unmodifiedText[4];
*/
    QString text(reinterpret_cast<QChar *>(character->text), 4);
    Qt::KeyboardModifiers modifiers = pepperKeyModifiersToQt(character->modifier);

    qDebug() << "CharacterEvent" << text << modifiers;

    QKeyEvent keyEvent(QEvent::KeyPress, 0, modifiers, text);
    QApplicationPrivate::handleKeyEvent(0, &keyEvent);
}

Qt::MouseButton pepperMouseButtonToQt(int32 pepperButton)
{
    Qt::MouseButton button;
    if (pepperButton == NPMouseButton_Left)
        button = Qt::LeftButton;
    if (pepperButton == NPMouseButton_Middle)
        button = Qt::MidButton;
    if (pepperButton == NPMouseButton_Right)
        button = Qt::RightButton;
    return button;
}

Qt::MouseButtons pepperMouseModifiersToQt(uint32 modifier)
{
    Qt::MouseButtons buttons;
    if (modifier & NPEventModifier_LeftButtonDown)
        buttons |= Qt::LeftButton;
    if (modifier & NPEventModifier_RightButtonDown)
        buttons |= Qt::RightButton;
    if (modifier & NPEventModifier_MiddleButtonDown)
        buttons |= Qt::MidButton;
    
    return buttons;
}

void QEventDispatcherPepper::processMouseEvent(NPMouseEvent *mouse, uint32 eventType)
{
/*
  NPMouseEvent:
  uint32 modifier;
  int32 button;
  int32 x;
  int32 y;
  int32 clickCount;
*/
    QPoint p(mouse->x, mouse->y);
    Qt::MouseButton button = pepperMouseButtonToQt(mouse->button);
    Qt::MouseButtons mouseModifiers = pepperMouseModifiersToQt(mouse->modifier);
    Qt::KeyboardModifiers keyModifiers = pepperKeyModifiersToQt(mouse->modifier);

    
    if (eventType == NPEventType_MouseDown) {
        qDebug() << "mouse down event" << p << button << mouse->button << mouse->modifier;
        
        QMouseEvent mouseEvent(QEvent::MouseButtonPress, p, p, button,
                               mouseModifiers, keyModifiers);
        QApplicationPrivate::handleMouseEvent(0, mouseEvent);
    }
    
    else if (eventType == NPEventType_MouseUp) {
        qDebug() << "mouse up event" << p << button << mouse->button << mouse->modifier;
        QMouseEvent mouseEvent(QEvent::MouseButtonRelease, p, p, button,
                               mouseModifiers, keyModifiers);
        QApplicationPrivate::handleMouseEvent(0, mouseEvent);
    }
    
    else if (eventType == NPEventType_MouseMove) {
        qDebug() << "mouse move event" << p << button << mouse->button << mouse->modifier;
        QMouseEvent mouseEvent(QEvent::MouseMove, p, p,
                               Qt::NoButton,
                               mouseModifiers,
                               keyModifiers);
        QApplicationPrivate::handleMouseEvent(0, mouseEvent);
    }
}

void QEventDispatcherPepper::processMouseWheelEvent(NPMouseWheelEvent *wheel)
{
/*
  uint32 modifier;
  float deltaX;
  float deltaY;
  float wheelTicksX;
  float wheelTicksY;
  uint32 scrollByPage;
*/
    Qt::KeyboardModifiers keyModifiers = pepperKeyModifiersToQt(wheel->modifier);
    Qt::MouseButtons mouseModifiers = pepperMouseModifiersToQt(wheel->modifier);

    if (wheel->wheelTicksX != 0) {
        QWheelEvent wheelEvent(QPoint(), wheel->wheelTicksX, mouseModifiers, keyModifiers, Qt::Horizontal);
        QApplicationPrivate::handleWheelEvent(0, wheelEvent);
    }
    if (wheel->wheelTicksY != 0) {
        QWheelEvent wheelEvent(QPoint(), wheel->wheelTicksY, mouseModifiers, keyModifiers, Qt::Vertical);
        QApplicationPrivate::handleWheelEvent(0, wheelEvent);
    }
}

void QEventDispatcherPepper::processMinimizeEvent(NPMinimizeEvent *minimize)
{
    Q_UNUSED(minimize)
/*
  int32 value;
*/
}

void QEventDispatcherPepper::processFocusEvent(NPFocusEvent *focus)
{
    Q_UNUSED(focus)
/*
  int32 value;
*/
}

void QEventDispatcherPepper::processDeviceEvent(NPDeviceEvent *device)
{
    Q_UNUSED(device)
/*  
  uint32 device_uid;
  uint32 subtype;
*/
}


QT_END_NAMESPACE