使用X11,我怎样才能让用户“远离键盘”的时间变得更短?同时忽略某些事件?

发布于 2024-12-29 13:14:30 字数 1251 浏览 0 评论 0原文

我正在制作一个小应用程序,需要知道用户空闲了多长时间 - 例如,不使用键盘或鼠标。 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,我得到了完全相同的行为。

我也许能够改进使用空闲时间的代码,这样这种行为就不再是一个问题,但我真的很想完全解决这个问题。如果我只获取自上次用户输入事件(并且仅上次用户输入事件)以来的时间,我会更愿意。我不介意使用其他一些库来实现这一目标,只要我的程序不会产生大量流量。

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

I'm making a little application that needs to know how long the user has been idle — as in, not using a keyboard or a mouse. Both XCB and Xlib promise to give me idle time through their respective screensaver extensions. Here is where I get idle time with 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;
}

However, this is behaving very differently than "ms_since_user_input" suggests. If I am watching a video (tested with Totem), the idle time resets to 0 within 30 seconds, without exception. The same thing happens with a number of games, which cause this even when they are paused!
Using XLib, I get the exact same behaviour.

I might be able to improve the code that uses the idle time so this behaviour isn't as much of a problem, but I'd really like to get rid of the problem completely. I would prefer if I was only getting the time since the last user input event (and only the last user input event). I wouldn't mind using some other libraries to get there, as long as my program doesn't generate a lot of traffic.

Do you have any ideas for how this can be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无妨# 2025-01-05 13:14:30

您在图腾中看到的是它试图避免屏幕保护程序启动。它通过定期发送关键事件来实现这一点。

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

由于屏幕保护程序使用与您使用的相同的扩展名,这会导致您的计数器为零。

What you're seeing with totem is it trying to avoid the screensaver kicking in. It does this by sending key event at regular intervals.

You can find the code that does that here: http://git.gnome.org/browse/totem/tree/lib/totem-scrsaver.c#n318

And since the screensaver uses the same extension as you're using this results in your counter hitting zero.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文