summaryrefslogtreecommitdiffstats
path: root/src/plugins/graphicssystems/minimaldfb/qdirectfbinput.cpp
blob: 4ba4ff8178c0137d92e18a7e2650f3cf7efab33c (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
#include "qdirectfbinput.h"
#include "qdirectfbconvenience.h"

#include <QThread>
#include <QDebug>
#include <private/qapplication_p.h>
#include <QMouseEvent>
#include <QEvent>

#include <directfb/directfb.h>

InputSocketWaiter::InputSocketWaiter(IDirectFBEventBuffer *eventBuffer, QObject *parent)
        : QThread(parent), eventBuffer(eventBuffer)
    {
        this->start();
    }

void InputSocketWaiter::run()
{
    while (1) {
        eventBuffer->WaitForEvent(eventBuffer);
        emit newEvent();
    }
}

QDirectFbInput::QDirectFbInput(QObject *parent)
    : QObject(parent)
{
    DFBResult ok = DirectFBCreate(&dfbInterface);
    if (ok != DFB_OK)
        DirectFBError("Failed to initialise QDirectFBInput", ok);

    ok = dfbInterface->CreateEventBuffer(dfbInterface,&eventBuffer);
    if (ok != DFB_OK)
        DirectFBError("Failed to initialise eventbuffer", ok);

    dfbInterface->GetDisplayLayer(dfbInterface,DLID_PRIMARY, &dfbDisplayLayer);

    InputSocketWaiter *inputHandler = new InputSocketWaiter(eventBuffer,this);
    connect(inputHandler,SIGNAL(newEvent()),this,SLOT(handleEvents()));
}

void QDirectFbInput::addWindow(DFBWindowID id, QWidget *tlw)
{
    tlwMap.insert(id,tlw);
    IDirectFBWindow *window;
    dfbDisplayLayer->GetWindow(dfbDisplayLayer,id,&window);

//    window->DisableEvents(window,DWET_ALL);
    window->EnableEvents(window,DFBWindowEventType(DWET_ALL));
    window->SetKeySelection(window,DWKS_ALL,NULL,0);
    window->AttachEventBuffer(window,eventBuffer);
}

void QDirectFbInput::handleEvents()
{
    DFBResult hasEvent = eventBuffer->HasEvent(eventBuffer);
    while(hasEvent == DFB_OK){
        DFBEvent event;
        DFBResult ok = eventBuffer->GetEvent(eventBuffer,&event);
        if (ok != DFB_OK)
            DirectFBError("Failed to get event",ok);
        if (event.clazz == DFEC_WINDOW) {
            switch (event.window.type) {
            case DWET_BUTTONDOWN:
            case DWET_BUTTONUP:
//            case DWET_MOTION:
            case DWET_WHEEL:
                handleMouseEvents(event);
                break;
            case DWET_KEYDOWN:
                qDebug() << "FOOOOBAR!";
            }

        } else
            qDebug() << "WHAT!";

        hasEvent = eventBuffer->HasEvent(eventBuffer);
    }
}

void QDirectFbInput::handleMouseEvents(const DFBEvent &event)
{
    QEvent::Type type = QDirectFbConvenience::eventType(event.window.type);
    QPoint p(event.window.cx, event.window.cy);
    QPoint globalPos = globalPoint(event);
    Qt::MouseButton button = QDirectFbConvenience::mouseButton(event.window.button);
    Qt::MouseButtons buttons = QDirectFbConvenience::mouseButtons(event.window.buttons);

    qDebug() << QDirectFbConvenience::keyMap()->value(event.window.key_symbol);
    QMouseEvent mouseEvent(type,p,globalPos,button, buttons,(Qt::KeyboardModifiers)0);
    QWidget *tlw = tlwMap.value(event.window.window_id);
    QApplicationPrivate::handleMouseEvent(tlw,mouseEvent);
}

inline QPoint QDirectFbInput::globalPoint(const DFBEvent &event) const
{
    IDirectFBWindow *window;
    dfbDisplayLayer->GetWindow(dfbDisplayLayer,event.window.window_id,&window);
    int x,y;
    window->GetPosition(window,&x,&y);
    return QPoint(event.window.cx +x, event.window.cy + y);
}