Skip to content

feat: Support TTY switching#466

Merged
zccrs merged 5 commits intolinuxdeepin:masterfrom
calsys456:master
Jul 31, 2025
Merged

feat: Support TTY switching#466
zccrs merged 5 commits intolinuxdeepin:masterfrom
calsys456:master

Conversation

@calsys456
Copy link
Collaborator

@calsys456 calsys456 commented Jul 24, 2025

helper.cpp: 使用wlr_session_change_vt实现TTY切换
wbackend.cpp, wserver.h: 暴露wlr_session对象,供wlr_session_change_vt使用
qwlrootswindow.cpp: 解决切换TTY后崩溃退出的问题

Summary by Sourcery

Enable TTY switching support by exposing and using wlr_session_change_vt on Ctrl+Alt+F-keys, lock the screen during switch, and add safety checks to avoid crashes after switching.

New Features:

  • Support switching virtual terminals with Ctrl+Alt+F1–F6 using wlr_session_change_vt and auto-lock the screen

Bug Fixes:

  • Prevent crashes after TTY switching by adding null checks for input devices and seats in QWlrootsRenderWindow event filters

Enhancements:

  • Expose the underlying wlr_session object in WServerInterface and capture it in WBackend to enable session operations

@deepin-ci-robot
Copy link

Hi @apr3vau. Thanks for your PR.

I'm waiting for a linuxdeepin member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@sourcery-ai
Copy link

sourcery-ai bot commented Jul 24, 2025

Reviewer's Guide

This PR adds TTY switching support by exposing the underlying wlr_session through WServer/WBackend, wiring up wlr_session_change_vt in the input helper, and hardening event filters to avoid null-dereference crashes after a VT switch.

Sequence diagram for TTY switching via keyboard shortcut

sequenceDiagram
    actor User
    participant Helper
    participant WBackend
    participant wlr_session

    User->>Helper: Press Ctrl+Alt+F1-F6
    Helper->>Helper: Detect modifiers and scanCode
    Helper->>Helper: setCurrentMode(LockScreen)
    Helper->>Helper: m_lockScreen->lock()
    Helper->>WBackend: m_backend->session()
    Helper->>wlr_session: wlr_session_change_vt(session, vt)
Loading

Class diagram for QWlrootsRenderWindow event filter hardening

classDiagram
    class QWlrootsRenderWindow {
        +bool beforeDisposeEventFilter(QEvent *event)
        +bool afterDisposeEventFilter(QEvent *event)
        -lastActiveCursor
    }
    class WInputDevice {
        +static from(device)
        +seat()
    }
    class WSeat {
        +cursor()
        +filterEventBeforeDisposeStage(window, event)
        +filterEventAfterDisposeStage(window, event)
    }
    QWlrootsRenderWindow --> WInputDevice : uses
    WInputDevice --> WSeat : seat()
    QWlrootsRenderWindow --> WSeat : calls filterEvent* methods
Loading

File-Level Changes

Change Details Files
Guard against null device/seat in QWlrootsRenderWindow event filters
  • Replace Q_ASSERTs with if(device && device->seat()) checks in beforeDisposeEventFilter
  • Apply the same guard in afterDisposeEventFilter
waylib/src/server/platformplugin/qwlrootswindow.cpp
Implement TTY switching on Ctrl+Alt+F1–F6 in input helper
  • Detect Ctrl+Alt modifiers and scan codes 67–72
  • Lock screen before switching to new VT mode
  • Invoke wlr_session_change_vt with the correct VT index
src/seat/helper.cpp
Expose wlr_session object to client code
  • Include <wlr/backend/session.h> in wserver.h and wbackend.cpp
  • Add inline session() accessor in WServerInterface
  • Capture and store the session pointer returned by qw_backend::autocreate in WBackend
waylib/src/server/kernel/wserver.h
waylib/src/server/kernel/wbackend.cpp

Possibly linked issues

  • Init repository #1: PR implements Ctrl+Alt+Fx TTY switching, directly addressing the issue's core functionality.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @apr3vau - I've reviewed your changes - here's some feedback:

  • The Ctrl+Alt modifier check kevent->modifiers() == Qt::ControlModifier | Qt::AltModifier has a precedence bug—wrap the OR in parentheses or use a bitmask test (e.g. (mods & (Ctrl|Alt)) == (Ctrl|Alt)).
  • Replace the hard-coded scanCode range (67–72) with named key constants (e.g. KEY_F1–KEY_F6 or Qt::Key_F1–Qt::Key_F6) for clarity and maintainability.
  • Guard the call to wlr_session_change_vt by verifying m_backend->session() is non-null to avoid potential null-pointer dereferences.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The Ctrl+Alt modifier check `kevent->modifiers() == Qt::ControlModifier | Qt::AltModifier` has a precedence bug—wrap the OR in parentheses or use a bitmask test (e.g. `(mods & (Ctrl|Alt)) == (Ctrl|Alt)`).
