Joypad Scol plugin
scolplugin.cpp
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source file is part of OpenSpace3D
4For the latest info, see http://www.openspace3d.com
5
6Copyright (c) 2012 I-maginer
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU Lesser General Public License as published by the Free Software
10Foundation; either version 2 of the License, or (at your option) any later
11version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public License along with
18this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20http://www.gnu.org/copyleft/lesser.txt
21
22-----------------------------------------------------------------------------
23*/
24
25/*
26 XInput dll for scol
27 First version : may 2010
28 Author : Bastien BOURINEAU
29*/
30
37#include "Joypad.h"
38
40#ifdef SCOL_STATIC
41extern cbmachine ww;
42extern mmachine mm;
43#else
44cbmachine ww;
45mmachine mm;
46#endif
47
48#if !defined(_WIN32) && !defined (ANDROID)
49extern int(*CallMainThread)(void*, mmachine m);
50#endif
51
53
56
57
58#ifdef ANDROID
59AInputQueue* AndroidInputQueue;
60#endif
61
76int destroyJoypadObj(mmachine m, SCOL_PTR_TYPE handsys, int handscol)
77{
78 Joypad* joypad = MMgetPointer<Joypad*>(m, MTOP(handscol));
79 SAFE_DELETE(joypad);
80
81 MMsetPointer<Joypad*>(m, MTOP(handscol), 0);
82
83 // Display debug message
84 MMechostr(MSKDEBUG,"Joypad object destroyed.\n");
85 return 0;
86}
87
88
108#if defined(_WIN32) || defined (ANDROID)
109int _OpenJoypad(mmachine m)
110#else
111int _OpenJoypadSync(mmachine m)
112#endif
113{
114#ifdef _SCOL_DEBUG_
115 MMechostr(0,"_OpenJoypad\n");
116#endif
117 // Declare local variables
118 int k = 0;
119
120 // Get the channel without pulling it (first element in the stack)
121 int pUser = MMget(m, 0);
122 int cbfun = MMget(m, 1);
123 int channel = MMget(m, 2);
124
125 // Test the channel
126 if (channel == NIL || cbfun == NIL)
127 {
128 MMechostr(MSKDEBUG, "Channel NIL\n");
129 SEDROP(m, 2); MMset(m, 0, NIL);
130 return 0;
131 }
132
133 Joypad* joypad = new Joypad();
134 if (joypad == NULL || (joypad->IsRunning() == false))
135 {
136 MMechostr(MSKDEBUG,"joypad failed\n");
137 SAFE_DELETE(joypad);
138 SEDROP(m, 2); MMset(m, 0, NIL);
139 return 0;
140 }
141
142 // re-push channel to create object
143 if ((k = MMpush(m, MMget(m, 2))))
144 return k; //channel
145
146 // Allocate a space in the stack for a table of joypad object
147 if ((MMpushPointer(m, joypad) != 0))
148 {
149 SAFE_DELETE(joypad);
150 MMset(m, 0, NIL);
151 return MERRMEM;
152 }
153
154 // Create a new scol joypad object
155 k = OBJcreate(m, OBJJOYPADSCOL, SCOL_PTR joypad, NIL, 0);
156
157 /* add reflex */
158 if ((k = MMpush(m, MMget(m, 2))))
159 return k; /* reading callback */
160 if ((k = MMpush(m, MMget(m, 2))))
161 return k; /* user param */
162 if ((k = OBJaddreflex(m, OBJJOYPADSCOL, SCOL_JOYPAD_DATA_CB)))
163 return k;
164
165 k = MMpull(m); /* save joypad object */
166 m->pp+=3;
167
168#ifdef _SCOL_DEBUG_
169 MMechostr(0,"ok\n");
170#endif
171
172 return MMpush(m, k);
173}
174
195#if defined(_WIN32) || defined (ANDROID)
196int _OpenJoypadEx(mmachine m)
197#else
198int _OpenJoypadExSync(mmachine m)
199#endif
200{
201#ifdef _SCOL_DEBUG_
202 MMechostr(0, "_OpenJoypadEx\n");
203#endif
204 // Declare local variables
205 int k = 0;
206
207 // Get the channel without pulling it (first element in the stack)
208 int pUser = MMpull(m);
209 int cbfun = MMpull(m);
210 int sIndex = MTOI(MMpull(m));
211 int channel = MMget(m, 0);
212
213 // Test the channel
214 if (channel == NIL || cbfun == NIL)
215 {
216 MMechostr(MSKDEBUG, "Channel NIL\n");
217 MMset(m, 0, NIL);
218 return 0;
219 }
220
221 Joypad* joypad = new Joypad(sIndex);
222 if (joypad == NULL || (joypad->IsRunning() == false))
223 {
224 MMechostr(MSKDEBUG, "joypad failed\n");
225 SAFE_DELETE(joypad);
226 MMset(m, 0, NIL);
227 return 0;
228 }
229
230 //restore stack
231 MMpush(m, cbfun);
232 MMpush(m, pUser);
233
234 // re-push channel to create object
235 if ((k = MMpush(m, MMget(m, 2))))
236 return k; //channel
237
238 // Allocate a space in the stack for a table of joypad object
239 if ((MMpushPointer(m, joypad) != 0))
240 {
241 SAFE_DELETE(joypad);
242 MMset(m, 0, NIL);
243 return MERRMEM;
244 }
245
246 // Create a new scol joypad object
247 k = OBJcreate(m, OBJJOYPADSCOL, SCOL_PTR joypad, NIL, 0);
248
249 /* add reflex */
250 if ((k = MMpush(m, MMget(m, 2))))
251 return k; /* reading callback */
252 if ((k = MMpush(m, MMget(m, 2))))
253 return k; /* user param */
254 if ((k = OBJaddreflex(m, OBJJOYPADSCOL, SCOL_JOYPAD_DATA_CB)))
255 return k;
256
257 k = MMpull(m); /* save joypad object */
258 m->pp += 3;
259
260#ifdef _SCOL_DEBUG_
261 MMechostr(0, "ok\n");
262#endif
263
264 return MMpush(m, k);
265}
266
267#if !defined(_WIN32) && !defined (ANDROID)
268int _OpenJoypad(mmachine m)
269{
270 return CallMainThread((void*)_OpenJoypadSync, m);
271}
272
273int _OpenJoypadEx(mmachine m)
274{
275 return CallMainThread((void*)_OpenJoypadExSync, m);
276}
277#endif
278
286int _CloseJoypad(mmachine m)
287{
288#ifdef _SCOL_DEBUG_
289 MMechostr(MSKDEBUG,"_CloseJoypad\n");
290#endif
291
292 int joypad = MMget(m, 0);
293 if (joypad == NIL)
294 {
295 MMset(m, 0, NIL);
296 return 0;
297 }
298
299 OBJdelTM(m, OBJJOYPADSCOL, joypad);
300 MMset(m, 0, ITOM(0));
301
302#ifdef _SCOL_DEBUG_
303 MMechostr(MSKDEBUG,"ok\n");
304#endif
305 return 0;
306}
307
317int _StartJoypadRumble(mmachine m)
318{
319#ifdef _SCOL_DEBUG_
320 MMechostr(MSKDEBUG, "_StartJoypadRumble\n");
321#endif
322
323 int iintensity = MMpull(m);
324 int idevice = MMget(m, 0);
325 if (idevice == NIL)
326 {
327 MMset(m, 0, NIL);
328 return 0;
329 }
330
331 Joypad* joypad = MMgetPointer<Joypad*>(m, MTOP(idevice));
332 if (joypad == NULL)
333 {
334 MMset(m, 0, NIL);
335 return 0;
336 }
337
338 float intensity = 0.5f;
339 if (intensity != NIL)
340 intensity = MTOF(iintensity);
341
342 joypad->StartRumble(intensity);
343 MMset(m, 0, ITOM(0));
344
345#ifdef _SCOL_DEBUG_
346 MMechostr(MSKDEBUG, "ok\n");
347#endif
348 return 0;
349}
350
358int _StopJoypadRumble(mmachine m)
359{
360#ifdef _SCOL_DEBUG_
361 MMechostr(MSKDEBUG, "_StopJoypadRumble\n");
362#endif
363
364 int idevice = MMget(m, 0);
365 if (idevice == NIL)
366 {
367 MMset(m, 0, NIL);
368 return 0;
369 }
370
371 Joypad* joypad = MMgetPointer<Joypad*>(m, MTOP(idevice));
372 if (joypad == NULL)
373 {
374 MMset(m, 0, NIL);
375 return 0;
376 }
377
378 joypad->StopRumble();
379 MMset(m, 0, ITOM(0));
380
381#ifdef _SCOL_DEBUG_
382 MMechostr(MSKDEBUG, "ok\n");
383#endif
384 return 0;
385}
386
396int getJoypadData(mmachine m, SCOL_PTR_TYPE id, SCOL_PTR_TYPE param)
397{
398 int k = 0;
399
400 // Cast id parameter to Joypad type
401 Joypad * joypad = (Joypad*) id;
402 DIJOYSTATE2* ndata = (DIJOYSTATE2*)param;
403
404 // Use : OBJbeginreflex(mmachine, type of object, ptr object, callback type)
405 if (OBJbeginreflex(m, OBJJOYPADSCOL, SCOL_PTR joypad, SCOL_JOYPAD_DATA_CB))
406 {
407 MMechostr(MSKDEBUG,"Joypad not found\n");
408 return 0;
409 }
410
411 int tuple = MMmalloc(m, 2, TYPETAB);
412 if(tuple==NIL)
413 {
414 MMpush(m,NIL);
415 return MERRMEM;
416 }
417
418 MMstore(m, tuple, 0, ITOM(ndata->lX));
419 MMstore(m, tuple, 1, ITOM(ndata->lY));
420
421 MMpush(m, PTOM(tuple));
422 MMpush(m, ITOM(ndata->lZ));
423
424 int tuple2 = MMmalloc(m, 2, TYPETAB);
425 if(tuple2==NIL)
426 {
427 MMpush(m, NIL);
428 return MERRMEM;
429 }
430 MMstore(m, tuple2, 0, ITOM(ndata->lRx));
431 MMstore(m, tuple2, 1, ITOM(ndata->lRy));
432
433 MMpush(m,PTOM(tuple2));
434 MMpush(m,ITOM(ndata->lRz));
435
436 int tuple3 = MMmalloc(m, 4, TYPETAB);
437 if(tuple3==NIL)
438 {
439 MMpush(m, NIL);
440 return MERRMEM;
441 }
442 MMstore(m, tuple3, 0, ITOM(ndata->rgdwPOV[0]));
443 MMstore(m, tuple3, 1, ITOM(ndata->rgdwPOV[1]));
444 MMstore(m, tuple3, 2, ITOM(ndata->rgdwPOV[2]));
445 MMstore(m, tuple3, 3, ITOM(ndata->rgdwPOV[3]));
446
447 MMpush(m,PTOM(tuple3));
448
449 int tuple4 = MMmalloc(m, 2, TYPETAB);
450 if(tuple4==NIL)
451 {
452 MMpush(m, NIL);
453 return MERRMEM;
454 }
455 MMstore(m, tuple4, 0, ITOM(ndata->rglSlider[0]));
456 MMstore(m, tuple4, 1, ITOM(ndata->rglSlider[1]));
457
458 MMpush(m,PTOM(tuple4));
459
460 // get which buttons are pressed
461 for (int i = 0; i < 16; i++)
462 {
463#ifdef _WIN32
464 if( ndata->rgbButtons[i] & 0x80 )
465#else
466 if (ndata->rgbButtons[i] == 1)
467#endif
468 {
469 if(MMpush(m,ITOM(1)))
470 return MERRMEM;
471 }
472 else
473 {
474 if(MMpush(m,ITOM(0)))
475 return MERRMEM;
476 }
477 }
478
479 if(MMpush(m, NIL))
480 return MERRMEM;
481
482 for(int i = 0; i < 16; i++)
483 {
484 if(MMpush(m,2*2))
485 return MERRMEM;
486 if((k=MBdeftab(m)))
487 return k;
488 }
489
490 // Call reflex previously defined
491 k = OBJcallreflex(m, 7 /*nb param after obj and u0*/);
492 delete ndata;
493 return k;
494}
495
496static NativeDefinition sJoypadDef[] =
497{
498 { "ObjJoypad", TYPTYPE, NULL, NULL },
499 { "_OpenJoypad", 3, "fun [Chn fun [ObjJoypad u0 [I I] I [I I] I [I I I I] [I I] [I r1]] u1 u0] ObjJoypad", _OpenJoypad },
500 { "_OpenJoypadEx", 4, "fun [Chn I fun [ObjJoypad u0 [I I] I [I I] I [I I I I] [I I] [I r1]] u1 u0] ObjJoypad", _OpenJoypadEx },
501 { "_CloseJoypad", 1, "fun [ObjJoypad] I", _CloseJoypad },
502 { "_StartJoypadRumble", 2, "fun [ObjJoypad F] I", _StartJoypadRumble },
503 { "_StopJoypadRumble", 1, "fun [ObjJoypad] I", _StopJoypadRumble }
504};
505
506// Everything inside _cond and _endcond is ignored by doxygen
508
513int LoadJoypad(mmachine m)
514{
515 MMechostr(MSKDEBUG,"\n" );
516 MMechostr(MSKDEBUG," > Loading Joypad Support\n");
517
518#if !defined(_WIN32) && !defined (ANDROID)
519 CallMainThread=(int (__cdecl *)(void*, mmachine))SCgetExtra("callMainThread");
520#endif
521
522 // Declare a new type of object ("ObjJoypad")
523 OBJJOYPADSCOL = OBJregister(1 /*nb of callback*/, 1, destroyJoypadObj, "OBJJOYPADSCOL");
524
525 // ----- Define callbacks
526 // Get a new user event
527 JOYPAD_DATA_CB = OBJgetUserEvent();
528 // Associate this event with a callback
529 OBJdefEvent(JOYPAD_DATA_CB, getJoypadData);
530
531 int k = PKhardpak2(m, "JoypadEngine.pkg-1.0", sizeof(sJoypadDef) / sizeof(sJoypadDef[0]), sJoypadDef);
532
533#ifdef ANDROID
534 AndroidInputQueue = (AInputQueue*)SCgetExtra("inputQueue");
535#endif
536
537 MMechostr(MSKDEBUG," > Successfully Loaded\n\n");
538 return k;
539}
540
542
547#ifndef SCOL_STATIC
548extern "C" SCOL_EXPORT int ScolLoadPlugin(mmachine m, cbmachine w)
549#else
550extern "C" SCOL_EXPORT int ScolJoypadLoadPlugin(mmachine m, cbmachine w)
551#endif
552{
553 SCOLinitplugin(w);
554 return LoadJoypad(m);
555}
556
557
562#ifndef SCOL_STATIC
563extern "C" SCOL_EXPORT int ScolUnloadPlugin()
564#else
565extern "C" SCOL_EXPORT int ScolJoypadUnloadPlugin()
566#endif
567{
568 return 0;
569}
570
571#ifndef SCOL_STATIC
572extern "C" SCOL_EXPORT void ScolHandleWindowEventPlugin(mmachine m, SCOL_PTR_TYPE event, int wparam, int lparam)
573#else
574extern "C" SCOL_EXPORT void ScolJoypadHandleWindowEventPlugin(mmachine m, SCOL_PTR_TYPE event, int wparam, int lparam)
575#endif
576{
577#ifdef ANDROID
578 Joypad::updateInputEvent((AInputEvent*)event);
579#endif
580}
bool StartRumble(float intensity)
Definition Joypad.cpp:1033
void StopRumble()
Definition Joypad.cpp:1039
bool IsRunning()
Definition Joypad.cpp:1027
int _CloseJoypad(mmachine m)
_CloseJoypad : Destroy joypad object
int _OpenJoypadSync(mmachine m)
_OpenJoypad : This function open a joypad and init the data callback
int _StartJoypadRumble(mmachine m)
_StartJoypadRumble : Start joypad rumble
int _OpenJoypadExSync(mmachine m)
_OpenJoypadEx : This function open a joypad and init the data callback
int _StopJoypadRumble(mmachine m)
_StopJoypadRumble : Stop the joypad rumble
SCOL_EXPORT int ScolJoypadUnloadPlugin()
SCOL_EXPORT void ScolJoypadHandleWindowEventPlugin(mmachine m, SCOL_PTR_TYPE event, int wparam, int lparam)
SCOL_EXPORT int ScolJoypadLoadPlugin(mmachine m, cbmachine w)
int _OpenJoypadEx(mmachine m)
SCOL_EXPORT int ScolLoadPlugin(mmachine m, cbmachine w)
Starting point of the DLL Function to add in Scol usm.ini for loading dll.
int _OpenJoypad(mmachine m)
int destroyJoypadObj(mmachine m, SCOL_PTR_TYPE handsys, int handscol)
Destroys a joypad Object in scol.
int SCOL_JOYPAD_DATA_CB
cbmachine ww
Scol machine declaration for MM macros.
SCOL_EXPORT int ScolUnloadPlugin()
Ending point of the DLL Function to add in Scol usm.ini for free dll.
int JOYPAD_DATA_CB
SCOL_EXPORT void ScolHandleWindowEventPlugin(mmachine m, SCOL_PTR_TYPE event, int wparam, int lparam)
int(* CallMainThread)(void *, mmachine m)
int OBJJOYPADSCOL
mmachine mm
int getJoypadData(mmachine m, SCOL_PTR_TYPE id, SCOL_PTR_TYPE param)
LONG lRy
Definition Joypad.h:52
LONG lRx
Definition Joypad.h:51
LONG lZ
Definition Joypad.h:50
DWORD rgdwPOV[4]
Definition Joypad.h:55
LONG lRz
Definition Joypad.h:53
LONG lX
Definition Joypad.h:48
LONG rglSlider[2]
Definition Joypad.h:54
LONG lY
Definition Joypad.h:49
int rgbButtons[16]
Definition Joypad.h:56