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