ff4ed467a53373bf9ad5500a89344b554587d544
[pcsx_rearmed.git] / plugins / dfinput / sdljoy.c
1 /*
2  * Copyright (c) 2009, Wei Mingzhi <whistler@openoffice.org>.
3  * All Rights Reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, see <http://www.gnu.org/licenses>.
17  */
18
19 #include "pad.h"
20
21 void InitSDLJoy() {
22         uint8_t                         i;
23
24         g.PadState[0].JoyKeyStatus = 0xFFFF;
25         g.PadState[1].JoyKeyStatus = 0xFFFF;
26
27         for (i = 0; i < 2; i++) {
28                 if (g.cfg.PadDef[i].DevNum >= 0) {
29                         g.PadState[i].JoyDev = SDL_JoystickOpen(g.cfg.PadDef[i].DevNum);
30                 } else {
31                         g.PadState[i].JoyDev = NULL;
32                 }
33         }
34
35         SDL_JoystickEventState(SDL_IGNORE);
36
37         InitAnalog();
38 }
39
40 void DestroySDLJoy() {
41         uint8_t                         i;
42
43         if (SDL_WasInit(SDL_INIT_JOYSTICK)) {
44                 for (i = 0; i < 2; i++) {
45                         if (g.PadState[i].JoyDev != NULL) {
46                                 SDL_JoystickClose(g.PadState[i].JoyDev);
47                         }
48                 }
49         }
50
51         for (i = 0; i < 2; i++) {
52                 g.PadState[i].JoyDev = NULL;
53         }
54 }
55
56 void CheckJoy() {
57         uint8_t                         i, j, n;
58
59         SDL_JoystickUpdate();
60
61         for (i = 0; i < 2; i++) {
62                 if (g.PadState[i].JoyDev == NULL) {
63                         continue;
64                 }
65
66                 for (j = 0; j < DKEY_TOTAL; j++) {
67                         switch (g.cfg.PadDef[i].KeyDef[j].JoyEvType) {
68                                 case AXIS:
69                                         n = abs(g.cfg.PadDef[i].KeyDef[j].J.Axis) - 1;
70
71                                         if (g.cfg.PadDef[i].KeyDef[j].J.Axis > 0) {
72                                                 if (SDL_JoystickGetAxis(g.PadState[i].JoyDev, n) > 16383) {
73                                                         g.PadState[i].JoyKeyStatus &= ~(1 << j);
74                                                 } else {
75                                                         g.PadState[i].JoyKeyStatus |= (1 << j);
76                                                 }
77                                         } else if (g.cfg.PadDef[i].KeyDef[j].J.Axis < 0) {
78                                                 if (SDL_JoystickGetAxis(g.PadState[i].JoyDev, n) < -16383) {
79                                                         g.PadState[i].JoyKeyStatus &= ~(1 << j);
80                                                 } else {
81                                                         g.PadState[i].JoyKeyStatus |= (1 << j);
82                                                 }
83                                         }
84                                         break;
85
86                                 case HAT:
87                                         n = (g.cfg.PadDef[i].KeyDef[j].J.Hat >> 8);
88
89                                         if (SDL_JoystickGetHat(g.PadState[i].JoyDev, n) & (g.cfg.PadDef[i].KeyDef[j].J.Hat & 0xFF)) {
90                                                 g.PadState[i].JoyKeyStatus &= ~(1 << j);
91                                         } else {
92                                                 g.PadState[i].JoyKeyStatus |= (1 << j);
93                                         }
94                                         break;
95
96                                 case BUTTON:
97                                         if (SDL_JoystickGetButton(g.PadState[i].JoyDev, g.cfg.PadDef[i].KeyDef[j].J.Button)) {
98                                                 g.PadState[i].JoyKeyStatus &= ~(1 << j);
99                                         } else {
100                                                 g.PadState[i].JoyKeyStatus |= (1 << j);
101                                         }
102                                         break;
103
104                                 default:
105                                         break;
106                         }
107                 }
108         }
109
110         CheckAnalog();
111 }