1 /*  RetroArch - A frontend for libretro.
2  *  Copyright (C) 2010-2014 - Hans-Kristian Arntzen
3  *  Copyright (C) 2011-2017 - Daniel De Matteis
4  *  Copyright (C) 2013-2014 - CatalystG
5  *
6  *  RetroArch is free software: you can redistribute it and/or modify it under the terms
7  *  of the GNU General Public License as published by the Free Software Found-
8  *  ation, either version 3 of the License, or (at your option) any later version.
9  *
10  *  RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11  *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  *  PURPOSE.  See the GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along with RetroArch.
15  *  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "../../config.def.h"
19 
20 #include "../../tasks/tasks_internal.h"
21 #include "../../configuration.h"
22 
qnx_joypad_name(unsigned pad)23 static const char *qnx_joypad_name(unsigned pad)
24 {
25    return input_config_get_device_name(pad);
26 }
27 
qnx_joypad_init(void * data)28 static void *qnx_joypad_init(void *data)
29 {
30    unsigned autoconf_pad;
31 
32    for (autoconf_pad = 0; autoconf_pad < MAX_USERS; autoconf_pad++)
33       input_autoconfigure_connect(
34             qnx_joypad_name(autoconf_pad),
35             NULL,
36             qnx_joypad.ident,
37             autoconf_pad,
38             0,
39             0
40             );
41 
42    return (void*)-1;
43 }
44 
qnx_joypad_button(unsigned port,uint16_t joykey)45 static int16_t qnx_joypad_button(unsigned port, uint16_t joykey)
46 {
47    qnx_input_device_t* controller       = NULL;
48    qnx_input_t *qnx                     = (qnx_input_t*)input_driver_get_data();
49 
50    if (!qnx || port >= DEFAULT_MAX_PADS)
51       return 0;
52 
53    controller = (qnx_input_device_t*)&qnx->devices[port];
54 
55    if (joykey <= 19)
56       return ((controller->buttons & (1 << joykey)) != 0);
57    return 0;
58 }
59 
qnx_joypad_axis_state(qnx_input_t * qnx,qnx_input_device_t * controller,unsigned port,uint32_t joyaxis)60 static int16_t qnx_joypad_axis_state(
61       qnx_input_t *qnx,
62       qnx_input_device_t *controller,
63       unsigned port, uint32_t joyaxis)
64 {
65    int val             = 0;
66    int axis            = -1;
67    bool is_neg         = false;
68    bool is_pos         = false;
69 
70    if (AXIS_NEG_GET(joyaxis) < 4)
71    {
72       axis   = AXIS_NEG_GET(joyaxis);
73       is_neg = true;
74    }
75    else if (AXIS_POS_GET(joyaxis) < 4)
76    {
77       axis   = AXIS_POS_GET(joyaxis);
78       is_pos = true;
79    }
80 
81    switch (axis)
82    {
83       case 0:
84       case 1:
85          val = controller->analog0[axis];
86          break;
87       case 2:
88       case 3:
89          val = controller->analog1[axis-2];
90          break;
91    }
92 
93    if (is_neg && val > 0)
94       return 0;
95    else if (is_pos && val < 0)
96       return 0;
97    return val;
98 }
99 
qnx_joypad_axis(unsigned port,uint32_t joyaxis)100 static int16_t qnx_joypad_axis(unsigned port, uint32_t joyaxis)
101 {
102    qnx_input_t            *qnx    = (qnx_input_t*)input_driver_get_data();
103    qnx_input_device_t* controller = NULL;
104    if (!qnx || port >= DEFAULT_MAX_PADS)
105       return 0;
106    controller                     = (qnx_input_device_t*)&qnx->devices[port];
107    return qnx_joypad_axis_state(qnx, controller, port, joyaxis);
108 }
109 
qnx_joypad_state(rarch_joypad_info_t * joypad_info,const struct retro_keybind * binds,unsigned port)110 static int16_t qnx_joypad_state(
111       rarch_joypad_info_t *joypad_info,
112       const struct retro_keybind *binds,
113       unsigned port)
114 {
115    unsigned i;
116    int16_t ret                    = 0;
117    qnx_input_t            *qnx    = (qnx_input_t*)input_driver_get_data();
118    qnx_input_device_t* controller = NULL;
119    uint16_t port_idx              = joypad_info->joy_idx;
120 
121    if (!qnx || port_idx >= DEFAULT_MAX_PADS)
122       return 0;
123    controller                     = (qnx_input_device_t*)&qnx->devices[port_idx];
124 
125    for (i = 0; i < RARCH_FIRST_CUSTOM_BIND; i++)
126    {
127       /* Auto-binds are per joypad, not per user. */
128       const uint64_t joykey  = (binds[i].joykey != NO_BTN)
129          ? binds[i].joykey  : joypad_info->auto_binds[i].joykey;
130       const uint32_t joyaxis = (binds[i].joyaxis != AXIS_NONE)
131          ? binds[i].joyaxis : joypad_info->auto_binds[i].joyaxis;
132       if (
133                (uint16_t)joykey != NO_BTN
134             && (joykey <= 19)
135             && ((controller->buttons & (1 << (uint16_t)joykey)) != 0)
136          )
137          ret |= ( 1 << i);
138       else if (joyaxis != AXIS_NONE &&
139             ((float)abs(qnx_joypad_axis_state(qnx, controller, port_idx, joyaxis))
140              / 0x8000) > joypad_info->axis_threshold)
141          ret |= (1 << i);
142    }
143 
144    return ret;
145 }
146 
qnx_joypad_poll(void)147 static void qnx_joypad_poll(void) { }
148 
qnx_joypad_query_pad(unsigned pad)149 static bool qnx_joypad_query_pad(unsigned pad)
150 {
151    return (pad < MAX_USERS);
152 }
153 
qnx_joypad_destroy(void)154 static void qnx_joypad_destroy(void) { }
155 
156 input_device_driver_t qnx_joypad = {
157    qnx_joypad_init,
158    qnx_joypad_query_pad,
159    qnx_joypad_destroy,
160    qnx_joypad_button,
161    qnx_joypad_state,
162    NULL,
163    qnx_joypad_axis,
164    qnx_joypad_poll,
165    NULL,
166    qnx_joypad_name,
167    "qnx",
168 };
169