Louvre  v2.12.0-1
C++ library for Wayland compositors
Public Member Functions | List of all members
LKeyboard Class Reference

Class for handling keyboard events. More...

+ Inheritance diagram for LKeyboard:

Public Member Functions

 LKeyboard (const void *params) noexcept
 Constructor of the LKeyboard class. More...
 
 ~LKeyboard ()
 Destructor of the LKeyboard class. More...
 
void setFocus (LSurface *surface)
 Set keyboard focus. More...
 
LSurfacefocus () const noexcept
 Focused surface. More...
 
void setGrab (LSurface *surface)
 Sets a keyboard grab. More...
 
LSurfacegrab () const noexcept
 Gets the grab surface. More...
 
const LKeyboardModifiersEvent::Modifiersmodifiers () const noexcept
 Current keyboard modifiers state. More...
 
Int32 keymapFd () const noexcept
 Keyboard map file descriptor. More...
 
Int32 keymapSize () const noexcept
 Size of the keyboard map in bytes. More...
 
UInt32 keymapFormat () const noexcept
 Keymap format. More...
 
Int32 repeatRate () const noexcept
 Repetition rate. More...
 
Int32 repeatDelay () const noexcept
 Repetition delay. More...
 
void setRepeatInfo (Int32 rate, Int32 msDelay) noexcept
 Set the key repeat rate and delay. More...
 
xkb_keysym_t keySymbol (UInt32 keyCode) noexcept
 Key symbol. More...
 
xkb_state * keymapState () const noexcept
 Keymap state. More...
 
bool isModActive (const char *name, xkb_state_component type) const noexcept
 Checks the state of a modifier. More...
 
const std::vector< UInt32 > & pressedKeys () const noexcept
 All currently pressed key codes. More...
 
bool isKeyCodePressed (UInt32 keyCode) const noexcept
 Checks if a key code is pressed. More...
 
bool isKeySymbolPressed (xkb_keysym_t keySymbol) const noexcept
 Check if a key symbol is pressed. More...
 
bool setKeymap (const char *rules=nullptr, const char *model=nullptr, const char *layout=nullptr, const char *variant=nullptr, const char *options=nullptr) noexcept
 Sets the keyboard map. More...
 
void sendKeyEvent (const LKeyboardKeyEvent &event) noexcept
 Sends a key event to the currently focused surface. More...
 
- Public Member Functions inherited from LFactoryObject
Type factoryObjectType () const noexcept
 Gets the base factory object type. More...
 
- Public Member Functions inherited from LObject
 LObject (const LObject &) noexcept
 Copy constructor. More...
 
LObjectoperator= (const LObject &) noexcept
 Assignment operator (each object has its own individual LWeak reference count). More...
 
void setUserData (UIntPtr data) const noexcept
 Store an unsigned integer value/pointer. More...
 
UIntPtr userData () const noexcept
 Retrieves the stored unsigned integer value/pointer. More...
 

Virtual Methods

virtual void keyEvent (const LKeyboardKeyEvent &event)
 Keyboard key event generated by the input backend. More...
 
virtual void focusChanged ()
 Notifies that the focused surface has changed. More...
 

Additional Inherited Members

- Public Types inherited from LFactoryObject
enum class  Type : Int32
 Base factory object types. More...
 
- Protected Member Functions inherited from LObject
 LObject () noexcept=default
 Constructor of the LObject class. More...
 
virtual ~LObject () noexcept
 Destructor of the LObject class. More...
 
void notifyDestruction () noexcept
 Notifies the object destruction. More...
 

Detailed Description

Class for handling keyboard events.

The LKeyboard class lets you handle keyboard events generated by the input backend, redirect them to clients, and configure parameters such as the keyboard map and key repeat rate.
There is a single instance of LKeyboard, which can be accessed from LSeat::keyboard().

Wayland Events

To send keyboard events to clients, you must first assign focus to a surface using setFocus() and then all subsequent keyboard events sent with sendKeyEvent() will be directed to that surface.

Constructor & Destructor Documentation

