Changeset View
Changeset View
Standalone View
Standalone View
logout-greeter/main.cpp
- This file was copied to logout-greeter/greeter.cpp.
Show All 16 Lines | |||||
17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
19 | AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | 19 | AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN | ||
20 | AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | 20 | AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
21 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | 21 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
22 | 22 | | |||
23 | ******************************************************************/ | 23 | ******************************************************************/ | ||
24 | #include <QApplication> | 24 | #include <QApplication> | ||
25 | #include <QQuickWindow> | ||||
25 | #include <QCommandLineParser> | 26 | #include <QCommandLineParser> | ||
26 | #include <QDebug> | | |||
27 | #include <QScreen> | | |||
28 | #include "shutdowndlg.h" | | |||
29 | 27 | | |||
30 | #include <KQuickAddons/QtQuickSettings> | 28 | #include <KQuickAddons/QtQuickSettings> | ||
31 | 29 | | |||
32 | #include <KWindowSystem> | | |||
33 | | ||||
34 | #include <KWayland/Client/connection_thread.h> | | |||
35 | #include <KWayland/Client/registry.h> | | |||
36 | #include <KWayland/Client/plasmashell.h> | | |||
37 | | ||||
38 | #include <unistd.h> | 30 | #include <unistd.h> | ||
39 | 31 | | |||
40 | class Greeter : public QObject | 32 | #include "greeter.h" | ||
41 | { | | |||
42 | Q_OBJECT | | |||
43 | public: | | |||
44 | Greeter(int fd, bool shutdownAllowed, bool choose, KWorkSpace::ShutdownType type); | | |||
45 | ~Greeter() override; | | |||
46 | | ||||
47 | void init(); | | |||
48 | | ||||
49 | bool eventFilter(QObject *watched, QEvent *event) override; | | |||
50 | | ||||
51 | private: | | |||
52 | void adoptScreen(QScreen *screen); | | |||
53 | void rejected(); | | |||
54 | void setupWaylandIntegration(); | | |||
55 | | ||||
56 | int m_fd; | | |||
57 | bool m_shutdownAllowed; | | |||
58 | bool m_choose; | | |||
59 | KWorkSpace::ShutdownType m_shutdownType; | | |||
60 | QVector<KSMShutdownDlg *> m_dialogs; | | |||
61 | KWayland::Client::PlasmaShell *m_waylandPlasmaShell; | | |||
62 | }; | | |||
63 | | ||||
64 | Greeter::Greeter(int fd, bool shutdownAllowed, bool choose, KWorkSpace::ShutdownType type) | | |||
65 | : QObject() | | |||
66 | , m_fd(fd) | | |||
67 | , m_shutdownAllowed(shutdownAllowed) | | |||
68 | , m_choose(choose) | | |||
69 | , m_shutdownType(type) | | |||
70 | , m_waylandPlasmaShell(nullptr) | | |||
71 | { | | |||
72 | } | | |||
73 | | ||||
74 | Greeter::~Greeter() | | |||
75 | { | | |||
76 | qDeleteAll(m_dialogs); | | |||
77 | } | | |||
78 | | ||||
79 | void Greeter::setupWaylandIntegration() | | |||
80 | { | | |||
81 | if (!KWindowSystem::isPlatformWayland()) { | | |||
82 | return; | | |||
83 | } | | |||
84 | using namespace KWayland::Client; | | |||
85 | ConnectionThread *connection = ConnectionThread::fromApplication(this); | | |||
86 | if (!connection) { | | |||
87 | return; | | |||
88 | } | | |||
89 | Registry *registry = new Registry(this); | | |||
90 | registry->create(connection); | | |||
91 | connect(registry, &Registry::plasmaShellAnnounced, this, | | |||
92 | [this, registry] (quint32 name, quint32 version) { | | |||
93 | m_waylandPlasmaShell = registry->createPlasmaShell(name, version, this); | | |||
94 | } | | |||
95 | ); | | |||
96 | registry->setup(); | | |||
97 | connection->roundtrip(); | | |||
98 | } | | |||
99 | | ||||
100 | void Greeter::init() | | |||
101 | { | | |||
102 | setupWaylandIntegration(); | | |||
103 | foreach (QScreen *screen, qApp->screens()) { | | |||
104 | adoptScreen(screen); | | |||
105 | } | | |||
106 | connect(qApp, &QGuiApplication::screenAdded, this, &Greeter::adoptScreen); | | |||
107 | } | | |||
108 | | ||||
109 | void Greeter::adoptScreen(QScreen* screen) | | |||
110 | { | | |||
111 | // TODO: last argument is the theme, maybe add command line option for it? | | |||
112 | KSMShutdownDlg *w = new KSMShutdownDlg(nullptr, m_shutdownAllowed, m_choose, m_shutdownType, m_waylandPlasmaShell); | | |||
113 | w->installEventFilter(this); | | |||
114 | m_dialogs << w; | | |||
115 | | ||||
116 | QObject::connect(screen, &QObject::destroyed, w, [w, this] { | | |||
117 | m_dialogs.removeOne(w); | | |||
118 | w->deleteLater(); | | |||
119 | }); | | |||
120 | connect(w, &KSMShutdownDlg::rejected, this, &Greeter::rejected); | | |||
121 | connect(w, &KSMShutdownDlg::accepted, this, | | |||
122 | [w, this] { | | |||
123 | if (m_fd != -1) { | | |||
124 | QFile f; | | |||
125 | if (f.open(m_fd, QFile::WriteOnly, QFile::AutoCloseHandle)) { | | |||
126 | f.write(QByteArray::number(int(w->shutdownType()))); | | |||
127 | f.close(); | | |||
128 | } | | |||
129 | } | | |||
130 | QApplication::quit(); | | |||
131 | } | | |||
132 | ); | | |||
133 | w->setScreen(screen); | | |||
134 | w->setGeometry(screen->geometry()); | | |||
135 | w->init(); | | |||
136 | } | | |||
137 | | ||||
138 | void Greeter::rejected() | | |||
139 | { | | |||
140 | if (m_fd != -1) { | | |||
141 | close(m_fd); | | |||
142 | } | | |||
143 | QApplication::exit(1); | | |||
144 | } | | |||
145 | | ||||
146 | bool Greeter::eventFilter(QObject *watched, QEvent *event) | | |||
147 | { | | |||
148 | if (qobject_cast<KSMShutdownDlg*>(watched)) { | | |||
149 | if (event->type() == QEvent::MouseButtonPress) { | | |||
150 | // check that the position is on no window | | |||
151 | QMouseEvent *me = static_cast<QMouseEvent*>(event); | | |||
152 | for (auto it = m_dialogs.constBegin(); it != m_dialogs.constEnd(); ++it) { | | |||
153 | if ((*it)->geometry().contains(me->globalPos())) { | | |||
154 | return false; | | |||
155 | } | | |||
156 | } | | |||
157 | // click outside, close | | |||
158 | rejected(); | | |||
159 | } | | |||
160 | } | | |||
161 | return false; | | |||
162 | } | | |||
163 | 33 | | |||
164 | int main(int argc, char *argv[]) | 34 | int main(int argc, char *argv[]) | ||
165 | { | 35 | { | ||
166 | // Qt does not currently (5.9.4) support fullscreen on xdg_shell v6. | 36 | // Qt does not currently (5.9.4) support fullscreen on xdg_shell v6. | ||
167 | qputenv("QT_WAYLAND_SHELL_INTEGRATION", "wl-shell"); | 37 | qputenv("QT_WAYLAND_SHELL_INTEGRATION", "wl-shell"); | ||
168 | 38 | | |||
169 | KWorkSpace::detectPlatform(argc, argv); | 39 | KWorkSpace::detectPlatform(argc, argv); | ||
170 | QQuickWindow::setDefaultAlphaBuffer(true); | 40 | QQuickWindow::setDefaultAlphaBuffer(true); | ||
▲ Show 20 Lines • Show All 58 Lines • Show Last 20 Lines |