Skip to content

Commit 320c061

Browse files
committed
Added hardware button handler.
1 parent def7348 commit 320c061

8 files changed

+153
-16
lines changed

DivvyDroid.pro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ SOURCES += \
2929
device/adbclient.cpp \
3030
device/deviceinfo.cpp \
3131
device/videothread.cpp \
32+
input/devicebuttonhandler.cpp \
3233
input/devicetouchhandler.cpp \
3334
input/inputhandler.cpp \
3435
main.cpp \
@@ -38,6 +39,7 @@ HEADERS += \
3839
device/adbclient.h \
3940
device/deviceinfo.h \
4041
device/videothread.h \
42+
input/devicebuttonhandler.h \
4143
input/devicetouchhandler.h \
4244
input/inputhandler.h \
4345
input/input-event-codes.h \

device/adbclient.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ AdbClient::AdbClient(QObject *parent)
1919
#endif
2020
}
2121

22+
AdbClient::~AdbClient()
23+
{
24+
m_sock.abort();
25+
}
26+
2227
bool
2328
AdbClient::write(const void *data, qint64 max)
2429
{

device/adbclient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class AdbClient : public QObject
2222
{
2323
public:
2424
explicit AdbClient(QObject *parent = nullptr);
25+
virtual ~AdbClient();
2526

2627
void connectToHost() { m_sock.connectToHost(QStringLiteral("localhost"), 5037, QIODevice::ReadWrite); }
2728
bool connectToDevice();

device/deviceinfo.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,22 @@ DeviceInfo::connect(const char *deviceId)
104104
qDebug() << "INPUT device" << devIndex << name << "is touch screen";
105105
aDev->m_inputTouch = devIndex;
106106
} else if(HAS_BIT(evBits, EV_SYN) && HAS_BIT(evBits, EV_KEY)) {
107-
if(inputHasKey(keyBits, KEY_HOME) && inputHasKey(keyBits, KEY_BACK)) {
108-
qDebug() << "INPUT device" << devIndex << name << "has some home/back key";
107+
qDebug() << "INPUT device" << devIndex << name << "has some keys";
108+
if(inputHasKey(keyBits, KEY_HOMEPAGE)) {
109+
qDebug() << "INPUT device" << devIndex << name << "has home key";
109110
aDev->m_inputHome = devIndex;
110-
} else if(inputHasKey(keyBits, KEY_POWER)) {
111-
qDebug() << "INPUT device" << devIndex << name << "has some power key";
111+
}
112+
if(inputHasKey(keyBits, KEY_BACK)) {
113+
qDebug() << "INPUT device" << devIndex << name << "has back key";
114+
aDev->m_inputBack = devIndex;
115+
}
116+
if(inputHasKey(keyBits, KEY_POWER)) {
117+
qDebug() << "INPUT device" << devIndex << name << "has power key";
112118
aDev->m_inputPower = devIndex;
113-
} else if(inputHasKey(keyBits, KEY_VOLUMEUP) && inputHasKey(keyBits, KEY_VOLUMEDOWN)) {
114-
qDebug() << "INPUT device" << devIndex << name << "has some volume keys";
119+
}
120+
if(inputHasKey(keyBits, KEY_VOLUMEUP) && inputHasKey(keyBits, KEY_VOLUMEDOWN)) {
121+
qDebug() << "INPUT device" << devIndex << name << "has volume keys";
115122
aDev->m_inputVolume = devIndex;
116-
} else {
117-
qDebug() << "INPUT device" << devIndex << name << "has some keys";
118123
}
119124
} else {
120125
qDebug() << "INPUT device" << devIndex << name << "is not supported";

device/deviceinfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class DeviceInfo
2828
inline int inputTouch() const { return m_inputTouch; }
2929
inline int inputPower() const { return m_inputPower; }
3030
inline int inputHome() const { return m_inputHome; }
31+
inline int inputBack() const { return m_inputBack; }
3132
inline int inputVolume() const { return m_inputVolume; }
3233

3334
static DeviceList deviceList();
@@ -42,6 +43,7 @@ class DeviceInfo
4243
int m_inputTouch;
4344
int m_inputPower;
4445
int m_inputHome;
46+
int m_inputBack;
4547
int m_inputVolume;
4648
quint32 m_screenWidth;
4749
quint32 m_screenHeight;

input/devicebuttonhandler.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "devicebuttonhandler.h"
2+
3+
#include "device/deviceinfo.h"
4+
#include "device/adbclient.h"
5+
#include "input/input-event-codes.h"
6+
7+
#include <QMouseEvent>
8+
9+
DeviceButtonHandler::DeviceButtonHandler(QObject *parent)
10+
: InputHandler(parent)
11+
{
12+
}
13+
14+
DeviceButtonHandler::~DeviceButtonHandler()
15+
{
16+
17+
}
18+
19+
bool
20+
DeviceButtonHandler::init(int deviceId, const WidgetKeyMap &keyMap)
21+
{
22+
if(deviceId == -1) {
23+
qDebug() << __FUNCTION__ << "failed opening device" << deviceId;
24+
return false;
25+
}
26+
27+
if(!InputHandler::init())
28+
return false;
29+
30+
m_adb.connectToDevice();
31+
if(!m_adb.send(QByteArray("dev:").append(INPUT_DEV_PATH).append(QString::number(deviceId)))) {
32+
qDebug() << __FUNCTION__ << "failed opening device" << deviceId;
33+
return false;
34+
}
35+
36+
m_keyMap = keyMap;
37+
for(QObject *obj : m_keyMap.keys())
38+
obj->installEventFilter(this);
39+
40+
return true;
41+
}
42+
43+
bool
44+
DeviceButtonHandler::eventFilter(QObject *obj, QEvent *ev)
45+
{
46+
if(!m_keyMap.contains(obj))
47+
return false;
48+
49+
const quint16 keyCode = m_keyMap[obj];
50+
51+
switch(ev->type()) {
52+
case QEvent::MouseButtonPress: {
53+
qDebug() << "KEY DOWN" << keyCode;
54+
m_adb.sendEvents(AdbEventList()
55+
<< AdbEvent(EV_KEY, keyCode, 1)
56+
<< AdbEvent(EV_SYN));
57+
return true;
58+
}
59+
case QEvent::MouseButtonRelease: {
60+
qDebug() << "KEY UP" << keyCode;
61+
m_adb.sendEvents(AdbEventList()
62+
<< AdbEvent(EV_KEY, keyCode, 0)
63+
<< AdbEvent(EV_SYN));
64+
return true;
65+
}
66+
67+
default:
68+
return false;
69+
}
70+
71+
return false;
72+
}

input/devicebuttonhandler.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef DEVICEBUTTONHANDLER_H
2+
#define DEVICEBUTTONHANDLER_H
3+
4+
#include "inputhandler.h"
5+
6+
#include <QMap>
7+
8+
typedef QMap<QObject *, quint16> WidgetKeyMap;
9+
10+
class DeviceButtonHandler : public InputHandler
11+
{
12+
public:
13+
explicit DeviceButtonHandler(QObject *parent = nullptr);
14+
virtual ~DeviceButtonHandler();
15+
16+
bool init(int deviceId, const WidgetKeyMap &keyMap);
17+
18+
protected:
19+
bool eventFilter(QObject *obj, QEvent *ev) override;
20+
21+
private:
22+
bool init() override { return false; }
23+
24+
WidgetKeyMap m_keyMap;
25+
};
26+
27+
#endif // DEVICEBUTTONHANDLER_H

mainwindow.cpp

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33

44
#include "device/deviceinfo.h"
55
#include "input/devicetouchhandler.h"
6+
#include "input/devicebuttonhandler.h"
67

78
#include <iostream>
89
#include <cstdarg>
910

11+
#include <QMouseEvent>
1012
#include <QDebug>
1113

1214
MainWindow::MainWindow(QWidget *parent)
@@ -47,14 +49,35 @@ MainWindow::init()
4749
if(touchHandler->init())
4850
ui->screen->installEventFilter(touchHandler);
4951

50-
// wake up
51-
/*
52-
AdbClient::sendEvents(m_keyDevice[KEY_POWER], AdbEventList()
53-
<< AdbEvent(EV_KEY, KEY_POWER, 1)
54-
<< AdbEvent(EV_SYN)
55-
<< AdbEvent(EV_KEY, KEY_POWER, 0)
56-
<< AdbEvent(EV_SYN));
57-
*/
52+
// init bottom keys
53+
(new DeviceButtonHandler(this))->init(
54+
aDev->inputHome(),
55+
WidgetKeyMap{{ ui->btnHome, KEY_HOMEPAGE }});
56+
(new DeviceButtonHandler(this))->init(
57+
aDev->inputBack(),
58+
WidgetKeyMap{{ ui->btnMenu, KEY_MENU }, { ui->btnBack, KEY_BACK }});
59+
60+
// init volume keys
61+
(new DeviceButtonHandler(this))->init(
62+
aDev->inputVolume(),
63+
WidgetKeyMap{{ ui->btnVolumeDown, KEY_VOLUMEDOWN }, { ui->btnVolumeUp, KEY_VOLUMEUP }});
64+
65+
// init power key
66+
(new DeviceButtonHandler(this))->init(
67+
aDev->inputPower(),
68+
WidgetKeyMap{{ ui->btnPower, KEY_POWER }});
69+
70+
// init unlock key
71+
connect(ui->btnUnlock, &QPushButton::clicked, [&](){
72+
if(!aDev->isScreenAwake()) {
73+
const QPoint pos(ui->btnPower->geometry().center());
74+
QCoreApplication::sendEvent(ui->btnPower,
75+
new QMouseEvent(QEvent::MouseButtonPress, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier));
76+
QCoreApplication::sendEvent(ui->btnPower,
77+
new QMouseEvent(QEvent::MouseButtonRelease, pos, Qt::LeftButton, Qt::LeftButton, Qt::NoModifier));
78+
}
79+
AdbClient::shell("wm dismiss-keyguard");
80+
});
5881
}
5982

6083
void

0 commit comments

Comments
 (0)