File: wiijoystick.c

package info (click to toggle)
fuse-emulator 1.1.1%2Bdfsg1-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,120 kB
  • ctags: 8,968
  • sloc: ansic: 78,960; sh: 11,228; perl: 3,742; makefile: 1,104; yacc: 236; lex: 140
file content (199 lines) | stat: -rw-r--r-- 6,686 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* wiijoystick.c: routines for dealing with the Wiimote as a joystick
   Copyright (c) 2008-2009 Bjoern Giesler, Marek Januszewski

   $Id: wiijoystick.c 3994 2009-04-04 20:17:42Z specu $

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program; if not, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

   Author contact information:

   E-mail: bjoern@giesler.de

*/

#include <config.h>
#include "fuse.h"
#include "input.h"
#include "ui/ui.h"
#include "ui/uijoystick.h"

#define GEKKO
#include <wiiuse/wpad.h>

typedef enum {
  WII_JOYSTICK_SIDEWAYS_RH, /* sideways config (fire on the right) */
  WII_JOYSTICK_SIDEWAYS_LH, /* sideways config (fire on the left) */
  WII_JOYSTICK_NUNCHUK,     /* nunchuk for directions, A+B for fire */
  WII_JOYSTICK_CLASSIC,     /* classic controller */
} WiiJoystickConfig;

int
ui_joystick_init( void )
{
  return 4;
}

void
ui_joystick_end( void )
{
}

void
ui_joystick_poll( void )
{
  input_event_t fuse_event;
  int ctrlr; /* Which controller */
  u32 wm_down; /* Wii Remote buttons that are down */
  u32 wm_up; /* Wii Remote buttons that are up */
  ubyte nunchuck_down; /* Nunchuck buttons that are down */
  ubyte nunchuck_up; /* Nunchuck buttons that are up */
  WPADData *wpad;
  joystick_t js;

  if( fuse_emulation_paused ) return;

#define POST_JOYPRESS(number, pressed) do {	  \
    fuse_event.type = INPUT_EVENT_JOYSTICK_PRESS; \
    fuse_event.types.joystick.which = number; \
    fuse_event.types.joystick.button = pressed; \
    input_event(&fuse_event); \
  } while(0)
  
