1/*
2 * Copyright 1999-2009 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Jeremy Friesner
7 */
8
9
10#include "ShortcutsServerFilter.h"
11
12#include <Path.h>
13#include <FindDirectory.h>
14
15#include "KeyCommandMap.h"
16#include "KeyInfos.h"
17#include "CommandExecutor.h"
18#include "ShortcutsFilterConstants.h"
19
20
21class BInputServerFilter;
22
23
24// Called by input_server on startup. Must be exported as a "C" function!
25BInputServerFilter* instantiate_input_filter()
26{
27	return new ShortcutsServerFilter;
28}
29
30
31ShortcutsServerFilter::ShortcutsServerFilter()
32	:
33	fExecutor(new CommandExecutor)
34{
35	BPath path;
36	if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) == B_OK)
37		path.Append(SHORTCUTS_SETTING_FILE_NAME);
38
39	fMap = new KeyCommandMap(path.Path());
40
41	InitKeyIndices();
42	fMessenger = BMessenger(fExecutor);
43	fMap->Run();
44	fExecutor->Run();
45}
46
47
48ShortcutsServerFilter::~ShortcutsServerFilter()
49{
50	if (fMap->Lock())
51		fMap->Quit();
52
53	if (fExecutor->Lock())
54		fExecutor->Quit();
55}
56
57
58status_t
59ShortcutsServerFilter::InitCheck()
60{
61	return B_OK;
62}
63
64
65filter_result
66ShortcutsServerFilter::Filter(BMessage* message, BList* outList)
67{
68	filter_result result = B_DISPATCH_MESSAGE;
69
70	switch(message->what) {
71		case B_KEY_DOWN:
72		case B_KEY_UP:
73		case B_UNMAPPED_KEY_DOWN:
74		case B_UNMAPPED_KEY_UP:
75			result = fMap->KeyEvent(message, outList, fMessenger);
76			break;
77
78		case B_MOUSE_MOVED:
79		case B_MOUSE_UP:
80		case B_MOUSE_DOWN:
81			fMap->MouseMessageReceived(message);
82			break;
83	}
84	fMap->DrainInjectedEvents(message, outList, fMessenger);
85
86	return result;
87}
88