Input.cpp
1 /******************************************************************************
2 
3  nomlib - C++11 cross-platform game engine
4 
5 Copyright (c) 2013, Jeffrey Carpenter <jeffrey.carp@gmail.com>
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 ******************************************************************************/
29 #include "nomlib/system/Input.hpp"
30 
31 namespace nom {
32 
33 Input::Input ( void ) :
34  joystick_ { Input::JoystickUniquePtr ( nullptr, priv::Free_Joystick ) },
35  joystick_id_ ( 0 )
36 {
37 NOM_LOG_TRACE ( NOM );
38 
39  if ( SDL_WasInit( SDL_INIT_JOYSTICK ) == false )
40  {
41  if ( SDL_InitSubSystem ( SDL_INIT_JOYSTICK ) < 0 )
42  {
43 NOM_LOG_ERR ( NOM, SDL_GetError() );
44  return;
45  }
46  }
47 
48 NOM_LOG_INFO ( NOM, std::to_string ( SDL_NumJoysticks() ) + " joysticks were found" );
49 
50  if ( SDL_NumJoysticks() > 0 )
51  {
52  this->joystick_.reset ( SDL_JoystickOpen ( 0 ) );
53 
54  if ( this->joystick_.get() )
55  {
56  for( int idx = 0; idx < SDL_NumJoysticks(); idx++ )
57  {
58 NOM_LOG_INFO ( NOM, SDL_JoystickNameForIndex ( idx ) );
59  }
60 
61  SDL_JoystickEventState ( SDL_ENABLE );
62 
63  this->joystick_id_ = SDL_JoystickInstanceID ( this->joystick_.get() );
64  }
65  }
66 }
67 
68 Input::~Input ( void )
69 {
70 NOM_LOG_TRACE ( NOM );
71 }
72 
73 
74 void Input::HandleInput ( EventType* event )
75 {
76  switch ( event->type )
77  {
78  default: /* Ignore unknown events */ break;
79 
80  case SDL_QUIT: this->onQuit(); break;
81 
82  case SDL_WINDOWEVENT:
83  {
84  switch ( event->window.event )
85  {
86  default: /* Ignore unknown events */ break;
87 
88  case SDL_WINDOWEVENT_CLOSE: this->onQuit(); break;
89 
90 /* TODO: SDL2 window events handling
91  case SDL_WINDOWEVENT_ENTER: break;
92  case SDL_WINDOWEVENT_EXPOSED: break;
93  case SDL_WINDOWEVENT_FOCUS_GAINED: break;
94  case SDL_WINDOWEVENT_FOCUS_LOST: break;
95  case SDL_WINDOWEVENT_HIDDEN: break;
96  case SDL_WINDOWEVENT_LEAVE: break;
97  case SDL_WINDOWEVENT_MAXIMIZED: break;
98  case SDL_WINDOWEVENT_MINIMIZED: break;
99  case SDL_WINDOWEVENT_MOVED: break;
100  case SDL_WINDOWEVENT_RESIZED: break;
101  case SDL_WINDOWEVENT_RESTORED: break;
102  case SDL_WINDOWEVENT_SHOWN: break;
103 TODO */
104  } // end event->window.event switch
105  } // end event->type switch
106 
107  case SDL_USEREVENT:
108  this->onUserEvent ( event->user.type, event->user.code, event->user.data1,
109  event->user.data2
110  );
111  break;
112 
113  case SDL_SYSWMEVENT: /* Ignore */ break;
114 
115  case SDL_KEYDOWN:
116  this->onKeyDown ( event->key.keysym.sym, event->key.keysym.mod, event->button.windowID );
117  break;
118 
119  case SDL_KEYUP:
120  this->onKeyUp ( event->key.keysym.sym, event->key.keysym.mod, event->button.windowID );
121  break;
122 
123  case SDL_MOUSEMOTION:
124  this->onMouseMotion ( event->motion.x, event->motion.y, event->motion.windowID );
125  break;
126 
127  case SDL_MOUSEBUTTONDOWN:
128  {
129  switch ( event->button.button )
130  {
131  case SDL_BUTTON_LEFT:
132  this->onMouseLeftButtonDown ( event->button.x, event->button.y, event->button.windowID );
133  break;
134 
135  case SDL_BUTTON_MIDDLE:
136  this->onMouseMiddleButtonDown ( event->button.x, event->button.y, event->button.windowID );
137  break;
138 
139  case SDL_BUTTON_RIGHT:
140  this->onMouseRightButtonDown ( event->button.x, event->button.y, event->button.windowID );
141  break;
142 
143  case SDL_BUTTON_X1:
144  this->onMouseButtonSixDown ( event->button.x, event->button.y, event->button.windowID );
145  break;
146 
147  case SDL_BUTTON_X2:
148  this->onMouseButtonSevenDown ( event->button.x, event->button.y, event->button.windowID );
149  break;
150  }
151  break;
152  } // end SDL_MOUSEBUTTONDOWN event
153 
154  case SDL_MOUSEBUTTONUP:
155  switch ( event->button.button )
156  {
157  case SDL_BUTTON_LEFT:
158  this->onMouseLeftButtonUp ( event->button.x, event->button.y, event->button.windowID );
159  break;
160 
161  case SDL_BUTTON_MIDDLE:
162  this->onMouseMiddleButtonUp ( event->button.x, event->button.y, event->button.windowID );
163  break;
164 
165  case SDL_BUTTON_RIGHT:
166  this->onMouseRightButtonUp ( event->button.x, event->button.y, event->button.windowID );
167  break;
168 
169  case SDL_BUTTON_X1:
170  this->onMouseButtonSixUp ( event->button.x, event->button.y, event->button.windowID );
171  break;
172 
173  case SDL_BUTTON_X2:
174  this->onMouseButtonSevenUp ( event->button.x, event->button.y, event->button.windowID );
175  break;
176  }
177  break;
178 
179  case SDL_MOUSEWHEEL:
180  {
181  this->onMouseWheel ( event->wheel.x, event->wheel.y, event->wheel.windowID );
182  } // end SDL_MOUSEWHEEL event
183  break;
184 
185  case SDL_JOYBUTTONDOWN:
186  this->onJoyButtonDown ( this->joystick_id_, event->jbutton.button );
187  break;
188 
189  case SDL_JOYBUTTONUP:
190  this->onJoyButtonUp ( this->joystick_id_, event->jbutton.button );
191  break;
192 
193  case SDL_JOYAXISMOTION:
194  this->onJoyAxis ( this->joystick_id_, event->jaxis.axis, event->jaxis.value );
195  break;
196 
197  case SDL_DROPFILE:
198  {
199  char* file = event->drop.file;
200  this->onDragDrop ( file, event->drop.timestamp );
201 
202  SDL_free ( file );
203  file = nullptr;
204  break;
205  } // SDL_DROPFILE
206 
207 
208  } // end switch event->type
209 }
210 
211 void Input::onUserEvent ( uint32 type, int32 code, void* data1, void* data2 )
212 {
213  // virtual implementation
214 }
215 
216 void Input::onQuit ( void )
217 {
218  // virtual implementation
219 }
220 
221 void Input::onResize ( int32 width, int32 height )
222 {
223  // virtual implementation
224 }
225 
226 void Input::onRestore ( void )
227 {
228  // virtual implementation
229 }
230 
231 void Input::onMinimize ( void )
232 {
233  // virtual implementation
234 }
235 
236 void Input::onInputFocus ( void )
237 {
238  // virtual implementation
239 }
240 
241 void Input::onInputBlur ( void )
242 {
243  // virtual implementation
244 }
245 
246 void Input::onMouseFocus ( void )
247 {
248  // virtual implementation
249 }
250 
251 void Input::onMouseBlur ( void )
252 {
253  // virtual implementation
254 }
255 
256 void Input::onExpose ( void )
257 {
258  // virtual implementation
259 }
260 
261 void Input::onKeyDown ( int32 key, int32 mod, uint32 window_id )
262 {
263 #if defined ( NOM_DEBUG_SDL2_KEYBOARD_INPUT )
264  NOM_LOG_TRACE ( NOM );
265  NOM_DUMP_VAR ( key );
266  NOM_DUMP_VAR ( mod );
267  NOM_DUMP_VAR ( window_id );
268 #endif
269 }
270 
271 void Input::onKeyUp ( int32 key, int32 mod, uint32 window_id )
272 {
273 #if defined ( NOM_DEBUG_SDL2_KEYBOARD_INPUT )
274  NOM_LOG_TRACE ( NOM );
275  NOM_DUMP_VAR ( key );
276  NOM_DUMP_VAR ( mod );
277  NOM_DUMP_VAR ( window_id );
278 #endif
279 }
280 
281 void Input::onMouseMotion ( int32 x, int32 y, uint32 window_id )
282 {
283 #if defined ( NOM_DEBUG_SDL2_MOUSE_INPUT )
284  NOM_LOG_TRACE ( NOM );
285  NOM_DUMP_VAR ( x );
286  NOM_DUMP_VAR ( y );
287  NOM_DUMP_VAR ( window_id );
288 #endif
289 }
290 
291 void Input::onMouseWheel ( int32 x, int32 y, uint32 window_id )
292 {
293 #if defined ( NOM_DEBUG_SDL2_MOUSE_INPUT )
294  NOM_LOG_TRACE ( NOM );
295  NOM_DUMP_VAR ( x );
296  NOM_DUMP_VAR ( y );
297  NOM_DUMP_VAR ( window_id );
298 #endif
299 }
300 
301 void Input::onMouseLeftButtonDown ( int32 x, int32 y, uint32 window_id )
302 {
303 #if defined ( NOM_DEBUG_SDL2_MOUSE_INPUT )
304  NOM_LOG_TRACE ( NOM );
305  NOM_DUMP_VAR ( x );
306  NOM_DUMP_VAR ( y );
307  NOM_DUMP_VAR ( window_id );
308 #endif
309 }
310 
311 void Input::onMouseMiddleButtonDown ( int32 x, int32 y, uint32 window_id )
312 {
313 #if defined ( NOM_DEBUG_SDL2_MOUSE_INPUT )
314  NOM_LOG_TRACE ( NOM );
315  NOM_DUMP_VAR ( x );
316  NOM_DUMP_VAR ( y );
317  NOM_DUMP_VAR ( window_id );
318 #endif
319 }
320 
321 void Input::onMouseRightButtonDown ( int32 x, int32 y, uint32 window_id )
322 {
323 #if defined ( NOM_DEBUG_SDL2_MOUSE_INPUT )
324  NOM_LOG_TRACE ( NOM );
325  NOM_DUMP_VAR ( x );
326  NOM_DUMP_VAR ( y );
327  NOM_DUMP_VAR ( window_id );
328 #endif
329 }
330 
331 void Input::onMouseButtonSixDown ( int32 x, int32 y, uint32 window_id )
332 {
333 #if defined ( NOM_DEBUG_SDL2_MOUSE_INPUT )
334  NOM_LOG_TRACE ( NOM );
335  NOM_DUMP_VAR ( x );
336  NOM_DUMP_VAR ( y );
337  NOM_DUMP_VAR ( window_id );
338 #endif
339 }
340 
341 void Input::onMouseButtonSevenDown ( int32 x, int32 y, uint32 window_id )
342 {
343 #if defined ( NOM_DEBUG_SDL2_MOUSE_INPUT )
344  NOM_LOG_TRACE ( NOM );
345  NOM_DUMP_VAR ( x );
346  NOM_DUMP_VAR ( y );
347  NOM_DUMP_VAR ( window_id );
348 #endif
349 }
350 
351 void Input::onMouseLeftButtonUp ( int32 x, int32 y, uint32 window_id )
352 {
353 #if defined ( NOM_DEBUG_SDL2_MOUSE_INPUT )
354  NOM_LOG_TRACE ( NOM );
355  NOM_DUMP_VAR ( x );
356  NOM_DUMP_VAR ( y );
357  NOM_DUMP_VAR ( window_id );
358 #endif
359 }
360 
361 void Input::onMouseMiddleButtonUp ( int32 x, int32 y, uint32 window_id )
362 {
363 #if defined ( NOM_DEBUG_SDL2_MOUSE_INPUT )
364  NOM_LOG_TRACE ( NOM );
365  NOM_DUMP_VAR ( x );
366  NOM_DUMP_VAR ( y );
367  NOM_DUMP_VAR ( window_id );
368 #endif
369 }
370 
371 void Input::onMouseRightButtonUp ( int32 x, int32 y, uint32 window_id )
372 {
373 #if defined ( NOM_DEBUG_SDL2_MOUSE_INPUT )
374  NOM_LOG_TRACE ( NOM );
375  NOM_DUMP_VAR ( x );
376  NOM_DUMP_VAR ( y );
377  NOM_DUMP_VAR ( window_id );
378 #endif
379 }
380 
381 void Input::onMouseButtonSixUp ( int32 x, int32 y, uint32 window_id )
382 {
383 #if defined ( NOM_DEBUG_SDL2_MOUSE_INPUT )
384  NOM_LOG_TRACE ( NOM );
385  NOM_DUMP_VAR ( x );
386  NOM_DUMP_VAR ( y );
387  NOM_DUMP_VAR ( window_id );
388 #endif
389 }
390 
391 void Input::onMouseButtonSevenUp ( int32 x, int32 y, uint32 window_id )
392 {
393 #if defined ( NOM_DEBUG_SDL2_MOUSE_INPUT )
394  NOM_LOG_TRACE ( NOM );
395  NOM_DUMP_VAR ( x );
396  NOM_DUMP_VAR ( y );
397  NOM_DUMP_VAR ( window_id );
398 #endif
399 }
400 
401 void Input::onJoyButtonDown ( int32 which, int32 button )
402 {
403 #if defined ( NOM_DEBUG_SDL2_JOYSTICK_BUTTON_INPUT )
404  NOM_LOG_TRACE ( NOM );
405  NOM_DUMP_VAR ( which );
406  NOM_DUMP_VAR ( button );
407 #endif
408 }
409 
410 void Input::onJoyButtonUp ( int32 which, int32 button )
411 {
412 #if defined ( NOM_DEBUG_SDL2_JOYSTICK_BUTTON_INPUT )
413  NOM_LOG_TRACE ( NOM );
414  NOM_DUMP_VAR ( which );
415  NOM_DUMP_VAR ( button );
416 #endif
417 }
418 
419 void Input::onJoyAxis ( int32 which, int32 axis, uint16 value )
420 {
421 #if defined ( NOM_DEBUG_SDL2_JOYSTICK_AXIS_INPUT )
422  NOM_LOG_TRACE ( NOM );
423  NOM_DUMP_VAR ( which );
424  NOM_DUMP_VAR ( axis );
425  NOM_DUMP_VAR ( value );
426 #endif
427 }
428 
429 void Input::onDragDrop ( const std::string& file_path, uint32 timestamp )
430 {
431 #if defined ( NOM_DEBUG_SDL2_DRAG_DROP_INPUT )
432  NOM_LOG_TRACE ( NOM );
433  NOM_DUMP_VAR ( file_path );
434  NOM_DUMP_VAR ( timestamp );
435 #endif
436 }
437 
438 
439 } // namespace nom
virtual void onDragDrop(const std::string &file_path, uint32 timestamp)
Definition: Input.cpp:429