some material
http://blog.chinaunix.net/u/4206/showart_721067.htmlhttp://blog.csdn.net/wushihua/archive/2010/07/02/5709359.aspx
linux 下:
0. bash
1. 用户任意设置“工作时间”,“休息时间”。目前考虑使用命令行参数 done
2. 动态获取“设备号”,在不同的机子上用可不需修改 (no so urgent)
*3. 时间显示
改进:
1.1 如果休息起来五分钟(默认休息时间)才回来,只剩下不多的工作时间,才做一会儿又要中断。
最好是在锁定键鼠五分钟后,在用户回来电脑时才开始让工作时间计时。
考虑方案一:
等待用户输入后才(解除鼠标锁定)开始进入下一轮工作计时(done)
较高级的方案:
休息时间结束后,捕捉到键盘或者鼠标动作才开始下一轮工作计时。
1.2 工作时间离开电脑,进入休息时间计时。若一定时间键鼠都没反应。
(done, 获取键盘鼠标的空闲,若比休息时间还长,进入新一轮的工作)
2. kill sleep后,可能键盘永远锁住。 解决关闭该程序的善后工作。(done , use signal)
锁住鼠标,不锁住键盘,在锁住鼠标时,Ctrl-C,结束程序,但鼠标没有解锁。
备注:
1. 是否锁定键盘得明确,考虑是否对两需求推出不同方案。暂考虑只锁键盘
some note:
1.
操作/dev/input/event*文件,向它写入个input_event结构体就可以模拟按键的输入
哪个event文件可通过cat /proc/bus/input/devices 查看。
N: Name="AT Translated Set 2 keyboard"
H: Handlers=kbd event3
N: Name="Logitech USB Optical Mouse"
H: Handlers=mouse1 event4
2.
Essentially keyboard and mouse idle time can be
gleaned (indirectly) from certain lines of the /proc/interrupts file. It
seems this file contains a counter for each device
http://software.itags.org/linux-unix/330299/
Sense mouse and keyboard inactivity final solution:
/*
This is a test example.
Ref:
http://coderrr.wordpress.com/2008/04/20/getting-idle-time-in-unix/
gcc -o idle idle_xscr2.c -lXss
*/
#include <stdio.h>
#include <X11/extensions/scrnsaver.h>
#include <unistd.h>
int main()
{
XScreenSaverInfo *info = XScreenSaverAllocInfo();
Display *display = XOpenDisplay(0);
int i=1;
for(;i<5; i++){
sleep(3);
XScreenSaverQueryInfo(display, DefaultRootWindow(display), info);
printf("%ld ms\n", info->idle);
}
return 0;
}
编译:
voide@fit:~/bash$ gcc lock.c -o lock -lXss
lock.c:24:38: error: X11/extensions/scrnsaver.h: No such file or directory
lock.c: In function ‘main’:
lock.c:41: error: ‘XScreenSaverInfo’ undeclared (first use in this function)
lock.c:41: error: (Each undeclared identifier is reported only once
lock.c:41: error: for each function it appears in.)
lock.c:41: error: ‘info’ undeclared (first use in this function)
lock.c:42: error: ‘Display’ undeclared (first use in this function)
lock.c:42: error: ‘display’ undeclared (first use in this function)
root@fit:/home/voide/bash# apt-get install libxss-dev
root@fit:/home/voide/bash# gcc lock.c -o lock -lXss
root@fit:/home/voide/bash# ./lock 1800 300 /* 1800s 300s*/
Use a short time for debug
sh: ./unlock.sh: Permission denied
root@fit:/home/voide/bash# chmod +x *.sh