File: perlinuxjoy.c

package info (click to toggle)
yabause 0.9.11.1-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 10,964 kB
  • sloc: ansic: 139,637; cpp: 6,793; asm: 5,797; objc: 986; pascal: 609; makefile: 335; sh: 54
file content (136 lines) | stat: -rw-r--r-- 3,151 bytes parent folder | download
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
/*  Copyright 2009 Guillaume Duhamel

	This file is part of Yabause.

	Yabause 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.

	Yabause 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 Yabause; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "debug.h"
#include "perlinuxjoy.h"
#include <linux/joystick.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

int  PERLinuxJoyInit(void);
void PERLinuxJoyDeInit(void);
int  PERLinuxJoyHandleEvents(void);
void PERLinuxJoyNothing(void);
u32  PERLinuxJoyScan(void);
void PERLinuxJoyFlush(void);
void PERLinuxKeyName(u32 key, char * name, int size);

PerInterface_struct PERLinuxJoy = {
PERCORE_LINUXJOY,
"Linux Joystick Interface",
PERLinuxJoyInit,
PERLinuxJoyDeInit,
PERLinuxJoyHandleEvents,
PERLinuxJoyNothing,
PERLinuxJoyScan,
1,
PERLinuxJoyFlush
#ifdef PERKEYNAME
,PERLinuxKeyName
#endif
};

static int hJOY = -1;

#define PACKEVENT(evt) ((evt.value < 0 ? 0x10000 : 0) | (evt.type << 8) | (evt.number))

//////////////////////////////////////////////////////////////////////////////

int PERLinuxJoyInit(void)
{
   hJOY = open("/dev/input/js0", O_RDONLY | O_NONBLOCK);

   if (hJOY == -1) return -1;

   return 0;
}

//////////////////////////////////////////////////////////////////////////////

void PERLinuxJoyDeInit(void)
{
   if (hJOY != -1) close(hJOY);
}

//////////////////////////////////////////////////////////////////////////////

void PERLinuxJoyNothing(void)
{
}

//////////////////////////////////////////////////////////////////////////////

int PERLinuxJoyHandleEvents(void)
{
   struct js_event evt;

   if (hJOY == -1) return -1;

   while (read(hJOY, &evt, sizeof(struct js_event)) > 0)
   {
      if (evt.value != 0)
      {
         PerKeyDown(PACKEVENT(evt));
      }
      else
      {
         PerKeyUp(PACKEVENT(evt));
         PerKeyUp(0x10000 | PACKEVENT(evt));
      }
   }

   // execute yabause
   if ( YabauseExec() != 0 )
   {
      return -1;
   }
   
   // return success
   return 0;
}

//////////////////////////////////////////////////////////////////////////////

u32 PERLinuxJoyScan(void) {
   struct js_event evt;

   if (hJOY == -1) return 0;

   if (read(hJOY, &evt, sizeof(struct js_event)) <= 0) return 0;

   return PACKEVENT(evt);
}

//////////////////////////////////////////////////////////////////////////////

void PERLinuxJoyFlush(void) {
   struct js_event evt;

   if (hJOY == -1) return;

   while (read(hJOY, &evt, sizeof(struct js_event)) > 0);
}

//////////////////////////////////////////////////////////////////////////////

void PERLinuxKeyName(u32 key, char * name, UNUSED int size)
{
   sprintf(name, "%x", (int)key);
}