161d7deeeSJérôme Duval /*****************************************************************************/ 261d7deeeSJérôme Duval // Mouse input server device addon 361d7deeeSJérôme Duval // Written by Stefano Ceccherini 461d7deeeSJérôme Duval // 561d7deeeSJérôme Duval // MouseInputDevice.cpp 661d7deeeSJérôme Duval // 761d7deeeSJérôme Duval // Copyright (c) 2004 Haiku Project 861d7deeeSJérôme Duval // 961d7deeeSJérôme Duval // Permission is hereby granted, free of charge, to any person obtaining a 1061d7deeeSJérôme Duval // copy of this software and associated documentation files (the "Software"), 1161d7deeeSJérôme Duval // to deal in the Software without restriction, including without limitation 1261d7deeeSJérôme Duval // the rights to use, copy, modify, merge, publish, distribute, sublicense, 1361d7deeeSJérôme Duval // and/or sell copies of the Software, and to permit persons to whom the 1461d7deeeSJérôme Duval // Software is furnished to do so, subject to the following conditions: 1561d7deeeSJérôme Duval // 1661d7deeeSJérôme Duval // The above copyright notice and this permission notice shall be included 1761d7deeeSJérôme Duval // in all copies or substantial portions of the Software. 1861d7deeeSJérôme Duval // 1961d7deeeSJérôme Duval // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 2061d7deeeSJérôme Duval // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 2161d7deeeSJérôme Duval // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 2261d7deeeSJérôme Duval // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 2361d7deeeSJérôme Duval // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 2461d7deeeSJérôme Duval // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 2561d7deeeSJérôme Duval // DEALINGS IN THE SOFTWARE. 2661d7deeeSJérôme Duval /*****************************************************************************/ 2733efb919SStefano Ceccherini 2833efb919SStefano Ceccherini // TODO: Use strlcpy instead of strcpy 2933efb919SStefano Ceccherini 3061d7deeeSJérôme Duval #include "MouseInputDevice.h" 31fc2045eeSJérôme Duval #include "kb_mouse_settings.h" 32fc2045eeSJérôme Duval #include "kb_mouse_driver.h" 3361d7deeeSJérôme Duval 3461d7deeeSJérôme Duval #include <stdlib.h> 3561d7deeeSJérôme Duval #include <unistd.h> 3661d7deeeSJérôme Duval 3733efb919SStefano Ceccherini #include <Directory.h> 3833efb919SStefano Ceccherini #include <Entry.h> 3933efb919SStefano Ceccherini #include <List.h> 4033efb919SStefano Ceccherini #include <NodeMonitor.h> 4133efb919SStefano Ceccherini #include <Path.h> 4233efb919SStefano Ceccherini #include <String.h> 4333efb919SStefano Ceccherini 443aa69c78SStefano Ceccherini #if DEBUG 455c506d7fSJérôme Duval #define LOG(text) fputs(text, sLogFile); fflush(sLogFile) 463aa69c78SStefano Ceccherini #else 473aa69c78SStefano Ceccherini #define LOG(text) 483aa69c78SStefano Ceccherini #endif 493aa69c78SStefano Ceccherini 5033efb919SStefano Ceccherini #include <Debug.h> 5133efb919SStefano Ceccherini 523aa69c78SStefano Ceccherini FILE *MouseInputDevice::sLogFile = NULL; 5333efb919SStefano Ceccherini 5433efb919SStefano Ceccherini static MouseInputDevice *sSingletonMouseDevice = NULL; 553aa69c78SStefano Ceccherini 563aa69c78SStefano Ceccherini // TODO: These are "stolen" from the kb_mouse driver on bebits, which uses 573aa69c78SStefano Ceccherini // the same protocol as BeOS one. They're just here to test this add-on with 583aa69c78SStefano Ceccherini // the BeOS mouse driver. 5961d7deeeSJérôme Duval const static uint32 kGetMouseMovements = 10099; 6053d77642SJérôme Duval const static uint32 kGetMouseAccel = 10101; 6153d77642SJérôme Duval const static uint32 kSetMouseAccel = 10102; 6253d77642SJérôme Duval const static uint32 kSetMouseType = 10104; 6353d77642SJérôme Duval const static uint32 kSetMouseMap = 10106; 6453d77642SJérôme Duval const static uint32 kSetClickSpeed = 10108; 6561d7deeeSJérôme Duval 6633efb919SStefano Ceccherini const static uint32 kMouseThreadPriority = B_FIRST_REAL_TIME_PRIORITY + 4; 6733efb919SStefano Ceccherini const static char *kMouseDevicesDirectory = "/dev/input/mouse"; 6833efb919SStefano Ceccherini 6933efb919SStefano Ceccherini // "/dev/" is automatically prepended by StartMonitoringDevice() 7033efb919SStefano Ceccherini const static char *kMouseDevicesDirectoryUSB = "input/mouse/usb"; 7133efb919SStefano Ceccherini 720dbc4befSStefano Ceccherini struct mouse_device { 7333efb919SStefano Ceccherini mouse_device(const char *path); 7433efb919SStefano Ceccherini ~mouse_device(); 7533efb919SStefano Ceccherini 7633efb919SStefano Ceccherini input_device_ref device_ref; 7733efb919SStefano Ceccherini char driver_path[B_PATH_NAME_LENGTH]; 780dbc4befSStefano Ceccherini int driver_fd; 790dbc4befSStefano Ceccherini thread_id device_watcher; 800dbc4befSStefano Ceccherini uint32 buttons_state; 8133efb919SStefano Ceccherini mouse_settings settings; 8233efb919SStefano Ceccherini bool active; 830dbc4befSStefano Ceccherini }; 840dbc4befSStefano Ceccherini 850dbc4befSStefano Ceccherini 8633efb919SStefano Ceccherini // forward declarations 8733efb919SStefano Ceccherini static void scan_recursively(const char *directory, BList *list); 8833efb919SStefano Ceccherini static char *get_short_name(const char *longName); 8933efb919SStefano Ceccherini 9033efb919SStefano Ceccherini 9161d7deeeSJérôme Duval extern "C" 9261d7deeeSJérôme Duval BInputServerDevice * 9361d7deeeSJérôme Duval instantiate_input_device() 9461d7deeeSJérôme Duval { 9561d7deeeSJérôme Duval return new MouseInputDevice(); 9661d7deeeSJérôme Duval } 9761d7deeeSJérôme Duval 9861d7deeeSJérôme Duval 9961d7deeeSJérôme Duval MouseInputDevice::MouseInputDevice() 10033efb919SStefano Ceccherini : fDevices(NULL) 10133efb919SStefano Ceccherini 10261d7deeeSJérôme Duval { 10333efb919SStefano Ceccherini ASSERT(sSingletonMouseDevice == NULL); 10433efb919SStefano Ceccherini sSingletonMouseDevice = this; 10561d7deeeSJérôme Duval 1063aa69c78SStefano Ceccherini #if DEBUG 1070dbc4befSStefano Ceccherini if (sLogFile == NULL) 1085c506d7fSJérôme Duval sLogFile = fopen("/var/log/mouse_device_log.log", "a"); 1093aa69c78SStefano Ceccherini #endif 11033efb919SStefano Ceccherini 11133efb919SStefano Ceccherini StartMonitoringDevice(kMouseDevicesDirectoryUSB); 11261d7deeeSJérôme Duval } 11361d7deeeSJérôme Duval 11461d7deeeSJérôme Duval 11561d7deeeSJérôme Duval MouseInputDevice::~MouseInputDevice() 11661d7deeeSJérôme Duval { 11733efb919SStefano Ceccherini StopMonitoringDevice(kMouseDevicesDirectoryUSB); 11861d7deeeSJérôme Duval 11933efb919SStefano Ceccherini for (int32 i = 0; i < fDevices->CountItems(); i++) 12033efb919SStefano Ceccherini delete (mouse_device *)fDevices->ItemAt(i); 12133efb919SStefano Ceccherini 12233efb919SStefano Ceccherini delete fDevices; 12361d7deeeSJérôme Duval 1243aa69c78SStefano Ceccherini #if DEBUG 1253aa69c78SStefano Ceccherini fclose(sLogFile); 1263aa69c78SStefano Ceccherini #endif 12761d7deeeSJérôme Duval } 12861d7deeeSJérôme Duval 12961d7deeeSJérôme Duval 13061d7deeeSJérôme Duval status_t 13133efb919SStefano Ceccherini MouseInputDevice::InitFromSettings(void *cookie, uint32 opcode) 13253d77642SJérôme Duval { 13333efb919SStefano Ceccherini mouse_device *device = (mouse_device *)cookie; 13433efb919SStefano Ceccherini 13553d77642SJérôme Duval // retrieve current values 13653d77642SJérôme Duval 13733efb919SStefano Ceccherini if (get_mouse_map(&device->settings.map) != B_OK) 13853d77642SJérôme Duval fprintf(stderr, "error when get_mouse_map\n"); 13953d77642SJérôme Duval else 14033efb919SStefano Ceccherini ioctl(device->driver_fd, kSetMouseMap, &device->settings.map); 14153d77642SJérôme Duval 14233efb919SStefano Ceccherini if (get_click_speed(&device->settings.click_speed) != B_OK) 14353d77642SJérôme Duval fprintf(stderr, "error when get_click_speed\n"); 14453d77642SJérôme Duval else 14533efb919SStefano Ceccherini ioctl(device->driver_fd, kSetClickSpeed, &device->settings.click_speed); 14653d77642SJérôme Duval 14733efb919SStefano Ceccherini if (get_mouse_speed(&device->settings.accel.speed) != B_OK) 14853d77642SJérôme Duval fprintf(stderr, "error when get_mouse_speed\n"); 14953d77642SJérôme Duval else { 15033efb919SStefano Ceccherini if (get_mouse_acceleration(&device->settings.accel.accel_factor) != B_OK) 15153d77642SJérôme Duval fprintf(stderr, "error when get_mouse_acceleration\n"); 15253d77642SJérôme Duval else { 15353d77642SJérôme Duval mouse_accel accel; 15433efb919SStefano Ceccherini ioctl(device->driver_fd, kGetMouseAccel, &accel); 15533efb919SStefano Ceccherini accel.speed = device->settings.accel.speed; 15633efb919SStefano Ceccherini accel.accel_factor = device->settings.accel.accel_factor; 15733efb919SStefano Ceccherini ioctl(device->driver_fd, kSetMouseAccel, &device->settings.accel); 15853d77642SJérôme Duval } 15953d77642SJérôme Duval } 16053d77642SJérôme Duval 16133efb919SStefano Ceccherini if (get_mouse_type(&device->settings.type) != B_OK) 16253d77642SJérôme Duval fprintf(stderr, "error when get_mouse_type\n"); 16353d77642SJérôme Duval else 16433efb919SStefano Ceccherini ioctl(device->driver_fd, kSetMouseType, &device->settings.type); 16553d77642SJérôme Duval 16653d77642SJérôme Duval return B_OK; 16733efb919SStefano Ceccherini 16853d77642SJérôme Duval } 16953d77642SJérôme Duval 17053d77642SJérôme Duval 17153d77642SJérôme Duval status_t 17261d7deeeSJérôme Duval MouseInputDevice::InitCheck() 17361d7deeeSJérôme Duval { 17433efb919SStefano Ceccherini BList list; 17533efb919SStefano Ceccherini scan_recursively(kMouseDevicesDirectory, &list); 17653d77642SJérôme Duval 17733efb919SStefano Ceccherini char path[B_PATH_NAME_LENGTH]; 17833efb919SStefano Ceccherini if (list.CountItems() > 0) { 17933efb919SStefano Ceccherini fDevices = new BList; 18033efb919SStefano Ceccherini for (int32 i = 0; i < list.CountItems(); i++) { 18133efb919SStefano Ceccherini strcpy(path, kMouseDevicesDirectory); 18233efb919SStefano Ceccherini strcat(path, "/"); 18333efb919SStefano Ceccherini strcat(path, (char *)list.ItemAt(i)); 18433efb919SStefano Ceccherini AddDevice(path); 18533efb919SStefano Ceccherini free(list.ItemAt(i)); 18661d7deeeSJérôme Duval } 18733efb919SStefano Ceccherini } 18833efb919SStefano Ceccherini 18933efb919SStefano Ceccherini if (fDevices && fDevices->CountItems() > 0) 19033efb919SStefano Ceccherini return BInputServerDevice::InitCheck(); 19133efb919SStefano Ceccherini 19261d7deeeSJérôme Duval return B_ERROR; 19361d7deeeSJérôme Duval } 19461d7deeeSJérôme Duval 19561d7deeeSJérôme Duval 19661d7deeeSJérôme Duval status_t 19761d7deeeSJérôme Duval MouseInputDevice::Start(const char *name, void *cookie) 19861d7deeeSJérôme Duval { 19933efb919SStefano Ceccherini mouse_device *device = (mouse_device *)cookie; 20033efb919SStefano Ceccherini 2013aa69c78SStefano Ceccherini char log[128]; 2023aa69c78SStefano Ceccherini snprintf(log, 128, "Start(%s)\n", name); 2033aa69c78SStefano Ceccherini 2043aa69c78SStefano Ceccherini LOG(log); 20533efb919SStefano Ceccherini 20633efb919SStefano Ceccherini char threadName[B_OS_NAME_LENGTH]; 20733efb919SStefano Ceccherini snprintf(threadName, B_OS_NAME_LENGTH, "%s watcher", name); 20833efb919SStefano Ceccherini 20933efb919SStefano Ceccherini device->active = true; 21033efb919SStefano Ceccherini device->device_watcher = spawn_thread(DeviceWatcher, threadName, 21133efb919SStefano Ceccherini kMouseThreadPriority, device); 21233efb919SStefano Ceccherini 21333efb919SStefano Ceccherini resume_thread(device->device_watcher); 21433efb919SStefano Ceccherini 21561d7deeeSJérôme Duval 21661d7deeeSJérôme Duval return B_OK; 21761d7deeeSJérôme Duval } 21861d7deeeSJérôme Duval 21961d7deeeSJérôme Duval 22061d7deeeSJérôme Duval status_t 2213aa69c78SStefano Ceccherini MouseInputDevice::Stop(const char *name, void *cookie) 22261d7deeeSJérôme Duval { 22333efb919SStefano Ceccherini mouse_device *device = (mouse_device *)cookie; 22433efb919SStefano Ceccherini 2253aa69c78SStefano Ceccherini char log[128]; 2263aa69c78SStefano Ceccherini snprintf(log, 128, "Stop(%s)\n", name); 2273aa69c78SStefano Ceccherini 2283aa69c78SStefano Ceccherini LOG(log); 22961d7deeeSJérôme Duval 23033efb919SStefano Ceccherini device->active = false; 23133efb919SStefano Ceccherini if (device->device_watcher >= 0) { 23233efb919SStefano Ceccherini status_t dummy; 23333efb919SStefano Ceccherini wait_for_thread(device->device_watcher, &dummy); 23433efb919SStefano Ceccherini } 23561d7deeeSJérôme Duval 23661d7deeeSJérôme Duval return B_OK; 23761d7deeeSJérôme Duval } 23861d7deeeSJérôme Duval 23961d7deeeSJérôme Duval 24061d7deeeSJérôme Duval status_t 24161d7deeeSJérôme Duval MouseInputDevice::Control(const char *name, void *cookie, 24261d7deeeSJérôme Duval uint32 command, BMessage *message) 24361d7deeeSJérôme Duval { 2443aa69c78SStefano Ceccherini char log[128]; 2453aa69c78SStefano Ceccherini snprintf(log, 128, "Control(%s, code: %lu)\n", name, command); 2463aa69c78SStefano Ceccherini 2473aa69c78SStefano Ceccherini LOG(log); 24853d77642SJérôme Duval 24953d77642SJérôme Duval if (command == B_NODE_MONITOR) 25053d77642SJérôme Duval HandleMonitor(message); 25153d77642SJérôme Duval else if (command >= B_MOUSE_TYPE_CHANGED 25253d77642SJérôme Duval && command <= B_MOUSE_ACCELERATION_CHANGED) { 25333efb919SStefano Ceccherini InitFromSettings(cookie, command); 25453d77642SJérôme Duval } 25553d77642SJérôme Duval return B_OK; 25653d77642SJérôme Duval } 25753d77642SJérôme Duval 25853d77642SJérôme Duval 25933efb919SStefano Ceccherini // TODO: Test this. USB doesn't work on my machine 26033efb919SStefano Ceccherini status_t 26133efb919SStefano Ceccherini MouseInputDevice::HandleMonitor(BMessage *message) 26233efb919SStefano Ceccherini { 26333efb919SStefano Ceccherini int32 opcode = 0; 26433efb919SStefano Ceccherini status_t status; 26533efb919SStefano Ceccherini status = message->FindInt32("opcode", &opcode); 26633efb919SStefano Ceccherini if (status < B_OK) 26733efb919SStefano Ceccherini return status; 26833efb919SStefano Ceccherini 26933efb919SStefano Ceccherini BEntry entry; 27033efb919SStefano Ceccherini BPath path; 27133efb919SStefano Ceccherini dev_t device; 27233efb919SStefano Ceccherini ino_t directory; 27333efb919SStefano Ceccherini ino_t node; 27433efb919SStefano Ceccherini const char *name = NULL; 27533efb919SStefano Ceccherini 27633efb919SStefano Ceccherini switch (opcode) { 27733efb919SStefano Ceccherini case B_ENTRY_CREATED: 27833efb919SStefano Ceccherini { 27933efb919SStefano Ceccherini message->FindInt32("device", &device); 28033efb919SStefano Ceccherini message->FindInt64("directory", &directory); 28133efb919SStefano Ceccherini message->FindInt64("node", &node); 28233efb919SStefano Ceccherini message->FindString("name", &name); 28333efb919SStefano Ceccherini 28433efb919SStefano Ceccherini entry_ref ref(device, directory, name); 28533efb919SStefano Ceccherini 28633efb919SStefano Ceccherini status = entry.SetTo(&ref); 28733efb919SStefano Ceccherini if (status == B_OK); 28833efb919SStefano Ceccherini status = entry.GetPath(&path); 28933efb919SStefano Ceccherini if (status == B_OK) 29033efb919SStefano Ceccherini status = path.InitCheck(); 29133efb919SStefano Ceccherini if (status == B_OK) 29233efb919SStefano Ceccherini status = AddDevice(path.Path()); 29333efb919SStefano Ceccherini 29433efb919SStefano Ceccherini break; 29533efb919SStefano Ceccherini } 29633efb919SStefano Ceccherini case B_ENTRY_REMOVED: 29733efb919SStefano Ceccherini { 29833efb919SStefano Ceccherini message->FindInt32("device", &device); 29933efb919SStefano Ceccherini message->FindInt64("directory", &directory); 30033efb919SStefano Ceccherini message->FindInt64("node", &node); 30133efb919SStefano Ceccherini message->FindString("name", &name); 30233efb919SStefano Ceccherini 30333efb919SStefano Ceccherini entry_ref ref(device, directory, name); 30433efb919SStefano Ceccherini 30533efb919SStefano Ceccherini status = entry.SetTo(&ref); 30633efb919SStefano Ceccherini if (status == B_OK); 30733efb919SStefano Ceccherini status = entry.GetPath(&path); 30833efb919SStefano Ceccherini if (status == B_OK) 30933efb919SStefano Ceccherini status = path.InitCheck(); 31033efb919SStefano Ceccherini if (status == B_OK) 31133efb919SStefano Ceccherini status = RemoveDevice(path.Path()); 31233efb919SStefano Ceccherini 31333efb919SStefano Ceccherini break; 31433efb919SStefano Ceccherini } 31533efb919SStefano Ceccherini default: 31633efb919SStefano Ceccherini status = B_BAD_VALUE; 31733efb919SStefano Ceccherini break; 31833efb919SStefano Ceccherini } 31933efb919SStefano Ceccherini 32033efb919SStefano Ceccherini return status; 32133efb919SStefano Ceccherini } 32233efb919SStefano Ceccherini 32333efb919SStefano Ceccherini 32453d77642SJérôme Duval status_t 32533efb919SStefano Ceccherini MouseInputDevice::AddDevice(const char *path) 32653d77642SJérôme Duval { 32733efb919SStefano Ceccherini mouse_device *device = new mouse_device(path); 32833efb919SStefano Ceccherini if (!device) { 32933efb919SStefano Ceccherini LOG("No memory\n"); 33033efb919SStefano Ceccherini return B_NO_MEMORY; 33133efb919SStefano Ceccherini } 33233efb919SStefano Ceccherini 33333efb919SStefano Ceccherini input_device_ref *devices[2]; 33433efb919SStefano Ceccherini devices[0] = &device->device_ref; 33533efb919SStefano Ceccherini devices[1] = NULL; 33633efb919SStefano Ceccherini 33733efb919SStefano Ceccherini fDevices->AddItem(device); 33833efb919SStefano Ceccherini 33933efb919SStefano Ceccherini InitFromSettings(device); 34033efb919SStefano Ceccherini 34133efb919SStefano Ceccherini return RegisterDevices(devices); 34233efb919SStefano Ceccherini } 34333efb919SStefano Ceccherini 34433efb919SStefano Ceccherini 34533efb919SStefano Ceccherini status_t 34633efb919SStefano Ceccherini MouseInputDevice::RemoveDevice(const char *path) 34733efb919SStefano Ceccherini { 34833efb919SStefano Ceccherini if (fDevices) { 34933efb919SStefano Ceccherini int32 i = 0; 35033efb919SStefano Ceccherini mouse_device *device = NULL; 35133efb919SStefano Ceccherini while ((device = (mouse_device *)fDevices->ItemAt(i)) != NULL) { 35233efb919SStefano Ceccherini if (!strcmp(device->driver_path, path)) { 35333efb919SStefano Ceccherini fDevices->RemoveItem(device); 35433efb919SStefano Ceccherini delete device; 35561d7deeeSJérôme Duval return B_OK; 35661d7deeeSJérôme Duval } 35733efb919SStefano Ceccherini } 35833efb919SStefano Ceccherini } 35933efb919SStefano Ceccherini 36033efb919SStefano Ceccherini return B_ENTRY_NOT_FOUND; 36133efb919SStefano Ceccherini } 36261d7deeeSJérôme Duval 36361d7deeeSJérôme Duval 36461d7deeeSJérôme Duval int32 36561d7deeeSJérôme Duval MouseInputDevice::DeviceWatcher(void *arg) 36661d7deeeSJérôme Duval { 36733efb919SStefano Ceccherini mouse_device *dev = (mouse_device *)arg; 36833efb919SStefano Ceccherini 36961d7deeeSJérôme Duval mouse_movement movements; 37061d7deeeSJérôme Duval BMessage *message; 37161d7deeeSJérôme Duval char log[128]; 37233efb919SStefano Ceccherini while (dev->active) { 3737d5d344bSJérôme Duval memset(&movements, 0, sizeof(movements)); 37433efb919SStefano Ceccherini if (ioctl(dev->driver_fd, kGetMouseMovements, &movements) < B_OK) 375145a357dSStefano Ceccherini continue; 37661d7deeeSJérôme Duval 37733efb919SStefano Ceccherini uint32 buttons = dev->buttons_state ^ movements.buttons; 37861d7deeeSJérôme Duval 37933efb919SStefano Ceccherini snprintf(log, 128, "%s: buttons: 0x%lx, x: %ld, y: %ld, clicks:%ld\n", 38033efb919SStefano Ceccherini dev->device_ref.name, movements.buttons, movements.xdelta, 381fc2045eeSJérôme Duval movements.ydelta, movements.clicks); 38261d7deeeSJérôme Duval 3833aa69c78SStefano Ceccherini LOG(log); 3843aa69c78SStefano Ceccherini 385fc2045eeSJérôme Duval // TODO: add acceleration computing 386fc2045eeSJérôme Duval int32 xdelta = movements.xdelta * dev->settings.accel.speed >> 15; 387*6be0f7efSJérôme Duval int32 ydelta = movements.ydelta * dev->settings.accel.speed >> 15; 3886ed407d8SJérôme Duval 3896ed407d8SJérôme Duval snprintf(log, 128, "%s: x: %ld, y: %ld, \n", 3906ed407d8SJérôme Duval dev->device_ref.name, xdelta, ydelta); 3916ed407d8SJérôme Duval 3926ed407d8SJérôme Duval LOG(log); 3936ed407d8SJérôme Duval 394fc2045eeSJérôme Duval 3953aa69c78SStefano Ceccherini // TODO: B_MOUSE_DOWN and B_MOUSE_UP messages don't seem 39633efb919SStefano Ceccherini // to be generated correctly. 3973aa69c78SStefano Ceccherini if (buttons != 0) { 398fc2045eeSJérôme Duval message = new BMessage(B_MOUSE_UP); 3993f8c0d7eSJérôme Duval if ((buttons & movements.buttons) > 0) { 4003aa69c78SStefano Ceccherini message->what = B_MOUSE_DOWN; 401fc2045eeSJérôme Duval message->AddInt32("clicks", movements.clicks); 4023aa69c78SStefano Ceccherini LOG("B_MOUSE_DOWN\n"); 403e361bf69SJérôme Duval } else { 4043aa69c78SStefano Ceccherini LOG("B_MOUSE_UP\n"); 4053aa69c78SStefano Ceccherini } 4063aa69c78SStefano Ceccherini 407fc2045eeSJérôme Duval message->AddInt64("when", movements.timestamp); 408e361bf69SJérôme Duval message->AddInt32("buttons", movements.buttons); 409fc2045eeSJérôme Duval message->AddInt32("x", xdelta); 410fc2045eeSJérôme Duval message->AddInt32("y", ydelta); 41133efb919SStefano Ceccherini sSingletonMouseDevice->EnqueueMessage(message); 412fc2045eeSJérôme Duval dev->buttons_state = movements.buttons; 4133aa69c78SStefano Ceccherini } 4143aa69c78SStefano Ceccherini 4153aa69c78SStefano Ceccherini if (movements.xdelta != 0 || movements.ydelta != 0) { 4163aa69c78SStefano Ceccherini message = new BMessage(B_MOUSE_MOVED); 4173aa69c78SStefano Ceccherini if (message) { 418fc2045eeSJérôme Duval message->AddInt64("when", movements.timestamp); 4193aa69c78SStefano Ceccherini message->AddInt32("buttons", movements.buttons); 420fc2045eeSJérôme Duval message->AddInt32("x", xdelta); 421fc2045eeSJérôme Duval message->AddInt32("y", ydelta); 4223aa69c78SStefano Ceccherini 42333efb919SStefano Ceccherini sSingletonMouseDevice->EnqueueMessage(message); 4243aa69c78SStefano Ceccherini } 42561d7deeeSJérôme Duval } 426fc2045eeSJérôme Duval 4277d5d344bSJérôme Duval if ((movements.wheel_ydelta != 0) || (movements.wheel_xdelta != 0)) { 428fc2045eeSJérôme Duval message = new BMessage(B_MOUSE_WHEEL_CHANGED); 429fc2045eeSJérôme Duval if (message) { 430fc2045eeSJérôme Duval message->AddInt64("when", movements.timestamp); 4317d5d344bSJérôme Duval message->AddFloat("be:wheel_delta_x", movements.wheel_xdelta); 4327d5d344bSJérôme Duval message->AddFloat("be:wheel_delta_y", movements.wheel_ydelta); 433fc2045eeSJérôme Duval 434fc2045eeSJérôme Duval sSingletonMouseDevice->EnqueueMessage(message); 435fc2045eeSJérôme Duval } 436fc2045eeSJérôme Duval } 437fc2045eeSJérôme Duval 43861d7deeeSJérôme Duval } 43961d7deeeSJérôme Duval 44061d7deeeSJérôme Duval return 0; 44161d7deeeSJérôme Duval } 44261d7deeeSJérôme Duval 44361d7deeeSJérôme Duval 44433efb919SStefano Ceccherini // mouse_device 44533efb919SStefano Ceccherini mouse_device::mouse_device(const char *path) 44633efb919SStefano Ceccherini { 44733efb919SStefano Ceccherini driver_fd = -1; 44833efb919SStefano Ceccherini device_watcher = -1; 44933efb919SStefano Ceccherini buttons_state = 0; 45033efb919SStefano Ceccherini active = false; 45133efb919SStefano Ceccherini strcpy(driver_path, path); 45233efb919SStefano Ceccherini device_ref.name = get_short_name(path); 45333efb919SStefano Ceccherini device_ref.type = B_POINTING_DEVICE; 45433efb919SStefano Ceccherini device_ref.cookie = this; 45533efb919SStefano Ceccherini 45633efb919SStefano Ceccherini // TODO: Add a function which checks if the object 45733efb919SStefano Ceccherini // has initialized correctly. Specifically, this is one 45833efb919SStefano Ceccherini // of the operations which could fail 45933efb919SStefano Ceccherini driver_fd = open(driver_path, O_RDWR); 46033efb919SStefano Ceccherini }; 46133efb919SStefano Ceccherini 46233efb919SStefano Ceccherini 46333efb919SStefano Ceccherini mouse_device::~mouse_device() 46433efb919SStefano Ceccherini { 46533efb919SStefano Ceccherini free(device_ref.name); 46633efb919SStefano Ceccherini if (driver_fd >= 0) 46733efb919SStefano Ceccherini close(driver_fd); 46833efb919SStefano Ceccherini } 46933efb919SStefano Ceccherini 47033efb919SStefano Ceccherini 47133efb919SStefano Ceccherini // static functions 47233efb919SStefano Ceccherini 47333efb919SStefano Ceccherini // On exit, "list" will contain a string for every file in 47433efb919SStefano Ceccherini // the tree, starting from the given "directory". Note that 47533efb919SStefano Ceccherini // the file names will also contain the name of the parent folder. 47633efb919SStefano Ceccherini static void 47733efb919SStefano Ceccherini scan_recursively(const char *directory, BList *list) 47833efb919SStefano Ceccherini { 47933efb919SStefano Ceccherini // TODO: See if it's simpler to use POSIX functions 48033efb919SStefano Ceccherini BEntry entry; 48133efb919SStefano Ceccherini BDirectory dir(directory); 48233efb919SStefano Ceccherini char buf[B_OS_NAME_LENGTH]; 48333efb919SStefano Ceccherini while (dir.GetNextEntry(&entry) == B_OK) { 48433efb919SStefano Ceccherini entry.GetName(buf); 48533efb919SStefano Ceccherini BPath child(&dir, buf); 48633efb919SStefano Ceccherini 48733efb919SStefano Ceccherini if (entry.IsDirectory()) 48833efb919SStefano Ceccherini scan_recursively(child.Path(), list); 48933efb919SStefano Ceccherini else { 49033efb919SStefano Ceccherini BPath parent(directory); 49133efb919SStefano Ceccherini BString deviceName = parent.Leaf(); 49233efb919SStefano Ceccherini deviceName << "/" << buf; 49333efb919SStefano Ceccherini list->AddItem(strdup(deviceName.String())); 49433efb919SStefano Ceccherini } 49533efb919SStefano Ceccherini } 49633efb919SStefano Ceccherini } 49733efb919SStefano Ceccherini 49833efb919SStefano Ceccherini 49933efb919SStefano Ceccherini static char * 50033efb919SStefano Ceccherini get_short_name(const char *longName) 50133efb919SStefano Ceccherini { 50233efb919SStefano Ceccherini BString string(longName); 50333efb919SStefano Ceccherini BString name; 50433efb919SStefano Ceccherini 50533efb919SStefano Ceccherini int32 slash = string.FindLast("/"); 50633efb919SStefano Ceccherini int32 previousSlash = string.FindLast("/", slash) + 1; 50733efb919SStefano Ceccherini string.CopyInto(name, slash, string.Length() - slash); 50833efb919SStefano Ceccherini 50933efb919SStefano Ceccherini int32 deviceIndex = atoi(name.String()) + 1; 51033efb919SStefano Ceccherini string.CopyInto(name, previousSlash, slash - previousSlash); 51133efb919SStefano Ceccherini name << " mouse " << deviceIndex; 51233efb919SStefano Ceccherini 51333efb919SStefano Ceccherini return strdup(name.String()); 51433efb919SStefano Ceccherini } 515