◆ LKeyboard()

LKeyboard ( const void *  params)
noexcept

Constructor of the LKeyboard class.

There is a single instance of LKeyboard, which can be accessed from LSeat::keyboard().

Parameters
paramsInternal parameters provided in LCompositor::createObjectRequest().

◆ ~LKeyboard()

~LKeyboard ( )

Destructor of the LKeyboard class.

Invoked after LCompositor::onAnticipatedObjectDestruction().

Member Function Documentation

◆ setFocus()

void setFocus ( LSurface surface)

Set keyboard focus.

This method assigns keyboard focus to the specified surface. Only one surface can have keyboard focus at a time, and this method automatically removes focus from any previously focused surface.
To remove focus from all surfaces, pass nullptr as the argument.

Note
Calling this method while there is a surface grabbing the keyboard is a no-op.

◆ focus()

LSurface * focus ( ) const
noexcept

Focused surface.

See also
setFocus().
Returns
The focused surface or nullptr if no surface has keyboard focus.

◆ setGrab()

void setGrab ( LSurface surface)

Sets a keyboard grab.

Setting a keyboard grab prevents other surfaces from gain focus with setFocus().
If the surface grabbing the keyboard is destroyed the grab is automatically assigned to its parent or unset if it has no parent.
For example, an LPopupRole can request to make a keyboard grab with LPopupRole::grabKeyboardRequest().

If 'surface' is set to nullptr, the keyboard grab is unset.

Parameters
surfaceThe surface that will grab the keyboard events or nullptr to disable it.

◆ grab()

LSurface * grab ( ) const
noexcept

Gets the grab surface.

See also
setGrab()
Returns
The grabbing surface, or nullptr if unset.

◆ modifiers()

const LKeyboardModifiersEvent::Modifiers & modifiers ( ) const
noexcept

Current keyboard modifiers state.

◆ keymapFd()

Int32 keymapFd ( ) const
noexcept

Keyboard map file descriptor.

File descriptor of the XKB keyboard map assigned with setKeymap().

◆ keymapSize()

Int32 keymapSize ( ) const
noexcept

Size of the keyboard map in bytes.

Size in bytes of the XKB keyboard map loaded with setKeymap().

◆ keymapFormat()

UInt32 keymapFormat ( ) const
noexcept

Keymap format.

The keymap format can be either WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1, which indicates the presence of a valid XKB keymap set, or WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP, indicating an issue with the XKB system configuration.

◆ repeatRate()

Int32 repeatRate ( ) const
noexcept

Repetition rate.

Number of repetitions per second of a key when held down, assigned with setRepeatInfo().

The default value is 32 repetitions per second.

◆ repeatDelay()

Int32 repeatDelay ( ) const
noexcept

Repetition delay.

Delay in milliseconds before triggering the repetition of a key by holding it down, set with setRepeatInfo().

The default value is 500 ms.

◆ setRepeatInfo()

void setRepeatInfo ( Int32  rate,
Int32  msDelay 
)
noexcept

Set the key repeat rate and delay.

Assigns the repeat rate and delay when holding down a key.

Note
This impacts how clients respond to a key being held down.
Parameters
rateNumber of repetitions per second. 0 turns off repetition.
msDelayDelay in milliseconds before triggering the replay.

◆ keySymbol()

xkb_keysym_t keySymbol ( UInt32  keyCode)
noexcept

Key symbol.

Returns the symbol of a raw key code generated by the input backend according to the current keymap state.

The complete list of symbols can be found in <xkbcommon/xkbcommon-keysyms.h>.

◆ keymapState()

xkb_state * keymapState ( ) const
noexcept

Keymap state.

XKB keyboard map state.

◆ isModActive()

bool isModActive ( const char *  name,
xkb_state_component  type 
) const
noexcept

Checks the state of a modifier.

