summaryrefslogtreecommitdiff
path: root/indra/newview/llwindowlistener.cpp
blob: e5deaa42cf8f46ad3f04b1cb9f0ce0db37788c6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/** 
 * @file llwindowlistener.cpp
 * @brief EventAPI interface for injecting input into LLWindow
 *
 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "linden_common.h"

#include "llwindowlistener.h"

#include "llcoord.h"
#include "llkeyboard.h"
#include "llwindowcallbacks.h"
#include "llui.h"
#include <map>

LLWindowListener::LLWindowListener(LLWindowCallbacks *window, const KeyboardGetter& kbgetter)
	: LLEventAPI("LLWindow", "Inject input events into the LLWindow instance"),
	  mWindow(window),
	  mKbGetter(kbgetter)
{
	std::string keySomething =
		"Given [\"keysym\"], [\"keycode\"] or [\"char\"], inject the specified ";
	std::string keyExplain =
		"(integer keycode values, or keysym string from any addKeyName() call in\n"
		"http://hg.secondlife.com/viewer-development/src/tip/indra/llwindow/llkeyboard.cpp )\n";
	std::string mask =
		"Specify optional [\"mask\"] as an array containing any of \"CTL\", \"ALT\",\n"
		"\"SHIFT\" or \"MAC_CONTROL\"; the corresponding modifier bits will be combined\n"
		"to form the mask used with the event.";

	std::string given = "Given ";
	std::string mouseParams =
		"optional [\"path\"], optional [\"x\"] and [\"y\"], inject the requested mouse ";
	std::string buttonParams =
		std::string("[\"button\"], ") + mouseParams;
	std::string buttonExplain =
		"(button values \"LEFT\", \"MIDDLE\", \"RIGHT\")\n";
	std::string paramsExplain =
		"[\"path\"] is as for LLUI::resolvePath(), described in\n"
		"http://hg.secondlife.com/viewer-development/src/tip/indra/llui/llui.h\n"
		"If you omit [\"path\"], you must specify both [\"x\"] and [\"y\"].\n"
		"If you specify [\"path\"] without both [\"x\"] and [\"y\"], will synthesize (x, y)\n"
		"in the center of the LLView selected by [\"path\"].\n"
		"You may specify [\"path\"] with both [\"x\"] and [\"y\"], will use your (x, y).\n"
		"This may cause the LLView selected by [\"path\"] to reject the event.\n"
		"Optional [\"reply\"] requests a reply event on the named LLEventPump.\n"
		"reply[\"error\"] isUndefined (None) on success, else an explanatory message.\n";

	add("keyDown",
		keySomething + "keypress event.\n" + keyExplain + mask,
		&LLWindowListener::keyDown);
	add("keyUp",
		keySomething + "key release event.\n" + keyExplain + mask,
		&LLWindowListener::keyUp);
	add("mouseDown",
		given + buttonParams + "click event.\n" + buttonExplain + paramsExplain + mask,
		&LLWindowListener::mouseDown);
	add("mouseUp",
		given + buttonParams + "release event.\n" + buttonExplain + paramsExplain + mask,
		&LLWindowListener::mouseUp);
	add("mouseMove",
		given + mouseParams + "movement event.\n" + paramsExplain + mask,
		&LLWindowListener::mouseMove);
	add("mouseScroll",
		"Given an integer number of [\"clicks\"], inject the requested mouse scroll event.\n"
		"(positive clicks moves downward through typical content)",
		&LLWindowListener::mouseScroll);
}

template <typename MAPPED>
class StringLookup
{
private:
	std::string mDesc;
	typedef std::map<std::string, MAPPED> Map;
	Map mMap;

public:
	StringLookup(const std::string& desc): mDesc(desc) {}

	MAPPED lookup(const typename Map::key_type& key) const
	{
		typename Map::const_iterator found = mMap.find(key);
		if (found == mMap.end())
		{
			LL_WARNS("LLWindowListener") << "Unknown " << mDesc << " '" << key << "'" << LL_ENDL;
			return MAPPED();
		}
		return found->second;
	}

protected:
	void add(const typename Map::key_type& key, const typename Map::mapped_type& value)
	{
		mMap.insert(typename Map::value_type(key, value));
	}
};

// helper for getMask()
static MASK lookupMask_(const std::string& maskname)
{
	// It's unclear to me whether MASK_MAC_CONTROL is important, but it's not
	// supported by maskFromString(). Handle that specially.
	if (maskname == "MAC_CONTROL")
	{
		return MASK_MAC_CONTROL;
	}
	else
	{
		// In case of lookup failure, return MASK_NONE, which won't affect our
		// caller's OR.
		MASK mask(MASK_NONE);
		LLKeyboard::maskFromString(maskname, &mask);
		return mask;
	}
}

static MASK getMask(const LLSD& event)
{
	LLSD masknames(event["mask"]);
	if (! masknames.isArray())
	{
		// If event["mask"] is a single string, perform normal lookup on it.
		return lookupMask_(masknames);
	}

	// Here event["mask"] is an array of mask-name strings. OR together their
	// corresponding bits.
	MASK mask(MASK_NONE);
	for (LLSD::array_const_iterator ai(masknames.beginArray()), aend(masknames.endArray());
		 ai != aend; ++ai)
	{
		mask |= lookupMask_(*ai);
	}
	return mask;
}

static KEY getKEY(const LLSD& event)
{
    if (event.has("keysym"))
	{
		// Initialize to KEY_NONE; that way we can ignore the bool return from
		// keyFromString() and, in the lookup-fail case, simply return KEY_NONE.
		KEY key(KEY_NONE);
		LLKeyboard::keyFromString(event["keysym"], &key);
		return key;
	}
	else if (event.has("keycode"))
	{
		return KEY(event["keycode"].asInteger());
	}
	else
	{
		return KEY(event["char"].asString()[0]);
	}
}

void LLWindowListener::keyDown(LLSD const & evt)
{
	mKbGetter()->handleTranslatedKeyDown(getKEY(evt), getMask(evt));
}

void LLWindowListener::keyUp(LLSD const & evt)
{
	mKbGetter()->handleTranslatedKeyUp(getKEY(evt), getMask(evt));
}

// for WhichButton
typedef BOOL (LLWindowCallbacks::*MouseFunc)(LLWindow *, LLCoordGL, MASK);
struct Actions
{
	Actions(const MouseFunc& d, const MouseFunc& u): down(d), up(u), valid(true) {}
	Actions(): valid(false) {}
	MouseFunc down, up;
	bool valid;
};

struct WhichButton: public StringLookup<Actions>
{
	WhichButton(): StringLookup<Actions>("mouse button")
	{
		add("LEFT",		Actions(&LLWindowCallbacks::handleMouseDown,
								&LLWindowCallbacks::handleMouseUp));
		add("RIGHT",	Actions(&LLWindowCallbacks::handleRightMouseDown,
								&LLWindowCallbacks::handleRightMouseUp));
		add("MIDDLE",	Actions(&LLWindowCallbacks::handleMiddleMouseDown,
								&LLWindowCallbacks::handleMiddleMouseUp));
	}
};
static WhichButton buttons;

static LLCoordGL getPos(const LLSD& event)
{
	return LLCoordGL(event["x"].asInteger(), event["y"].asInteger());
}

void LLWindowListener::mouseDown(LLSD const & evt)
{
	Actions actions(buttons.lookup(evt["button"]));
	if (actions.valid)
	{
		(mWindow->*(actions.down))(NULL, getPos(evt), getMask(evt));
	}
}

void LLWindowListener::mouseUp(LLSD const & evt)
{
	Actions actions(buttons.lookup(evt["button"]));
	if (actions.valid)
	{
		(mWindow->*(actions.up))(NULL, getPos(evt), getMask(evt));
	}
}

void LLWindowListener::mouseMove(LLSD const & evt)
{
	mWindow->handleMouseMove(NULL, getPos(evt), getMask(evt));
}

void LLWindowListener::mouseScroll(LLSD const & evt)
{
	S32 clicks = evt["clicks"].asInteger();

	mWindow->handleScrollWheel(NULL, clicks);
}