Tscope5
joystick_internal.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // __ ______
4 // / /_______________ ____ ___ / ____/
5 // / __/ ___/ ___/ __ \/ __ \/ _ \ /___ )
6 // / /_(__ ) /__/ /_/ / /_/ / __/ ____/ /
7 // \__/____/\___/\____/ .___/\___/ /_____/
8 // /_/
9 //
10 /// \file joystick_internal.c
11 /// Definitions of internal joystick functions.
12 ////////////////////////////////////////////////////////////////////////////////
13 
14 #include "../include/tscope5/joystick_internal.h"
15 #include "../include/tscope5/system_internal.h"
16 #include "../include/tscope5/timer_internal.h"
17 
18 /// Is the joystick subsystem installed?
20 
21 ////////////////////////////////////////////////////////////////////////////////
22 /// Do some checks at the start of each joystick function.
23 ///
24 /// \param calling_function Name the function that calls for
25 /// this check or installation.
26 ///
27 /// Checks whether the joystick subsystem is installed.
28 /// If not, the joystick subsystem is installed.
29 ////////////////////////////////////////////////////////////////////////////////
30 void ts5_check_joystick(char *calling_function)
31 {
32  ts5_log(TS5_LOGLEVEL_6, "%s: ts5_check_joystick\n", calling_function);
34  ts5_install_joystick(calling_function);
35  }
36 }
37 
38 
39 ////////////////////////////////////////////////////////////////////////////////
40 /// Install the joystick subsystem.
41 ///
42 /// \param calling_function Name the function that calls for
43 /// this check or installation.
44 ///
45 /// This function is called automatically if necessary.
46 ////////////////////////////////////////////////////////////////////////////////
47 void ts5_install_joystick(char *calling_function)
48 {
50  ts5_install_timer(calling_function);
51  }
52 
54 
56  ts5_log(TS5_LOGLEVEL_1, "%s: Installing Tscope5 joystick\n",
57  calling_function);
58 
59  if (!al_install_joystick()) {
60  ts5_fatal("%s: could not install Tscope5 joystick\n",
61  calling_function);
62  }
63 
64  al_register_event_source(_ts5_data.timer.response_queue,
65  al_get_joystick_event_source());
66 
67  _ts5_status.timer.num_joysticks = al_get_num_joysticks();
68  _ts5_status.timer.joystick =
69  (TS5_JOYSTICK_STATUS *)al_malloc(sizeof(TS5_JOYSTICK_STATUS)*
70  _ts5_status.timer.num_joysticks);
71 
72  int i;
73  for (i=0; i<_ts5_status.timer.num_joysticks; i++) {
74 
75  ALLEGRO_JOYSTICK *joy = al_get_joystick(i);
76 
77  _ts5_status.timer.joystick[i].num_buttons =
78  al_get_joystick_num_buttons(joy);
79 
80  _ts5_status.timer.joystick[i].num_defined_buttons = 0;
81  _ts5_status.timer.joystick[i].num_active_buttons = 0;
82 
83  _ts5_status.timer.joystick[i].button_press_defined =
84  (int *)al_malloc(sizeof(int)
85  * _ts5_status.timer.joystick[i].num_buttons);
86 
87  _ts5_status.timer.joystick[i].button_press_active =
88  (int *)al_malloc(sizeof(int)
89  * _ts5_status.timer.joystick[i].num_buttons);
90 
91  _ts5_status.timer.joystick[i].button_release_defined =
92  (int *)al_malloc(sizeof(int)
93  * _ts5_status.timer.joystick[i].num_buttons);
94 
95  _ts5_status.timer.joystick[i].button_release_active =
96  (int *)al_malloc(sizeof(int)
97  * _ts5_status.timer.joystick[i].num_buttons);
98 
99  int j;
100  for (j=0; j<_ts5_status.timer.joystick[i].num_buttons; j++) {
101  _ts5_status.timer.joystick[i].button_press_defined[j] = 0;
102  _ts5_status.timer.joystick[i].button_press_active[j] = 0;
103  _ts5_status.timer.joystick[i].button_release_defined[j] = 0;
104  _ts5_status.timer.joystick[i].button_release_active[j] = 0;
105  }
106  }
107 
108  atexit(ts5_uninstall_joystick);
109  }
110 }
111 
112 
113 ////////////////////////////////////////////////////////////////////////////////
114 /// Uninstall the joystick subsystem.
115 ///
116 /// This function is called automatically at the end of the program.
117 ////////////////////////////////////////////////////////////////////////////////
119 {
121 
122  ts5_log(TS5_LOGLEVEL_1, "Uninstalling Tscope5 joystick\n");
123 
124  int i;
125  for (i=0; i<_ts5_status.timer.num_joysticks; i++) {
126 
127  al_free(_ts5_status.timer.joystick[i].button_press_defined);
128  _ts5_status.timer.joystick[i].button_press_defined = NULL;
129 
130  al_free(_ts5_status.timer.joystick[i].button_press_active);
131  _ts5_status.timer.joystick[i].button_press_active = NULL;
132 
133  al_free(_ts5_status.timer.joystick[i].button_release_defined);
134  _ts5_status.timer.joystick[i].button_release_defined = NULL;
135 
136  al_free(_ts5_status.timer.joystick[i].button_release_active);
137  _ts5_status.timer.joystick[i].button_release_active = NULL;
138  }
139 
140  al_free(_ts5_status.timer.joystick);
141  _ts5_status.timer.joystick=NULL;
142 
143  _ts5_status.timer.num_joysticks = 0;
144  _ts5_status.timer.joystick_is_response_device = 0;
145 
146  al_unregister_event_source(_ts5_data.timer.response_queue,
147  al_get_joystick_event_source());
148 
149  #ifdef TS5_MACOSX
150  al_uninstall_joystick();
151 
152  #endif
153 
155  }
156 }
157 
void ts5_install_joystick(char *calling_function)
Install the joystick subsystem.
int _ts5_is_timer_installed
Is the timer subsystem installed?
void ts5_check_joystick(char *calling_function)
Do some checks at the start of each joystick function.
int _ts5_is_joystick_installed
Is the joystick subsystem installed?
void ts5_install_timer(char *calling_function)
Install the timer subsystem.
void ts5_log(const unsigned int level, const char *format,...)
Send info to a logging window.
Definition: system.c:45
void ts5_uninstall_joystick()
Uninstall the joystick subsystem.
void ts5_fatal(const char *format,...)
Exit safely with an error message.
Definition: system.c:533