From 25a02260c4131f5b7338c22579bfb00003c03786 Mon Sep 17 00:00:00 2001 From: Matt Thompson Date: Thu, 23 Jul 2009 04:27:22 -0500 Subject: [PATCH] Made the source tree a little nicer, added some info for Fedora users. --- README | 44 +++-- src/CMakeLists.txt | 2 - src/umd.c | 469 +++------------------------------------------ src/{umd.c => umd_linux.c} | 167 +--------------- src/umd_osx.c | 29 +++ src/umd_private.h | 28 +++ src/umd_win32.c | 149 ++++++++++++++ 7 files changed, 265 insertions(+), 623 deletions(-) rewrite README (68%) rewrite src/umd.c (89%) copy src/{umd.c => umd_linux.c} (64%) create mode 100644 src/umd_osx.c create mode 100644 src/umd_private.h create mode 100644 src/umd_win32.c diff --git a/README b/README dissimilarity index 68% index 6355d9c..44d298f 100644 --- a/README +++ b/README @@ -1,14 +1,30 @@ ------------------------ - Ultimate Mouse Driver ------------------------ - -The umd library is a simple to use, crossplatform, GPL tool to control the mouse. It supports relative and absolute motion and can click just like a three button mouse. It has a backend for Linux 2.6, Win32, and OSX. - -------------------------------- - Platform specific usage notes -------------------------------- - - Linux: -------- - Your kernel must support uinput. To set the permissions for your uinput device, copy the file extras/95-uinput.rules to /etc/udev/rules.d and reload your udev (/etc/init.d/udev reload). Then, load the module (modprobe uinput). You will probably need to set up an fdi for uinput devices as well. Just copy extras/umd-mouse.fdi to /etc/hal/fdi/preprobe and restart your hal (/etc/init.d/hal restart). - +----------------------- + Ultimate Mouse Driver +----------------------- + +The umd library is a simple to use, crossplatform, GPL tool to control the +mouse. It supports relative and absolute motion and can click just like a three +button mouse. It has a backend for Linux 2.6, Win32, and OSX. + +------------------------------- + Platform specific usage notes +------------------------------- + + Linux: +------- +Your kernel must support uinput. To set the permissions for your uinput device, copy the file extras/95-uinput.rules to /etc/udev/rules.d and reload your udev: + +Ubuntu or Debian: /etc/init.d/udev reload +Fedora or Redhat: /etc/init.d/udev-post reload + +Then, load the module (modprobe uinput). You will probably need to set up an +fdi for uinput devices as well. Just copy extras/umd-mouse.fdi to +/etc/hal/fdi/preprobe and restart your hal: + +Ubuntu or Debian: /etc/init.d/hal restart +Fedora or Redhat: /etc/init.d/haldaemon restart + +Note: Please make sure your users are in the same group that is defined in +the extras/95-uinput.rules file. If you are having trouble try reloading +the uinput module to see if it gets the right permissions from udev. + diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 75c5807..6013d30 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,8 +3,6 @@ aux_source_directory(. SRC) add_library(umd SHARED ${SRC}) add_library(umds STATIC ${SRC}) -target_link_libraries(ecoboard ${OPENCV_LIBRARIES} ${QT_LIBRARIES}) - install(FILES umd.h PUBLIC_HEADER DESTINATION include) diff --git a/src/umd.c b/src/umd.c dissimilarity index 89% index 48069e7..1878c91 100644 --- a/src/umd.c +++ b/src/umd.c @@ -1,441 +1,28 @@ -/************************************************************************* - Copyright (C) 2009 Matthew Thompson - - This file is part of libumd. - - libumd is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - libumd is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with libumd. If not, see . -*************************************************************************/ - -#include "umd.h" -#include -#include - -/************************************* - * LINUX HEADERS - *************************************/ -#if defined(__linux) || defined(linux) - -#include -#include -#include -#include -#include -#include -#include -#include - -/************************************* - * WIN32 HEADERS - *************************************/ -#elif defined(_WIN32) || defined(__WIN32__) - -#define _WIN32_WINNT 0x0500 -#include - -/************************************* - * OSX HEADERS - *************************************/ -#elif defined(__MACOSX__) || defined(__APPLE__) - -#else -# error This library does not support your platform! -#endif - -#define ERROR_LEN 256 -static char error_buff[ERROR_LEN] = ""; - -#define SET_ERROR(message) \ - { snprintf(error_buff, ERROR_LEN, "libumd error (%s:%d)\n\t%s", \ - __FILE__, __LINE__ -1, message); } - -const char *UMD_GetError() -{ - return error_buff; -} - -/************************************* - * LINUX DRIVER - *************************************/ -#if defined(__linux) || defined(linux) - -static int fd = -1; -static struct uinput_user_dev uidev; -static struct input_event event; - -static char *dev_filename[] = {"/dev/uinput", "/dev/input/uinput", "/dev/misc/uinput", 0}; - -int UMD_Init() -{ - int i; - unsigned int events[] = { EV_SYN, EV_KEY, EV_ABS, EV_REL, EV_MAX }; - unsigned int keys[] = { BTN_MOUSE, BTN_LEFT, BTN_RIGHT, BTN_MIDDLE, KEY_MAX }; - unsigned int mouseabs[] = { ABS_X, ABS_Y, ABS_MAX }; - unsigned int mouserel[] = { REL_X, REL_Y, REL_MAX }; - - for (i=0; dev_filename[i] != 0; i++) { - if ((fd = open(dev_filename[i], O_WRONLY)) >= 0) - break; - } - - if (fd <= 0) { - switch (errno) { - case EACCES: case EROFS: - SET_ERROR("Could not open uinput device due to permissions"); - break; - case ENODEV: case ENXIO: case ENOTDIR: - SET_ERROR("Could not find uinput device to open"); - break; - default: - SET_ERROR("Could not open uinput device"); - } - return -1; - } - - memset(&event, 0, sizeof event); - - memset(&uidev, 0, sizeof uidev); - snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "UMD Virtual Mouse (userspace driver)"); - - if (write(fd, &uidev, sizeof uidev) != sizeof uidev) { - SET_ERROR("Could not write to uinput device"); - } - - for (i=0; events[i] != EV_MAX; i++) { - if (ioctl(fd, UI_SET_EVBIT, events[i]) < 0) { - SET_ERROR("Could not set up uinput device"); - close(fd); - } - } - - for (i=0; keys[i] != KEY_MAX; i++) { - if (ioctl(fd, UI_SET_KEYBIT, keys[i]) < 0) { - SET_ERROR("Could not set up uinput device"); - close(fd); - } - } - - for (i=0; mouseabs[i] != ABS_MAX; i++) { - if (ioctl(fd, UI_SET_ABSBIT, mouseabs[i]) < 0) { - SET_ERROR("Could not set up uinput device"); - close(fd); - } - } - - for (i=0; mouserel[i] != REL_MAX; i++) { - if (ioctl(fd, UI_SET_RELBIT, mouserel[i]) < 0) { - SET_ERROR("Could not set up uinput device"); - close(fd); - } - } - - if (ioctl(fd, UI_DEV_CREATE, 0) < 0) { - SET_ERROR("Could not create uinput device"); - close(fd); - return -1; - } - - usleep(500 * 1000); // TODO: replace this with something more sensible - - return 0; -} - -int UMD_Quit() -{ - if (fd <= 0) { - SET_ERROR("Could not quit, umd was not initialized"); - return -1; - } - - if (ioctl(fd, UI_DEV_DESTROY) != 0) { - SET_ERROR("Could not destroy virtual mouse device"); - close(fd); - return -1; - } - - close(fd); - - return 0; -} - -static int motion_event(unsigned int type, int x, int y) -{ - unsigned int code_x, code_y; - if (type == EV_ABS) { - code_x = ABS_X; - code_y = ABS_Y; - } - else { - code_x = REL_X; - code_y = REL_Y; - } - - gettimeofday(&event.time, NULL); - - event.type = type; - event.code = code_x; - event.value = x; - if (write(fd, &event, sizeof event) != sizeof event) - return -1; - - event.type = type; - event.code = code_y; - event.value = y; - if (write(fd, &event, sizeof event) != sizeof event) - return -1; - - event.type = EV_SYN; - event.code = SYN_REPORT; - event.value = 0; - if (write(fd, &event, sizeof event) != sizeof event) - return -1; - - return 0; -} - -int UMD_Warp(int x, int y) -{ - if (fd < 0) { - SET_ERROR("Could not warp mouse, umd was not initialized"); - return -1; - } - - if (motion_event(EV_ABS, x, y) < 0) { - SET_ERROR("Could not warp mouse"); - return -1; - } - - return 0; -} - -int UMD_Move(int x, int y) -{ - if (fd < 0) { - SET_ERROR("Could not move mouse, umd was not initialized"); - return -1; - } - - if (motion_event(EV_REL, x, y) < 0) { - SET_ERROR("Could not move mouse"); - return -1; - } - - return 0; -} - -int UMD_Click(int button, int state) -{ - unsigned int code; - - switch(button) { - case UMD_LEFT: - code = BTN_LEFT; - break; - case UMD_RIGHT: - code = BTN_RIGHT; - break; - case UMD_MIDDLE: - code = BTN_MIDDLE; - break; - default: - SET_ERROR("Unrecognized mouse button in click"); - return -1; - } - - gettimeofday(&event.time, NULL); - - event.type = EV_KEY; - event.code = code; - event.value = 1; - if (write(fd, &event, sizeof event) != sizeof event) - return -1; - - event.type = EV_SYN; - event.code = SYN_REPORT; - event.value = state?1:0; - if (write(fd, &event, sizeof event) != sizeof event) - return -1; - - return 0; - -write_error: - SET_ERROR("Could not set position of virtual device"); - close(fd); - return -1; -} - -int UMD_ClickAt(int button, int state, int x, int y) -{ - if (UMD_Warp(x, y) < 0) - return -1; - - if (UMD_Click(button, state) < 0) - return -1; - - return 0; -} - -int UMD_SingleClick(int button) -{ - if (UMD_Click(button, 1) < 0) - return -1; - - if (UMD_Click(button, 0) < 0) - return -1; - - return 0; -} - -int UMD_SingleClickAt(int button, int x, int y) -{ - if (UMD_ClickAt(button, 1, x, y) < 0) - return -1; - - if (UMD_Click(button, 0) < 0) - return -1; - - return 0; -} - -/************************************* - * WIN32 DRIVER - *************************************/ -#elif defined(_WIN32) || defined(__WIN32__) - -int UMD_Init() -{ - return 0; -} - -int UMD_Quit() -{ - return 0; -} - -int UMD_Warp(int x, int y) -{ - SetCursorPos(x, y); - - return 0; -} - -int UMD_Move(int x, int y) -{ - POINT point; - - if (GetCursorPos(&point) == 0) { - SET_ERROR("Could not get current cursor position to move"); - return -1; - } - - SetCursorPos(point.x + x, point.y + y); - - return 0; -} - -static int get_flags(int button, int state) -{ - int flags = 0; - - switch(button) { - case UMD_LEFT: - flags = state? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_LEFTUP; - break; - case UMD_RIGHT: - flags = state? MOUSEEVENTF_RIGHTDOWN : MOUSEEVENTF_RIGHTUP; - break; - case UMD_MIDDLE: - flags = state? MOUSEEVENTF_MIDDLEDOWN : MOUSEEVENTF_MIDDLEUP; - break; - default: - SET_ERROR("Unrecognized mouse button in click"); - return 0; - } - - return flags; -} - -int UMD_Click(int button, int state) -{ - INPUT input; - int flags = get_flags(button, state); - - if (flags == 0) - return -1; - - memset(&input, 0, sizeof input); - input.type = INPUT_MOUSE; - input.mi.dwFlags = flags; - - if (SendInput(1, &input, sizeof input) != 1) { - SET_ERROR("Could not send click event"); - return -1; - } - - return 0; -} - -int UMD_ClickAt(int button, int state, int x, int y) -{ - INPUT input; - int flags = get_flags(button, state); - - if (flags == 0) - return -1; - - memset(&input, 0, sizeof input); - input.type = INPUT_MOUSE; - input.mi.dwFlags = flags | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE; - input.mi.dx = x; - input.mi.dy = y; - - if (SendInput(1, &input, sizeof input) != 1) { - SET_ERROR("Could not send click at event"); - return -1; - } - - return 0; -} - -int UMD_SingleClick(int button) -{ - if (UMD_Click(button, 1) < 0) - return -1; - - if (UMD_Click(button, 0) < 0) - return -1; - - return 0; -} - -int UMD_SingleClickAt(int button, int x, int y) -{ - if (UMD_ClickAt(button, 1, x, y) < 0) - return -1; - - if (UMD_Click(button, 0) < 0) - return -1; - - return 0; -} - -/************************************* - * OSX DRIVER - *************************************/ -#elif defined(__MACOSX__) || defined(__APPLE__) - -#endif - - - - +/************************************************************************* + Copyright (C) 2009 Matthew Thompson + + This file is part of libumd. + + libumd is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + libumd is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with libumd. If not, see . +*************************************************************************/ + +#include "umd_private.h" + +char error_buff[ERROR_LEN] = ""; + +const char *UMD_GetError() +{ + return error_buff; +} + diff --git a/src/umd.c b/src/umd_linux.c similarity index 64% copy from src/umd.c copy to src/umd_linux.c index 48069e7..7988ce7 100644 --- a/src/umd.c +++ b/src/umd_linux.c @@ -18,12 +18,8 @@ *************************************************************************/ #include "umd.h" -#include -#include +#include "umd_private.h" -/************************************* - * LINUX HEADERS - *************************************/ #if defined(__linux) || defined(linux) #include @@ -35,40 +31,6 @@ #include #include -/************************************* - * WIN32 HEADERS - *************************************/ -#elif defined(_WIN32) || defined(__WIN32__) - -#define _WIN32_WINNT 0x0500 -#include - -/************************************* - * OSX HEADERS - *************************************/ -#elif defined(__MACOSX__) || defined(__APPLE__) - -#else -# error This library does not support your platform! -#endif - -#define ERROR_LEN 256 -static char error_buff[ERROR_LEN] = ""; - -#define SET_ERROR(message) \ - { snprintf(error_buff, ERROR_LEN, "libumd error (%s:%d)\n\t%s", \ - __FILE__, __LINE__ -1, message); } - -const char *UMD_GetError() -{ - return error_buff; -} - -/************************************* - * LINUX DRIVER - *************************************/ -#if defined(__linux) || defined(linux) - static int fd = -1; static struct uinput_user_dev uidev; static struct input_event event; @@ -307,133 +269,6 @@ int UMD_SingleClickAt(int button, int x, int y) return 0; } -/************************************* - * WIN32 DRIVER - *************************************/ -#elif defined(_WIN32) || defined(__WIN32__) - -int UMD_Init() -{ - return 0; -} - -int UMD_Quit() -{ - return 0; -} - -int UMD_Warp(int x, int y) -{ - SetCursorPos(x, y); - - return 0; -} - -int UMD_Move(int x, int y) -{ - POINT point; - - if (GetCursorPos(&point) == 0) { - SET_ERROR("Could not get current cursor position to move"); - return -1; - } - - SetCursorPos(point.x + x, point.y + y); - - return 0; -} - -static int get_flags(int button, int state) -{ - int flags = 0; - - switch(button) { - case UMD_LEFT: - flags = state? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_LEFTUP; - break; - case UMD_RIGHT: - flags = state? MOUSEEVENTF_RIGHTDOWN : MOUSEEVENTF_RIGHTUP; - break; - case UMD_MIDDLE: - flags = state? MOUSEEVENTF_MIDDLEDOWN : MOUSEEVENTF_MIDDLEUP; - break; - default: - SET_ERROR("Unrecognized mouse button in click"); - return 0; - } - - return flags; -} - -int UMD_Click(int button, int state) -{ - INPUT input; - int flags = get_flags(button, state); - - if (flags == 0) - return -1; - - memset(&input, 0, sizeof input); - input.type = INPUT_MOUSE; - input.mi.dwFlags = flags; - - if (SendInput(1, &input, sizeof input) != 1) { - SET_ERROR("Could not send click event"); - return -1; - } - - return 0; -} - -int UMD_ClickAt(int button, int state, int x, int y) -{ - INPUT input; - int flags = get_flags(button, state); - - if (flags == 0) - return -1; - - memset(&input, 0, sizeof input); - input.type = INPUT_MOUSE; - input.mi.dwFlags = flags | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE; - input.mi.dx = x; - input.mi.dy = y; - - if (SendInput(1, &input, sizeof input) != 1) { - SET_ERROR("Could not send click at event"); - return -1; - } - - return 0; -} - -int UMD_SingleClick(int button) -{ - if (UMD_Click(button, 1) < 0) - return -1; - - if (UMD_Click(button, 0) < 0) - return -1; - - return 0; -} - -int UMD_SingleClickAt(int button, int x, int y) -{ - if (UMD_ClickAt(button, 1, x, y) < 0) - return -1; - - if (UMD_Click(button, 0) < 0) - return -1; - - return 0; -} - -/************************************* - * OSX DRIVER - *************************************/ -#elif defined(__MACOSX__) || defined(__APPLE__) - #endif diff --git a/src/umd_osx.c b/src/umd_osx.c new file mode 100644 index 0000000..356282a --- /dev/null +++ b/src/umd_osx.c @@ -0,0 +1,29 @@ +/************************************************************************* + Copyright (C) 2009 Matthew Thompson + + This file is part of libumd. + + libumd is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + libumd is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with libumd. If not, see . +*************************************************************************/ + +#include "umd.h" +#include "umd_private.h" + +#if defined(__MACOSX__) || defined(__APPLE__) + +#endif + + + + diff --git a/src/umd_private.h b/src/umd_private.h new file mode 100644 index 0000000..da5d8a7 --- /dev/null +++ b/src/umd_private.h @@ -0,0 +1,28 @@ +/************************************************************************* + Copyright (C) 2009 Matthew Thompson + + This file is part of libumd. + + libumd is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + libumd is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with libumd. If not, see . +*************************************************************************/ + +#include +#include + +#define ERROR_LEN 256 +extern char error_buff[ERROR_LEN]; + +#define SET_ERROR(message) \ + { snprintf(error_buff, ERROR_LEN, "libumd error (%s:%d)\n\t%s", \ + __FILE__, __LINE__ -1, message); } diff --git a/src/umd_win32.c b/src/umd_win32.c new file mode 100644 index 0000000..f15c892 --- /dev/null +++ b/src/umd_win32.c @@ -0,0 +1,149 @@ +/************************************************************************* + Copyright (C) 2009 Matthew Thompson + + This file is part of libumd. + + libumd is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + libumd is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with libumd. If not, see . +*************************************************************************/ + +#include "umd.h" +#include "umd_private.h" + +#if defined(_WIN32) || defined(__WIN32__) + +#define _WIN32_WINNT 0x0500 +#include + +int UMD_Init() +{ + return 0; +} + +int UMD_Quit() +{ + return 0; +} + +int UMD_Warp(int x, int y) +{ + SetCursorPos(x, y); + + return 0; +} + +int UMD_Move(int x, int y) +{ + POINT point; + + if (GetCursorPos(&point) == 0) { + SET_ERROR("Could not get current cursor position to move"); + return -1; + } + + SetCursorPos(point.x + x, point.y + y); + + return 0; +} + +static int get_flags(int button, int state) +{ + int flags = 0; + + switch(button) { + case UMD_LEFT: + flags = state? MOUSEEVENTF_LEFTDOWN : MOUSEEVENTF_LEFTUP; + break; + case UMD_RIGHT: + flags = state? MOUSEEVENTF_RIGHTDOWN : MOUSEEVENTF_RIGHTUP; + break; + case UMD_MIDDLE: + flags = state? MOUSEEVENTF_MIDDLEDOWN : MOUSEEVENTF_MIDDLEUP; + break; + default: + SET_ERROR("Unrecognized mouse button in click"); + return 0; + } + + return flags; +} + +int UMD_Click(int button, int state) +{ + INPUT input; + int flags = get_flags(button, state); + + if (flags == 0) + return -1; + + memset(&input, 0, sizeof input); + input.type = INPUT_MOUSE; + input.mi.dwFlags = flags; + + if (SendInput(1, &input, sizeof input) != 1) { + SET_ERROR("Could not send click event"); + return -1; + } + + return 0; +} + +int UMD_ClickAt(int button, int state, int x, int y) +{ + INPUT input; + int flags = get_flags(button, state); + + if (flags == 0) + return -1; + + memset(&input, 0, sizeof input); + input.type = INPUT_MOUSE; + input.mi.dwFlags = flags | MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE; + input.mi.dx = x; + input.mi.dy = y; + + if (SendInput(1, &input, sizeof input) != 1) { + SET_ERROR("Could not send click at event"); + return -1; + } + + return 0; +} + +int UMD_SingleClick(int button) +{ + if (UMD_Click(button, 1) < 0) + return -1; + + if (UMD_Click(button, 0) < 0) + return -1; + + return 0; +} + +int UMD_SingleClickAt(int button, int x, int y) +{ + if (UMD_ClickAt(button, 1, x, y) < 0) + return -1; + + if (UMD_Click(button, 0) < 0) + return -1; + + return 0; +} + +#endif + + + + -- 2.11.4.GIT