Replace the TabletInputDevice by a MouseInputDevice based reimplementation.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@41946 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Michael Lotz
2011-06-05 19:15:43 +00:00
parent fdad31e6a5
commit 1c1f322ec1
2 changed files with 552 additions and 400 deletions
@@ -1,39 +1,26 @@
/*****************************************************************************/ /*
// Tablet input server device addon * Copyright 2004-2011, Haiku.
// Adapted by Jerome Duval and written by Stefano Ceccherini * Distributed under the terms of the MIT License.
// *
// TabletInputDevice.cpp * Authors:
// * Stefano Ceccherini ([email protected])
// Copyright (c) 2005 Haiku Project * Jérôme Duval
// * Axel Dörfler, [email protected]
// Permission is hereby granted, free of charge, to any person obtaining a * Clemens Zeidler, [email protected]
// copy of this software and associated documentation files (the "Software"), * Stephan Aßmus, [email protected]
// to deal in the Software without restriction, including without limitation * Michael Lotz, [email protected]
// 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.
/*****************************************************************************/
// TODO: Use strlcpy instead of strcpy
#include "TabletInputDevice.h" #include "TabletInputDevice.h"
#include "kb_mouse_settings.h"
#include "kb_mouse_driver.h"
#include <errno.h>
#include <new>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <Autolock.h>
#include <Debug.h> #include <Debug.h>
#include <Directory.h> #include <Directory.h>
#include <Entry.h> #include <Entry.h>
@@ -41,330 +28,327 @@
#include <Path.h> #include <Path.h>
#include <String.h> #include <String.h>
#if DEBUG #include <kb_mouse_settings.h>
inline void LOG(const char *fmt, ...) { char buf[1024]; va_list ap; va_start(ap, fmt); vsprintf(buf, fmt, ap); va_end(ap); \ #include <keyboard_mouse_driver.h>
fputs(buf, TabletInputDevice::sLogFile); fflush(TabletInputDevice::sLogFile); }
#define LOG_ERR(text...) LOG(text)
FILE *TabletInputDevice::sLogFile = NULL; #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 #else
#define LOG(text...) # define TRACE(x...) do {} while (0)
#define LOG_ERR(text...) fprintf(stderr, text) # 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 #endif
#define CALLED() LOG("%s\n", __PRETTY_FUNCTION__)
static TabletInputDevice *sSingletonTabletDevice = NULL;
const static uint32 kTabletThreadPriority = B_FIRST_REAL_TIME_PRIORITY + 4; 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 { class TabletDevice {
tablet_device(const char *path); public:
~tablet_device(); TabletDevice(TabletInputDevice& target,
const char* path);
~TabletDevice();
input_device_ref device_ref; status_t Start();
char path[B_PATH_NAME_LENGTH]; void Stop();
int fd;
thread_id device_watcher; status_t UpdateSettings();
mouse_settings settings;
bool active; 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 extern "C" BInputServerDevice*
static char *get_short_name(const char *longName);
extern "C"
BInputServerDevice *
instantiate_input_device() 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); TD_CALLED();
sSingletonTabletDevice = this;
#if DEBUG fDeviceRef.name = _BuildShortName();
sLogFile = fopen("/var/log/tablet_device_log.log", "a"); fDeviceRef.type = B_POINTING_DEVICE;
#endif fDeviceRef.cookie = this;
CALLED(); };
StartMonitoringDevice(kTabletDevicesDirectoryUSB);
}
TabletInputDevice::~TabletInputDevice() TabletDevice::~TabletDevice()
{ {
CALLED(); TD_CALLED();
StopMonitoringDevice(kTabletDevicesDirectoryUSB); TRACE("delete\n");
for (int32 i = 0; i < fDevices.CountItems(); i++) if (fActive)
delete (tablet_device *)fDevices.ItemAt(i); Stop();
#if DEBUG free(fDeviceRef.name);
fclose(sLogFile);
#endif
} }
status_t status_t
TabletInputDevice::InitFromSettings(void *cookie, uint32 opcode) TabletDevice::Start()
{ {
CALLED(); TD_CALLED();
tablet_device *device = (tablet_device *)cookie;
// retrieve current values fDevice = open(fPath.String(), O_RDWR);
// let the control thread handle any error on opening the device
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");
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);
}
}
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;
}
status_t
TabletInputDevice::InitCheck()
{
CALLED();
RecursiveScan(kTabletDevicesDirectory);
return B_OK;
}
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)
return B_ERROR;
InitFromSettings(device);
char threadName[B_OS_NAME_LENGTH]; char threadName[B_OS_NAME_LENGTH];
snprintf(threadName, B_OS_NAME_LENGTH, "%s watcher", name); snprintf(threadName, B_OS_NAME_LENGTH, "%s watcher", fDeviceRef.name);
device->active = true; fThread = spawn_thread(_ControlThreadEntry, threadName,
device->device_watcher = spawn_thread(DeviceWatcher, threadName, kTabletThreadPriority, (void*)this);
kTabletThreadPriority, device);
resume_thread(device->device_watcher);
return B_OK;
}
status_t
TabletInputDevice::Stop(const char *name, void *cookie)
{
tablet_device *device = (tablet_device *)cookie;
LOG("%s(%s)\n", __PRETTY_FUNCTION__, 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;
}
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; status_t status;
if ((status = message->FindInt32("opcode", &opcode)) < B_OK) if (fThread < 0)
return status; status = fThread;
else {
fActive = true;
status = resume_thread(fThread);
}
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
TabletDevice::UpdateSettings()
{
TD_CALLED();
if (fThread < 0)
return B_ERROR;
// trigger updating the settings in the control thread
fUpdateSettings = true;
if ((opcode != B_ENTRY_CREATED)
&& (opcode != B_ENTRY_REMOVED))
return B_OK; return B_OK;
}
BEntry entry; char*
BPath path; TabletDevice::_BuildShortName() const
dev_t device; {
ino_t directory; BString string(fPath);
const char *name = NULL; BString name;
message->FindInt32("device", &device); int32 slash = string.FindLast("/");
message->FindInt64("directory", &directory); string.CopyInto(name, slash + 1, string.Length() - slash);
message->FindString("name", &name); int32 index = atoi(name.String()) + 1;
entry_ref ref(device, directory, name); int32 previousSlash = string.FindLast("/", slash);
string.CopyInto(name, previousSlash + 1, slash - previousSlash - 1);
if ((status = entry.SetTo(&ref)) != B_OK) if (name.Length() < 4)
return status; name.ToUpper();
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 else
RemoveDevice(path.Path()); name.Capitalize();
return status; name << " Tablet " << index;
return strdup(name.String());
} }
// #pragma mark - control thread
status_t status_t
TabletInputDevice::AddDevice(const char *path) TabletDevice::_ControlThreadEntry(void* arg)
{ {
CALLED(); TabletDevice* device = (TabletDevice*)arg;
device->_ControlThread();
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);
}
status_t
TabletInputDevice::RemoveDevice(const char *path)
{
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; return B_OK;
} }
}
return B_ENTRY_NOT_FOUND;
}
int32 void
TabletInputDevice::DeviceWatcher(void *arg) TabletDevice::_ControlThread()
{ {
tablet_device *dev = (tablet_device *)arg; TD_CALLED();
if (fDevice < 0) {
_ControlThreadCleanup();
return;
}
_UpdateSettings();
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; tablet_movement movements;
tablet_movement old_movements;
uint32 buttons_state = 0; snooze_until(nextTransferTime, B_SYSTEM_TIMEBASE);
BMessage *message; nextTransferTime += kTransferDelay;
while (dev->active) {
memset(&movements, 0, sizeof(movements)); if (ioctl(fDevice, MS_READ, &movements, sizeof(movements)) != B_OK) {
if (ioctl(dev->fd, MS_READ, &movements, LOG_ERR("Tablet device exiting, %s\n", strerror(errno));
sizeof(movements)) != B_OK) { _ControlThreadCleanup();
snooze(10000); // this is a realtime thread, and something is wrong... return;
continue;
} }
uint32 buttons = buttons_state ^ movements.buttons; // take care of updating the settings first, if necessary
if (fUpdateSettings) {
fUpdateSettings = false;
_UpdateSettings();
}
LOG("%s: buttons: 0x%lx, x: %f, y: %f, clicks:%ld, wheel_x:%ld, wheel_y:%ld\n", dev->device_ref.name, movements.buttons, LOG_EVENT("%s: buttons: 0x%lx, x: %f, y: %f, clicks: %ld, contact: %c, "
movements.xpos, movements.ypos, movements.clicks, movements.wheel_xdelta, movements.wheel_ydelta); "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);
movements.xpos /= 10206.0; // Send single messages for each event
movements.ypos /= 7422.0;
float x = movements.xpos;
float y = movements.ypos;
LOG("%s: x: %f, y: %f, \n", dev->device_ref.name, x, y);
uint32 buttons = lastButtons ^ movements.buttons;
if (buttons != 0) { if (buttons != 0) {
message = new BMessage(B_MOUSE_UP); bool pressedButton = (buttons & movements.buttons) > 0;
if ((buttons & movements.buttons) > 0) { BMessage* message = _BuildMouseMessage(
message->what = B_MOUSE_DOWN; 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); message->AddInt32("clicks", movements.clicks);
LOG("B_MOUSE_DOWN\n"); LOG_EVENT("B_MOUSE_DOWN\n");
} else { } else
LOG("B_MOUSE_UP\n"); LOG_EVENT("B_MOUSE_UP\n");
fTarget.EnqueueMessage(message);
lastButtons = movements.buttons;
}
} }
message->AddInt64("when", movements.timestamp); if (movements.xpos != lastXPosition
message->AddInt32("buttons", movements.buttons); || movements.ypos != lastYPosition) {
message->AddFloat("x", movements.xpos); BMessage* message = _BuildMouseMessage(B_MOUSE_MOVED,
message->AddFloat("y", movements.ypos); movements.timestamp, movements.buttons, movements.xpos,
sSingletonTabletDevice->EnqueueMessage(message); movements.ypos);
buttons_state = movements.buttons; if (message != NULL) {
}
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);
message->AddFloat("be:tablet_x", movements.xpos); message->AddFloat("be:tablet_x", movements.xpos);
message->AddFloat("be:tablet_y", movements.ypos); message->AddFloat("be:tablet_y", movements.ypos);
message->AddFloat("be:tablet_pressure", movements.pressure); message->AddFloat("be:tablet_pressure", movements.pressure);
@@ -374,52 +358,180 @@ TabletInputDevice::DeviceWatcher(void *arg)
message->AddFloat("be:tablet_tilt_y", movements.tilt_y); 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)) { if (movements.wheel_ydelta != 0 || movements.wheel_xdelta != 0) {
message = new BMessage(B_MOUSE_WHEEL_CHANGED); BMessage* message = new BMessage(B_MOUSE_WHEEL_CHANGED);
if (message) { if (message == NULL)
message->AddInt64("when", movements.timestamp); continue;
message->AddFloat("be:wheel_delta_x", movements.wheel_xdelta);
message->AddFloat("be:wheel_delta_y", movements.wheel_ydelta);
sSingletonTabletDevice->EnqueueMessage(message); 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 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; BEntry entry;
BDirectory dir(directory); BDirectory dir(directory);
while (dir.GetNextEntry(&entry) == B_OK) { while (dir.GetNextEntry(&entry) == B_OK) {
@@ -427,26 +539,81 @@ TabletInputDevice::RecursiveScan(const char *directory)
entry.GetPath(&path); entry.GetPath(&path);
if (entry.IsDirectory()) if (entry.IsDirectory())
RecursiveScan(path.Path()); _RecursiveScan(path.Path());
else else
AddDevice(path.Path()); _AddDevice(path.Path());
} }
} }
static char * TabletDevice*
get_short_name(const char *longName) TabletInputDevice::_FindDevice(const char* path) const
{ {
BString string(longName); TID_CALLED();
BString name;
int32 slash = string.FindLast("/"); for (int32 i = fDevices.CountItems() - 1; i >= 0; i--) {
string.CopyInto(name, slash + 1, string.Length() - slash); TabletDevice* device = fDevices.ItemAt(i);
int32 index = atoi(name.String()) + 1; if (strcmp(device->Path(), path) == 0)
return device;
int32 previousSlash = string.FindLast("/", slash); }
string.CopyInto(name, previousSlash + 1, slash - previousSlash - 1);
name << " Tablet " << index; return NULL;
}
return strdup(name.String());
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;
} }
@@ -1,43 +1,28 @@
/*****************************************************************************/ /*
// Tablet input server device addon * Copyright 2004-2011, Haiku.
// Adapted by Jerome Duval, written by Stefano Ceccherini * Distributed under the terms of the MIT License.
// *
// TabletInputDevice.h * Authors:
// * Stefano Ceccherini
// Copyright (c) 2005 Haiku Project * Michael Lotz, [email protected]
// */
// Permission is hereby granted, free of charge, to any person obtaining a #ifndef TABLET_INPUT_DEVICE_H
// copy of this software and associated documentation files (the "Software"), #define TABLET_INPUT_DEVICE_H
// 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
#include <InputServerDevice.h> #include <InputServerDevice.h>
#include <InterfaceDefs.h> #include <InterfaceDefs.h>
#include <List.h> #include <Locker.h>
#include <stdio.h>
#define DEBUG 1 #include <ObjectList.h>
class TabletDevice;
class TabletInputDevice : public BInputServerDevice { class TabletInputDevice : public BInputServerDevice {
public: public:
TabletInputDevice(); TabletInputDevice();
~TabletInputDevice(); virtual ~TabletInputDevice();
virtual status_t InitCheck(); virtual status_t InitCheck();
@@ -46,24 +31,24 @@ public:
virtual status_t Control(const char* name, void* cookie, virtual status_t Control(const char* name, void* cookie,
uint32 command, BMessage* message); uint32 command, BMessage* message);
private: private:
status_t HandleMonitor(BMessage *message); friend class TabletDevice;
status_t InitFromSettings(void *cookie, uint32 opcode = 0); // TODO: needed by the control thread to remove a dead device
void RecursiveScan(const char *directory); // find a better way...
status_t AddDevice(const char *path); status_t _HandleMonitor(BMessage* message);
status_t RemoveDevice(const char *path); void _RecursiveScan(const char* directory);
static int32 DeviceWatcher(void *arg); TabletDevice* _FindDevice(const char* path) const;
status_t _AddDevice(const char* path);
status_t _RemoveDevice(const char* path);
BList fDevices; private:
#ifdef DEBUG BObjectList<TabletDevice> fDevices;
public: BLocker fDeviceListLock;
static FILE *sLogFile;
#endif
}; };
extern "C" BInputServerDevice* instantiate_input_device(); extern "C" BInputServerDevice* instantiate_input_device();
#endif #endif // TABLET_INPUT_DEVICE_H