使用 xlib 检测正在运行的屏幕保护程序

4
我希望您能够检测屏幕保护程序是否正在运行,与此相关的是IT技术。以下是我目前所得到的代码:
```html

我正在尝试检测屏幕保护程序是否正在运行。

这是我迄今为止得到的代码:

```
/* LDFLAGS='-L/usr/X11R6/lib/ -lX11 -lXext -lXss' make xidle */
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/scrnsaver.h>

int
main(int argc, char *argv[])
{
    XScreenSaverInfo info;
    Display *dpy = XOpenDisplay(NULL);

    if(NULL == dpy) {
        fprintf(stderr, "failed to open display\n");
        return 1;
    }

    int a = 0;
    int b = 0;
    XScreenSaverQueryExtension(dpy, &a, &b);
    printf("%d %d\n", a, b);

    XScreenSaverQueryInfo(dpy, RootWindow(dpy, DefaultScreen(dpy)), &info);
    printf("%d %d %d %d\n", info.state, info.til_or_since, info.idle, info.kind);
    return 0;
}

但是 info.state 总是 3(ScreenSaverDisabled)。我已经使用 xscreensaver 和 gnome-screensaver 进行了测试。

以下是一些示例输出:

92 0
3 0 9903 0

无论是运行屏幕保护程序还是不运行(当然 info.idle 除外),都是一样的。

附加信息:

$ X -version
X.Org X Server 1.13.0
Release Date: 2012-09-05
X Protocol Version 11, Revision 0

窗口管理器:i3

发行版:Arch Linux

编辑: 在[这里][1]的帮助下,我创建了一个XCB版本,但仍然无法正常工作。为了排除我的测试过程中的错误,这是代码示例: 当我在后台运行xscreensaver时,我使用以下代码在无限循环中运行。要实际激活屏幕保护程序,我使用xscreensaver-command --activate。

#include <stdlib.h>
#include <stdio.h>
#include <xcb/xcb.h>
#include <xcb/screensaver.h>

static xcb_connection_t * connection;
static xcb_screen_t * screen;

/**
 * Connects to the X server (via xcb) and gets the screen
 */
void magic_begin () {
    connection = xcb_connect (NULL, NULL);
    screen = xcb_setup_roots_iterator (xcb_get_setup (connection)).data;
}

/**
 * Asks X for the time the user has been idle
 * @returns idle time in milliseconds
 */
unsigned long magic_get_state () {
    xcb_screensaver_query_info_cookie_t cookie;
    xcb_screensaver_query_info_reply_t *info;

    cookie = xcb_screensaver_query_info (connection, screen->root);
    info = xcb_screensaver_query_info_reply (connection, cookie, NULL);

    int state = info->state;

    return state;
}

int main(int arc, char *argv[])
{
    magic_begin();
    int state = magic_get_state();
    printf("state: %d\n", state);
}


[1]: https://dev59.com/pF_Va4cB1Zd3GeqPW8zE

这段代码在我的Ubuntu/VirtualBox上看起来没问题,而且运行正常:https://gist.github.com/4142683 - Andrey Sidorov
你使用的是哪个版本的Ubuntu?你是如何测试的?我已经尝试过Ubuntu 10.04,但它也没有起作用。我正在运行xscreensaver,然后运行xscreensaver-command --activate。 - knopwob
Ubuntu 12.04,通过ssh连接到虚拟机并观察输出,直到屏幕保护程序因超时而启用。 - Andrey Sidorov
1个回答

2

我去了xorg irc频道,被告知至少xscreensaver没有使用我所使用的扩展。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接