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
|
/* OnePAD - author: arcum42(@gmail.com)
* Copyright (C) 2009
*
* Based on ZeroPAD, author zerofrog@gmail.com
* Copyright (C) 2006-2007
*
* 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
*/
#include "GamePad.h"
#include "onepad.h"
#include "keyboard.h"
#include <string.h>
#include <gtk/gtk.h>
#include "linux.h"
Display *GSdsp;
Window GSwin;
void SysMessage(const char *fmt, ...)
{
va_list list;
char msg[512];
va_start(list, fmt);
vsprintf(msg, fmt, list);
va_end(list);
if (msg[strlen(msg)-1] == '\n') msg[strlen(msg)-1] = 0;
GtkWidget *dialog;
dialog = gtk_message_dialog_new (NULL,
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO,
GTK_BUTTONS_OK,
"%s", msg);
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}
EXPORT_C_(void) PADabout()
{
SysMessage("OnePad is a rewrite of Zerofrog's ZeroPad, done by arcum42.");
}
EXPORT_C_(s32) PADtest()
{
return 0;
}
s32 _PADopen(void *pDsp)
{
GSdsp = *(Display**)pDsp;
GSwin = (Window)*(((u32*)pDsp)+1);
SetAutoRepeat(false);
return 0;
}
void _PADclose()
{
SetAutoRepeat(true);
vector<GamePad*>::iterator it = s_vgamePad.begin();
// Delete everything in the vector vjoysticks.
while (it != s_vgamePad.end())
{
delete *it;
it ++;
}
s_vgamePad.clear();
}
void PollForJoystickInput(int cpad)
{
int joyid = conf->get_joyid(cpad);
if (!GamePadIdWithinBounds(joyid)) return;
GamePad::UpdateGamePadState();
for (int i = 0; i < MAX_KEYS; i++)
{
GamePad* gamePad = s_vgamePad[joyid];
switch (type_of_joykey(cpad, i))
{
case PAD_JOYBUTTONS:
{
int value = gamePad->GetButton(key_to_button(cpad, i));
if (value)
key_status->press(cpad, i);
else
key_status->release(cpad, i);
break;
}
case PAD_HAT:
{
int value = gamePad->GetHat(key_to_axis(cpad, i));
// key_to_hat_dir and SDL_JoystickGetHat are a 4 bits bitmap, one for each directions. Only 1 bit can be high for
// key_to_hat_dir. SDL_JoystickGetHat handles diagonal too (2 bits) so you must check the intersection
// '&' not only equality '=='. -- Gregory
if (key_to_hat_dir(cpad, i) & value)
key_status->press(cpad, i);
else
key_status->release(cpad, i);
break;
}
case PAD_AXIS:
{
int value = gamePad->GetAxisFromKey(cpad, i);
bool sign = key_to_axis_sign(cpad, i);
bool full_axis = key_to_axis_type(cpad, i);
if (IsAnalogKey(i)) {
if (abs(value) > gamePad->GetDeadzone())
key_status->press(cpad, i, value);
else
key_status->release(cpad, i);
} else {
if (full_axis) {
value += 0x8000;
if (value > gamePad->GetDeadzone())
key_status->press(cpad, i, min(value/256 , 0xFF));
else
key_status->release(cpad, i);
} else {
if (sign && (-value > gamePad->GetDeadzone()))
key_status->press(cpad, i, min(-value /128, 0xFF));
else if (!sign && (value > gamePad->GetDeadzone()))
key_status->press(cpad, i, min(value /128, 0xFF));
else
key_status->release(cpad, i);
}
}
}
default: break;
}
}
}
EXPORT_C_(void) PADupdate(int pad)
{
// Gamepad inputs don't count as an activity. Therefore screensaver will
// be fired after a couple of minute.
// Emulate an user activity
static int count = 0;
count++;
if ((count & 0xFFF) == 0) {
// 1 call every 4096 Vsync is enough
XResetScreenSaver(GSdsp);
}
// Actually PADupdate is always call with pad == 0. So you need to update both
// pads -- Gregory
for (int cpad = 0; cpad < 2; cpad++) {
// Poll keyboard/mouse event
key_status->keyboard_state_acces(cpad);
PollForX11KeyboardInput(cpad);
// Get joystick state
key_status->joystick_state_acces(cpad);
PollForJoystickInput(cpad);
key_status->commit_status(cpad);
}
}
EXPORT_C_(void) PADconfigure()
{
LoadConfig();
DisplayDialog();
return;
}
|