File: MouseInput.cpp

package info (click to toggle)
spring 0.81.2.1%2Bdfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 28,496 kB
  • ctags: 37,096
  • sloc: cpp: 238,659; ansic: 13,784; java: 12,175; awk: 3,428; python: 1,159; xml: 738; perl: 405; sh: 297; makefile: 267; pascal: 228; objc: 192
file content (220 lines) | stat: -rw-r--r-- 5,309 bytes parent folder | download
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
/*
	This workaround fixes the windows slow mouse movement problem (happens on fullscreen mode + pressing keys).
	The code hacks around the mouse input from DirectInput, which SDL uses in fullscreen mode.
	Instead it installs a window message proc and reads input from WM_MOUSEMOVE.
	On non-windows, the normal SDL events are used for mouse input

	new:
	It also workarounds a issue with SDL+windows and hardware cursors (->it has to block WM_SETCURSOR),
	so it is used now always even in window mode!

	newer:
	SDL_Event struct is used for new input handling. Several people confirmed its working.
*/

#include "StdAfx.h"
#include "mmgr.h"

#include "Game/UI/MouseHandler.h"

#include <boost/bind.hpp>
#include <SDL_events.h>
#include <SDL_syswm.h>
#include "Rendering/GL/FBO.h"
#include "GlobalUnsynced.h"
#ifdef _WIN32
#include "Platform/Win/win32.h"
#include "Platform/Win/wsdl.h"
#endif
#include "MouseInput.h"
#include "InputHandler.h"
#include "LogOutput.h"



IMouseInput* mouseInput = NULL;


IMouseInput::IMouseInput()
{
	inputCon = input.AddHandler(boost::bind(&IMouseInput::HandleSDLMouseEvent, this, _1));
}


IMouseInput::~IMouseInput()
{}

bool IMouseInput::HandleSDLMouseEvent (const SDL_Event& event)
{
	switch (event.type) {
		case SDL_MOUSEMOTION: {
			mousepos = int2(event.motion.x, event.motion.y);
			if (mouse) {
				mouse->MouseMove(mousepos.x, mousepos.y);
			}
			break;
		}
		case SDL_MOUSEBUTTONDOWN: {
			mousepos = int2(event.button.x, event.button.y);
			if (mouse) {
				if (event.button.button == SDL_BUTTON_WHEELUP) {
					mouse->MouseWheel(1.0f);
				} else if (event.button.button == SDL_BUTTON_WHEELDOWN) {
					mouse->MouseWheel(-1.0f);
				} else {
					mouse->MousePress(event.button.x, event.button.y, event.button.button);
				}
			}
			break;
		}
		case SDL_MOUSEBUTTONUP: {
			mousepos = int2(event.button.x, event.button.y);
			if (mouse) {
				mouse->MouseRelease(event.button.x, event.button.y, event.button.button);
			}
			break;
		}
	}
	return false;
}

//////////////////////////////////////////////////////////////////////

#ifdef WIN32

class CWin32MouseInput : public IMouseInput
{
public:
	static CWin32MouseInput *inst;

	LONG_PTR sdl_wndproc;
	HWND wnd;
	HCURSOR hCursor;

	// SDL runs the window in a different thread, hence the indirectness of the mouse pos handling
	static LRESULT CALLBACK SpringWndProc(HWND wnd, UINT msg, WPARAM wParam, LPARAM lParam)
	{
		if (mouse) {
			switch (msg) {
				case WM_XBUTTONDOWN:
					if ((short)LOWORD(wParam) & MK_XBUTTON1)
						mouse->MousePress((short)LOWORD(lParam), (short)HIWORD(lParam), 4);
					if ((short)LOWORD(wParam) & MK_XBUTTON2)
						mouse->MousePress((short)LOWORD(lParam), (short)HIWORD(lParam), 5);
					break;
				case WM_XBUTTONUP:
					if ((short)LOWORD(wParam) & MK_XBUTTON1)
						mouse->MouseRelease((short)LOWORD(lParam), (short)HIWORD(lParam), 4);
					if ((short)LOWORD(wParam) & MK_XBUTTON2)
						mouse->MouseRelease((short)LOWORD(lParam), (short)HIWORD(lParam), 5);
					break;
				}
		}

		switch (msg) {
			case WM_MOUSEWHEEL:
				return wsdl::OnMouseWheel(wnd, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (int)(short)HIWORD(wParam), (UINT)(short)LOWORD(wParam));
			case WM_MOUSEMOVE:
			{
				// is polled, prevent doubled messages
				return TRUE;
			}
			// (can't use message crackers: they do not provide for passing uMsg)
			case WM_LBUTTONDOWN:
			case WM_LBUTTONUP:
			case WM_RBUTTONDOWN:
			case WM_RBUTTONUP:
			case WM_MBUTTONDOWN:
			case WM_MBUTTONUP:
			{
				return wsdl::OnMouseButton(wnd, msg, (int)(short)LOWORD(lParam), (int)(short)HIWORD(lParam), (UINT)wParam);
			}
			case WM_SETCURSOR:
				if (inst->hCursor!=NULL) {
					Uint16 hittest = LOWORD(lParam);
					if ( hittest == HTCLIENT ) {
						SetCursor(inst->hCursor);
						return TRUE;
					}
				}
				break;
			case WM_ACTIVATE:
				// wsdl::OnActivate();
				// FIXME: move to SpringApp somehow and use GLContext.h instead!
				if(fullscreen) {
					if (wParam == WA_INACTIVE) {
						FBO::GLContextLost();
					}else if (wParam == WA_ACTIVE) {
						FBO::GLContextReinit();
					}
				}
				break;
		}
		return CallWindowProc((WNDPROC)inst->sdl_wndproc, wnd, msg, wParam, lParam);
	}

	void SetWMMouseCursor(void* wmcursor)
	{
		hCursor = (HCURSOR)wmcursor;
	}

	void InstallWndCallback()
	{
		sdl_wndproc = GetWindowLongPtr(wnd, GWLP_WNDPROC);
		SetWindowLongPtr(wnd,GWLP_WNDPROC,(LONG_PTR)SpringWndProc);
	}

	CWin32MouseInput()
	{
		inst = this;

		hCursor = NULL;

		sdl_wndproc = 0;

		SDL_SysWMinfo info;
		SDL_VERSION(&info.version);
		if(!SDL_GetWMInfo(&info))
			return;

		wnd = info.window;
		wsdl::SetHandle(wnd);

		InstallWndCallback();
	}
	~CWin32MouseInput()
	{
		// reinstall the SDL window proc
		SetWindowLongPtr(wnd, GWLP_WNDPROC, sdl_wndproc);
	}
	virtual void Update()
	{
		// according to the docs, WM_MOUSEMOVE is laggy, so use special poll aproach
		wsdl::mouse_update();
	};
};
CWin32MouseInput* CWin32MouseInput::inst = 0;
#endif

void IMouseInput::SetPos(int2 pos)
{
	mousepos = pos;
#ifdef WIN32
	wsdl::SDL_WarpMouse(pos.x, pos.y);
#else
	SDL_WarpMouse(pos.x, pos.y);
#endif
}

IMouseInput *IMouseInput::Get ()
{
	if (!mouseInput) {
#ifdef WIN32
		mouseInput = new CWin32MouseInput;
#else
		mouseInput = new IMouseInput;
#endif
	}
	return mouseInput;
}