[Seek Help] 如何判断锁定登录界面 xset q | grep -i 'Monitor is Off' 等方法都不管用
Tofloor
poster avatar
182******61
deepin
2024-12-03 10:16
Author

如何判断锁定登录界面 xset q | grep -i 'Monitor is Off' ,loginctl show-session ​(loginctl | grep (whoami) | awk '{print $1}') -p LockedHint, sudo grep $(whoami) /etc/shadow,等方法都不管用

Reply Favorite View the author
All Replies
五星
deepin
2024-12-03 10:27
#1

不如换个思路,如果不能进行某个操作,就说明锁定了。

Reply View the author
182******61
deepin
2024-12-03 10:35
#2

主要是没有什么操作不能处理的,我是在程序里做判断的,系统状态与没有锁定界面时的状态一样,我后台还是能象没有锁定界面时一样做各种操作

Reply View the author
五星
deepin
2024-12-03 11:53
#3
182******61

主要是没有什么操作不能处理的,我是在程序里做判断的,系统状态与没有锁定界面时的状态一样,我后台还是能象没有锁定界面时一样做各种操作

import time
import Xlib
from Xlib import display

def is_locked():
    d = display.Display()
    root = d.screen().root

    # 获取根窗口的属性
    window_props = root.get_full_property(d.intern_atom('_NET_ACTIVE_WINDOW'), Xlib.X.AnyPropertyType)

    if window_props is None or window_props.value[0] == 0:
        # 如果没有活动窗口,可能处于锁定状态
        return True

    # 获取活动窗口的属性
    active_window = d.create_resource_object('window', window_props.value[0])
    window_name = active_window.get_full_property(d.intern_atom('_NET_WM_NAME'), Xlib.X.AnyPropertyType)

    if window_name is not None:
        window_name = window_name.value.decode('utf-8')
        # 检查窗口名称是否包含锁定相关的字符串
        if 'lock' in window_name.lower():
            return True

    return False

def main():
    while True:
        if is_locked():
            print("桌面处于锁定状态")
        else:
            print("桌面未锁定")
        # 设置延迟时间,例如每 5 秒检查一次
        time.sleep(5)

if __name__ == "__main__":
    main()

Reply View the author
182******61
deepin
2024-12-03 14:21
#4

这前尝试过这样的方法,我是用c++实现的:

Reply View the author
182******61
deepin
2024-12-03 14:21
#5

int getActiveWindow(Display *display, Window window, Window *active_window) {
// 检查窗口是否具有_NET_ACTIVE_WINDOW属性
Atom active_window_atom = XInternAtom(display, "_NET_ACTIVE_WINDOW", False);
int ret = -1;

unsigned char *prop_data;
Atom actual_type;
int actual_format;
unsigned long nitems, bytes_after;

if (XGetWindowProperty(display, window, active_window_atom, 0, ~0L, False, XA_WINDOW, &actual_type, &actual_format, &nitems, &bytes_after, &prop_data) == Success) {
    if (actual_type == XA_WINDOW && actual_format == 32 && nitems > 0) {
        *active_window = *((Window *)prop_data);
        ret = Success;
    }
    XFree(prop_data);
}

if(ret != Success) {
    std::cout << "Failed to get _NET_ACTIVE_WINDOW property"<< std::endl;
}

return ret;

}

int getWindowName(Display* display, Window window, char* windowName){
char buf[256] = {0};
XTextProperty tp;
XGetTextProperty(display,window,&tp,XInternAtom(display, "_NET_WM_NAME", False));
int ret = -1;
if (tp.nitems > 0){
int count = 0, i, ret2;
char **list = NULL;
ret2 = XmbTextPropertyToTextList(display, &tp, &list, &count);
if((ret2 == Success || ret2 > 0) && list != NULL){
for(i=0; i sprintf(buf,"%s", list[0]);
XFreeStringList(list);
} else {
sprintf(buf,"%s", tp.value);
}

    sprintf(windowName, "%s", buf);
    ret = 0;

    XFree(tp.value);
}
return ret;

}

int utils::getActiveWindowName(char* name) {

Display *display = XOpenDisplay(NULL);
if (!display) {
return -1;
}

Window root = DefaultRootWindow(display), activeWindow;

getActiveWindow(display, root, &activeWindow);

getWindowName(display, activeWindow, name);

std::cout << "window_name:"<< name << std::endl;

XCloseDisplay(display);
return 0;

}

Reply View the author
182******61
deepin
2024-12-03 14:22
#6

但是只能获取到锁定窗口以外的其它窗口名称

Reply View the author
182******61
deepin
2024-12-03 15:14
#7

用xdotool也一样, xdotool getwindowfocus getwindowname,获取不到锁定窗口的名称

Reply View the author
忘记、过去
deepin
2024-12-03 16:25
#8
busctl --user get-property org.deepin.dde.LockFront1 /org/deepin/dde/LockFront1 org.deepin.dde.LockFront1 Visible

可以试试从锁屏界面的 D-Bus 属性 Visible 获取锁屏界面是否显示,执行后会返回属性类型和对应值,如 b false 代表 bool 类型,值为 false

Reply View the author
182******61
deepin
2024-12-04 11:35
#9

连一个锁定界面的判断接口都没有,这玩意也是没治了

Reply View the author