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