一:LUA脚本:就是用xinput来完成对鼠标速度的控制
function luaMouseSpeed(s)
local cmdStr
local pStr
local pFile
local message = ""
cmdStr = "xinput --list |grep mouse"
print("mouse speed cmd: ", cmdStr)
pFile = io.popen(cmdStr)
pStr = pFile:read("*all")
pFile:close()
local a = string.find(pStr, 'id')
local id = (string.sub(pStr,a+3,56))
message = "xinput --set-prop" .. " " ..id .."Device Accel Constant Deceleration" .. " " .. s
print("message:", message)
os.execute(message)
end
C调用:
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
int main()
{
char luaPath[128] = {0};
int ret = 0;
lua_State *luaHandle = luaL_newstate();
luaL_openlibs(luaHandle);
snprintf(luaPath, sizeof(luaPath),"/home/wdj/mouse.lua");
ret = luaL_dofile(luaHandle, luaPath);
if(ret != LUA_OK)
{
printf("\nerror luaL_dofile!\n");
}
int speed = 1000;
lua_getglobal(luaHandle, "luaMouseSpeed");
if(lua_isfunction(luaHandle, -1))
{
lua_pushnumber(luaHandle, speed);
}
else
printf("\nerror lua_isfunction\n");
lua_pcall(luaHandle, 1, 0, 0);
lua_close(luaHandle);
return 0;
}
编译:gcc main.c -I …/include -L …/lib/ -llua -lm -ldl
补充:
二:系统命令
有些linux版本的xinput表现不一致,很麻烦。
/* xset m speed */
int setMouseSpeed(int speed)
{
char cmdStr[128] = {0};
snprintf(cmdStr, sizeof(cmdStr), "xset m %d", speed);
system(cmdStr);
return 0;
}