Changeset View
Changeset View
Standalone View
Standalone View
kcms/keyboard/tastenbrett/main.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright 2019 Harald Sitter <sitter@kde.org> | ||||
3 | | ||||
4 | This program is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU General Public License as | ||||
6 | published by the Free Software Foundation; either version 2 of | ||||
7 | the License or (at your option) version 3 or any later version | ||||
8 | accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | defined in Section 14 of version 3 of the license. | ||||
11 | | ||||
12 | This program is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | GNU General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU General Public License | ||||
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include <QCommandLineParser> | ||||
22 | #include <QDebug> | ||||
23 | #include <QQmlApplicationEngine> | ||||
24 | #include <QQmlContext> | ||||
25 | #include <QX11Info> | ||||
26 | | ||||
27 | #include "application.h" | ||||
28 | #include "config-workspace.h" | ||||
29 | #include "doodad.h" | ||||
30 | #include "geometry.h" | ||||
31 | | ||||
32 | // kind-of copy from xkb_rules.cpp (less complicated) | ||||
33 | static QString getRulesName() | ||||
34 | { | ||||
35 | XkbRF_VarDefsRec vd; | ||||
36 | char *tmp = nullptr; | ||||
37 | | ||||
38 | if (XkbRF_GetNamesProp(QX11Info::display(), &tmp, &vd) && tmp != nullptr) { | ||||
39 | const QString name(tmp); | ||||
40 | XFree(tmp); | ||||
41 | return name; | ||||
42 | } | ||||
43 | | ||||
44 | return QStringLiteral("evdev"); // default to evdev | ||||
45 | } | ||||
46 | | ||||
47 | static QString findXkbRulesFile() | ||||
48 | { | ||||
49 | const QString rulesName = getRulesName(); | ||||
50 | return QStringLiteral("%1/rules/%2").arg(XKBDIR, rulesName); | ||||
51 | } | ||||
52 | | ||||
53 | int main(int argc, char *argv[]) | ||||
54 | { | ||||
55 | setenv("QT_QPA_PLATFORM", "xcb", 1); | ||||
56 | QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); | ||||
57 | Application app(argc, argv); | ||||
58 | Q_ASSERT(app.platformName() == QStringLiteral("xcb")); | ||||
59 | | ||||
60 | QCommandLineParser parser; | ||||
61 | parser.addHelpOption(); | ||||
62 | parser.addVersionOption(); | ||||
63 | QCommandLineOption modelOption(QStringList { "m", "model" }, {}, QStringLiteral("MODEL")); | ||||
64 | parser.addOption(modelOption); | ||||
65 | QCommandLineOption layoutOption(QStringList { "l", "layout" }, {}, QStringLiteral("LAYOUT")); | ||||
66 | parser.addOption(layoutOption); | ||||
67 | QCommandLineOption variantOption(QStringList { "a", "variant" }, {}, QStringLiteral("VARIANT")); | ||||
68 | parser.addOption(variantOption); | ||||
69 | QCommandLineOption optionsOption(QStringList { "o", "options" }, {}, QStringLiteral("OPTIONS")); | ||||
70 | parser.addOption(optionsOption); | ||||
71 | parser.process(app); | ||||
72 | | ||||
73 | XkbRF_VarDefsRec varDefs; | ||||
74 | memset(&varDefs, 0, sizeof(XkbRF_VarDefsRec)); | ||||
75 | | ||||
76 | // Models worth testing for obvious mistakes: | ||||
77 | // pc104, tm2020 (fancy), kinesis (fancy) | ||||
78 | QString model = parser.value(modelOption); | ||||
79 | const QString layout = parser.value(layoutOption); | ||||
80 | const QString variant = parser.value(variantOption); | ||||
81 | const QString options = parser.value(optionsOption); | ||||
82 | | ||||
83 | // Hold these so so we can pass data into xkb getter. | ||||
84 | QByteArray modelArray = model.toUtf8(); | ||||
85 | QByteArray layoutArray = layout.toUtf8(); | ||||
86 | QByteArray variantArray = variant.toUtf8(); | ||||
87 | QByteArray optionsArray = options.toUtf8(); | ||||
88 | | ||||
89 | varDefs.model = modelArray.data(); | ||||
90 | varDefs.layout = layoutArray.data(); | ||||
91 | varDefs.variant = variantArray.data(); | ||||
92 | varDefs.options = optionsArray.data(); | ||||
93 | | ||||
94 | XkbRF_RulesPtr rules = XkbRF_Load(findXkbRulesFile().toUtf8().data(), // needs to be non-const! | ||||
95 | qgetenv("LOCALE").data(), | ||||
96 | True, | ||||
97 | True); | ||||
98 | Q_ASSERT(rules); | ||||
99 | QSharedPointer<XkbRF_RulesRec> rulesCleanup(rules, [](XkbRF_RulesPtr obj) { | ||||
100 | XkbRF_Free(obj, True); | ||||
101 | }); | ||||
102 | | ||||
103 | XkbComponentNamesRec componentNames; | ||||
104 | memset(&componentNames, 0, sizeof(XkbComponentNamesRec)); | ||||
105 | | ||||
106 | XkbRF_GetComponents(rules, &varDefs, &componentNames); | ||||
107 | | ||||
108 | XkbDescPtr xkb = XkbGetKeyboardByName(QX11Info::display(), | ||||
109 | XkbUseCoreKbd, | ||||
110 | &componentNames, | ||||
111 | 0, | ||||
112 | XkbGBN_GeometryMask | | ||||
113 | XkbGBN_KeyNamesMask | | ||||
114 | XkbGBN_OtherNamesMask | | ||||
115 | XkbGBN_ClientSymbolsMask | | ||||
116 | XkbGBN_IndicatorMapMask, | ||||
117 | false); | ||||
118 | Q_ASSERT(xkb); | ||||
119 | QSharedPointer<XkbDescRec> xkbCleanup(xkb, [](XkbDescPtr obj) { | ||||
120 | XkbFreeKeyboard(obj, 0, True); | ||||
121 | }); | ||||
122 | | ||||
123 | Geometry geometry(xkb->geom, xkb); | ||||
124 | | ||||
125 | // Register the doodads so we can perform easy type checks with them | ||||
126 | // and determine how to render the individual object. | ||||
127 | const char uri[] = "org.kde.tastenbrett.private"; | ||||
128 | qmlRegisterUncreatableType<TextDoodad>(uri, 1, 0, "TextDoodad", QString()); | ||||
129 | qmlRegisterUncreatableType<LogoDoodad>(uri, 1, 0, "LogoDoodad", QString()); | ||||
130 | qmlRegisterUncreatableType<ShapeDoodad>(uri, 1, 0, "ShapeDoodad", QString()); | ||||
131 | qmlRegisterUncreatableType<IndicatorDoodad>(uri, 1, 0, "IndicatorDoodad", QString()); | ||||
132 | | ||||
133 | // The way this is currently written we need the engine after | ||||
134 | // we have a geometry, lest geometry is dtor'd before the engine | ||||
135 | // causing exhaustive error spam on shutdown. | ||||
136 | // Also, the above stuff is blocking, but optimizing it is hardly | ||||
137 | // worth the effort. The Xkb calls altogether take ~8ms (I am not | ||||
138 | // certain putting xkb into a qfuture is thread-safe or even | ||||
139 | // faster). Constructing our QObjects takes 1ms. | ||||
140 | QQmlApplicationEngine engine; | ||||
141 | const QUrl url(QStringLiteral("qrc:/qml/main.qml")); | ||||
142 | QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, | ||||
143 | &app, [url](QObject *obj, const QUrl &objUrl) { | ||||
144 | if (!obj && url == objUrl) | ||||
145 | QCoreApplication::exit(-1); | ||||
146 | }, Qt::QueuedConnection); | ||||
147 | engine.rootContext()->setContextProperty("geometry", &geometry); | ||||
148 | engine.load(url); | ||||
149 | | ||||
150 | return app.exec(); | ||||
151 | } |