显示标签为“编程技巧”的博文。显示所有博文
显示标签为“编程技巧”的博文。显示所有博文

2009年6月1日星期一

vim macro

Recording macros

Occasionally, you'll find yourself doing the same thing over and over to blocks of text in your document. vim will let you record an ad-hoc macro to perform the operation.
  • qregister Start macro recording into the named register. For instance, qa starts recording and puts the macro into register a.

  • q End recording.

  • @register Replay the macro stored in the named register. For instance, @a replays the macro in register a.

2007年11月28日星期三

如何取得系统的闲置(idle)时间

在windows平台上,利用调用GetLastInputInfo()函数得到最后一次输入事件发生的时间,通过计算当前时间与最后输入时间的时间差得到系统闲置时间。

定义:

#define _WIN32_WINNT 0x0500
#include <windows.h>
#define EXPORT __declspec(dllexport)

typedef BOOL (WINAPI *GETLASTINPUTINFO)(LASTINPUTINFO *);
static HMODULE g_user32 = NULL;
static GETLASTINPUTINFO g_GetLastInputInfo = NULL;

初始化:

g_user32 = LoadLibrary("user32.dll");
if (g_user32) {
 g_GetLastInputInfo = (GETLASTINPUTINFO)GetProcAddress(g_user32, "GetLastInputInfo");
}

得到系统闲置时间:

int idle_time = 0;

if (g_GetLastInputInfo != NULL) {
 LASTINPUTINFO lii;
 memset(&lii, 0, sizeof(lii));
 lii.cbSize = sizeof(lii);
 if (g_GetLastInputInfo(&lii)) {
  idle_time = lii.dwTime;
 }
 idle_time = (GetTickCount() - idle_time) / 1000;
}


在Linux下,则是利用X11屏保扩展函数中的相关调用得到系统闲置时间。

定义:

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/scrnsaver.h>
#include <gdk/gdkx.h>
#include <gtk/gtk.h>

得到系统闲置时间:

static XScreenSaverInfo *mit_info = NULL;
int idle_time, event_base, error_base;

gtk_init (NULL, NULL);
if (XScreenSaverQueryExtension(GDK_DISPLAY(), &event_base, &error_base))
{
 if (mit_info == NULL)
  mit_info = XScreenSaverAllocInfo();
 XScreenSaverQueryInfo(GDK_DISPLAY(), GDK_ROOT_WINDOW(), mit_info);
 idle_time = (mit_info->idle) / 1000;
}
else
 idle_time = 0;