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
13#include <Path.h>
14#include <FindDirectory.h>
15
16
17#include "KeyCommandMap.h"
18#include "KeyInfos.h"
19#include "CommandExecutor.h"
20#include "ShortcutsFilterConstants.h"
21
22class BInputServerFilter;
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_NO_ERROR;
62}
63
64
65filter_result
66ShortcutsServerFilter::Filter(BMessage* msg, BList* outlist)
67{
68	filter_result ret = B_DISPATCH_MESSAGE;
69
70	switch(msg->what)
71	{
72		case B_KEY_DOWN:
73		case B_KEY_UP:
74		case B_UNMAPPED_KEY_DOWN:
75		case B_UNMAPPED_KEY_UP:
76			ret = fMap->KeyEvent(msg, outlist, fMessenger);
77		break;
78
79		case B_MOUSE_MOVED:
80		case B_MOUSE_UP:
81		case B_MOUSE_DOWN:
82			fMap->MouseMoved(msg);
83		break;
84	}
85	fMap->DrainInjectedEvents(msg, outlist, fMessenger);
86	return ret;
87}
88