Almost rewrote the mouse input add-on to handle multiple devices, added usb devices monitoring (not tested as USB doesn't work on my machine).

Now it generates the device name from the folder name (i.e. /dev/input/mouse/serial/0 ---> "serial mouse 1").


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@8860 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stefano Ceccherini
2004-09-06 06:42:58 +00:00
parent f72376a8dd
commit 33efb91915
2 changed files with 290 additions and 72 deletions
@@ -24,11 +24,21 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE. // DEALINGS IN THE SOFTWARE.
/*****************************************************************************/ /*****************************************************************************/
// TODO: Use strlcpy instead of strcpy
#include "MouseInputDevice.h" #include "MouseInputDevice.h"
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <Directory.h>
#include <Entry.h>
#include <List.h>
#include <NodeMonitor.h>
#include <Path.h>
#include <String.h>
#define DEBUG 1 #define DEBUG 1
#if DEBUG #if DEBUG
#define LOG(text) fputs(text, sLogFile); fflush(sLogFile) #define LOG(text) fputs(text, sLogFile); fflush(sLogFile)
@@ -36,8 +46,11 @@
#define LOG(text) #define LOG(text)
#endif #endif
#include <Debug.h>
FILE *MouseInputDevice::sLogFile = NULL; FILE *MouseInputDevice::sLogFile = NULL;
bool MouseInputDevice::sQuit = false;
static MouseInputDevice *sSingletonMouseDevice = NULL;
// TODO: These are "stolen" from the kb_mouse driver on bebits, which uses // TODO: These are "stolen" from the kb_mouse driver on bebits, which uses
// the same protocol as BeOS one. They're just here to test this add-on with // the same protocol as BeOS one. They're just here to test this add-on with
@@ -49,6 +62,12 @@ const static uint32 kSetMouseType = 10104;
const static uint32 kSetMouseMap = 10106; const static uint32 kSetMouseMap = 10106;
const static uint32 kSetClickSpeed = 10108; const static uint32 kSetClickSpeed = 10108;
const static uint32 kMouseThreadPriority = B_FIRST_REAL_TIME_PRIORITY + 4;
const static char *kMouseDevicesDirectory = "/dev/input/mouse";
// "/dev/" is automatically prepended by StartMonitoringDevice()
const static char *kMouseDevicesDirectoryUSB = "input/mouse/usb";
struct mouse_movement { struct mouse_movement {
int32 ser_fd_index; int32 ser_fd_index;
uint32 buttons; uint32 buttons;
@@ -61,12 +80,24 @@ struct mouse_movement {
struct mouse_device { struct mouse_device {
mouse_device(const char *path);
~mouse_device();
input_device_ref device_ref;
char driver_path[B_PATH_NAME_LENGTH];
int driver_fd; int driver_fd;
thread_id device_watcher; thread_id device_watcher;
uint32 buttons_state; uint32 buttons_state;
mouse_settings settings;
bool active;
}; };
// forward declarations
static void scan_recursively(const char *directory, BList *list);
static char *get_short_name(const char *longName);
extern "C" extern "C"
BInputServerDevice * BInputServerDevice *
instantiate_input_device() instantiate_input_device()
@@ -76,30 +107,29 @@ instantiate_input_device()
MouseInputDevice::MouseInputDevice() MouseInputDevice::MouseInputDevice()
: fThread(-1) : fDevices(NULL)
{ {
// TODO: Open "/dev/input/mouse/serial/0" as well, and what about USB mouses ? ASSERT(sSingletonMouseDevice == NULL);
fFd = open("dev/input/mouse/ps2/0", O_RDWR); sSingletonMouseDevice = this;
if (fFd >= 0)
fThread = spawn_thread(DeviceWatcher, "mouse watcher thread",
B_FIRST_REAL_TIME_PRIORITY+4, this);
#if DEBUG #if DEBUG
if (sLogFile == NULL) if (sLogFile == NULL)
sLogFile = fopen("/var/log/mouse_device_log.log", "a"); sLogFile = fopen("/var/log/mouse_device_log.log", "a");
#endif #endif
StartMonitoringDevice(kMouseDevicesDirectoryUSB);
} }
MouseInputDevice::~MouseInputDevice() MouseInputDevice::~MouseInputDevice()
{ {
if (fThread >= 0) { StopMonitoringDevice(kMouseDevicesDirectoryUSB);
status_t dummy;
wait_for_thread(fThread, &dummy);
}
if (fFd >= 0) for (int32 i = 0; i < fDevices->CountItems(); i++)
close(fFd); delete (mouse_device *)fDevices->ItemAt(i);
delete fDevices;
#if DEBUG #if DEBUG
fclose(sLogFile); fclose(sLogFile);
@@ -108,62 +138,67 @@ MouseInputDevice::~MouseInputDevice()
status_t status_t
MouseInputDevice::InitFromSettings(uint32 opcode) MouseInputDevice::InitFromSettings(void *cookie, uint32 opcode)
{ {
mouse_device *device = (mouse_device *)cookie;
// retrieve current values // retrieve current values
if (get_mouse_map(&fSettings.map)!=B_OK) if (get_mouse_map(&device->settings.map) != B_OK)
fprintf(stderr, "error when get_mouse_map\n"); fprintf(stderr, "error when get_mouse_map\n");
else else
ioctl(fFd, kSetMouseMap, &fSettings.map); ioctl(device->driver_fd, kSetMouseMap, &device->settings.map);
if (get_click_speed(&fSettings.click_speed)!=B_OK) if (get_click_speed(&device->settings.click_speed) != B_OK)
fprintf(stderr, "error when get_click_speed\n"); fprintf(stderr, "error when get_click_speed\n");
else else
ioctl(fFd, kSetClickSpeed, &fSettings.click_speed); ioctl(device->driver_fd, kSetClickSpeed, &device->settings.click_speed);
if (get_mouse_speed(&fSettings.accel.speed)!=B_OK) if (get_mouse_speed(&device->settings.accel.speed) != B_OK)
fprintf(stderr, "error when get_mouse_speed\n"); fprintf(stderr, "error when get_mouse_speed\n");
else { else {
if (get_mouse_acceleration(&fSettings.accel.accel_factor)!=B_OK) if (get_mouse_acceleration(&device->settings.accel.accel_factor) != B_OK)
fprintf(stderr, "error when get_mouse_acceleration\n"); fprintf(stderr, "error when get_mouse_acceleration\n");
else { else {
mouse_accel accel; mouse_accel accel;
ioctl(fFd, kGetMouseAccel, &accel); ioctl(device->driver_fd, kGetMouseAccel, &accel);
accel.speed = fSettings.accel.speed; accel.speed = device->settings.accel.speed;
accel.accel_factor = fSettings.accel.accel_factor; accel.accel_factor = device->settings.accel.accel_factor;
ioctl(fFd, kSetMouseAccel, &fSettings.accel); ioctl(device->driver_fd, kSetMouseAccel, &device->settings.accel);
} }
} }
if (get_mouse_type(&fSettings.type)!=B_OK) if (get_mouse_type(&device->settings.type) != B_OK)
fprintf(stderr, "error when get_mouse_type\n"); fprintf(stderr, "error when get_mouse_type\n");
else else
ioctl(fFd, kSetMouseType, &fSettings.type); ioctl(device->driver_fd, kSetMouseType, &device->settings.type);
return B_OK; return B_OK;
} }
status_t status_t
MouseInputDevice::InitCheck() MouseInputDevice::InitCheck()
{ {
InitFromSettings(); BList list;
scan_recursively(kMouseDevicesDirectory, &list);
input_device_ref **devices = NULL; char path[B_PATH_NAME_LENGTH];
devices = (input_device_ref **)malloc(sizeof(input_device_ref *) * 2); if (list.CountItems() > 0) {
if (!devices) fDevices = new BList;
return B_NO_MEMORY; for (int32 i = 0; i < list.CountItems(); i++) {
strcpy(path, kMouseDevicesDirectory);
input_device_ref mouse1 = { "Mouse 1", B_POINTING_DEVICE, (void *)this }; strcat(path, "/");
strcat(path, (char *)list.ItemAt(i));
devices[0] = &mouse1; AddDevice(path);
devices[1] = NULL; free(list.ItemAt(i));
}
if (fFd >= 0 && fThread >= 0) {
RegisterDevices(devices);
return BInputServerDevice::InitCheck();
} }
if (fDevices && fDevices->CountItems() > 0)
return BInputServerDevice::InitCheck();
return B_ERROR; return B_ERROR;
} }
@@ -171,11 +206,22 @@ MouseInputDevice::InitCheck()
status_t status_t
MouseInputDevice::Start(const char *name, void *cookie) MouseInputDevice::Start(const char *name, void *cookie)
{ {
mouse_device *device = (mouse_device *)cookie;
char log[128]; char log[128];
snprintf(log, 128, "Start(%s)\n", name); snprintf(log, 128, "Start(%s)\n", name);
LOG(log); LOG(log);
resume_thread(fThread);
char threadName[B_OS_NAME_LENGTH];
snprintf(threadName, B_OS_NAME_LENGTH, "%s watcher", name);
device->active = true;
device->device_watcher = spawn_thread(DeviceWatcher, threadName,
kMouseThreadPriority, device);
resume_thread(device->device_watcher);
return B_OK; return B_OK;
} }
@@ -184,12 +230,18 @@ MouseInputDevice::Start(const char *name, void *cookie)
status_t status_t
MouseInputDevice::Stop(const char *name, void *cookie) MouseInputDevice::Stop(const char *name, void *cookie)
{ {
mouse_device *device = (mouse_device *)cookie;
char log[128]; char log[128];
snprintf(log, 128, "Stop(%s)\n", name); snprintf(log, 128, "Stop(%s)\n", name);
LOG(log); LOG(log);
suspend_thread(fThread); device->active = false;
if (device->device_watcher >= 0) {
status_t dummy;
wait_for_thread(device->device_watcher, &dummy);
}
return B_OK; return B_OK;
} }
@@ -208,41 +260,139 @@ MouseInputDevice::Control(const char *name, void *cookie,
HandleMonitor(message); HandleMonitor(message);
else if (command >= B_MOUSE_TYPE_CHANGED else if (command >= B_MOUSE_TYPE_CHANGED
&& command <= B_MOUSE_ACCELERATION_CHANGED) { && command <= B_MOUSE_ACCELERATION_CHANGED) {
InitFromSettings(command); InitFromSettings(cookie, command);
} }
return B_OK; return B_OK;
} }
// TODO: Test this. USB doesn't work on my machine
status_t status_t
MouseInputDevice::HandleMonitor(BMessage *message) MouseInputDevice::HandleMonitor(BMessage *message)
{ {
return B_OK; int32 opcode = 0;
status_t status;
status = message->FindInt32("opcode", &opcode);
if (status < B_OK)
return status;
BEntry entry;
BPath path;
dev_t device;
ino_t directory;
ino_t node;
const char *name = NULL;
switch (opcode) {
case B_ENTRY_CREATED:
{
message->FindInt32("device", &device);
message->FindInt64("directory", &directory);
message->FindInt64("node", &node);
message->FindString("name", &name);
entry_ref ref(device, directory, name);
status = entry.SetTo(&ref);
if (status == B_OK);
status = entry.GetPath(&path);
if (status == B_OK)
status = path.InitCheck();
if (status == B_OK)
status = AddDevice(path.Path());
break;
}
case B_ENTRY_REMOVED:
{
message->FindInt32("device", &device);
message->FindInt64("directory", &directory);
message->FindInt64("node", &node);
message->FindString("name", &name);
entry_ref ref(device, directory, name);
status = entry.SetTo(&ref);
if (status == B_OK);
status = entry.GetPath(&path);
if (status == B_OK)
status = path.InitCheck();
if (status == B_OK)
status = RemoveDevice(path.Path());
break;
}
default:
status = B_BAD_VALUE;
break;
}
return status;
}
status_t
MouseInputDevice::AddDevice(const char *path)
{
mouse_device *device = new mouse_device(path);
if (!device) {
LOG("No memory\n");
return B_NO_MEMORY;
}
input_device_ref *devices[2];
devices[0] = &device->device_ref;
devices[1] = NULL;
fDevices->AddItem(device);
InitFromSettings(device);
return RegisterDevices(devices);
}
status_t
MouseInputDevice::RemoveDevice(const char *path)
{
if (fDevices) {
int32 i = 0;
mouse_device *device = NULL;
while ((device = (mouse_device *)fDevices->ItemAt(i)) != NULL) {
if (!strcmp(device->driver_path, path)) {
fDevices->RemoveItem(device);
delete device;
return B_OK;
}
}
}
return B_ENTRY_NOT_FOUND;
} }
int32 int32
MouseInputDevice::DeviceWatcher(void *arg) MouseInputDevice::DeviceWatcher(void *arg)
{ {
MouseInputDevice *dev = (MouseInputDevice *)arg; mouse_device *dev = (mouse_device *)arg;
mouse_movement movements; mouse_movement movements;
BMessage *message; BMessage *message;
char log[128]; char log[128];
while (!sQuit) { while (dev->active) {
if (ioctl(dev->fFd, kGetMouseMovements, &movements) < B_OK) if (ioctl(dev->driver_fd, kGetMouseMovements, &movements) < B_OK)
continue; continue;
uint32 buttons = dev->fButtons ^ movements.buttons; uint32 buttons = dev->buttons_state ^ movements.buttons;
snprintf(log, 128, "buttons: %ld, x: %ld, y: %ld, clicks:%ld\n", snprintf(log, 128, "%s: buttons: 0x%lx, x: %ld, y: %ld, clicks:%ld\n",
movements.buttons, movements.xdelta, movements.ydelta, dev->device_ref.name, movements.buttons, movements.xdelta,
movements.click_count); movements.ydelta, movements.click_count);
LOG(log); LOG(log);
// TODO: B_MOUSE_DOWN and B_MOUSE_UP messages don't seem // TODO: B_MOUSE_DOWN and B_MOUSE_UP messages don't seem
// to reach the application, // to be generated correctly.
// for some reason. Check if they reach the input server.
if (buttons != 0) { if (buttons != 0) {
message = new BMessage; message = new BMessage;
if ((buttons & movements.buttons) > 0) { if ((buttons & movements.buttons) > 0) {
@@ -260,10 +410,9 @@ MouseInputDevice::DeviceWatcher(void *arg)
message->AddInt32("x", movements.xdelta); message->AddInt32("x", movements.xdelta);
message->AddInt32("y", movements.ydelta); message->AddInt32("y", movements.ydelta);
dev->EnqueueMessage(message); dev->buttons_state = movements.buttons;
dev->fButtons = movements.buttons;
sSingletonMouseDevice->EnqueueMessage(message);
} }
if (movements.xdelta != 0 || movements.ydelta != 0) { if (movements.xdelta != 0 || movements.ydelta != 0) {
@@ -274,7 +423,7 @@ MouseInputDevice::DeviceWatcher(void *arg)
message->AddInt32("x", movements.xdelta); message->AddInt32("x", movements.xdelta);
message->AddInt32("y", movements.ydelta); message->AddInt32("y", movements.ydelta);
dev->EnqueueMessage(message); sSingletonMouseDevice->EnqueueMessage(message);
} }
} }
} }
@@ -283,3 +432,74 @@ MouseInputDevice::DeviceWatcher(void *arg)
} }
// mouse_device
mouse_device::mouse_device(const char *path)
{
driver_fd = -1;
device_watcher = -1;
buttons_state = 0;
active = false;
strcpy(driver_path, path);
device_ref.name = get_short_name(path);
device_ref.type = B_POINTING_DEVICE;
device_ref.cookie = this;
// TODO: Add a function which checks if the object
// has initialized correctly. Specifically, this is one
// of the operations which could fail
driver_fd = open(driver_path, O_RDWR);
};
mouse_device::~mouse_device()
{
free(device_ref.name);
if (driver_fd >= 0)
close(driver_fd);
}
// static functions
// On exit, "list" will contain a string for every file in
// the tree, starting from the given "directory". Note that
// the file names will also contain the name of the parent folder.
static void
scan_recursively(const char *directory, BList *list)
{
// TODO: See if it's simpler to use POSIX functions
BEntry entry;
BDirectory dir(directory);
char buf[B_OS_NAME_LENGTH];
while (dir.GetNextEntry(&entry) == B_OK) {
entry.GetName(buf);
BPath child(&dir, buf);
if (entry.IsDirectory())
scan_recursively(child.Path(), list);
else {
BPath parent(directory);
BString deviceName = parent.Leaf();
deviceName << "/" << buf;
list->AddItem(strdup(deviceName.String()));
}
}
}
static char *
get_short_name(const char *longName)
{
BString string(longName);
BString name;
int32 slash = string.FindLast("/");
int32 previousSlash = string.FindLast("/", slash) + 1;
string.CopyInto(name, slash, string.Length() - slash);
int32 deviceIndex = atoi(name.String()) + 1;
string.CopyInto(name, previousSlash, slash - previousSlash);
name << " mouse " << deviceIndex;
return strdup(name.String());
}
@@ -31,7 +31,6 @@
#include <InterfaceDefs.h> #include <InterfaceDefs.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
// TODO : these structs are needed in : input_server, mouse driver, mouse addon, mouse prefs // TODO : these structs are needed in : input_server, mouse driver, mouse addon, mouse prefs
// => factorisation has to be done in a global header, possibly private // => factorisation has to be done in a global header, possibly private
@@ -49,6 +48,8 @@ typedef struct {
bigtime_t click_speed; bigtime_t click_speed;
} mouse_settings; } mouse_settings;
class BList;
class MouseInputDevice : public BInputServerDevice { class MouseInputDevice : public BInputServerDevice {
public: public:
MouseInputDevice(); MouseInputDevice();
@@ -63,19 +64,16 @@ public:
uint32 command, BMessage *message); uint32 command, BMessage *message);
private: private:
status_t HandleMonitor(BMessage *message); status_t HandleMonitor(BMessage *message);
status_t InitFromSettings(uint32 opcode = 0); status_t InitFromSettings(void *cookie, uint32 opcode = 0);
int32 fButtons; status_t AddDevice(const char *path);
status_t RemoveDevice(const char *path);
thread_id fThread;
int fFd;
mouse_settings fSettings;
static bool sQuit;
static FILE *sLogFile;
static int32 DeviceWatcher(void *arg); static int32 DeviceWatcher(void *arg);
BList *fDevices;
static FILE *sLogFile;
}; };
extern "C" BInputServerDevice *instantiate_input_device(); extern "C" BInputServerDevice *instantiate_input_device();