有了X11,我怎么能让用户的时间“远离键盘”而忽略某些事件呢?

我正在做一个小应用程序,需要知道用户闲置了多久 – 如在不使用键盘或鼠标。 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”build议的行为非常不同。 如果我正在观看video(使用Totem进行testing),空闲时间在30秒内重置为0,无一例外。 同样的事情发生在很多游戏中,即使它们被暂停,也是如此。 使用XLib,我得到完全相同的行为。

我可能能够改进使用空闲时间的代码,所以这种行为不是一个很大的问题,但我真的想完全摆脱这个问题。 我宁愿如果我只是得到自上次用户input事件(和只有最后一个用户input事件)以来的时间。 只要我的程序不会产生大量的stream量,我不会介意使用其他一些库来达到目的。

你有什么想法可以做到这一点?

你用图腾看到的是它试图避免屏幕保护程序踢。它通过定期发送关键事件来做到这一点。

你可以找到这样的代码: http : //git.gnome.org/browse/totem/tree/lib/totem-scrsaver.c#318

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