#define POST_JOYRELEASE(number, pressed) do {	    \
    fuse_event.type = INPUT_EVENT_JOYSTICK_RELEASE; \
    fuse_event.types.joystick.which = number; \
    fuse_event.types.joystick.button = pressed; \
    input_event(&fuse_event); \
  } while(0)

  for( ctrlr = 0; ctrlr < 2; ctrlr++ ) {

    wpad = WPAD_Data( ctrlr );
    if( !wpad ) continue;
  
    wm_down = wpad->btns_d;
    wm_up = wpad->btns_u;

    nunchuck_down = 0;
    nunchuck_up = 0;
    if( wpad->exp.type == EXP_NUNCHUK ) {
      nunchuck_down = wpad->exp.nunchuk.btns;
      nunchuck_up = wpad->exp.nunchuk.btns_released;
    }
  
    if( wm_down & WPAD_BUTTON_LEFT )
      POST_JOYPRESS( ctrlr, INPUT_JOYSTICK_DOWN );
    if( wm_down & WPAD_BUTTON_RIGHT )
      POST_JOYPRESS( ctrlr, INPUT_JOYSTICK_UP );
    if( wm_down & WPAD_BUTTON_UP )
      POST_JOYPRESS( ctrlr, INPUT_JOYSTICK_LEFT );
    if( wm_down & WPAD_BUTTON_DOWN )
      POST_JOYPRESS( ctrlr, INPUT_JOYSTICK_RIGHT );
  
    if( wm_down & WPAD_BUTTON_1 )
      POST_JOYPRESS( ctrlr, INPUT_JOYSTICK_FIRE_1 );
    if( wm_down & WPAD_BUTTON_2 )
      POST_JOYPRESS( ctrlr, INPUT_JOYSTICK_FIRE_2 );
  
    if( wm_down & WPAD_BUTTON_A )
      POST_JOYPRESS( ctrlr, INPUT_JOYSTICK_FIRE_3 );
    if( wm_down & WPAD_BUTTON_B )
      POST_JOYPRESS( ctrlr, INPUT_JOYSTICK_FIRE_4 );
    if( wm_down & WPAD_BUTTON_PLUS )
      POST_JOYPRESS( ctrlr, INPUT_JOYSTICK_FIRE_5 );
    if( wm_down & WPAD_BUTTON_MINUS )
      POST_JOYPRESS( ctrlr, INPUT_JOYSTICK_FIRE_6 );
    if( nunchuck_down & WPAD_NUNCHUK_BUTTON_Z )
      POST_JOYPRESS( ctrlr, INPUT_JOYSTICK_FIRE_7 );
    if( nunchuck_down & WPAD_NUNCHUK_BUTTON_C )
      POST_JOYPRESS( ctrlr, INPUT_JOYSTICK_FIRE_8 );
  
    if( wm_up & WPAD_BUTTON_LEFT )
      POST_JOYRELEASE( ctrlr, INPUT_JOYSTICK_DOWN );
    if( wm_up & WPAD_BUTTON_RIGHT )
      POST_JOYRELEASE( ctrlr, INPUT_JOYSTICK_UP );
    if( wm_up & WPAD_BUTTON_UP )
      POST_JOYRELEASE( ctrlr, INPUT_JOYSTICK_LEFT );
    if( wm_up & WPAD_BUTTON_DOWN )
      POST_JOYRELEASE( ctrlr, INPUT_JOYSTICK_RIGHT );
  
    if( wm_up & WPAD_BUTTON_1 )
      POST_JOYRELEASE( ctrlr, INPUT_JOYSTICK_FIRE_1 );
    if( wm_up & WPAD_BUTTON_2 )
      POST_JOYRELEASE( ctrlr, INPUT_JOYSTICK_FIRE_2 );
  
    if( wm_up & WPAD_BUTTON_A )
      POST_JOYRELEASE( ctrlr, INPUT_JOYSTICK_FIRE_3 );
    if( wm_up & WPAD_BUTTON_B )
      POST_JOYRELEASE( ctrlr, INPUT_JOYSTICK_FIRE_4 );
    if( wm_up & WPAD_BUTTON_PLUS )
      POST_JOYRELEASE( ctrlr, INPUT_JOYSTICK_FIRE_5 );
    if( wm_up & WPAD_BUTTON_MINUS )
      POST_JOYRELEASE( ctrlr, INPUT_JOYSTICK_FIRE_6 );
    if( nunchuck_up & WPAD_NUNCHUK_BUTTON_Z )
      POST_JOYRELEASE( ctrlr, INPUT_JOYSTICK_FIRE_7 );
    if( nunchuck_up & WPAD_NUNCHUK_BUTTON_C )
      POST_JOYRELEASE( ctrlr, INPUT_JOYSTICK_FIRE_8 );
      
    if( wpad->exp.type == EXP_NUNCHUK ) {

      js = wpad->exp.nunchuk.js;

      if( js.mag < 0.5 ) {
        /* stick tilted only halfway from the center - release all directions */
        POST_JOYRELEASE( ctrlr, INPUT_JOYSTICK_DOWN );
        POST_JOYRELEASE( ctrlr, INPUT_JOYSTICK_UP );
        POST_JOYRELEASE( ctrlr, INPUT_JOYSTICK_LEFT );
        POST_JOYRELEASE( ctrlr, INPUT_JOYSTICK_RIGHT );
      }
      else {
        int left_held = 0;
        int right_held = 0;
        int up_held = 0;
        int down_held = 0;
      	
        /* left */
        if( js.ang >= 270-60 && js.ang <= 270+60 ) {
          POST_JOYPRESS( ctrlr, INPUT_JOYSTICK_LEFT );
          left_held = 1;
        }
        /* right */
        if( js.ang >= 90-60 && js.ang <= 90+60 ) {
          POST_JOYPRESS( ctrlr, INPUT_JOYSTICK_RIGHT );
          right_held = 1;
        }
        /* up */
        if( js.ang >= 360-60 || js.ang <= 60 ) {
          POST_JOYPRESS( ctrlr, INPUT_JOYSTICK_UP );
          up_held = 1;
        }
        /* down */
        if( js.ang >= 180-60 && js.ang <= 180+60 ) {
          POST_JOYPRESS( ctrlr, INPUT_JOYSTICK_DOWN );
          down_held = 1;
        }
        
        /* the below prevents an issue when user makes 180 deg circle with
           the nunchuck from for example left to right while mag > 0.5, in
           which case spectrum would get both left and right signal from
           joystick. The below is to prevent that. */
        if( !down_held ) POST_JOYRELEASE( ctrlr, INPUT_JOYSTICK_DOWN );
        if( !up_held ) POST_JOYRELEASE( ctrlr, INPUT_JOYSTICK_UP );
        if( !left_held ) POST_JOYRELEASE( ctrlr, INPUT_JOYSTICK_LEFT );
        if( !right_held ) POST_JOYRELEASE( ctrlr, INPUT_JOYSTICK_RIGHT );
      }
    }
  }
}