Parameters
nameModifier name defined in <xkbcommon/xkbcommon_names.h> such as XKB_MOD_NAME_SHIFT, XKB_LED_NAME_CAPS, etc.
typeIn XKB, the XKB_STATE_MODS_DEPRESSED, XKB_STATE_MODS_LATCHED, XKB_STATE_MODS_LOCKED, and XKB_STATE_MODS_EFFECTIVE are flags used to represent different aspects of the keyboard modifier keys' states.

XKB_STATE_MODS_DEPRESSED: This flag represents the currently depressed (pressed down) modifiers. When a modifier key is pressed, it falls into the depressed state. This flag helps identify which modifiers are currently held down.

XKB_STATE_MODS_LATCHED: This flag represents modifiers that are latched. A latched modifier is one that will be activated with the next non-modifier key press. Once the latched modifier is used, it returns to its original state.

XKB_STATE_MODS_LOCKED: This flag represents modifiers that are locked. When a modifier is locked, it stays active until explicitly unlocked. For example, if Caps Lock is activated, the Caps Lock state is locked until Caps Lock is pressed again.

XKB_STATE_MODS_EFFECTIVE: This flag represents the effective state of the modifiers. It takes into account the depressed, latched, and locked states to provide a combined view of the current modifier key states.

Returns
true if the modifier is active and false otherwise.

◆ pressedKeys()

const std::vector< UInt32 > & pressedKeys ( ) const
noexcept

All currently pressed key codes.

◆ isKeyCodePressed()

bool isKeyCodePressed ( UInt32  keyCode) const
noexcept

Checks if a key code is pressed.

◆ isKeySymbolPressed()

bool isKeySymbolPressed ( xkb_keysym_t  keySymbol) const
noexcept

Check if a key symbol is pressed.

◆ setKeymap()

bool setKeymap ( const char *  rules = nullptr,
const char *  model = nullptr,
const char *  layout = nullptr,
const char *  variant = nullptr,
const char *  options = nullptr 
)
noexcept

Sets the keyboard map.

This method finds and assigns a keyboard map that matches the parameters passed in the arguments.
The keyboard map is automatically sent to clients when they connect to the compositor and use the wl_keyboard interface of the Wayland protocol. If a client is already connected, Louvre re-sends it the new keyboard map.

Parameters
rulesRules on how to interpret the other arguments. Can be nullptr. More information.
modelKeyboard model. Can be nullptr. More information.
layoutKeyboard layouts separated by comma (e.g. "latam"). Can be nullptr. More information.
variantVariants of layouts. Can be nullptr. More information.
optionsPreferences not related to layouts. Can be nullptr. More information.

◆ sendKeyEvent()

void sendKeyEvent ( const LKeyboardKeyEvent event)
noexcept

Sends a key event to the currently focused surface.

See also
setFocus() and setGrab().

◆ keyEvent()

virtual void keyEvent ( const LKeyboardKeyEvent event)
virtual

Keyboard key event generated by the input backend.

Default implementation

