借助X11,如何获得用户的时间“远离键盘"?而忽略某些事件? [英] With X11, how can I get the user's time "away from keyboard" while ignoring certain events?

查看:107
本文介绍了借助X11,如何获得用户的时间“远离键盘"?而忽略某些事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个小应用程序,该应用程序需要知道用户空闲了多长时间-例如,不使用键盘或鼠标. XCB和Xlib都承诺通过它们各自的屏保扩展程序给我空闲时间.这是我使用XCB的空闲时间:

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;
}

但是,这与"ms_since_user_input"建议的行为有很大不同.如果我正在观看视频(使用Totem测试),则空闲时间将在30秒内重置为0,无一例外.许多游戏也会发生相同的情况,即使暂停游戏也会导致这种情况! 使用XLib,我得到的行为完全相同.

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.

您对此有什么想法吗?

推荐答案

您使用图腾看到的是它试图避免屏幕保护程序启动.它是通过定期发送按键事件来实现的.

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.

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

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.

这篇关于借助X11,如何获得用户的时间“远离键盘"?而忽略某些事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