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 close(fDevice); 232 return status; 233 } 234 235 return fDevice >= 0 ? B_OK : B_ERROR; 236 } 237 238 239 void 240 MouseDevice::Stop() 241 { 242 MD_CALLED(); 243 244 fActive = false; 245 // this will stop the thread as soon as it reads the next packet 246 247 close(fDevice); 248 fDevice = -1; 249 250 if (fThread >= B_OK) { 251 // unblock the thread, which might wait on a semaphore. 252 suspend_thread(fThread); 253 resume_thread(fThread); 254 255 status_t dummy; 256 wait_for_thread(fThread, &dummy); 257 } 258 } 259 260 261 status_t 262 MouseDevice::UpdateSettings() 263 { 264 MD_CALLED(); 265 266 if (fThread < 0) 267 return B_ERROR; 268 269 // trigger updating the settings in the control thread 270 fUpdateSettings = true; 271 272 return B_OK; 273 } 274 275 276 status_t 277 MouseDevice::UpdateTouchpadSettings(const BMessage* message) 278 { 279 if (!fIsTouchpad) 280 return B_BAD_TYPE; 281 if (fThread < 0) 282 return B_ERROR; 283 284 BAutolock _(fTouchpadSettingsLock); 285 286 // trigger updating the settings in the control thread 287 fUpdateSettings = true; 288 289 delete fTouchpadSettingsMessage; 290 fTouchpadSettingsMessage = new BMessage(*message); 291 292 return B_OK; 293 } 294 295 296 char* 297 MouseDevice::_BuildShortName() const 298 { 299 BString string(fPath); 300 BString name; 301 302 int32 slash = string.FindLast("/"); 303 string.CopyInto(name, slash + 1, string.Length() - slash); 304 int32 index = atoi(name.String()) + 1; 305 306 int32 previousSlash = string.FindLast("/", slash); 307 string.CopyInto(name, previousSlash + 1, slash - previousSlash - 1); 308 309 if (name == "ps2") 310 name = "PS/2"; 311 312 if (name.Length() < 4) 313 name.ToUpper(); 314 else 315 name.Capitalize(); 316 317 if (string.FindFirst("touchpad") >= 0) { 318 name << " Touchpad "; 319 } else { 320 if (string.FindFirst("intelli") >= 0) 321 name.Prepend("Extended "); 322 323 name << " Mouse "; 324 } 325 name << index; 326 327 return strdup(name.String()); 328 } 329 330 331 // #pragma mark - control thread 332 333 334 status_t 335 MouseDevice::_ControlThreadEntry(void* arg) 336 { 337 MouseDevice* device = (MouseDevice*)arg; 338 device->_ControlThread(); 339 return B_OK; 340 } 341 342 343 void 344 MouseDevice::_ControlThread() 345 { 346 MD_CALLED(); 347 348 if (fDevice < 0) { 349 _ControlThreadCleanup(); 350 // TOAST! 351 return; 352 } 353 354 // touchpad settings 355 if (ioctl(fDevice, MS_IS_TOUCHPAD, NULL) == B_OK) { 356 TRACE("is touchpad %s\n", fPath.String()); 357 fIsTouchpad = true; 358 359 fTouchpadSettings = kDefaultTouchpadSettings; 360 361 BPath path; 362 status_t status = _GetTouchpadSettingsPath(path); 363 BFile settingsFile(path.Path(), B_READ_ONLY); 364 if (status == B_OK && settingsFile.InitCheck() == B_OK) { 365 if (settingsFile.Read(&fTouchpadSettings, sizeof(touchpad_settings)) 366 != sizeof(touchpad_settings)) { 367 TRACE("failed to load settings\n"); 368 } 369 } 370 _UpdateTouchpadSettings(); 371 } 372 373 _UpdateSettings(); 374 375 uint32 lastButtons = 0; 376 float historyDeltaX = 0.0; 377 float historyDeltaY = 0.0; 378 379 static const bigtime_t kTransferDelay = 1000000 / 125; 380 // 125 transfers per second should be more than enough 381 #define USE_REGULAR_INTERVAL 1 382 #if USE_REGULAR_INTERVAL 383 bigtime_t nextTransferTime = system_time() + kTransferDelay; 384 #endif 385 386 while (fActive) { 387 mouse_movement movements; 388 389 #if USE_REGULAR_INTERVAL 390 snooze_until(nextTransferTime, B_SYSTEM_TIMEBASE); 391 nextTransferTime += kTransferDelay; 392 #endif 393 394 if (ioctl(fDevice, MS_READ, &movements) != B_OK) { 395 _ControlThreadCleanup(); 396 // TOAST! 397 return; 398 } 399 400 // take care of updating the settings first, if necessary 401 if (fUpdateSettings) { 402 fUpdateSettings = false; 403 if (fIsTouchpad) { 404 BAutolock _(fTouchpadSettingsLock); 405 if (fTouchpadSettingsMessage != NULL) { 406 _ReadTouchpadSettingsMsg(fTouchpadSettingsMessage); 407 _UpdateTouchpadSettings(); 408 delete fTouchpadSettingsMessage; 409 fTouchpadSettingsMessage = NULL; 410 } else 411 _UpdateSettings(); 412 } else 413 _UpdateSettings(); 414 } 415 416 uint32 buttons = lastButtons ^ movements.buttons; 417 418 uint32 remappedButtons = _RemapButtons(movements.buttons); 419 int32 deltaX, deltaY; 420 _ComputeAcceleration(movements, deltaX, deltaY, historyDeltaX, 421 historyDeltaY); 422 423 LOG_EVENT("%s: buttons: 0x%lx, x: %ld, y: %ld, clicks:%ld, " 424 "wheel_x:%ld, wheel_y:%ld\n", 425 fDeviceRef.name, movements.buttons, 426 movements.xdelta, movements.ydelta, movements.clicks, 427 movements.wheel_xdelta, movements.wheel_ydelta); 428 LOG_EVENT("%s: x: %ld, y: %ld (%.4f, %.4f)\n", fDeviceRef.name, 429 deltaX, deltaY, historyDeltaX, historyDeltaY); 430 431 // Send single messages for each event 432 433 if (buttons != 0) { 434 bool pressedButton = (buttons & movements.buttons) > 0; 435 BMessage* message = _BuildMouseMessage( 436 pressedButton ? B_MOUSE_DOWN : B_MOUSE_UP, 437 movements.timestamp, remappedButtons, deltaX, deltaY); 438 if (message != NULL) { 439 if (pressedButton) { 440 message->AddInt32("clicks", movements.clicks); 441 LOG_EVENT("B_MOUSE_DOWN\n"); 442 } else 443 LOG_EVENT("B_MOUSE_UP\n"); 444 445 fTarget.EnqueueMessage(message); 446 lastButtons = movements.buttons; 447 } 448 } 449 450 if (movements.xdelta != 0 || movements.ydelta != 0) { 451 BMessage* message = _BuildMouseMessage(B_MOUSE_MOVED, 452 movements.timestamp, remappedButtons, deltaX, deltaY); 453 if (message != NULL) 454 fTarget.EnqueueMessage(message); 455 } 456 457 if ((movements.wheel_ydelta != 0) || (movements.wheel_xdelta != 0)) { 458 BMessage* message = new BMessage(B_MOUSE_WHEEL_CHANGED); 459 if (message == NULL) 460 continue; 461 462 if (message->AddInt64("when", movements.timestamp) == B_OK 463 && message->AddFloat("be:wheel_delta_x", 464 movements.wheel_xdelta) == B_OK 465 && message->AddFloat("be:wheel_delta_y", 466 movements.wheel_ydelta) == B_OK) 467 fTarget.EnqueueMessage(message); 468 else 469 delete message; 470 } 471 472 #if !USE_REGULAR_INTERVAL 473 snooze(kTransferDelay); 474 #endif 475 } 476 } 477 478 479 void 480 MouseDevice::_ControlThreadCleanup() 481 { 482 // NOTE: Only executed when the control thread detected an error 483 // and from within the control thread! 484 485 if (fActive) { 486 fThread = -1; 487 fTarget._RemoveDevice(fPath.String()); 488 } else { 489 // In case active is already false, another thread 490 // waits for this thread to quit, and may already hold 491 // locks that _RemoveDevice() wants to acquire. In another 492 // words, the device is already being removed, so we simply 493 // quit here. 494 } 495 } 496 497 498 void 499 MouseDevice::_UpdateSettings() 500 { 501 MD_CALLED(); 502 503 // retrieve current values 504 505 if (get_mouse_map(&fSettings.map) != B_OK) 506 LOG_ERR("error when get_mouse_map\n"); 507 else { 508 fDeviceRemapsButtons 509 = ioctl(fDevice, MS_SET_MAP, &fSettings.map) == B_OK; 510 } 511 512 if (get_click_speed(&fSettings.click_speed) != B_OK) 513 LOG_ERR("error when get_click_speed\n"); 514 else 515 ioctl(fDevice, MS_SET_CLICKSPEED, &fSettings.click_speed); 516 517 if (get_mouse_speed(&fSettings.accel.speed) != B_OK) 518 LOG_ERR("error when get_mouse_speed\n"); 519 else { 520 if (get_mouse_acceleration(&fSettings.accel.accel_factor) != B_OK) 521 LOG_ERR("error when get_mouse_acceleration\n"); 522 else { 523 mouse_accel accel; 524 ioctl(fDevice, MS_GET_ACCEL, &accel); 525 accel.speed = fSettings.accel.speed; 526 accel.accel_factor = fSettings.accel.accel_factor; 527 ioctl(fDevice, MS_SET_ACCEL, &fSettings.accel); 528 } 529 } 530 531 if (get_mouse_type(&fSettings.type) != B_OK) 532 LOG_ERR("error when get_mouse_type\n"); 533 else 534 ioctl(fDevice, MS_SET_TYPE, &fSettings.type); 535 } 536 537 538 status_t 539 MouseDevice::_GetTouchpadSettingsPath(BPath& path) 540 { 541 status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &path); 542 if (status < B_OK) 543 return status; 544 return path.Append(TOUCHPAD_SETTINGS_FILE); 545 } 546 547 548 status_t 549 MouseDevice::_ReadTouchpadSettingsMsg(BMessage* message) 550 { 551 message->FindBool("scroll_twofinger", &fTouchpadSettings.scroll_twofinger); 552 message->FindBool("scroll_twofinger_horizontal", 553 &fTouchpadSettings.scroll_twofinger_horizontal); 554 message->FindFloat("scroll_rightrange", 555 &fTouchpadSettings.scroll_rightrange); 556 message->FindFloat("scroll_bottomrange", 557 &fTouchpadSettings.scroll_bottomrange); 558 559 message->FindInt16("scroll_xstepsize", 560 (int16*)&fTouchpadSettings.scroll_xstepsize); 561 message->FindInt16("scroll_ystepsize", 562 (int16*)&fTouchpadSettings.scroll_ystepsize); 563 message->FindInt8("scroll_acceleration", 564 (int8*)&fTouchpadSettings.scroll_acceleration); 565 message->FindInt8("tapgesture_sensibility", 566 (int8*)&fTouchpadSettings.tapgesture_sensibility); 567 568 return B_OK; 569 } 570 571 572 status_t 573 MouseDevice::_UpdateTouchpadSettings() 574 { 575 if (fIsTouchpad) { 576 ioctl(fDevice, MS_SET_TOUCHPAD_SETTINGS, &fTouchpadSettings); 577 return B_OK; 578 } 579 return B_ERROR; 580 } 581 582 583 BMessage* 584 MouseDevice::_BuildMouseMessage(uint32 what, uint64 when, uint32 buttons, 585 int32 deltaX, int32 deltaY) const 586 { 587 BMessage* message = new BMessage(what); 588 if (message == NULL) 589 return NULL; 590 591 if (message->AddInt64("when", when) < B_OK 592 || message->AddInt32("buttons", buttons) < B_OK 593 || message->AddInt32("x", deltaX) < B_OK 594 || message->AddInt32("y", deltaY) < B_OK) { 595 delete message; 596 return NULL; 597 } 598 599 return message; 600 } 601 602 603 void 604 MouseDevice::_ComputeAcceleration(const mouse_movement& movements, 605 int32& _deltaX, int32& _deltaY, float& historyDeltaX, 606 float& historyDeltaY) const 607 { 608 // basic mouse speed 609 float deltaX = (float)movements.xdelta * fSettings.accel.speed / 65536.0 610 + historyDeltaX; 611 float deltaY = (float)movements.ydelta * fSettings.accel.speed / 65536.0 612 + historyDeltaY; 613 614 // acceleration 615 double acceleration = 1; 616 if (fSettings.accel.accel_factor) { 617 acceleration = 1 + sqrt(deltaX * deltaX + deltaY * deltaY) 618 * fSettings.accel.accel_factor / 524288.0; 619 } 620 621 deltaX *= acceleration; 622 deltaY *= acceleration; 623 624 if (deltaX >= 0) 625 _deltaX = (int32)floorf(deltaX); 626 else 627 _deltaX = (int32)ceilf(deltaX); 628 629 if (deltaY >= 0) 630 _deltaY = (int32)floorf(deltaY); 631 else 632 _deltaY = (int32)ceilf(deltaY); 633 634 historyDeltaX = deltaX - _deltaX; 635 historyDeltaY = deltaY - _deltaY; 636 } 637 638 639 uint32 640 MouseDevice::_RemapButtons(uint32 buttons) const 641 { 642 if (fDeviceRemapsButtons) 643 return buttons; 644 645 uint32 newButtons = 0; 646 for (int32 i = 0; buttons; i++) { 647 if (buttons & 0x1) { 648 #if defined(HAIKU_TARGET_PLATFORM_HAIKU) || defined(HAIKU_TARGET_PLATFORM_DANO) 649 newButtons |= fSettings.map.button[i]; 650 #else 651 if (i == 0) 652 newButtons |= fSettings.map.left; 653 if (i == 1) 654 newButtons |= fSettings.map.right; 655 if (i == 2) 656 newButtons |= fSettings.map.middle; 657 #endif 658 } 659 buttons >>= 1; 660 } 661 662 return newButtons; 663 } 664 665 666 // #pragma mark - 667 668 669 MouseInputDevice::MouseInputDevice() 670 : 671 fDevices(2, true), 672 fDeviceListLock("MouseInputDevice list") 673 { 674 MID_CALLED(); 675 676 StartMonitoringDevice(kMouseDevicesDirectory); 677 StartMonitoringDevice(kTouchpadDevicesDirectory); 678 _RecursiveScan(kMouseDevicesDirectory); 679 _RecursiveScan(kTouchpadDevicesDirectory); 680 } 681 682 683 MouseInputDevice::~MouseInputDevice() 684 { 685 MID_CALLED(); 686 687 StopMonitoringDevice(kTouchpadDevicesDirectory); 688 StopMonitoringDevice(kMouseDevicesDirectory); 689 fDevices.MakeEmpty(); 690 } 691 692 693 status_t 694 MouseInputDevice::InitCheck() 695 { 696 MID_CALLED(); 697 698 return BInputServerDevice::InitCheck(); 699 } 700 701 702 status_t 703 MouseInputDevice::Start(const char* name, void* cookie) 704 { 705 MID_CALLED(); 706 707 MouseDevice* device = (MouseDevice*)cookie; 708 709 return device->Start(); 710 } 711 712 713 status_t 714 MouseInputDevice::Stop(const char* name, void* cookie) 715 { 716 TRACE("%s(%s)\n", __PRETTY_FUNCTION__, name); 717 718 MouseDevice* device = (MouseDevice*)cookie; 719 device->Stop(); 720 721 return B_OK; 722 } 723 724 725 status_t 726 MouseInputDevice::Control(const char* name, void* cookie, 727 uint32 command, BMessage* message) 728 { 729 TRACE("%s(%s, code: %lu)\n", __PRETTY_FUNCTION__, name, command); 730 731 MouseDevice* device = (MouseDevice*)cookie; 732 733 if (command == B_NODE_MONITOR) 734 return _HandleMonitor(message); 735 736 if (command == MS_SET_TOUCHPAD_SETTINGS) 737 return device->UpdateTouchpadSettings(message); 738 739 if (command >= B_MOUSE_TYPE_CHANGED 740 && command <= B_MOUSE_ACCELERATION_CHANGED) 741 return device->UpdateSettings(); 742 743 return B_BAD_VALUE; 744 } 745 746 747 status_t 748 MouseInputDevice::_HandleMonitor(BMessage* message) 749 { 750 MID_CALLED(); 751 752 const char* path; 753 int32 opcode; 754 if (message->FindInt32("opcode", &opcode) != B_OK 755 || (opcode != B_ENTRY_CREATED && opcode != B_ENTRY_REMOVED) 756 || message->FindString("path", &path) != B_OK) 757 return B_BAD_VALUE; 758 759 if (opcode == B_ENTRY_CREATED) 760 return _AddDevice(path); 761 762 #if 0 763 return _RemoveDevice(path); 764 #else 765 // Don't handle B_ENTRY_REMOVED, let the control thread take care of it. 766 return B_OK; 767 #endif 768 } 769 770 771 void 772 MouseInputDevice::_RecursiveScan(const char* directory) 773 { 774 MID_CALLED(); 775 776 BEntry entry; 777 BDirectory dir(directory); 778 while (dir.GetNextEntry(&entry) == B_OK) { 779 BPath path; 780 entry.GetPath(&path); 781 782 if (!strcmp(path.Leaf(), "serial")) { 783 // skip serial 784 continue; 785 } 786 787 if (entry.IsDirectory()) 788 _RecursiveScan(path.Path()); 789 else 790 _AddDevice(path.Path()); 791 } 792 } 793 794 795 MouseDevice* 796 MouseInputDevice::_FindDevice(const char* path) const 797 { 798 MID_CALLED(); 799 800 for (int32 i = fDevices.CountItems() - 1; i >= 0; i--) { 801 MouseDevice* device = fDevices.ItemAt(i); 802 if (strcmp(device->Path(), path) == 0) 803 return device; 804 } 805 806 return NULL; 807 } 808 809 810 status_t 811 MouseInputDevice::_AddDevice(const char* path) 812 { 813 MID_CALLED(); 814 815 BAutolock _(fDeviceListLock); 816 817 _RemoveDevice(path); 818 819 MouseDevice* device = new(std::nothrow) MouseDevice(*this, path); 820 if (!device) { 821 TRACE("No memory\n"); 822 return B_NO_MEMORY; 823 } 824 825 if (!fDevices.AddItem(device)) { 826 TRACE("No memory in list\n"); 827 delete device; 828 return B_NO_MEMORY; 829 } 830 831 input_device_ref* devices[2]; 832 devices[0] = device->DeviceRef(); 833 devices[1] = NULL; 834 835 TRACE("adding path: %s, name: %s\n", path, devices[0]->name); 836 837 return RegisterDevices(devices); 838 } 839 840 841 status_t 842 MouseInputDevice::_RemoveDevice(const char* path) 843 { 844 MID_CALLED(); 845 846 BAutolock _(fDeviceListLock); 847 848 MouseDevice* device = _FindDevice(path); 849 if (device == NULL) { 850 TRACE("%s not found\n", path); 851 return B_ENTRY_NOT_FOUND; 852 } 853 854 input_device_ref* devices[2]; 855 devices[0] = device->DeviceRef(); 856 devices[1] = NULL; 857 858 TRACE("removing path: %s, name: %s\n", path, devices[0]->name); 859 860 UnregisterDevices(devices); 861 862 fDevices.RemoveItem(device); 863 864 return B_OK; 865 } 866