1 /* 2 * Copyright 2004-2010, Haiku. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Stefano Ceccherini (stefano.ceccherini@gmail.com) 7 * Jérôme Duval 8 * Axel Dörfler, axeld@pinc-software.de 9 * Clemens Zeidler, haiku@clemens-zeidler.de 10 * Stephan Aßmus, superstippi@gmx.de 11 */ 12 13 14 #include "MouseInputDevice.h" 15 16 #include <errno.h> 17 #include <new> 18 #include <stdio.h> 19 #include <stdlib.h> 20 #include <unistd.h> 21 22 #include <Autolock.h> 23 #include <Debug.h> 24 #include <Directory.h> 25 #include <Entry.h> 26 #include <File.h> 27 #include <FindDirectory.h> 28 #include <NodeMonitor.h> 29 #include <Path.h> 30 #include <String.h> 31 #include <View.h> 32 33 #include <kb_mouse_settings.h> 34 #include <keyboard_mouse_driver.h> 35 #include <touchpad_settings.h> 36 37 38 #undef TRACE 39 //#define TRACE_MOUSE_DEVICE 40 #ifdef TRACE_MOUSE_DEVICE 41 42 class FunctionTracer { 43 public: 44 FunctionTracer(const void* pointer, const char* className, 45 const char* functionName, 46 int32& depth) 47 : fFunctionName(), 48 fPrepend(), 49 fFunctionDepth(depth), 50 fPointer(pointer) 51 { 52 fFunctionDepth++; 53 fPrepend.Append(' ', fFunctionDepth * 2); 54 fFunctionName << className << "::" << functionName << "()"; 55 56 debug_printf("%p -> %s%s {\n", fPointer, fPrepend.String(), 57 fFunctionName.String()); 58 } 59 60 ~FunctionTracer() 61 { 62 debug_printf("%p -> %s}\n", fPointer, fPrepend.String()); 63 fFunctionDepth--; 64 } 65 66 private: 67 BString fFunctionName; 68 BString fPrepend; 69 int32& fFunctionDepth; 70 const void* fPointer; 71 }; 72 73 74 static int32 sFunctionDepth = -1; 75 # define MD_CALLED(x...) FunctionTracer _ft(this, "MouseDevice", \ 76 __FUNCTION__, sFunctionDepth) 77 # define MID_CALLED(x...) FunctionTracer _ft(this, "MouseInputDevice", \ 78 __FUNCTION__, sFunctionDepth) 79 # define TRACE(x...) do { BString _to; \ 80 _to.Append(' ', (sFunctionDepth + 1) * 2); \ 81 debug_printf("%p -> %s", this, _to.String()); \ 82 debug_printf(x); } while (0) 83 # define LOG_EVENT(text...) do {} while (0) 84 # define LOG_ERR(text...) TRACE(text) 85 #else 86 # define TRACE(x...) do {} while (0) 87 # define MD_CALLED(x...) TRACE(x) 88 # define MID_CALLED(x...) TRACE(x) 89 # define LOG_ERR(x...) debug_printf(x) 90 # define LOG_EVENT(x...) TRACE(x) 91 #endif 92 93 94 const static uint32 kMouseThreadPriority = B_FIRST_REAL_TIME_PRIORITY + 4; 95 const static char* kMouseDevicesDirectory = "/dev/input/mouse"; 96 const static char* kTouchpadDevicesDirectory = "/dev/input/touchpad"; 97 98 99 class MouseDevice { 100 public: 101 MouseDevice(MouseInputDevice& target, 102 const char* path); 103 ~MouseDevice(); 104 105 status_t Start(); 106 void Stop(); 107 108 status_t UpdateSettings(); 109 status_t UpdateTouchpadSettings(const BMessage* message); 110 111 const char* Path() const { return fPath.String(); } 112 input_device_ref* DeviceRef() { return &fDeviceRef; } 113 114 private: 115 char* _BuildShortName() const; 116 117 static status_t _ControlThreadEntry(void* arg); 118 void _ControlThread(); 119 void _ControlThreadCleanup(); 120 void _UpdateSettings(); 121 122 status_t _GetTouchpadSettingsPath(BPath& path); 123 status_t _ReadTouchpadSettingsMsg(BMessage* message); 124 status_t _UpdateTouchpadSettings(); 125 126 BMessage* _BuildMouseMessage(uint32 what, 127 uint64 when, uint32 buttons, 128 int32 deltaX, int32 deltaY) const; 129 void _ComputeAcceleration( 130 const mouse_movement& movements, 131 int32& deltaX, int32& deltaY, 132 float& historyDeltaX, 133 float& historyDeltaY) const; 134 uint32 _RemapButtons(uint32 buttons) const; 135 136 private: 137 MouseInputDevice& fTarget; 138 BString fPath; 139 int fDevice; 140 141 input_device_ref fDeviceRef; 142 mouse_settings fSettings; 143 bool fDeviceRemapsButtons; 144 145 thread_id fThread; 146 volatile bool fActive; 147 volatile bool fUpdateSettings; 148 149 bool fIsTouchpad; 150 touchpad_settings fTouchpadSettings; 151 BMessage* fTouchpadSettingsMessage; 152 BLocker fTouchpadSettingsLock; 153 }; 154 155 156 extern "C" BInputServerDevice* 157 instantiate_input_device() 158 { 159 return new(std::nothrow) MouseInputDevice(); 160 } 161 162 163 // #pragma mark - 164 165 166 MouseDevice::MouseDevice(MouseInputDevice& target, const char* driverPath) 167 : 168 fTarget(target), 169 fPath(driverPath), 170 fDevice(-1), 171 fDeviceRemapsButtons(false), 172 fThread(-1), 173 fActive(false), 174 fUpdateSettings(false), 175 fIsTouchpad(false), 176 fTouchpadSettingsMessage(NULL), 177 fTouchpadSettingsLock("Touchpad settings lock") 178 { 179 MD_CALLED(); 180 181 fDeviceRef.name = _BuildShortName(); 182 fDeviceRef.type = B_POINTING_DEVICE; 183 fDeviceRef.cookie = this; 184 185 #ifdef HAIKU_TARGET_PLATFORM_HAIKU 186 fSettings.map.button[0] = B_PRIMARY_MOUSE_BUTTON; 187 fSettings.map.button[1] = B_SECONDARY_MOUSE_BUTTON; 188 fSettings.map.button[2] = B_TERTIARY_MOUSE_BUTTON; 189 #endif 190 }; 191 192 193 MouseDevice::~MouseDevice() 194 { 195 MD_CALLED(); 196 TRACE("delete\n"); 197 198 if (fActive) 199 Stop(); 200 201 free(fDeviceRef.name); 202 delete fTouchpadSettingsMessage; 203 } 204 205 206 status_t 207 MouseDevice::Start() 208 { 209 MD_CALLED(); 210 211 fDevice = open(fPath.String(), O_RDWR); 212 // let the control thread handle any error on opening the device 213 214 char threadName[B_OS_NAME_LENGTH]; 215 snprintf(threadName, B_OS_NAME_LENGTH, "%s watcher", fDeviceRef.name); 216 217 fThread = spawn_thread(_ControlThreadEntry, threadName, 218 kMouseThreadPriority, (void*)this); 219 220 status_t status; 221 if (fThread < B_OK) 222 status = fThread; 223 else { 224 fActive = true; 225 status = resume_thread(fThread); 226 } 227 228 if (status < B_OK) { 229 LOG_ERR("%s: can't spawn/resume watching thread: %s\n", 230 fDeviceRef.name, strerror(status)); 231 if (fDevice >= B_OK) 232 close(fDevice); 233 234 return status; 235 } 236 237 return fDevice >= 0 ? B_OK : B_ERROR; 238 } 239 240 241 void 242 MouseDevice::Stop() 243 { 244 MD_CALLED(); 245 246 fActive = false; 247 // this will stop the thread as soon as it reads the next packet 248 249 close(fDevice); 250 fDevice = -1; 251 252 if (fThread >= B_OK) { 253 // unblock the thread, which might wait on a semaphore. 254 suspend_thread(fThread); 255 resume_thread(fThread); 256 257 status_t dummy; 258 wait_for_thread(fThread, &dummy); 259 } 260 } 261 262 263 status_t 264 MouseDevice::UpdateSettings() 265 { 266 MD_CALLED(); 267 268 if (fThread < 0) 269 return B_ERROR; 270 271 // trigger updating the settings in the control thread 272 fUpdateSettings = true; 273 274 return B_OK; 275 } 276 277 278 status_t 279 MouseDevice::UpdateTouchpadSettings(const BMessage* message) 280 { 281 if (!fIsTouchpad) 282 return B_BAD_TYPE; 283 if (fThread < 0) 284 return B_ERROR; 285 286 BAutolock _(fTouchpadSettingsLock); 287 288 // trigger updating the settings in the control thread 289 fUpdateSettings = true; 290 291 delete fTouchpadSettingsMessage; 292 fTouchpadSettingsMessage = new BMessage(*message); 293 294 return B_OK; 295 } 296 297 298 char* 299 MouseDevice::_BuildShortName() const 300 { 301 BString string(fPath); 302 BString name; 303 304 int32 slash = string.FindLast("/"); 305 string.CopyInto(name, slash + 1, string.Length() - slash); 306 int32 index = atoi(name.String()) + 1; 307 308 int32 previousSlash = string.FindLast("/", slash); 309 string.CopyInto(name, previousSlash + 1, slash - previousSlash - 1); 310 311 if (name == "ps2") 312 name = "PS/2"; 313 314 if (name.Length() < 4) 315 name.ToUpper(); 316 else 317 name.Capitalize(); 318 319 if (string.FindFirst("touchpad") >= 0) { 320 name << " Touchpad "; 321 } else { 322 if (string.FindFirst("intelli") >= 0) 323 name.Prepend("Extended "); 324 325 name << " Mouse "; 326 } 327 name << index; 328 329 return strdup(name.String()); 330 } 331 332 333 // #pragma mark - control thread 334 335 336 status_t 337 MouseDevice::_ControlThreadEntry(void* arg) 338 { 339 MouseDevice* device = (MouseDevice*)arg; 340 device->_ControlThread(); 341 return B_OK; 342 } 343 344 345 void 346 MouseDevice::_ControlThread() 347 { 348 MD_CALLED(); 349 350 if (fDevice < 0) { 351 _ControlThreadCleanup(); 352 // TOAST! 353 return; 354 } 355 356 // touchpad settings 357 if (ioctl(fDevice, MS_IS_TOUCHPAD, NULL) == B_OK) { 358 TRACE("is touchpad %s\n", fPath.String()); 359 fIsTouchpad = true; 360 361 fTouchpadSettings = kDefaultTouchpadSettings; 362 363 BPath path; 364 status_t status = _GetTouchpadSettingsPath(path); 365 BFile settingsFile(path.Path(), B_READ_ONLY); 366 if (status == B_OK && settingsFile.InitCheck() == B_OK) { 367 if (settingsFile.Read(&fTouchpadSettings, sizeof(touchpad_settings)) 368 != sizeof(touchpad_settings)) { 369 TRACE("failed to load settings\n"); 370 } 371 } 372 _UpdateTouchpadSettings(); 373 } 374 375 _UpdateSettings(); 376 377 uint32 lastButtons = 0; 378 float historyDeltaX = 0.0; 379 float historyDeltaY = 0.0; 380 381 static const bigtime_t kTransferDelay = 1000000 / 125; 382 // 125 transfers per second should be more than enough 383 #define USE_REGULAR_INTERVAL 1 384 #if USE_REGULAR_INTERVAL 385 bigtime_t nextTransferTime = system_time() + kTransferDelay; 386 #endif 387 388 while (fActive) { 389 mouse_movement movements; 390 391 #if USE_REGULAR_INTERVAL 392 snooze_until(nextTransferTime, B_SYSTEM_TIMEBASE); 393 nextTransferTime += kTransferDelay; 394 #endif 395 396 if (ioctl(fDevice, MS_READ, &movements) != B_OK) { 397 _ControlThreadCleanup(); 398 // TOAST! 399 return; 400 } 401 402 // take care of updating the settings first, if necessary 403 if (fUpdateSettings) { 404 fUpdateSettings = false; 405 if (fIsTouchpad) { 406 BAutolock _(fTouchpadSettingsLock); 407 if (fTouchpadSettingsMessage != NULL) { 408 _ReadTouchpadSettingsMsg(fTouchpadSettingsMessage); 409 _UpdateTouchpadSettings(); 410 delete fTouchpadSettingsMessage; 411 fTouchpadSettingsMessage = NULL; 412 } else 413 _UpdateSettings(); 414 } else 415 _UpdateSettings(); 416 } 417 418 uint32 buttons = lastButtons ^ movements.buttons; 419 420 uint32 remappedButtons = _RemapButtons(movements.buttons); 421 int32 deltaX, deltaY; 422 _ComputeAcceleration(movements, deltaX, deltaY, historyDeltaX, 423 historyDeltaY); 424 425 LOG_EVENT("%s: buttons: 0x%lx, x: %ld, y: %ld, clicks:%ld, " 426 "wheel_x:%ld, wheel_y:%ld\n", 427 fDeviceRef.name, movements.buttons, 428 movements.xdelta, movements.ydelta, movements.clicks, 429 movements.wheel_xdelta, movements.wheel_ydelta); 430 LOG_EVENT("%s: x: %ld, y: %ld (%.4f, %.4f)\n", fDeviceRef.name, 431 deltaX, deltaY, historyDeltaX, historyDeltaY); 432 433 // Send single messages for each event 434 435 if (buttons != 0) { 436 bool pressedButton = (buttons & movements.buttons) > 0; 437 BMessage* message = _BuildMouseMessage( 438 pressedButton ? B_MOUSE_DOWN : B_MOUSE_UP, 439 movements.timestamp, remappedButtons, deltaX, deltaY); 440 if (message != NULL) { 441 if (pressedButton) { 442 message->AddInt32("clicks", movements.clicks); 443 LOG_EVENT("B_MOUSE_DOWN\n"); 444 } else 445 LOG_EVENT("B_MOUSE_UP\n"); 446 447 fTarget.EnqueueMessage(message); 448 lastButtons = movements.buttons; 449 } 450 } 451 452 if (movements.xdelta != 0 || movements.ydelta != 0) { 453 BMessage* message = _BuildMouseMessage(B_MOUSE_MOVED, 454 movements.timestamp, remappedButtons, deltaX, deltaY); 455 if (message != NULL) 456 fTarget.EnqueueMessage(message); 457 } 458 459 if ((movements.wheel_ydelta != 0) || (movements.wheel_xdelta != 0)) { 460 BMessage* message = new BMessage(B_MOUSE_WHEEL_CHANGED); 461 if (message == NULL) 462 continue; 463 464 if (message->AddInt64("when", movements.timestamp) == B_OK 465 && message->AddFloat("be:wheel_delta_x", 466 movements.wheel_xdelta) == B_OK 467 && message->AddFloat("be:wheel_delta_y", 468 movements.wheel_ydelta) == B_OK) 469 fTarget.EnqueueMessage(message); 470 else 471 delete message; 472 } 473 474 #if !USE_REGULAR_INTERVAL 475 snooze(kTransferDelay); 476 #endif 477 } 478 } 479 480 481 void 482 MouseDevice::_ControlThreadCleanup() 483 { 484 // NOTE: Only executed when the control thread detected an error 485 // and from within the control thread! 486 487 if (fActive) { 488 fThread = -1; 489 fTarget._RemoveDevice(fPath.String()); 490 } else { 491 // In case active is already false, another thread 492 // waits for this thread to quit, and may already hold 493 // locks that _RemoveDevice() wants to acquire. In another 494 // words, the device is already being removed, so we simply 495 // quit here. 496 } 497 } 498 499 500 void 501 MouseDevice::_UpdateSettings() 502 { 503 MD_CALLED(); 504 505 // retrieve current values 506 507 if (get_mouse_map(&fSettings.map) != B_OK) 508 LOG_ERR("error when get_mouse_map\n"); 509 else { 510 fDeviceRemapsButtons 511 = ioctl(fDevice, MS_SET_MAP, &fSettings.map) == B_OK; 512 } 513 514 if (get_click_speed(&fSettings.click_speed) != B_OK) 515 LOG_ERR("error when get_click_speed\n"); 516 else 517 ioctl(fDevice, MS_SET_CLICKSPEED, &fSettings.click_speed); 518 519 if (get_mouse_speed(&fSettings.accel.speed) != B_OK) 520 LOG_ERR("error when get_mouse_speed\n"); 521 else { 522 if (get_mouse_acceleration(&fSettings.accel.accel_factor) != B_OK) 523 LOG_ERR("error when get_mouse_acceleration\n"); 524 else { 525 mouse_accel accel; 526 ioctl(fDevice, MS_GET_ACCEL, &accel); 527 accel.speed = fSettings.accel.speed; 528 accel.accel_factor = fSettings.accel.accel_factor; 529 ioctl(fDevice, MS_SET_ACCEL, &fSettings.accel); 530 } 531 } 532 533 if (get_mouse_type(&fSettings.type) != B_OK) 534 LOG_ERR("error when get_mouse_type\n"); 535 else 536 ioctl(fDevice, MS_SET_TYPE, &fSettings.type); 537 } 538 539 540 status_t 541 MouseDevice::_GetTouchpadSettingsPath(BPath& path) 542 { 543 status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path); 544 if (status < B_OK) 545 return status; 546 return path.Append(TOUCHPAD_SETTINGS_FILE); 547 } 548 549 550 status_t 551 MouseDevice::_ReadTouchpadSettingsMsg(BMessage* message) 552 { 553 message->FindBool("scroll_twofinger", &fTouchpadSettings.scroll_twofinger); 554 message->FindBool("scroll_twofinger_horizontal", 555 &fTouchpadSettings.scroll_twofinger_horizontal); 556 message->FindFloat("scroll_rightrange", 557 &fTouchpadSettings.scroll_rightrange); 558 message->FindFloat("scroll_bottomrange", 559 &fTouchpadSettings.scroll_bottomrange); 560 561 message->FindInt16("scroll_xstepsize", 562 (int16*)&fTouchpadSettings.scroll_xstepsize); 563 message->FindInt16("scroll_ystepsize", 564 (int16*)&fTouchpadSettings.scroll_ystepsize); 565 message->FindInt8("scroll_acceleration", 566 (int8*)&fTouchpadSettings.scroll_acceleration); 567 message->FindInt8("tapgesture_sensibility", 568 (int8*)&fTouchpadSettings.tapgesture_sensibility); 569 570 return B_OK; 571 } 572 573 574 status_t 575 MouseDevice::_UpdateTouchpadSettings() 576 { 577 if (fIsTouchpad) { 578 ioctl(fDevice, MS_SET_TOUCHPAD_SETTINGS, &fTouchpadSettings); 579 return B_OK; 580 } 581 return B_ERROR; 582 } 583 584 585 BMessage* 586 MouseDevice::_BuildMouseMessage(uint32 what, uint64 when, uint32 buttons, 587 int32 deltaX, int32 deltaY) const 588 { 589 BMessage* message = new BMessage(what); 590 if (message == NULL) 591 return NULL; 592 593 if (message->AddInt64("when", when) < B_OK 594 || message->AddInt32("buttons", buttons) < B_OK 595 || message->AddInt32("x", deltaX) < B_OK 596 || message->AddInt32("y", deltaY) < B_OK) { 597 delete message; 598 return NULL; 599 } 600 601 return message; 602 } 603 604 605 void 606 MouseDevice::_ComputeAcceleration(const mouse_movement& movements, 607 int32& _deltaX, int32& _deltaY, float& historyDeltaX, 608 float& historyDeltaY) const 609 { 610 // basic mouse speed 611 float deltaX = (float)movements.xdelta * fSettings.accel.speed / 65536.0 612 + historyDeltaX; 613 float deltaY = (float)movements.ydelta * fSettings.accel.speed / 65536.0 614 + historyDeltaY; 615 616 // acceleration 617 double acceleration = 1; 618 if (fSettings.accel.accel_factor) { 619 acceleration = 1 + sqrt(deltaX * deltaX + deltaY * deltaY) 620 * fSettings.accel.accel_factor / 524288.0; 621 } 622 623 deltaX *= acceleration; 624 deltaY *= acceleration; 625 626 if (deltaX >= 0) 627 _deltaX = (int32)floorf(deltaX); 628 else 629 _deltaX = (int32)ceilf(deltaX); 630 631 if (deltaY >= 0) 632 _deltaY = (int32)floorf(deltaY); 633 else 634 _deltaY = (int32)ceilf(deltaY); 635 636 historyDeltaX = deltaX - _deltaX; 637 historyDeltaY = deltaY - _deltaY; 638 } 639 640 641 uint32 642 MouseDevice::_RemapButtons(uint32 buttons) const 643 { 644 if (fDeviceRemapsButtons) 645 return buttons; 646 647 uint32 newButtons = 0; 648 for (int32 i = 0; buttons; i++) { 649 if (buttons & 0x1) { 650 #if defined(HAIKU_TARGET_PLATFORM_HAIKU) || defined(HAIKU_TARGET_PLATFORM_DANO) 651 newButtons |= fSettings.map.button[i]; 652 #else 653 if (i == 0) 654 newButtons |= fSettings.map.left; 655 if (i == 1) 656 newButtons |= fSettings.map.right; 657 if (i == 2) 658 newButtons |= fSettings.map.middle; 659 #endif 660 } 661 buttons >>= 1; 662 } 663 664 return newButtons; 665 } 666 667 668 // #pragma mark - 669 670 671 MouseInputDevice::MouseInputDevice() 672 : 673 fDevices(2, true), 674 fDeviceListLock("MouseInputDevice list") 675 { 676 MID_CALLED(); 677 678 StartMonitoringDevice(kMouseDevicesDirectory); 679 StartMonitoringDevice(kTouchpadDevicesDirectory); 680 _RecursiveScan(kMouseDevicesDirectory); 681 _RecursiveScan(kTouchpadDevicesDirectory); 682 } 683 684 685 MouseInputDevice::~MouseInputDevice() 686 { 687 MID_CALLED(); 688 689 StopMonitoringDevice(kTouchpadDevicesDirectory); 690 StopMonitoringDevice(kMouseDevicesDirectory); 691 fDevices.MakeEmpty(); 692 } 693 694 695 status_t 696 MouseInputDevice::InitCheck() 697 { 698 MID_CALLED(); 699 700 return BInputServerDevice::InitCheck(); 701 } 702 703 704 status_t 705 MouseInputDevice::Start(const char* name, void* cookie) 706 { 707 MID_CALLED(); 708 709 MouseDevice* device = (MouseDevice*)cookie; 710 711 return device->Start(); 712 } 713 714 715 status_t 716 MouseInputDevice::Stop(const char* name, void* cookie) 717 { 718 TRACE("%s(%s)\n", __PRETTY_FUNCTION__, name); 719 720 MouseDevice* device = (MouseDevice*)cookie; 721 device->Stop(); 722 723 return B_OK; 724 } 725 726 727 status_t 728 MouseInputDevice::Control(const char* name, void* cookie, 729 uint32 command, BMessage* message) 730 { 731 TRACE("%s(%s, code: %lu)\n", __PRETTY_FUNCTION__, name, command); 732 733 MouseDevice* device = (MouseDevice*)cookie; 734 735 if (command == B_NODE_MONITOR) 736 return _HandleMonitor(message); 737 738 if (command == MS_SET_TOUCHPAD_SETTINGS) 739 return device->UpdateTouchpadSettings(message); 740 741 if (command >= B_MOUSE_TYPE_CHANGED 742 && command <= B_MOUSE_ACCELERATION_CHANGED) 743 return device->UpdateSettings(); 744 745 return B_BAD_VALUE; 746 } 747 748 749 status_t 750 MouseInputDevice::_HandleMonitor(BMessage* message) 751 { 752 MID_CALLED(); 753 754 const char* path; 755 int32 opcode; 756 if (message->FindInt32("opcode", &opcode) != B_OK 757 || (opcode != B_ENTRY_CREATED && opcode != B_ENTRY_REMOVED) 758 || message->FindString("path", &path) != B_OK) 759 return B_BAD_VALUE; 760 761 if (opcode == B_ENTRY_CREATED) 762 return _AddDevice(path); 763 764 #if 0 765 return _RemoveDevice(path); 766 #else 767 // Don't handle B_ENTRY_REMOVED, let the control thread take care of it. 768 return B_OK; 769 #endif 770 } 771 772 773 void 774 MouseInputDevice::_RecursiveScan(const char* directory) 775 { 776 MID_CALLED(); 777 778 BEntry entry; 779 BDirectory dir(directory); 780 while (dir.GetNextEntry(&entry) == B_OK) { 781 BPath path; 782 entry.GetPath(&path); 783 784 if (!strcmp(path.Leaf(), "serial")) { 785 // skip serial 786 continue; 787 } 788 789 if (entry.IsDirectory()) 790 _RecursiveScan(path.Path()); 791 else 792 _AddDevice(path.Path()); 793 } 794 } 795 796 797 MouseDevice* 798 MouseInputDevice::_FindDevice(const char* path) const 799 { 800 MID_CALLED(); 801 802 for (int32 i = fDevices.CountItems() - 1; i >= 0; i--) { 803 MouseDevice* device = fDevices.ItemAt(i); 804 if (strcmp(device->Path(), path) == 0) 805 return device; 806 } 807 808 return NULL; 809 } 810 811 812 status_t 813 MouseInputDevice::_AddDevice(const char* path) 814 { 815 MID_CALLED(); 816 817 BAutolock _(fDeviceListLock); 818 819 _RemoveDevice(path); 820 821 MouseDevice* device = new(std::nothrow) MouseDevice(*this, path); 822 if (!device) { 823 TRACE("No memory\n"); 824 return B_NO_MEMORY; 825 } 826 827 if (!fDevices.AddItem(device)) { 828 TRACE("No memory in list\n"); 829 delete device; 830 return B_NO_MEMORY; 831 } 832 833 input_device_ref* devices[2]; 834 devices[0] = device->DeviceRef(); 835 devices[1] = NULL; 836 837 TRACE("adding path: %s, name: %s\n", path, devices[0]->name); 838 839 return RegisterDevices(devices); 840 } 841 842 843 status_t 844 MouseInputDevice::_RemoveDevice(const char* path) 845 { 846 MID_CALLED(); 847 848 BAutolock _(fDeviceListLock); 849 850 MouseDevice* device = _FindDevice(path); 851 if (device == NULL) { 852 TRACE("%s not found\n", path); 853 return B_ENTRY_NOT_FOUND; 854 } 855 856 input_device_ref* devices[2]; 857 devices[0] = device->DeviceRef(); 858 devices[1] = NULL; 859 860 TRACE("removing path: %s, name: %s\n", path, devices[0]->name); 861 862 UnregisterDevices(devices); 863 864 fDevices.RemoveItem(device); 865 866 return B_OK; 867 } 868