File: Joystick.cpp

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (174 lines) | stat: -rwxr-xr-x 4,499 bytes parent folder | download | duplicates (7)
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
// Joystick.cpp : Defines the entry point for the DLL application.
//

#include "StdAfx.h"



#if PSYCH_SYSTEM == PSYCH_WINDOWS

BOOL WINAPI LibMain(
	HINSTANCE hinstDLL,  //handle to this DLL module
	DWORD fdwReason,     //reason for calling function
	LPVOID lpReserved)	 //reserved

{

	//perform actions based on the reason for calling.
	switch(fdwReason)
	{
		case DLL_PROCESS_ATTACH:

			break;
		case DLL_THREAD_ATTACH:

			break;
		case DLL_THREAD_DETACH:

			break;
		case DLL_PROCESS_DETACH:

			break;
	}
			

	return TRUE;  //successful DLL_PROCESS_ATTACH
}
#endif


static MexFunctionPtr SelectFunction(char *command)
{
	ProjectTable *joystickTable=GetProjectTable();

	// See if help is being requested
	if (command[strlen(command)-1] == '?') {
		joystickTable->giveHelp=TRUE;
		command[strlen(command)-1]=0;
	}
	else {
		joystickTable->giveHelp=FALSE;
	}
	
	// Recommended commands
	if(PsychMatch(command,"GetNumJoysticks"))return &JOYSTICKGetNumJoysticks;
	if(PsychMatch(command,"GetNumButtons"))return &JOYSTICKGetNumButtons;
	if(PsychMatch(command,"GetNumAxes"))return &JOYSTICKGetNumAxes;
	if(PsychMatch(command,"GetNumBalls"))return &JOYSTICKGetNumBalls;
	if(PsychMatch(command,"GetNumHats"))return &JOYSTICKGetNumHats;
	if(PsychMatch(command,"GetButton"))return &JOYSTICKGetButton;
	if(PsychMatch(command,"GetAxis"))return &JOYSTICKGetAxis;
	if(PsychMatch(command,"GetBall"))return &JOYSTICKGetBall;
	if(PsychMatch(command,"GetHat"))return &JOYSTICKGetHat;
	if(PsychMatch(command,"GetJoystickName"))return &JOYSTICKGetJoystickName;
	if(PsychMatch(command,"GetJoystickNumbersFromName"))return &JOYSTICKGetJoystickNumbersFromName;
	if(PsychMatch(command,"Unplug"))return &JOYSTICKUnplug;

	// Unknown command.
	return NULL;
}





/*
ROUTINE: mexFunction
PURPOSE:
	Main entry point.  Serves as a dispatch and handles
	first time initialization.
*/

#define MAX_SYNOPSIS 100
#define MAX_CMD_NAME_LENGTH 100

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, CONSTmxArray *prhs[])
{
	ProjectTable *joystickTable=GetProjectTable();
	static Boolean firstTime = TRUE;
	static char *synopsis[MAX_SYNOPSIS];
	MexFunctionPtr f;
	char command[MAX_CMD_NAME_LENGTH];

	// Initialization
	if (firstTime) {
		
		if (SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0){
			PrintfExit("Could not initialize SDL: %s\n", SDL_GetError());
		}
		mexPrintf("\nDEBUGGING VERSION OF JOYSTICK\n\n");
		
		
		InitializeSynopsis(synopsis,sizeof(synopsis)/sizeof(synopsis[0]));

		// Install cleanup routine
		mexAtExit(&JoystickExitFunction);
	
		firstTime = FALSE;
	}

	// If no args passed, we just print out a synopsis
	if (nrhs == 0) {	
		int i;
		for (i = 0; synopsis[i] != NULL; i++) {
			printf("%s\n",synopsis[i]);
		}
		return;
	}

	// If windowPtr or [] is passed, remove the arg, and save it for later
	if(IsDefaultMat(prhs[0]) || mxIsNumeric(prhs[0])){
		joystickTable->joystickNumberArgument=prhs[0];
		nrhs--;
		prhs++;
	}else {
		joystickTable->joystickNumberArgument=NULL;
	}

	// Get command from first remaining argument
	if (!mxIsChar(prhs[0])) {
		PrintfExit("Usage: Joystick('Command',...);\n");
	}
	mxGetString(prhs[0],command,sizeof(command));
	nrhs--;
	prhs++;
	// Call the appropriate routine
	f=SelectFunction(command);
	if (f == NULL) {
		PrintfExit("Joystick: unknown '%s' command.",command);
	}
	(*f)(nlhs,plhs,nrhs,prhs);

	// Superfluous tidying up.
	joystickTable->giveHelp=0;
}



void InitializeSynopsis(char *synopsis[],int maxStrings)
{
	int i=0;

	synopsis[i++] = "Usage:";
	synopsis[i++] = "\n% Get information on joystick availability.";
	synopsis[i++] = useGetNumJoysticks;
	synopsis[i++] = useGetJoystickName;
	synopsis[i++] = useGetJoystickNumbersFromName;
	synopsis[i++] = "\n% Get information on joystick configuration.";
	synopsis[i++] = useGetNumButtons;
	synopsis[i++] = useGetNumAxes;
	synopsis[i++] = useGetNumBalls;	
	synopsis[i++] = useGetNumHats;
	synopsis[i++] = "\n% Read joystick states.";
	synopsis[i++] = useGetButton;
	synopsis[i++] = useGetAxis;
	synopsis[i++] = useGetBall;
	synopsis[i++] = useGetHat;
	synopsis[i++] = "\n% Unplug a joystick.";
	synopsis[i++] = useUnplug;
	synopsis[i++] = NULL;
	if (i > maxStrings) {
		PrintfExit("%s: increase dimension of synopsis[] from %ld to at least %ld and recompile.",__FILE__,(long)maxStrings,(long)i);
	}
}