From 1c1f322ec155ccec5aca2bda489e79d2f520095d Mon Sep 17 00:00:00 2001 From: Michael Lotz Date: Sun, 5 Jun 2011 19:15:43 +0000 Subject: [PATCH] Replace the TabletInputDevice by a MouseInputDevice based reimplementation. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@41946 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../devices/tablet/TabletInputDevice.cpp | 851 +++++++++++------- .../devices/tablet/TabletInputDevice.h | 101 +-- 2 files changed, 552 insertions(+), 400 deletions(-) diff --git a/src/add-ons/input_server/devices/tablet/TabletInputDevice.cpp b/src/add-ons/input_server/devices/tablet/TabletInputDevice.cpp index 7ce8e0645c..283231abb9 100644 --- a/src/add-ons/input_server/devices/tablet/TabletInputDevice.cpp +++ b/src/add-ons/input_server/devices/tablet/TabletInputDevice.cpp @@ -1,39 +1,26 @@ -/*****************************************************************************/ -// Tablet input server device addon -// Adapted by Jerome Duval and written by Stefano Ceccherini -// -// TabletInputDevice.cpp -// -// Copyright (c) 2005 Haiku Project -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. -/*****************************************************************************/ +/* + * Copyright 2004-2011, Haiku. + * Distributed under the terms of the MIT License. + * + * Authors: + * Stefano Ceccherini (stefano.ceccherini@gmail.com) + * Jérôme Duval + * Axel Dörfler, axeld@pinc-software.de + * Clemens Zeidler, haiku@clemens-zeidler.de + * Stephan Aßmus, superstippi@gmx.de + * Michael Lotz, mmlr@mlotz.ch + */ -// TODO: Use strlcpy instead of strcpy #include "TabletInputDevice.h" -#include "kb_mouse_settings.h" -#include "kb_mouse_driver.h" +#include +#include +#include #include #include +#include #include #include #include @@ -41,330 +28,327 @@ #include #include -#if DEBUG - 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, TabletInputDevice::sLogFile); fflush(TabletInputDevice::sLogFile); } - #define LOG_ERR(text...) LOG(text) -FILE *TabletInputDevice::sLogFile = NULL; +#include +#include + + +#undef TRACE +//#define TRACE_TABLET_DEVICE +#ifdef TRACE_TABLET_DEVICE + + class FunctionTracer { + public: + FunctionTracer(const void* pointer, const char* className, + const char* functionName, + int32& depth) + : fFunctionName(), + fPrepend(), + fFunctionDepth(depth), + fPointer(pointer) + { + fFunctionDepth++; + fPrepend.Append(' ', fFunctionDepth * 2); + fFunctionName << className << "::" << functionName << "()"; + + debug_printf("%p -> %s%s {\n", fPointer, fPrepend.String(), + fFunctionName.String()); + } + + ~FunctionTracer() + { + debug_printf("%p -> %s}\n", fPointer, fPrepend.String()); + fFunctionDepth--; + } + + private: + BString fFunctionName; + BString fPrepend; + int32& fFunctionDepth; + const void* fPointer; + }; + + + static int32 sFunctionDepth = -1; +# define TD_CALLED(x...) FunctionTracer _ft(this, "TabletDevice", \ + __FUNCTION__, sFunctionDepth) +# define TID_CALLED(x...) FunctionTracer _ft(this, "TabletInputDevice", \ + __FUNCTION__, sFunctionDepth) +# define TRACE(x...) do { BString _to; \ + _to.Append(' ', (sFunctionDepth + 1) * 2); \ + debug_printf("%p -> %s", this, _to.String()); \ + debug_printf(x); } while (0) +# define LOG_EVENT(text...) do {} while (0) +# define LOG_ERR(text...) TRACE(text) #else - #define LOG(text...) - #define LOG_ERR(text...) fprintf(stderr, text) +# define TRACE(x...) do {} while (0) +# define TD_CALLED(x...) TRACE(x) +# define TID_CALLED(x...) TRACE(x) +# define LOG_ERR(x...) debug_printf(x) +# define LOG_EVENT(x...) TRACE(x) #endif -#define CALLED() LOG("%s\n", __PRETTY_FUNCTION__) - -static TabletInputDevice *sSingletonTabletDevice = NULL; const static uint32 kTabletThreadPriority = B_FIRST_REAL_TIME_PRIORITY + 4; -const static char *kTabletDevicesDirectory = "/dev/input/tablet"; +const static char* kTabletDevicesDirectory = "/dev/input/tablet"; -// "/dev/" is automatically prepended by StartMonitoringDevice() -const static char *kTabletDevicesDirectoryUSB = "input/tablet/usb"; -struct tablet_device { - tablet_device(const char *path); - ~tablet_device(); - - input_device_ref device_ref; - char path[B_PATH_NAME_LENGTH]; - int fd; - thread_id device_watcher; - mouse_settings settings; - bool active; +class TabletDevice { +public: + TabletDevice(TabletInputDevice& target, + const char* path); + ~TabletDevice(); + + status_t Start(); + void Stop(); + + status_t UpdateSettings(); + + const char* Path() const { return fPath.String(); } + input_device_ref* DeviceRef() { return &fDeviceRef; } + +private: + char* _BuildShortName() const; + + static status_t _ControlThreadEntry(void* arg); + void _ControlThread(); + void _ControlThreadCleanup(); + void _UpdateSettings(); + + BMessage* _BuildMouseMessage(uint32 what, + uint64 when, uint32 buttons, + float xPosition, float yPosition) const; + +private: + TabletInputDevice& fTarget; + BString fPath; + int fDevice; + + input_device_ref fDeviceRef; + mouse_settings fSettings; + + thread_id fThread; + volatile bool fActive; + volatile bool fUpdateSettings; }; -// forward declarations -static char *get_short_name(const char *longName); - - -extern "C" -BInputServerDevice * +extern "C" BInputServerDevice* instantiate_input_device() { - return new TabletInputDevice(); + return new(std::nothrow) TabletInputDevice(); } -TabletInputDevice::TabletInputDevice() +// #pragma mark - + + +TabletDevice::TabletDevice(TabletInputDevice& target, const char* driverPath) + : + fTarget(target), + fPath(driverPath), + fDevice(-1), + fThread(-1), + fActive(false), + fUpdateSettings(false) { - ASSERT(sSingletonTabletDevice == NULL); - sSingletonTabletDevice = this; - -#if DEBUG - sLogFile = fopen("/var/log/tablet_device_log.log", "a"); -#endif - CALLED(); - - StartMonitoringDevice(kTabletDevicesDirectoryUSB); -} + TD_CALLED(); + + fDeviceRef.name = _BuildShortName(); + fDeviceRef.type = B_POINTING_DEVICE; + fDeviceRef.cookie = this; +}; -TabletInputDevice::~TabletInputDevice() +TabletDevice::~TabletDevice() { - CALLED(); - StopMonitoringDevice(kTabletDevicesDirectoryUSB); + TD_CALLED(); + TRACE("delete\n"); - for (int32 i = 0; i < fDevices.CountItems(); i++) - delete (tablet_device *)fDevices.ItemAt(i); - -#if DEBUG - fclose(sLogFile); -#endif + if (fActive) + Stop(); + + free(fDeviceRef.name); } status_t -TabletInputDevice::InitFromSettings(void *cookie, uint32 opcode) +TabletDevice::Start() { - CALLED(); - tablet_device *device = (tablet_device *)cookie; - - // retrieve current values + TD_CALLED(); - if (get_mouse_map(&device->settings.map) != B_OK) - LOG_ERR("error when get_mouse_map\n"); - else - ioctl(device->fd, MS_SET_MAP, &device->settings.map); - - if (get_click_speed(&device->settings.click_speed) != B_OK) - LOG_ERR("error when get_click_speed\n"); - else - ioctl(device->fd, MS_SET_CLICKSPEED, &device->settings.click_speed); - - if (get_mouse_speed(&device->settings.accel.speed) != B_OK) - LOG_ERR("error when get_mouse_speed\n"); + fDevice = open(fPath.String(), O_RDWR); + // let the control thread handle any error on opening the device + + char threadName[B_OS_NAME_LENGTH]; + snprintf(threadName, B_OS_NAME_LENGTH, "%s watcher", fDeviceRef.name); + + fThread = spawn_thread(_ControlThreadEntry, threadName, + kTabletThreadPriority, (void*)this); + + status_t status; + if (fThread < 0) + status = fThread; else { - if (get_mouse_acceleration(&device->settings.accel.accel_factor) != B_OK) - LOG_ERR("error when get_mouse_acceleration\n"); - else { - mouse_accel accel; - ioctl(device->fd, MS_GET_ACCEL, &accel); - accel.speed = device->settings.accel.speed; - accel.accel_factor = device->settings.accel.accel_factor; - ioctl(device->fd, MS_SET_ACCEL, &device->settings.accel); - } + fActive = true; + status = resume_thread(fThread); } - - if (get_mouse_type(&device->settings.type) != B_OK) - LOG_ERR("error when get_mouse_type\n"); - else - ioctl(device->fd, MS_SET_TYPE, &device->settings.type); - return B_OK; + if (status < B_OK) { + LOG_ERR("%s: can't spawn/resume watching thread: %s\n", + fDeviceRef.name, strerror(status)); + if (fDevice >= 0) + close(fDevice); + return status; + } + + return fDevice >= 0 ? B_OK : B_ERROR; +} + + +void +TabletDevice::Stop() +{ + TD_CALLED(); + + fActive = false; + // this will stop the thread as soon as it reads the next packet + + close(fDevice); + fDevice = -1; + + if (fThread >= 0) { + // unblock the thread, which might wait on a semaphore. + suspend_thread(fThread); + resume_thread(fThread); + + status_t dummy; + wait_for_thread(fThread, &dummy); + } } status_t -TabletInputDevice::InitCheck() +TabletDevice::UpdateSettings() { - CALLED(); - RecursiveScan(kTabletDevicesDirectory); - - return B_OK; -} + TD_CALLED(); - -status_t -TabletInputDevice::Start(const char *name, void *cookie) -{ - tablet_device *device = (tablet_device *)cookie; - - LOG("%s(%s)\n", __PRETTY_FUNCTION__, name); - - device->fd = open(device->path, O_RDWR); - if (device->fd<0) + if (fThread < 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, - kTabletThreadPriority, device); - - resume_thread(device->device_watcher); - + // trigger updating the settings in the control thread + fUpdateSettings = true; + return B_OK; } -status_t -TabletInputDevice::Stop(const char *name, void *cookie) +char* +TabletDevice::_BuildShortName() const { - tablet_device *device = (tablet_device *)cookie; - - LOG("%s(%s)\n", __PRETTY_FUNCTION__, name); + BString string(fPath); + BString 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); - } - - return B_OK; -} + int32 slash = string.FindLast("/"); + string.CopyInto(name, slash + 1, string.Length() - slash); + int32 index = atoi(name.String()) + 1; + int32 previousSlash = string.FindLast("/", slash); + string.CopyInto(name, previousSlash + 1, slash - previousSlash - 1); -status_t -TabletInputDevice::Control(const char *name, void *cookie, - uint32 command, BMessage *message) -{ - LOG("%s(%s, code: %lu)\n", __PRETTY_FUNCTION__, name, command); - - if (command == B_NODE_MONITOR) - HandleMonitor(message); - else if (command >= B_MOUSE_TYPE_CHANGED - && command <= B_MOUSE_ACCELERATION_CHANGED) { - InitFromSettings(cookie, command); - } - return B_OK; -} - - -// TODO: Test this. USB doesn't work on my machine -status_t -TabletInputDevice::HandleMonitor(BMessage *message) -{ - 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()); + if (name.Length() < 4) + name.ToUpper(); else - RemoveDevice(path.Path()); - - return status; + name.Capitalize(); + + name << " Tablet " << index; + return strdup(name.String()); } +// #pragma mark - control thread + + status_t -TabletInputDevice::AddDevice(const char *path) +TabletDevice::_ControlThreadEntry(void* arg) { - CALLED(); - - tablet_device *device = new tablet_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); - - return RegisterDevices(devices); + TabletDevice* device = (TabletDevice*)arg; + device->_ControlThread(); + return B_OK; } -status_t -TabletInputDevice::RemoveDevice(const char *path) +void +TabletDevice::_ControlThread() { - CALLED(); - int32 i = 0; - tablet_device *device = NULL; - while ((device = (tablet_device *)fDevices.ItemAt(i)) != NULL) { - if (!strcmp(device->path, path)) { - fDevices.RemoveItem(device); - delete device; - return B_OK; - } + TD_CALLED(); + + if (fDevice < 0) { + _ControlThreadCleanup(); + return; } - - return B_ENTRY_NOT_FOUND; -} + _UpdateSettings(); -int32 -TabletInputDevice::DeviceWatcher(void *arg) -{ - tablet_device *dev = (tablet_device *)arg; - - tablet_movement movements; - tablet_movement old_movements; - uint32 buttons_state = 0; - BMessage *message; - while (dev->active) { - memset(&movements, 0, sizeof(movements)); - if (ioctl(dev->fd, MS_READ, &movements, - sizeof(movements)) != B_OK) { - snooze(10000); // this is a realtime thread, and something is wrong... - continue; + static const bigtime_t kTransferDelay = 1000000 / 125; + // 125 transfers per second should be more than enough + bigtime_t nextTransferTime = system_time() + kTransferDelay; + uint32 lastButtons = 0; + float lastXPosition = 0; + float lastYPosition = 0; + + while (fActive) { + tablet_movement movements; + + snooze_until(nextTransferTime, B_SYSTEM_TIMEBASE); + nextTransferTime += kTransferDelay; + + if (ioctl(fDevice, MS_READ, &movements, sizeof(movements)) != B_OK) { + LOG_ERR("Tablet device exiting, %s\n", strerror(errno)); + _ControlThreadCleanup(); + return; } - - uint32 buttons = buttons_state ^ movements.buttons; - - LOG("%s: buttons: 0x%lx, x: %f, y: %f, clicks:%ld, wheel_x:%ld, wheel_y:%ld\n", dev->device_ref.name, movements.buttons, - movements.xpos, movements.ypos, movements.clicks, movements.wheel_xdelta, movements.wheel_ydelta); - movements.xpos /= 10206.0; - movements.ypos /= 7422.0; - float x = movements.xpos; - float y = movements.ypos; + // take care of updating the settings first, if necessary + if (fUpdateSettings) { + fUpdateSettings = false; + _UpdateSettings(); + } - LOG("%s: x: %f, y: %f, \n", dev->device_ref.name, x, y); - + LOG_EVENT("%s: buttons: 0x%lx, x: %f, y: %f, clicks: %ld, contact: %c, " + "pressure: %f, wheel_x: %ld, wheel_y: %ld, eraser: %c, " + "tilt: %f/%f\n", fDeviceRef.name, movements.buttons, movements.xpos, + movements.ypos, movements.clicks, movements.has_contact ? 'y' : 'n', + movements.pressure, movements.wheel_xdelta, movements.wheel_ydelta, + movements.eraser ? 'y' : 'n', movements.tilt_x, movements.tilt_y); + + // Send single messages for each event + + uint32 buttons = lastButtons ^ movements.buttons; if (buttons != 0) { - message = new BMessage(B_MOUSE_UP); - if ((buttons & movements.buttons) > 0) { - message->what = B_MOUSE_DOWN; - message->AddInt32("clicks", movements.clicks); - LOG("B_MOUSE_DOWN\n"); - } else { - LOG("B_MOUSE_UP\n"); + bool pressedButton = (buttons & movements.buttons) > 0; + BMessage* message = _BuildMouseMessage( + pressedButton ? B_MOUSE_DOWN : B_MOUSE_UP, + movements.timestamp, movements.buttons, movements.xpos, + movements.ypos); + if (message != NULL) { + if (pressedButton) { + message->AddInt32("clicks", movements.clicks); + LOG_EVENT("B_MOUSE_DOWN\n"); + } else + LOG_EVENT("B_MOUSE_UP\n"); + + fTarget.EnqueueMessage(message); + lastButtons = movements.buttons; } - - message->AddInt64("when", movements.timestamp); - message->AddInt32("buttons", movements.buttons); - message->AddFloat("x", movements.xpos); - message->AddFloat("y", movements.ypos); - sSingletonTabletDevice->EnqueueMessage(message); - buttons_state = movements.buttons; } - - if (movements.xpos != 0.0 || movements.ypos != 0.0) { - message = new BMessage(B_MOUSE_MOVED); - if (message) { - message->AddInt64("when", movements.timestamp); - message->AddInt32("buttons", movements.buttons); - message->AddFloat("x", x); - message->AddFloat("y", y); + + if (movements.xpos != lastXPosition + || movements.ypos != lastYPosition) { + BMessage* message = _BuildMouseMessage(B_MOUSE_MOVED, + movements.timestamp, movements.buttons, movements.xpos, + movements.ypos); + if (message != NULL) { message->AddFloat("be:tablet_x", movements.xpos); message->AddFloat("be:tablet_y", movements.ypos); message->AddFloat("be:tablet_pressure", movements.pressure); @@ -373,53 +357,181 @@ TabletInputDevice::DeviceWatcher(void *arg) message->AddFloat("be:tablet_tilt_x", movements.tilt_x); message->AddFloat("be:tablet_tilt_y", movements.tilt_y); } - - sSingletonTabletDevice->EnqueueMessage(message); + + fTarget.EnqueueMessage(message); + lastXPosition = movements.xpos; + lastYPosition = movements.ypos; } } - - if ((movements.wheel_ydelta != 0) || (movements.wheel_xdelta != 0)) { - message = new BMessage(B_MOUSE_WHEEL_CHANGED); - if (message) { - message->AddInt64("when", movements.timestamp); - message->AddFloat("be:wheel_delta_x", movements.wheel_xdelta); - message->AddFloat("be:wheel_delta_y", movements.wheel_ydelta); - - sSingletonTabletDevice->EnqueueMessage(message); - } + + if (movements.wheel_ydelta != 0 || movements.wheel_xdelta != 0) { + BMessage* message = new BMessage(B_MOUSE_WHEEL_CHANGED); + if (message == NULL) + continue; + + if (message->AddInt64("when", movements.timestamp) == B_OK + && message->AddFloat("be:wheel_delta_x", + movements.wheel_xdelta) == B_OK + && message->AddFloat("be:wheel_delta_y", + movements.wheel_ydelta) == B_OK) + fTarget.EnqueueMessage(message); + else + delete message; } - - old_movements = movements; - } - - return 0; } -// tablet_device -tablet_device::tablet_device(const char *driver_path) -{ - fd = -1; - device_watcher = -1; - active = false; - strcpy(path, driver_path); - device_ref.name = get_short_name(path); - device_ref.type = B_POINTING_DEVICE; - device_ref.cookie = this; -}; - - -tablet_device::~tablet_device() -{ - free(device_ref.name); -} - - void -TabletInputDevice::RecursiveScan(const char *directory) +TabletDevice::_ControlThreadCleanup() { - CALLED(); + // NOTE: Only executed when the control thread detected an error + // and from within the control thread! + + if (fActive) { + fThread = -1; + fTarget._RemoveDevice(fPath.String()); + } else { + // In case active is already false, another thread + // waits for this thread to quit, and may already hold + // locks that _RemoveDevice() wants to acquire. In other + // words, the device is already being removed, so we simply + // quit here. + } +} + + +void +TabletDevice::_UpdateSettings() +{ + TD_CALLED(); + + if (get_click_speed(&fSettings.click_speed) != B_OK) + LOG_ERR("error when get_click_speed\n"); + else + ioctl(fDevice, MS_SET_CLICKSPEED, &fSettings.click_speed); +} + + +BMessage* +TabletDevice::_BuildMouseMessage(uint32 what, uint64 when, uint32 buttons, + float xPosition, float yPosition) const +{ + BMessage* message = new BMessage(what); + if (message == NULL) + return NULL; + + if (message->AddInt64("when", when) < B_OK + || message->AddInt32("buttons", buttons) < B_OK + || message->AddFloat("x", xPosition) < B_OK + || message->AddFloat("y", yPosition) < B_OK) { + delete message; + return NULL; + } + + return message; +} + + +// #pragma mark - + + +TabletInputDevice::TabletInputDevice() + : + fDevices(2, true), + fDeviceListLock("TabletInputDevice list") +{ + TID_CALLED(); + + StartMonitoringDevice(kTabletDevicesDirectory); + _RecursiveScan(kTabletDevicesDirectory); +} + + +TabletInputDevice::~TabletInputDevice() +{ + TID_CALLED(); + + StopMonitoringDevice(kTabletDevicesDirectory); + fDevices.MakeEmpty(); +} + + +status_t +TabletInputDevice::InitCheck() +{ + TID_CALLED(); + + return BInputServerDevice::InitCheck(); +} + + +status_t +TabletInputDevice::Start(const char* name, void* cookie) +{ + TID_CALLED(); + + TabletDevice* device = (TabletDevice*)cookie; + + return device->Start(); +} + + +status_t +TabletInputDevice::Stop(const char* name, void* cookie) +{ + TRACE("%s(%s)\n", __PRETTY_FUNCTION__, name); + + TabletDevice* device = (TabletDevice*)cookie; + device->Stop(); + + return B_OK; +} + + +status_t +TabletInputDevice::Control(const char* name, void* cookie, + uint32 command, BMessage* message) +{ + TRACE("%s(%s, code: %lu)\n", __PRETTY_FUNCTION__, name, command); + + TabletDevice* device = (TabletDevice*)cookie; + + if (command == B_NODE_MONITOR) + return _HandleMonitor(message); + + if (command == B_CLICK_SPEED_CHANGED) + return device->UpdateSettings(); + + return B_BAD_VALUE; +} + + +status_t +TabletInputDevice::_HandleMonitor(BMessage* message) +{ + TID_CALLED(); + + const char* path; + int32 opcode; + if (message->FindInt32("opcode", &opcode) != B_OK + || (opcode != B_ENTRY_CREATED && opcode != B_ENTRY_REMOVED) + || message->FindString("path", &path) != B_OK) + return B_BAD_VALUE; + + if (opcode == B_ENTRY_CREATED) + return _AddDevice(path); + + // Don't handle B_ENTRY_REMOVED, let the control thread take care of it. + return B_OK; +} + + +void +TabletInputDevice::_RecursiveScan(const char* directory) +{ + TID_CALLED(); + BEntry entry; BDirectory dir(directory); while (dir.GetNextEntry(&entry) == B_OK) { @@ -427,26 +539,81 @@ TabletInputDevice::RecursiveScan(const char *directory) entry.GetPath(&path); if (entry.IsDirectory()) - RecursiveScan(path.Path()); + _RecursiveScan(path.Path()); else - AddDevice(path.Path()); + _AddDevice(path.Path()); } } -static char * -get_short_name(const char *longName) +TabletDevice* +TabletInputDevice::_FindDevice(const char* path) const { - BString string(longName); - BString name; - - int32 slash = string.FindLast("/"); - string.CopyInto(name, slash + 1, string.Length() - slash); - int32 index = atoi(name.String()) + 1; - - int32 previousSlash = string.FindLast("/", slash); - string.CopyInto(name, previousSlash + 1, slash - previousSlash - 1); - name << " Tablet " << index; - - return strdup(name.String()); + TID_CALLED(); + + for (int32 i = fDevices.CountItems() - 1; i >= 0; i--) { + TabletDevice* device = fDevices.ItemAt(i); + if (strcmp(device->Path(), path) == 0) + return device; + } + + return NULL; +} + + +status_t +TabletInputDevice::_AddDevice(const char* path) +{ + TID_CALLED(); + + BAutolock _(fDeviceListLock); + + _RemoveDevice(path); + + TabletDevice* device = new(std::nothrow) TabletDevice(*this, path); + if (device == NULL) { + TRACE("No memory\n"); + return B_NO_MEMORY; + } + + if (!fDevices.AddItem(device)) { + TRACE("No memory in list\n"); + delete device; + return B_NO_MEMORY; + } + + input_device_ref* devices[2]; + devices[0] = device->DeviceRef(); + devices[1] = NULL; + + TRACE("adding path: %s, name: %s\n", path, devices[0]->name); + + return RegisterDevices(devices); +} + + +status_t +TabletInputDevice::_RemoveDevice(const char* path) +{ + TID_CALLED(); + + BAutolock _(fDeviceListLock); + + TabletDevice* device = _FindDevice(path); + if (device == NULL) { + TRACE("%s not found\n", path); + return B_ENTRY_NOT_FOUND; + } + + input_device_ref* devices[2]; + devices[0] = device->DeviceRef(); + devices[1] = NULL; + + TRACE("removing path: %s, name: %s\n", path, devices[0]->name); + + UnregisterDevices(devices); + + fDevices.RemoveItem(device); + + return B_OK; } diff --git a/src/add-ons/input_server/devices/tablet/TabletInputDevice.h b/src/add-ons/input_server/devices/tablet/TabletInputDevice.h index 32fadba503..9bffa84e96 100644 --- a/src/add-ons/input_server/devices/tablet/TabletInputDevice.h +++ b/src/add-ons/input_server/devices/tablet/TabletInputDevice.h @@ -1,69 +1,54 @@ -/*****************************************************************************/ -// Tablet input server device addon -// Adapted by Jerome Duval, written by Stefano Ceccherini -// -// TabletInputDevice.h -// -// Copyright (c) 2005 Haiku Project -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included -// in all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. -/*****************************************************************************/ -#ifndef __TABLETINPUTDEVICE_H -#define __TABLETINPUTDEVICE_H +/* + * Copyright 2004-2011, Haiku. + * Distributed under the terms of the MIT License. + * + * Authors: + * Stefano Ceccherini + * Michael Lotz, mmlr@mlotz.ch + */ +#ifndef TABLET_INPUT_DEVICE_H +#define TABLET_INPUT_DEVICE_H + #include #include -#include -#include +#include -#define DEBUG 1 +#include + + +class TabletDevice; class TabletInputDevice : public BInputServerDevice { public: - TabletInputDevice(); - ~TabletInputDevice(); - - virtual status_t InitCheck(); - - virtual status_t Start(const char *name, void *cookie); - virtual status_t Stop(const char *name, void *cookie); - - virtual status_t Control(const char *name, void *cookie, - uint32 command, BMessage *message); + TabletInputDevice(); + virtual ~TabletInputDevice(); + + virtual status_t InitCheck(); + + virtual status_t Start(const char* name, void* cookie); + virtual status_t Stop(const char* name, void* cookie); + + virtual status_t Control(const char* name, void* cookie, + uint32 command, BMessage* message); + 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; -#ifdef DEBUG -public: - static FILE *sLogFile; -#endif + friend class TabletDevice; + // TODO: needed by the control thread to remove a dead device + // find a better way... + + status_t _HandleMonitor(BMessage* message); + void _RecursiveScan(const char* directory); + + TabletDevice* _FindDevice(const char* path) const; + status_t _AddDevice(const char* path); + status_t _RemoveDevice(const char* path); + +private: + BObjectList fDevices; + BLocker fDeviceListLock; }; -extern "C" BInputServerDevice *instantiate_input_device(); - -#endif +extern "C" BInputServerDevice* instantiate_input_device(); +#endif // TABLET_INPUT_DEVICE_H