From c2fbfb71f0f3834b2d53b1a98c75ea6672cb36d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Duval?= Date: Tue, 5 Oct 2004 15:59:40 +0000 Subject: [PATCH] clean up, now avoid locking when shutdowning git-svn-id: file:///srv/svn/repos/haiku/trunk/current@9208 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../devices/keyboard/KeyboardInputDevice.cpp | 16 +- .../devices/keyboard/KeyboardInputDevice.h | 5 +- .../devices/mouse/MouseInputDevice.cpp | 311 +++++++----------- .../devices/mouse/MouseInputDevice.h | 12 +- 4 files changed, 142 insertions(+), 202 deletions(-) diff --git a/src/add-ons/input_server/devices/keyboard/KeyboardInputDevice.cpp b/src/add-ons/input_server/devices/keyboard/KeyboardInputDevice.cpp index 058bf38e24..13042b5ff3 100644 --- a/src/add-ons/input_server/devices/keyboard/KeyboardInputDevice.cpp +++ b/src/add-ons/input_server/devices/keyboard/KeyboardInputDevice.cpp @@ -38,6 +38,8 @@ inline void LOG(const char *fmt, ...) { char buf[1024]; va_list ap; va_start(ap, fmt); vsprintf(buf, fmt, ap); va_end(ap); \ fputs(buf, KeyboardInputDevice::sLogFile); fflush(KeyboardInputDevice::sLogFile); } #define LOG_ERR(text...) LOG(text) + +FILE *KeyboardInputDevice::sLogFile = NULL; #else #define LOG(text...) #define LOG_ERR(text...) fprintf(stderr, text) @@ -354,8 +356,6 @@ const uint32 at_keycode_map[] = { }; -FILE *KeyboardInputDevice::sLogFile = NULL; - extern "C" BInputServerDevice * instantiate_input_device() @@ -441,7 +441,12 @@ KeyboardInputDevice::Start(const char *name, void *cookie) { CALLED(); keyboard_device *device = (keyboard_device *)cookie; + + if ((device->fd = open(device->path, O_RDWR)) < B_OK) + return B_ERROR; + InitFromSettings(device); + char threadName[B_OS_NAME_LENGTH]; snprintf(threadName, B_OS_NAME_LENGTH, "%s watcher", name); @@ -462,9 +467,13 @@ KeyboardInputDevice::Stop(const char *name, void *cookie) keyboard_device *device = (keyboard_device *)cookie; LOG("Stop(%s)\n", name); + + close(device->fd); device->active = false; if (device->device_watcher >= 0) { + suspend_thread(device->device_watcher); + resume_thread(device->device_watcher); status_t dummy; wait_for_thread(device->device_watcher, &dummy); } @@ -545,6 +554,7 @@ KeyboardInputDevice::AddDevice(const char *path) delete device; return B_ERROR; } + close(device->fd); device->device_watcher = -1; device->active = false; @@ -563,8 +573,6 @@ KeyboardInputDevice::AddDevice(const char *path) fDevices.AddItem(device); - InitFromSettings(device); - return RegisterDevices(devices); } diff --git a/src/add-ons/input_server/devices/keyboard/KeyboardInputDevice.h b/src/add-ons/input_server/devices/keyboard/KeyboardInputDevice.h index 8af01f4bcf..ee10594e0e 100644 --- a/src/add-ons/input_server/devices/keyboard/KeyboardInputDevice.h +++ b/src/add-ons/input_server/devices/keyboard/KeyboardInputDevice.h @@ -60,8 +60,9 @@ public: virtual status_t Control(const char *name, void *cookie, uint32 command, BMessage *message); - - static FILE *sLogFile; +#ifdef DEBUG + static FILE *sLogFile; +#endif private: status_t HandleMonitor(BMessage *message); status_t InitFromSettings(void *cookie, uint32 opcode = 0); diff --git a/src/add-ons/input_server/devices/mouse/MouseInputDevice.cpp b/src/add-ons/input_server/devices/mouse/MouseInputDevice.cpp index cfa38f178c..d578cb055c 100644 --- a/src/add-ons/input_server/devices/mouse/MouseInputDevice.cpp +++ b/src/add-ons/input_server/devices/mouse/MouseInputDevice.cpp @@ -34,35 +34,27 @@ #include #include +#include #include #include -#include #include #include #include #if DEBUG - #define LOG(text) fputs(text, sLogFile); fflush(sLogFile) + inline void LOG(const char *fmt, ...) { char buf[1024]; va_list ap; va_start(ap, fmt); vsprintf(buf, fmt, ap); va_end(ap); \ + fputs(buf, MouseInputDevice::sLogFile); fflush(MouseInputDevice::sLogFile); } + #define LOG_ERR(text...) LOG(text) +FILE *MouseInputDevice::sLogFile = NULL; #else - #define LOG(text) + #define LOG(text...) + #define LOG_ERR(text...) fprintf(stderr, text) #endif -#include - -FILE *MouseInputDevice::sLogFile = NULL; +#define CALLED() LOG("%s\n", __PRETTY_FUNCTION__) static MouseInputDevice *sSingletonMouseDevice = NULL; -// 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 BeOS mouse driver. -const static uint32 kGetMouseMovements = 10099; -const static uint32 kGetMouseAccel = 10101; -const static uint32 kSetMouseAccel = 10102; -const static uint32 kSetMouseType = 10104; -const static uint32 kSetMouseMap = 10106; -const static uint32 kSetClickSpeed = 10108; - const static uint32 kMouseThreadPriority = B_FIRST_REAL_TIME_PRIORITY + 4; const static char *kMouseDevicesDirectory = "/dev/input/mouse"; @@ -74,17 +66,15 @@ struct mouse_device { ~mouse_device(); input_device_ref device_ref; - char driver_path[B_PATH_NAME_LENGTH]; - int driver_fd; + char path[B_PATH_NAME_LENGTH]; + int fd; thread_id device_watcher; - 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); @@ -97,16 +87,14 @@ instantiate_input_device() MouseInputDevice::MouseInputDevice() - : fDevices(NULL) - { ASSERT(sSingletonMouseDevice == NULL); sSingletonMouseDevice = this; #if DEBUG - if (sLogFile == NULL) - sLogFile = fopen("/var/log/mouse_device_log.log", "a"); + sLogFile = fopen("/var/log/mouse_device_log.log", "a"); #endif + CALLED(); StartMonitoringDevice(kMouseDevicesDirectoryUSB); } @@ -114,12 +102,11 @@ MouseInputDevice::MouseInputDevice() MouseInputDevice::~MouseInputDevice() { + CALLED(); StopMonitoringDevice(kMouseDevicesDirectoryUSB); - for (int32 i = 0; i < fDevices->CountItems(); i++) - delete (mouse_device *)fDevices->ItemAt(i); - - delete fDevices; + for (int32 i = 0; i < fDevices.CountItems(); i++) + delete (mouse_device *)fDevices.ItemAt(i); #if DEBUG fclose(sLogFile); @@ -135,33 +122,33 @@ MouseInputDevice::InitFromSettings(void *cookie, uint32 opcode) // retrieve current values if (get_mouse_map(&device->settings.map) != B_OK) - fprintf(stderr, "error when get_mouse_map\n"); + LOG_ERR("error when get_mouse_map\n"); else - ioctl(device->driver_fd, kSetMouseMap, &device->settings.map); + ioctl(device->fd, MS_SET_MAP, &device->settings.map); if (get_click_speed(&device->settings.click_speed) != B_OK) - fprintf(stderr, "error when get_click_speed\n"); + LOG_ERR("error when get_click_speed\n"); else - ioctl(device->driver_fd, kSetClickSpeed, &device->settings.click_speed); + ioctl(device->fd, MS_SET_CLICKSPEED, &device->settings.click_speed); if (get_mouse_speed(&device->settings.accel.speed) != B_OK) - fprintf(stderr, "error when get_mouse_speed\n"); + LOG_ERR("error when get_mouse_speed\n"); else { if (get_mouse_acceleration(&device->settings.accel.accel_factor) != B_OK) - fprintf(stderr, "error when get_mouse_acceleration\n"); + LOG_ERR("error when get_mouse_acceleration\n"); else { mouse_accel accel; - ioctl(device->driver_fd, kGetMouseAccel, &accel); + ioctl(device->fd, MS_GET_ACCEL, &accel); accel.speed = device->settings.accel.speed; accel.accel_factor = device->settings.accel.accel_factor; - ioctl(device->driver_fd, kSetMouseAccel, &device->settings.accel); + ioctl(device->fd, MS_SET_ACCEL, &device->settings.accel); } } if (get_mouse_type(&device->settings.type) != B_OK) - fprintf(stderr, "error when get_mouse_type\n"); + LOG_ERR("error when get_mouse_type\n"); else - ioctl(device->driver_fd, kSetMouseType, &device->settings.type); + ioctl(device->fd, MS_SET_TYPE, &device->settings.type); return B_OK; @@ -171,25 +158,10 @@ MouseInputDevice::InitFromSettings(void *cookie, uint32 opcode) status_t MouseInputDevice::InitCheck() { - BList list; - scan_recursively(kMouseDevicesDirectory, &list); + CALLED(); + RecursiveScan(kMouseDevicesDirectory); - char path[B_PATH_NAME_LENGTH]; - if (list.CountItems() > 0) { - fDevices = new BList; - for (int32 i = 0; i < list.CountItems(); i++) { - strcpy(path, kMouseDevicesDirectory); - strcat(path, "/"); - strcat(path, (char *)list.ItemAt(i)); - AddDevice(path); - free(list.ItemAt(i)); - } - } - - if (fDevices && fDevices->CountItems() > 0) - return BInputServerDevice::InitCheck(); - - return B_ERROR; + return B_OK; } @@ -198,21 +170,23 @@ MouseInputDevice::Start(const char *name, void *cookie) { mouse_device *device = (mouse_device *)cookie; - char log[128]; - snprintf(log, 128, "Start(%s)\n", name); - - LOG(log); + LOG("%s(%s)\n", __PRETTY_FUNCTION__, name); + + device->fd = open(device->path, O_RDWR); + if (device->fd<0) + return B_ERROR; + + InitFromSettings(device); 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); + kMouseThreadPriority, device); resume_thread(device->device_watcher); - return B_OK; } @@ -222,13 +196,14 @@ MouseInputDevice::Stop(const char *name, void *cookie) { mouse_device *device = (mouse_device *)cookie; - char log[128]; - snprintf(log, 128, "Stop(%s)\n", name); + LOG("%s(%s)\n", __PRETTY_FUNCTION__, name); - LOG(log); + close(device->fd); device->active = false; if (device->device_watcher >= 0) { + suspend_thread(device->device_watcher); + resume_thread(device->device_watcher); status_t dummy; wait_for_thread(device->device_watcher, &dummy); } @@ -241,10 +216,7 @@ status_t MouseInputDevice::Control(const char *name, void *cookie, uint32 command, BMessage *message) { - char log[128]; - snprintf(log, 128, "Control(%s, code: %lu)\n", name, command); - - LOG(log); + LOG("%s(%s, code: %lu)\n", __PRETTY_FUNCTION__, name, command); if (command == B_NODE_MONITOR) HandleMonitor(message); @@ -260,83 +232,61 @@ MouseInputDevice::Control(const char *name, void *cookie, status_t MouseInputDevice::HandleMonitor(BMessage *message) { - 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; + CALLED(); + int32 opcode = 0; + status_t status; + if ((status = message->FindInt32("opcode", &opcode)) < B_OK) + return status; + + if ((opcode != B_ENTRY_CREATED) + && (opcode != B_ENTRY_REMOVED)) + return B_OK; + + + BEntry entry; + BPath path; + dev_t device; + ino_t directory; + const char *name = NULL; + + message->FindInt32("device", &device); + message->FindInt64("directory", &directory); + message->FindString("name", &name); + + entry_ref ref(device, directory, name); + + if ((status = entry.SetTo(&ref)) != B_OK) + return status; + if ((status = entry.GetPath(&path)) != B_OK) + return status; + if ((status = path.InitCheck()) != B_OK) + return status; + + if (opcode == B_ENTRY_CREATED) + AddDevice(path.Path()); + else + RemoveDevice(path.Path()); + + return status; } status_t MouseInputDevice::AddDevice(const char *path) { + CALLED(); + 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); + fDevices.AddItem(device); return RegisterDevices(devices); } @@ -345,16 +295,15 @@ MouseInputDevice::AddDevice(const char *path) 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; - } - } + CALLED(); + int32 i = 0; + mouse_device *device = NULL; + while ((device = (mouse_device *)fDevices.ItemAt(i)) != NULL) { + if (!strcmp(device->path, path)) { + fDevices.RemoveItem(device); + delete device; + return B_OK; + } } return B_ENTRY_NOT_FOUND; @@ -367,33 +316,23 @@ MouseInputDevice::DeviceWatcher(void *arg) mouse_device *dev = (mouse_device *)arg; mouse_movement movements; + uint32 buttons_state; BMessage *message; - char log[128]; while (dev->active) { memset(&movements, 0, sizeof(movements)); - if (ioctl(dev->driver_fd, kGetMouseMovements, &movements) < B_OK) + if (ioctl(dev->fd, MS_READ, &movements) < B_OK) continue; - uint32 buttons = dev->buttons_state ^ movements.buttons; + uint32 buttons = buttons_state ^ movements.buttons; - snprintf(log, 128, "%s: buttons: 0x%lx, x: %ld, y: %ld, clicks:%ld\n", - dev->device_ref.name, movements.buttons, movements.xdelta, - movements.ydelta, movements.clicks); - - LOG(log); + LOG("%s: buttons: 0x%lx, x: %ld, y: %ld, clicks:%ld\n", dev->device_ref.name, movements.buttons, movements.xdelta, movements.ydelta, movements.clicks); // TODO: add acceleration computing int32 xdelta = movements.xdelta * dev->settings.accel.speed >> 15; int32 ydelta = movements.ydelta * dev->settings.accel.speed >> 15; - snprintf(log, 128, "%s: x: %ld, y: %ld, \n", - dev->device_ref.name, xdelta, ydelta); + LOG("%s: x: %ld, y: %ld, \n", dev->device_ref.name, xdelta, ydelta); - LOG(log); - - - // TODO: B_MOUSE_DOWN and B_MOUSE_UP messages don't seem - // to be generated correctly. if (buttons != 0) { message = new BMessage(B_MOUSE_UP); if ((buttons & movements.buttons) > 0) { @@ -409,7 +348,7 @@ MouseInputDevice::DeviceWatcher(void *arg) message->AddInt32("x", xdelta); message->AddInt32("y", ydelta); sSingletonMouseDevice->EnqueueMessage(message); - dev->buttons_state = movements.buttons; + buttons_state = movements.buttons; } if (movements.xdelta != 0 || movements.ydelta != 0) { @@ -442,57 +381,47 @@ MouseInputDevice::DeviceWatcher(void *arg) // mouse_device -mouse_device::mouse_device(const char *path) +mouse_device::mouse_device(const char *driver_path) { - driver_fd = -1; + fd = -1; device_watcher = -1; - buttons_state = 0; active = false; - strcpy(driver_path, path); + strcpy(path, driver_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) +void +MouseInputDevice::RecursiveScan(const char *directory) { - // 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); - + CALLED(); + bool found_ps2 = false; + BEntry entry; + BDirectory dir(directory); + while (dir.GetNextEntry(&entry) == B_OK) { + BPath path; + entry.GetPath(&path); + + char name[B_FILE_NAME_LENGTH]; + entry.GetName(name); + if (strcmp(name, "ps2")==0) + found_ps2 = true; + if (strcmp(name,"serial")==0 && found_ps2) + continue; + if (entry.IsDirectory()) - scan_recursively(child.Path(), list); - else { - BPath parent(directory); - BString deviceName = parent.Leaf(); - deviceName << "/" << buf; - list->AddItem(strdup(deviceName.String())); - } - } + RecursiveScan(path.Path()); + else + AddDevice(path.Path()); + } } diff --git a/src/add-ons/input_server/devices/mouse/MouseInputDevice.h b/src/add-ons/input_server/devices/mouse/MouseInputDevice.h index f6029f0615..aded6a2aac 100644 --- a/src/add-ons/input_server/devices/mouse/MouseInputDevice.h +++ b/src/add-ons/input_server/devices/mouse/MouseInputDevice.h @@ -29,10 +29,9 @@ #include #include - +#include #include -class BList; class MouseInputDevice : public BInputServerDevice { public: MouseInputDevice(); @@ -48,15 +47,18 @@ public: private: status_t HandleMonitor(BMessage *message); status_t InitFromSettings(void *cookie, uint32 opcode = 0); + void RecursiveScan(const char *directory); status_t AddDevice(const char *path); status_t RemoveDevice(const char *path); static int32 DeviceWatcher(void *arg); - BList *fDevices; - - static FILE *sLogFile; + BList fDevices; +#ifdef DEBUG +public: + static FILE *sLogFile; +#endif }; extern "C" BInputServerDevice *instantiate_input_device();