在驱动不支持的笔记本上实现合盖休眠功能

Enable lid function on laptop

Posted by Eric on March 9, 2023

终于解决了我笔记本无法合盖休眠的问题, 我还以为要从底层编写驱动, 最终还是通过python代码加上systemd系统服务的方式解决了. 太难了, 困扰了我一年多的问题.

首先, 肯定是没有网上说的改一个配置就行那么简单, 要不然我也不会忙活这么久了.

1. 代码

/opt/lid_signal.py

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import subprocess
import time

def get_lid_state():
    output = subprocess.check_output("cat /proc/acpi/button/lid/LID0/state", shell=True)
    state_bytes = output.split(b":")[-1].strip()
    state = state_bytes.decode()
    return state

def main():
    print("Starting lid signal detection...")
    while True:
        lid_state = get_lid_state()
        if lid_state == "closed":
            subprocess.run(["systemctl", "suspend"])  # call systemctl suspend command
        time.sleep(5)

if __name__ == "__main__":
    main()

/etc/systemd/system/lid_signal.service

1
2
3
4
5
6
7
8
9
10
11
12
[Unit]
Description=Lid Signal Detection
After=multi-user.target

[Service]
Type=simple
ExecStart=/usr/bin/python3 /opt/lid_signal.py
Restart=always

[Install]
WantedBy=multi-user.target

2. 修改权限

根据情况看需不需要改service文件

1
sudo chmod 755 /opt/lid_signal.py

3. 启动服务

1
2
3
4
sudo systemctl daemon-reload
sudo systemctl enable lid_signal.service
sudo systemctl start lid_signal.service