使用X11,如何在忽略某些事件的同时将用户的时间“远离键盘”?

时间:2012-01-28 22:12:27

标签: linux x11 xcb

我正在制作一个需要了解用户闲置时间的应用程序 - 例如,不使用键盘或鼠标。 XCB和Xlib都承诺通过各自的屏幕保护程序扩展为我提供空闲时间。这是我在XCB闲暇时间的地方:

#include <stdlib.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_idle_time () {
    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);

    uint32_t idle = info->ms_since_user_input;
    free (info);

    return idle;
}

然而,这与“ms_since_user_input”建议的行为非常不同。如果我正在观看视频(使用Totem测试),空闲时间将在30秒内重置为0,无一例外。许多游戏都会发生同样的事情,即使它们被暂停也会导致这种情况发生! 使用XLib,我得到完全相同的行为。

我可能能够改进使用空闲时间的代码,所以这种行为不是一个问题,但我真的想完全摆脱这个问题。如果我只是从上次用户输入事件(并且只有最后一个用户输入事件)获得时间,我更愿意。我不介意使用其他一些库来实现,只要我的程序不会产生大量流量。

您对如何做到这一点有什么想法吗?

1 个答案:

答案 0 :(得分:6)

你用图腾看到的是它试图避开屏幕保护程序。它通过定期发送键事件来实现。

您可以在此处找到执行此操作的代码:http://git.gnome.org/browse/totem/tree/lib/totem-scrsaver.c#n318

由于屏幕保护程序使用与您相同的扩展程序,因此您的计数器将使用此结果达到零。