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 "geometry.h" | ||||
30 | | ||||
31 | // kind-of copy from xkb_rules.cpp (less complicated) | ||||
32 | static QString getRulesName() | ||||
33 | { | ||||
34 | XkbRF_VarDefsRec vd; | ||||
35 | char *tmp = nullptr; | ||||
36 | | ||||
37 | if (XkbRF_GetNamesProp(QX11Info::display(), &tmp, &vd) && tmp != nullptr ) { | ||||
38 | const QString name(tmp); | ||||
39 | XFree(tmp); | ||||
40 | return name; | ||||
41 | } | ||||
42 | | ||||
43 | return QStringLiteral("evdev"); // default to evdev | ||||
44 | } | ||||
45 | | ||||
46 | static QString findXkbRulesFile() | ||||
47 | { | ||||
48 | const QString rulesName = getRulesName(); | ||||
49 | return QStringLiteral("%1/rules/%2").arg(XKBDIR, rulesName); | ||||
50 | } | ||||
51 | | ||||
52 | int main(int argc, char *argv[]) | ||||
53 | { | ||||
54 | setenv("QT_QPA_PLATFORM", "xcb", 1); | ||||
55 | QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); | ||||
56 | Application app(argc, argv); | ||||
57 | Q_ASSERT(app.platformName() == QStringLiteral("xcb")); | ||||
58 | | ||||
59 | QCommandLineParser parser; | ||||
60 | parser.addHelpOption(); | ||||
61 | parser.addVersionOption(); | ||||
62 | QCommandLineOption modelOption(QStringList { "m", "model" }, {}, QStringLiteral("MODEL")); | ||||
63 | parser.addOption(modelOption); | ||||
64 | QCommandLineOption layoutOption(QStringList { "l", "layout" }, {}, QStringLiteral("LAYOUT")); | ||||
65 | parser.addOption(layoutOption); | ||||
66 | QCommandLineOption variantOption(QStringList { "a", "variant" }, {}, QStringLiteral("VARIANT")); | ||||
67 | parser.addOption(variantOption); | ||||
68 | QCommandLineOption optionsOption(QStringList { "o", "options" }, {}, QStringLiteral("OPTIONS")); | ||||
69 | parser.addOption(optionsOption); | ||||
70 | parser.process(app); | ||||
71 | | ||||
72 | XkbRF_VarDefsRec varDefs; | ||||
73 | memset(&varDefs, 0, sizeof(XkbRF_VarDefsRec)); | ||||
74 | | ||||
75 | QString model = parser.value(modelOption); | ||||
76 | // model = "kinesis"; // fancy model | ||||
77 | // model = "tm2020"; // fancy model | ||||
78 | // model = "pc102"; | ||||
79 | const QString layout = parser.value(layoutOption); | ||||
80 | const QString variant = parser.value(variantOption); | ||||
81 | const QString options = parser.value(optionsOption); | ||||
82 | qDebug() << "---- " << model << layout << variant << options; | ||||
83 | | ||||
84 | // Hold these so so we can pass data into xkb getter. | ||||
85 | QByteArray modelArray = model.toUtf8(); | ||||
86 | QByteArray layoutArray = layout.toUtf8(); | ||||
87 | QByteArray variantArray = variant.toUtf8(); | ||||
88 | QByteArray optionsArray = options.toUtf8(); | ||||
89 | | ||||
90 | varDefs.model = modelArray.data(); | ||||
91 | varDefs.layout = layoutArray.data(); | ||||
92 | varDefs.variant = variantArray.data(); | ||||
93 | varDefs.options = optionsArray.data(); | ||||
94 | | ||||
95 | // TODO: can this be made more dynamic? can anything but evdev be used? | ||||
96 | XkbRF_RulesPtr rules = XkbRF_Load(findXkbRulesFile().toUtf8().data(), // needs to be non-const! | ||||
97 | qgetenv("LOCALE").data(), | ||||
98 | True, | ||||
99 | True); | ||||
100 | Q_ASSERT(rules); | ||||
101 | QSharedPointer<XkbRF_RulesRec> rulesCleanup(rules, [](XkbRF_RulesPtr obj) { | ||||
102 | XkbRF_Free(obj, True); | ||||
103 | }); | ||||
104 | | ||||
105 | XkbComponentNamesRec componentNames; | ||||
106 | memset(&componentNames, 0, sizeof(XkbComponentNamesRec)); | ||||
107 | | ||||
108 | XkbRF_GetComponents(rules, &varDefs, &componentNames); | ||||
109 | | ||||
110 | XkbDescPtr xkb = XkbGetKeyboardByName(QX11Info::display(), | ||||
111 | XkbUseCoreKbd, | ||||
112 | &componentNames, | ||||
113 | 0, | ||||
114 | XkbGBN_GeometryMask | | ||||
115 | XkbGBN_KeyNamesMask | | ||||
116 | XkbGBN_OtherNamesMask | | ||||
117 | XkbGBN_ClientSymbolsMask | | ||||
118 | XkbGBN_IndicatorMapMask, | ||||
119 | false); | ||||
120 | Q_ASSERT(xkb); | ||||
121 | QSharedPointer<XkbDescRec> xkbCleanup(xkb, [](XkbDescPtr obj) { | ||||
122 | XkbFreeKeyboard(obj, 0, True); | ||||
123 | }); | ||||
124 | | ||||
125 | Geometry geometry(xkb->geom, xkb); | ||||
126 | | ||||
127 | // The way this is currently written we need the engine after | ||||
128 | // we have a geometry, lest geometry is dtor'd before the engine | ||||
129 | // causing exhaustive error spam on shutdown. | ||||
130 | // Also, the above stuff is blocking, but optimizing it is hardly | ||||
131 | // worth the effort. The Xkb calls altogether take ~8ms (I am not | ||||
132 | // certain putting xkb into a qfuture is thread-safe or even | ||||
133 | // faster). Constructing our QObjects takes 1ms. | ||||
134 | QQmlApplicationEngine engine; | ||||
135 | const QUrl url(QStringLiteral("qrc:/qml/main.qml")); | ||||
136 | QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, | ||||
137 | &app, [url](QObject *obj, const QUrl &objUrl) { | ||||
138 | if (!obj && url == objUrl) | ||||
139 | QCoreApplication::exit(-1); | ||||
140 | }, Qt::QueuedConnection); | ||||
141 | engine.rootContext()->setContextProperty("geometry", &geometry); | ||||
142 | engine.load(url); | ||||
143 | | ||||
144 | return app.exec(); | ||||
145 | } | ||||
146 | | ||||
147 | #include "main.moc" |