- Replace the hard-coded scanCode range (67–72) with named key constants (e.g. KEY_F1–KEY_F6 or Qt::Key_F1–Qt::Key_F6) for clarity and maintainability.
- Guard the call to wlr_session_change_vt by verifying `m_backend->session()` is non-null to avoid potential null-pointer dereferences.

## Individual Comments

### Comment 1
<location> `src/seat/helper.cpp:1200` </location>
<code_context>
         }

+        // Switch TTY with Ctrl + Alt + F1-F6
+        if (kevent->modifiers() == Qt::ControlModifier | Qt::AltModifier) {
+            auto scanCode = kevent->nativeScanCode();
+            if (scanCode >= 67 && scanCode <= 72) {
</code_context>

<issue_to_address>
Bitwise OR operator precedence may cause incorrect modifier check.

Operator precedence causes the bitwise OR to be evaluated before equality, so this checks if modifiers equal the combined value. If you want to check that both modifiers are set, use bitwise AND or testFlag() instead.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@calsys456
Copy link
Collaborator Author

calsys456 commented Jul 25, 2025

经测试发现了问题:

  • 配合ddm工作时出现无法切换到TTY的问题,seatd报错[ERROR] [seatd/seat.c:640] Could not set next session: no such client
  • Ctrl + Alt键的检测失效,单独按下F1-F12也会触发TTY切换请求
  • 从TTY切回treeland时,锁屏动画播放过程中分辨率不正确

@zccrs
Copy link
Member

zccrs commented Jul 25, 2025

经测试发现了问题:

  • 配合ddm工作时出现无法切换到TTY的问题,seatd报错[ERROR] [seatd/seat.c:640] Could not set next session: no such client
  • Ctrl + Alt键的检测失效,单独按下F1-F12也会触发TTY切换请求

正在尝试修复

这才是符合我的预期的。因为是 vt_bound 模式,我准备移除掉对 vt_bound 模式的依赖,使用正常模式。在 ddm 里设置vt控制进程,使用ddm来阻塞tty的切换。

@zccrs
Copy link
Member

zccrs commented Jul 25, 2025

经测试发现了问题:

  • 配合ddm工作时出现无法切换到TTY的问题,seatd报错[ERROR] [seatd/seat.c:640] Could not set next session: no such client
  • Ctrl + Alt键的检测失效,单独按下F1-F12也会触发TTY切换请求

正在尝试修复

配合ddm时肯定无法切换,因为是 vt_bound 模式,我准备移除掉对 vt_bound 模式的依赖,使用正常模式。在 ddm 里设置vt控制进程,使用ddm来阻塞tty的切换。

这个提交里不用解决这个问题,先做到在lightdm启动时能切换tty即可。

Add qwlroots interface for wl_session object, which can serve for VT
switching.
Support TTY switching by calling wlr_session_change_vt(). Currently not
working with ddm because of the VT_BOUND mode.
When switching back from TTY the Greeter will be re-created, and somehow
leads to an incorrect lockscreen animation on HiRes screen. This commit
adds skip function to lockscreen animation, and skips the animation when
the first time it adds to an output.
Qt will create virtual device for QInputEvent when no physical device
available (e.g. switched to another VT), which will leads to a failed
assertion and make treeland crash. This commit applies a special guarded
getter for such case.
Use parameter to control lockscreen animation instead of QML property.
@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: apr3vau, zccrs

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@zccrs zccrs merged commit c3501ea into linuxdeepin:master Jul 31, 2025
16 checks passed
Dami-star added a commit to Dami-star/waylib that referenced this pull request Aug 14, 2025
Qt will create virtual device for QInputEvent when no physical device
available (e.g. switched to another VT), which will leads to a failed
assertion and make treeland crash. This commit applies a special guarded
getter for such case.

check from linuxdeepin/treeland/pull/466
Dami-star added a commit to Dami-star/waylib that referenced this pull request Aug 14, 2025
Qt will create virtual device for QInputEvent when no physical device
available (e.g. switched to another VT), which will leads to a failed
assertion and make treeland crash. This commit applies a special guarded
getter for such case.

check from linuxdeepin/treeland/pull/466
zccrs pushed a commit to vioken/waylib that referenced this pull request Aug 14, 2025
Qt will create virtual device for QInputEvent when no physical device
available (e.g. switched to another VT), which will leads to a failed
assertion and make treeland crash. This commit applies a special guarded
getter for such case.

check from linuxdeepin/treeland/pull/466
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants