xref: /haiku/src/add-ons/input_server/devices/keyboard/KeyboardInputDevice.h (revision 1acbe440b8dd798953bec31d18ee589aa3f71b73)
1 /*
2  * Copyright 2004-2006, Jérôme Duval. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef KEYBOARD_INPUT_DEVICE_H
6 #define KEYBOARD_INPUT_DEVICE_H
7 
8 
9 #include "Keymap.h"
10 #include "TMWindow.h"
11 #include "kb_mouse_settings.h"
12 
13 #include <Handler.h>
14 #include <InputServerDevice.h>
15 #include <List.h>
16 
17 #include <stdio.h>
18 
19 
20 class KeyboardInputDevice;
21 
22 struct keyboard_device : public BHandler {
23 	keyboard_device(const char *path);
24 	virtual ~keyboard_device();
25 
26 	virtual void MessageReceived(BMessage* message);
27 	status_t EnqueueInlineInputMethod(int32 opcode, const char* string = NULL,
28 		bool confirmed = false, BMessage* keyDown = NULL);
29 
30 	KeyboardInputDevice *owner;
31 	input_device_ref device_ref;
32 	char path[B_PATH_NAME_LENGTH];
33 	int fd;
34 	thread_id device_watcher;
35 	kb_settings settings;
36 	volatile bool active;
37 	bool isAT;
38 	volatile bool input_method_started;
39 	uint32 modifiers;
40 };
41 
42 
43 class KeyboardInputDevice : public BInputServerDevice {
44 	public:
45 		KeyboardInputDevice();
46 		~KeyboardInputDevice();
47 
48 		virtual status_t InitCheck();
49 
50 		virtual status_t Start(const char *name, void *cookie);
51 		virtual status_t Stop(const char *name, void *cookie);
52 
53 		virtual status_t Control(const char *name, void *cookie,
54 			uint32 command, BMessage *message);
55 
56 		virtual status_t SystemShuttingDown();
57 
58 #ifdef DEBUG
59 		static FILE *sLogFile;
60 #endif
61 
62 	private:
63 		status_t _HandleMonitor(BMessage *message);
64 		status_t _InitFromSettings(void *cookie, uint32 opcode = 0);
65 		void _RecursiveScan(const char *directory);
66 
67 		status_t _AddDevice(const char *path);
68 		status_t _RemoveDevice(const char *path);
69 
70 		static int32 _DeviceWatcher(void *arg);
71 
72 		void _SetLeds(keyboard_device *device);
73 
74 		BList fDevices;
75 		Keymap	fKeymap;
76 		TMWindow *fTMWindow;
77 };
78 
79 extern "C" BInputServerDevice *instantiate_input_device();
80 
81 #if DEBUG
82 	inline void LOG(const char *fmt, ...) { char buf[1024]; va_list ap; va_start(ap, fmt); vsprintf(buf, fmt, ap); va_end(ap); \
83 		fputs(buf, KeyboardInputDevice::sLogFile); fflush(KeyboardInputDevice::sLogFile); }
84 	#define LOG_ERR(text...) LOG(text)
85 #else
86 	#define LOG(text...)
87 	#define LOG_ERR(text...) fprintf(stderr, text)
88 #endif
89 
90 #define CALLED() LOG("%s\n", __PRETTY_FUNCTION__)
91 
92 #endif	// KEYBOARD_INPUT_DEVICE_H
93