void LKeyboard::keyEvent(const LKeyboardKeyEvent &event)
{
const bool sessionLocked { sessionLockManager()->state() != LSessionLockManager::Unlocked };
sendKeyEvent(event);
const bool L_CTRL { isKeyCodePressed(KEY_LEFTCTRL) };
const bool R_CTRL { isKeyCodePressed(KEY_RIGHTCTRL) };
const bool L_SHIFT { isKeyCodePressed(KEY_LEFTSHIFT) };
const bool L_ALT { isKeyCodePressed(KEY_LEFTALT) };
const bool mods { L_ALT || L_SHIFT || L_CTRL || R_CTRL };
const xkb_keysym_t sym { keySymbol(event.keyCode()) };
if (event.state() == LKeyboardKeyEvent::Released)
{
if (event.keyCode() == KEY_ESC && L_CTRL && L_SHIFT)
{
return;
}
else if (L_CTRL && !L_SHIFT)
else if (!L_CTRL && L_SHIFT)
else if (!L_CTRL && !L_SHIFT)
if (sessionLocked)
return;
if (event.keyCode() == KEY_F1 && !mods)
LLauncher::launch("weston-terminal");
else if (L_CTRL && (sym == XKB_KEY_q || sym == XKB_KEY_Q))
{
if (focus())
}
else if (L_CTRL && (sym == XKB_KEY_m || sym == XKB_KEY_M))
{
if (focus() && focus()->toplevel() && !focus()->toplevel()->fullscreen())
focus()->setMinimized(true);
}
// Screenshot
else if (L_CTRL && L_SHIFT && event.keyCode() == KEY_3)
{
if (cursor()->output() && cursor()->output()->bufferTexture(0))
{
std::filesystem::path path { getenvString("HOME") };
if (path.empty())
return;
path /= "Desktop/Louvre_Screenshoot_";
char timeString[32];
const auto now { std::chrono::system_clock::now() };
const auto time { std::chrono::system_clock::to_time_t(now) };
std::strftime(timeString, sizeof(timeString), "%Y-%m-%d %H:%M:%S.png", std::localtime(&time));
path += timeString;
cursor()->output()->bufferTexture(0)->save(path);
}
}
}
// Key pressed
else
{
// CTRL sets Copy as the preferred action in drag & drop session
if (L_CTRL)
// SHIFT sets Move as the preferred action in drag & drop session
else if (L_SHIFT)
}
}
void destroyLater() noexcept
Terminates the client connection with the compositor.
Definition: LClient.cpp:122
void finish() noexcept
Uninitializes the compositor.
Definition: LCompositor.cpp:409
LOutput * output() const noexcept
Gets the current cursor output.
Definition: LCursor.cpp:348
void setPreferredAction(Action action) noexcept
Assigns the preferred action.
Definition: LDND.cpp:220
@ NoAction
No preferred action.
Definition: LDND.h:47
@ Copy
The preferred action is to copy.
Definition: LDND.h:50
@ Move
The preferred action is to move.
Definition: LDND.h:53
LSurface * focus() const noexcept
Focused surface.
Definition: LKeyboard.cpp:271
virtual void keyEvent(const LKeyboardKeyEvent &event)
Keyboard key event generated by the input backend.
void sendKeyEvent(const LKeyboardKeyEvent &event) noexcept
Sends a key event to the currently focused surface.
Definition: LKeyboard.cpp:352
bool isKeyCodePressed(UInt32 keyCode) const noexcept
Checks if a key code is pressed.
Definition: LKeyboard.cpp:400
xkb_keysym_t keySymbol(UInt32 keyCode) noexcept
Key symbol.
Definition: LKeyboard.cpp:371
@ Released
The key is not being pressed.
Definition: LKeyboardKeyEvent.h:22
static pid_t launch(const std::string &command)
Launches an application.
Definition: LLauncher.cpp:195
LTexture * bufferTexture(UInt32 bufferIndex) noexcept
Access the texture of a specific buffer.
Definition: LOutput.cpp:266
LDND * dnd() const noexcept
Access to the drag & drop session manager.
Definition: LSeat.h:197
State state() const noexcept
Gets the current state of the session.
Definition: LSessionLockManager.h:65
@ Unlocked
Definition: LSessionLockManager.h:27
LClient * client() const noexcept
Client owner of the surface.
Definition: LSurface.cpp:476
void setMinimized(bool state)
Sets the minimized property.
Definition: LSurface.cpp:174
bool save(const std::filesystem::path &name) const noexcept
Save the texture as a PNG file.
Definition: LTexture.cpp:468
LCursor * cursor() noexcept
Gets the compositor's cursor.
Definition: LCompositor.cpp:47
LCompositor * compositor() noexcept
Gets the static LCompositor instance.
Definition: LCompositor.cpp:37
LSessionLockManager * sessionLockManager() noexcept
Gets the compositor's session lock manager.
Definition: LCompositor.cpp:52
LSeat * seat() noexcept
Gets the compositor's seat.
Definition: LCompositor.cpp:42

◆ focusChanged()

virtual void focusChanged ( )
virtual

Notifies that the focused surface has changed.

Default Implementation

{
/* No default implementation. */
}
virtual void focusChanged()
Notifies that the focused surface has changed.