-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathksldapp.h
476 lines (404 loc) · 12.3 KB
/
ksldapp.h
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
SPDX-FileCopyrightText: 1999 Martin R. Jones <[email protected]>
SPDX-FileCopyrightText: 2003 Oswald Buddenhagen <[email protected]>
SPDX-FileCopyrightText: 2008 Chani Armitage <[email protected]>
SPDX-FileCopyrightText: 2011 Martin Gräßlin <[email protected]>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include <QElapsedTimer>
#include <QProcessEnvironment>
#include <KScreenLocker/kscreenlocker_export.h>
// forward declarations
class GlobalAccel;
class LogindIntegration;
class QTimer;
class KSldTest;
class PowerManagementInhibition;
namespace ScreenLocker
{
/**
* Enum for the different ways to establish a lock.
*/
enum class EstablishLock {
/**
* Require password from the start. Use if invoked explicitly by the user.
*/
Immediate,
/**
* Allow the user to log back in without a password for a configured grace time.
*/
Delayed,
/**
* UI should default to showing the "switch user dialog".
*/
DefaultToSwitchUser,
};
/**
* @returns a string representation of the given EstablishLock value.
**/
QString establishLockToString(EstablishLock establishLock);
class AbstractLocker;
class WaylandServer;
/**
* @class KSldApp
* @brief The KSldApp class represents the application responsible for screen locking.
*
* KSldApp provides functionality for acquiring and releasing screen locks, managing lock states,
* handling user activity, and interacting with the lock window. It also includes methods for
* configuring the lock screen behavior and managing power management inhibition.
*
* KSldApp implements the D-Bus interface "org.kde.ksld.App".
*
* @note KSldApp is primarily used internally by the KDE screen locker component.
*/
class KSCREENLOCKER_EXPORT KSldApp : public QObject
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.kde.ksld.App")
public:
/**
* @brief The LockState enum represents the current state of the screen lock.
*/
enum LockState {
/** The screen is unlocked. */
Unlocked,
/** The screen is in the process of being locked. */
AcquiringLock,
/** The screen is locked. */
Locked,
};
Q_ENUM(LockState)
static KSldApp *self();
explicit KSldApp(QObject *parent = nullptr);
~KSldApp() override;
/**
* @returns the current lock state.
**/
LockState lockState() const
{
return m_lockState;
}
/**
* @returns the number of milliseconds passed since the screen has been locked.
**/
uint activeTime() const;
/**
* @brief Configures the KSldApp instance.
*
* This function is responsible for configuring the KSldApp instance by loading the screen saver settings,
* setting up the idle support, and managing the lock and password requirements.
*
* @note This function assumes that the KScreenSaverSettings and KIdleTime instances have been properly initialized.
*/
void configure();
/**
* @brief Handles user activity.
*
* This function is called when user activity is detected.
*
* If the grace time is still active or password requirement is disabled,
* it unlocks the screen. If a lock window is present, it also notifies
* the lock window about the user activity.
*/
void userActivity();
/**
* @return true if the application is in the grace time, false otherwise.
*/
bool isGraceTime() const;
/**
* @brief Sets the Wayland file descriptor.
*
* @param fd The Wayland file descriptor to set.
*/
void setWaylandFd(int fd);
/**
* @brief Sets the environment for the greeter process.
*
* @param env The QProcessEnvironment object containing the environment
* variables to be set for the greeter process.
*/
void setGreeterEnvironment(const QProcessEnvironment &env);
/**
* Can be used by the lock window to remove the lock during grace time.
**/
void unlock();
/**
* @brief Increments the inhibit counter.
*
* The inhibit counter is used to keep track of how many times the application
* has been inhibited from locking the screen.
*/
void inhibit();
/**
* @brief Decrements the inhibit counter.
*
* The inhibit counter is used to keep track of how many times the application
* has been inhibited from locking the screen.
*/
void uninhibit();
/**
* @brief Locks the screen.
*
* If the screen is already locked or in the process of acquiring the lock, verify that the lock is established.
* If the screen is unlocked, it attempts to establish the lock by calling the establishGrab() function.
* If the lock cannot be established, it retries up to three times.
* If the lock is successfully established, it shows the lock window, starts the lock process, and emits the lockStateChanged() signal.
*
* @param establishLock The type of lock to establish (eg. immediate or delayed).
* @param attemptCount The number of attempts made to establish the lock.
*/
void lock(EstablishLock establishLock, int attemptCount = 0);
/**
* @brief Initializes the KSldApp object.
*/
void initialize();
/**
* @brief Handles events for the KSldApp class.
*
* Checks if the event is a key press event and if a global accelerator is set.
* If both conditions are met, it calls the keyEvent function of the global accelerator
* and sets the event as accepted if the key event is handled.
*
* @param event The event to be handled.
* @return Returns false to indicate that the event was not handled by this function.
*/
bool event(QEvent *event) override;
/**
* For testing
* @internal
**/
int idleId() const
{
return m_idleId;
}
/**
* For testing
* @internal
**/
void setIdleId(int idleId)
{
m_idleId = idleId;
}
/**
* For testing
* @internal
**/
void setGraceTime(int msec)
{
m_lockGrace = msec;
}
/**
* @brief Returns whether software rendering is forced.
*/
bool forceSoftwareRendering() const
{
return m_forceSoftwareRendering;
}
/**
* @brief Sets whether software rendering should be forced.
*/
void setForceSoftwareRendering(bool force)
{
m_forceSoftwareRendering = force;
}
Q_SIGNALS:
/**
* Emitted when the screen is about to be locked.
*/
void aboutToLock();
/**
* Emitted before the greeter is (re)started
*/
void aboutToStartGreeter();
/**
* Emitted when the screen has been locked.
*/
void locked();
/**
* Emitted when the screen has been unlocked.
*/
void unlocked();
/**
* Emitted when the lock state has changed.
*/
void lockStateChanged();
private Q_SLOTS:
/**
* @brief Cleans up resources used by KSldApp.
*
* This function terminates the lock process if it is running, deletes the lock process and lock window objects,
* and restores the X screensaver parameters to their original values.
*/
void cleanUp();
/**
* @brief Ends the grace time.
*/
void endGraceTime();
/**
* @brief Suspends the application and locks the screen if necessary.
*
* This function is called when the system is about to suspend. If the application is not using logind,
* it checks if the screen should be locked upon resuming from suspend. If so, it locks the screen immediately.
*/
void solidSuspend();
public Q_SLOTS:
/**
* @brief Called when the lock screen is shown.
*
* Updates the lock state and emits signals to inform other components.
*/
void lockScreenShown();
private:
/**
* @brief Initializes the X11 environment for the KSldApp.
*/
void initializeX11();
/**
* @brief Establishes the grab for keyboard and mouse input.
*
* Ensures that only the lock window receives keyboard and mouse input.
*
* @return True if the grab is successfully established, false otherwise.
*/
bool establishGrab();
/**
* @brief Starts the lock process.
*
* This function starts the lock process based on the given EstablishLock parameter.
* It sets up the necessary environment variables and command line arguments for the lock process.
* If the Wayland server fails to start, an error is emitted and the function returns.
*
* @param establishLock The type of lock to establish (eg. immediate or delayed).
*/
void startLockProcess(EstablishLock establishLock);
/**
* @brief Shows the lock window.
*
* Creates the lock window if it doesn't exist and sets up the necessary
* connections.
*/
void showLockWindow();
/**
* @brief Hides the lock window.
*/
void hideLockWindow();
/**
* @brief Performs the unlocking operation.
*
* This function is responsible for unlocking the screen. It releases the keyboard and pointer grabs,
* hides the lock window, and cleans up state.
*/
void doUnlock();
/**
* @brief The lock process has requested to unlock the screen.
*
* Cleans up some state and begins the unlocking process.
*/
void lockProcessRequestedUnlock();
/**
* @brief Checks whether the FDO (FreeDesktop.org) power is inhibited or not.
*
* @return true if the FDO power is inhibited, false otherwise.
*/
bool isFdoPowerInhibited() const;
/**
* @brief The current state of the lock.
*/
LockState m_lockState;
/**
* The lock process used to lock the screen.
**/
QProcess *m_lockProcess;
/**
* The lock window used to display the lock screen.
**/
AbstractLocker *m_lockWindow;
/**
* The Wayland server instance used by the lock window.
**/
WaylandServer *m_waylandServer;
/**
* Timer to measure how long the screen is locked.
* This information is required by DBus Interface.
**/
QElapsedTimer m_lockedTimer;
/**
* @brief The unique identifier for the idle timer.
*/
int m_idleId;
/**
* Number of milliseconds after locking in which user activity will result in screen being
* unlocked without requiring a password.
**/
int m_lockGrace;
/**
* Controls whether user activity may remove the lock. Only enabled after idle timeout.
**/
bool m_inGraceTime;
/**
* Grace time ends when timer expires.
**/
QTimer *m_graceTimer;
/**
* @brief The counter for inhibiting screen locking.
*
* This variable keeps track of the number of inhibitions on screen locking.
*/
int m_inhibitCounter;
/**
* Provides handling for interactions with the logind service, such as
* session management and power management.
*/
LogindIntegration *m_logind;
/**
* @brief Provides handling for global accelerators.
*/
GlobalAccel *m_globalAccel = nullptr;
/**
* @brief Whether the XInput2 extension is available.
*/
bool m_hasXInput2 = false;
/**
* @brief Whether software rendering should be forced.
*/
bool m_forceSoftwareRendering = false;
/**
* @brief Whether the application is running on X11.
*/
bool m_isX11;
/**
* @brief Whether the application is running on Wayland.
*/
bool m_isWayland;
/**
* @brief Counter for tracking the number of times the greeter has crashed.
*/
int m_greeterCrashedCounter = 0;
/**
* @brief The environment for the greeter process.
*/
QProcessEnvironment m_greeterEnv;
/**
* @brief Interface for power management inhibition.
*/
PowerManagementInhibition *m_powerManagementInhibition;
/**
* Whether the lock screen should require a password to unlock.
*
* If this is false the lock screen will be dismissed on user activity,
* without requiring a password. This allows users to use the lock screen
* as a screen saver.
**/
bool m_requirePassword = true;
/**
* @brief The file descriptor for the Wayland connection.
*
* This file descriptor is used to establish the Wayland connection for the
* lock window.
*/
int m_waylandFd = -1;
// for auto tests
friend KSldTest;
};
} // namespace