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

标签 c linux xlib screensaver

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

这是我目前得到的代码:

/* 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

编辑: 在 [this][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]: http://stackoverflow.com/questions/9049087/with-x11-how-can-i-get-the-users-time-away-from-keyboard-while-ignoring-cert

最佳答案

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

关于c - 使用 xlib 检测正在运行的屏幕保护程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13545302/

相关文章:

c、指针和 printf 的参数列表。使困惑

linux - bash - 根据过程条件返回值

linux - crontab 下安装的脚本未运行

linux - 当我在 X11 Windows 上绘图时,我可以立即看到它吗?

pointers - X11:如何真正捕获鼠标指针?

c++ - GTK 窗口的事件窗口的 X11 屏幕截图失败

c - 如何在 Windows 上调试 C 程序?

objective-c - ObjC 访问 C 函数中的属性

c - GDB 说 "no symbol table,"但 nm 显示文件有调试符号

linux - Perl 无法设置语言环境,即使它已安装