1 /** @file b_main.cpp Event and device state bindings system.
2 *
3 * @todo Pretty much everything in this file belongs in InputSystem.
4 *
5 * @authors Copyright © 2009-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
6 * @authors Copyright © 2007-2014 Daniel Swanson <danij@dengine.net>
7 *
8 * @par License
9 * GPL: http://www.gnu.org/licenses/gpl.html
10 *
11 * <small>This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version. This program is distributed in the hope that it
15 * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
17 * Public License for more details. You should have received a copy of the GNU
18 * General Public License along with this program; if not, see:
19 * http://www.gnu.org/licenses</small>
20 */
21
22 #include "ui/b_main.h"
23 #include "ui/inputsystem.h"
24
25 #include "dd_main.h" // App_GameLoaded
26 #include "dd_def.h"
27 #include "clientapp.h"
28 #include "BindContext"
29
30 using namespace de;
31
32 /**
33 * Binding context fallback for the "global" context.
34 *
35 * @param ddev Event being processed.
36 *
37 * @return @c true if the event was eaten and can be processed by the rest of the
38 * binding context stack.
39 */
globalContextFallback(ddevent_t const * ddev)40 static int globalContextFallback(ddevent_t const *ddev)
41 {
42 if (App_GameLoaded() && !BusyMode_Active())
43 {
44 event_t ev;
45 if (InputSystem::convertEvent(*ddev, ev))
46 {
47 // The game's normal responder only returns true if the bindings can't
48 // be used (like when chatting). Note that if the event is eaten here,
49 // the rest of the bindings contexts won't get a chance to process the
50 // event.
51 if (gx.Responder && gx.Responder(&ev))
52 {
53 return true;
54 }
55 }
56 }
57
58 return false;
59 }
60
61 /// @note Called once on init.
B_Init()62 void B_Init()
63 {
64 InputSystem &isys = ClientApp::inputSystem();
65
66 // In dedicated mode we have fewer binding contexts available.
67
68 // The contexts are defined in reverse order, with the context of lowest
69 // priority defined first.
70
71 isys.newContext(DEFAULT_BINDING_CONTEXT_NAME);
72
73 // Game contexts.
74 /// @todo Game binding context setup obviously belong to the game plugin, so shouldn't be here.
75 isys.newContext("map");
76 isys.newContext("map-freepan");
77 isys.newContext("finale"); // uses a fallback responder to handle script events
78 isys.newContext("menu")->acquireAll();
79 isys.newContext("gameui");
80 isys.newContext("shortcut");
81 isys.newContext("chat")->acquire(IDEV_KEYBOARD);
82 isys.newContext("message")->acquireAll();
83
84 // Binding context for the console.
85 BindContext *bc = isys.newContext(CONSOLE_BINDING_CONTEXT_NAME);
86 bc->protect(); // Only we can (de)activate.
87 bc->acquire(IDEV_KEYBOARD); // Console takes over all keyboard events.
88
89 // UI doesn't let anything past it.
90 isys.newContext(UI_BINDING_CONTEXT_NAME)->acquireAll();
91
92 // Top-level context that is always active and overrides every other context.
93 // To be used only for system-level functionality.
94 bc = isys.newContext(GLOBAL_BINDING_CONTEXT_NAME);
95 bc->protect();
96 bc->setDDFallbackResponder(globalContextFallback);
97 bc->activate();
98
99 /*
100 B_BindCommand("joy-hat-angle3", "print {angle 3}");
101 B_BindCommand("joy-hat-center", "print center");
102
103 B_BindCommand("game:key-m-press", "print hello");
104 B_BindCommand("key-codex20-up", "print {space released}");
105 B_BindCommand("key-up-down + key-shift + key-ctrl", "print \"shifted and controlled up\"");
106 B_BindCommand("key-up-down + key-shift", "print \"shifted up\"");
107 B_BindCommand("mouse-left", "print mbpress");
108 B_BindCommand("mouse-right-up", "print \"right mb released\"");
109 B_BindCommand("joy-x-neg1.0 + key-ctrl-up", "print \"joy x negative without ctrl\"");
110 B_BindCommand("joy-x- within 0.1 + joy-y-pos1", "print \"joy x centered\"");
111 B_BindCommand("joy-x-pos1.0", "print \"joy x positive\"");
112 B_BindCommand("joy-x-neg1.0", "print \"joy x negative\"");
113 B_BindCommand("joy-z-pos1.0", "print \"joy z positive\"");
114 B_BindCommand("joy-z-neg1.0", "print \"joy z negative\"");
115 B_BindCommand("joy-w-pos1.0", "print \"joy w positive\"");
116 B_BindCommand("joy-w-neg1.0", "print \"joy w negative\"");
117 */
118
119 /*B_BindControl("turn", "key-left-staged-inverse");
120 B_BindControl("turn", "key-right-staged");
121 B_BindControl("turn", "mouse-x");
122 B_BindControl("turn", "joy-x + key-shift-up + joy-hat-center + key-code123-down");
123 */
124
125 // Bind all the defaults for the engine only.
126 isys.bindDefaults();
127
128 isys.initialContextActivations();
129 }
130