• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 The zlib/libpng License
3 
4 Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com)
5 
6 This software is provided 'as-is', without any express or implied warranty. In no event will
7 the authors be held liable for any damages arising from the use of this software.
8 
9 Permission is granted to anyone to use this software for any purpose, including commercial
10 applications, and to alter it and redistribute it freely, subject to the following
11 restrictions:
12 
13     1. The origin of this software must not be misrepresented; you must not claim that
14 		you wrote the original software. If you use this software in a product,
15 		an acknowledgment in the product documentation would be appreciated but is
16 		not required.
17 
18     2. Altered source versions must be plainly marked as such, and must not be
19 		misrepresented as being the original software.
20 
21     3. This notice may not be removed or altered from any source distribution.
22 */
23 #include "linux/LinuxInputManager.h"
24 #include "linux/LinuxKeyboard.h"
25 #include "linux/LinuxJoyStickEvents.h"
26 #include "linux/LinuxMouse.h"
27 #include "OISException.h"
28 #include <cstdlib>
29 #include <stdio.h>
30 
31 using namespace OIS;
32 
33 //--------------------------------------------------------------------------------//
LinuxInputManager()34 LinuxInputManager::LinuxInputManager() : InputManager("X11InputManager")
35 {
36 	window = 0;
37 
38 	//Default settings
39 	grabMouse = true;
40 	grabKeyboard = true;
41 	hideMouse = true;
42 	mGrabs = true;
43 	keyboardUsed = mouseUsed = false;
44 
45 	//Setup our internal factories
46 	mFactories.push_back(this);
47 }
48 
49 //--------------------------------------------------------------------------------//
~LinuxInputManager()50 LinuxInputManager::~LinuxInputManager()
51 {
52 	//Close all joysticks
53 	LinuxJoyStick::_clearJoys(unusedJoyStickList);
54 }
55 
56 //--------------------------------------------------------------------------------//
_initialize(ParamList & paramList)57 void LinuxInputManager::_initialize( ParamList &paramList )
58 {
59 	_parseConfigSettings( paramList );
60 
61 	//Enumerate all devices attached
62 	_enumerateDevices();
63 }
64 
65 //--------------------------------------------------------------------------------//
_parseConfigSettings(ParamList & paramList)66 void LinuxInputManager::_parseConfigSettings( ParamList &paramList )
67 {
68 	ParamList::iterator i = paramList.find("WINDOW");
69 	if( i == paramList.end() )
70 		{
71 			printf("OIS: No Window specified... Not using x11 keyboard/mouse\n");
72 			return;
73 		}
74 
75 		window = strtoull(i->second.c_str(), 0, 10);
76 
77 	//--------- Keyboard Settings ------------//
78 	i = paramList.find("x11_keyboard_grab");
79 	if( i != paramList.end() )
80 		if( i->second == "false" )
81 			grabKeyboard = false;
82 
83 	//--------- Mouse Settings ------------//
84 	i = paramList.find("x11_mouse_grab");
85 	if( i != paramList.end() )
86 		if( i->second == "false" )
87 			grabMouse = false;
88 
89 	i = paramList.find("x11_mouse_hide");
90 	if( i != paramList.end() )
91 		if( i->second == "false" )
92 			hideMouse = false;
93 }
94 
95 //--------------------------------------------------------------------------------//
_enumerateDevices()96 void LinuxInputManager::_enumerateDevices()
97 {
98 	//Enumerate all attached devices
99 	unusedJoyStickList = LinuxJoyStick::_scanJoys();
100 	joySticks = unusedJoyStickList.size();
101 }
102 
103 //----------------------------------------------------------------------------//
freeDeviceList()104 DeviceList LinuxInputManager::freeDeviceList()
105 {
106 	DeviceList ret;
107 
108 	if(window)
109 	{
110 		if(keyboardUsed == false)
111 			ret.insert(std::make_pair(OISKeyboard, mInputSystemName));
112 
113 		if(mouseUsed == false)
114 			ret.insert(std::make_pair(OISMouse, mInputSystemName));
115 	}
116 
117 	for(JoyStickInfoList::iterator i = unusedJoyStickList.begin(); i != unusedJoyStickList.end(); ++i)
118 		ret.insert(std::make_pair(OISJoyStick, i->vendor));
119 
120 	return ret;
121 }
122 
123 //----------------------------------------------------------------------------//
totalDevices(Type iType)124 int LinuxInputManager::totalDevices(Type iType)
125 {
126 	switch(iType)
127 	{
128 	case OISKeyboard: return window ? 1 : 0;
129 	case OISMouse: return window ? 1 : 0;
130 	case OISJoyStick: return joySticks;
131 	default: return 0;
132 	}
133 }
134 
135 //----------------------------------------------------------------------------//
freeDevices(Type iType)136 int LinuxInputManager::freeDevices(Type iType)
137 {
138 	switch(iType)
139 	{
140 	case OISKeyboard: return window ? (keyboardUsed ? 0 : 1) : 0;
141 	case OISMouse: return window ? (mouseUsed ? 0 : 1) : 0;
142 	case OISJoyStick: return (int)unusedJoyStickList.size();
143 	default: return 0;
144 	}
145 }
146 
147 //----------------------------------------------------------------------------//
vendorExist(Type iType,const std::string & vendor)148 bool LinuxInputManager::vendorExist(Type iType, const std::string & vendor)
149 {
150 	if((iType == OISKeyboard || iType == OISMouse) && vendor == mInputSystemName)
151 	{
152 		return window ? true : false;
153 	}
154 	else if( iType == OISJoyStick )
155 	{
156 		for(JoyStickInfoList::iterator i = unusedJoyStickList.begin(); i != unusedJoyStickList.end(); ++i)
157 			if(i->vendor == vendor)
158 				return true;
159 	}
160 
161 	return false;
162 }
163 
164 //----------------------------------------------------------------------------//
createObject(InputManager * creator,Type iType,bool bufferMode,const std::string & vendor)165 Object* LinuxInputManager::createObject(InputManager *creator, Type iType, bool bufferMode, const std::string & vendor)
166 {
167 	Object *obj = 0;
168 
169 	switch(iType)
170 	{
171 	case OISKeyboard:
172 	{
173 		if(window && keyboardUsed == false)
174 			obj = new LinuxKeyboard(this, bufferMode, grabKeyboard);
175 
176 		break;
177 	}
178 	case OISMouse:
179 	{
180 		if(window && mouseUsed == false)
181 			obj = new LinuxMouse(this, bufferMode, grabMouse, hideMouse);
182 
183 		break;
184 	}
185 	case OISJoyStick:
186 	{
187 		for(JoyStickInfoList::iterator i = unusedJoyStickList.begin(); i != unusedJoyStickList.end(); ++i)
188 		{
189 			if(vendor == "" || i->vendor == vendor)
190 			{
191 				obj = new LinuxJoyStick(this, bufferMode, *i);
192 				unusedJoyStickList.erase(i);
193 				break;
194 			}
195 		}
196 		break;
197 	}
198 	default:
199 		break;
200 	}
201 
202 	if(obj == 0)
203 		OIS_EXCEPT(E_InputDeviceNonExistant, "No devices match requested type.");
204 
205 	return obj;
206 }
207 
208 //----------------------------------------------------------------------------//
destroyObject(Object * obj)209 void LinuxInputManager::destroyObject( Object* obj )
210 {
211 	if(obj)
212 	{
213 		if(obj->type() == OISJoyStick)
214 		{
215 			unusedJoyStickList.push_back( ((LinuxJoyStick*)obj)->_getJoyInfo() );
216 		}
217 
218 		delete obj;
219 	}
220 }
221