Announcement

Collapse
No announcement yet.

Polling winmm.dll for joystick axis position

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Polling winmm.dll for joystick axis position

    Hi all, long time since I was around these parts but once again APMS has me pulled back in again.

    I need some help if I may be so bold, regarding the use of the winmm.dll in system32 folder to get the current value of a joystick axis.

    I specifically need help making sense of how to call the dll function correctly, the documentation for C++ is here http://msdn.microsoft.com/en-gb/libr...=vs.85%29.aspx

    I basically need the current value of the Z axis on joystick 1.

    As always all help is appreciated.

  • #2
    I have not tested this, but this should work. If you'll be polling for a position very often, I suggest you allocate an instance of the info structure once and simply modify it via the call.

    Code:
    local winmm = Library.Load("Winmm.dll");
    if(not winmm)then
    	error("Winmm.dll could not be loaded");
    end
    
    -- the info structure
    JOYINFOEX = MemoryEx.DefineStruct{
    	UDWORD "dwSize";
    	UDWORD "dwFlags";
    	UDWORD "dwXpos";
    	UDWORD "dwYpos";
    	UDWORD "dwZpos";
    	UDWORD "dwRpos";
    	UDWORD "dwUpos";
    	UDWORD "dwVpos";
    	UDWORD "dwButtons";
    	UDWORD "dwButtonNumber";
    	UDWORD "dwPOV";
    	UDWORD "dwReserved1";
    	UDWORD "dwReserved2";
    };
    
    -- joyGetPosEx can return any of these values
    JOYERR_NOERROR 			= 0x00000000;
    MMSYSERR_NODRIVER 		= 0x00000006;
    MMSYSERR_INVALPARAM 	= 0x0000000B;
    MMSYSERR_BADDEVICEID	= 0x00000002;
    JOYERR_UNPLUGGED		= 0x000000A7;
    JOYERR_PARMS			= 0x000000A5;
    
    -- simple wrapper for the call, returns a new buffer - don't forget to free it! 
    JoyInfoEx = function(uJoyID) 
    	local info = JOYINFOEX:New();
    	if(info)then
    		local result = winmm.JoyInfoEx(uJoyID, info:GetPointer());
    		
    		return result, info;
    	end
    end;
    
    --[[ Usage: 
    	local res, info = JoyInfoEx(7);
    	if(info)then
    		if(res == JOYERR_NOERROR)then
    			Dialog.Message("test", tostring(info.dwXpos));
    		end
    		
    		info:Free();
    	end
    ]]
    Bas Groothedde
    Imagine Programming :: Blog

    AMS8 Plugins
    IMXLH Compiler

    Comment


    • #3
      Im sure you're on the right track there but the dialog outputs zero no matter which axis I attempt to poll.

      Comment


      • #4
        You are setting the correct identifier for the joystick in the function call?

        Comment


        • #5
          Yup tried 0-16 (I know there's 15 but wasn't sure if it was zero based)

          Comment


          • #6
            anyone with any further insight?

            Comment


            • #7
              Originally posted by Desrat View Post
              anyone with any further insight?
              I unfortunately have no joystick :(
              Bas Groothedde
              Imagine Programming :: Blog

              AMS8 Plugins
              IMXLH Compiler

              Comment


              • #8
                I used to load window's dlls with the alien module. I wrote a tested example of what you need. Hope it helps.

                PHP Code:
                require "alien";

                local tbl = {
                    [
                0x00000000] = 'JOYERR_NOERROR',
                    [
                0x00000006] = 'MMSYSERR_NODRIVER',
                    [
                0x0000000B] = 'MMSYSERR_INVALPARAM',
                    [
                0x00000002] = 'MMSYSERR_BADDEVICEID',
                    [
                0x000000A7] = 'JOYERR_UNPLUGGED',
                    [
                0x000000A5] = 'JOYERR_PARMS'
                }

                local winmm alien.load("C:\\Windows\\System32\\winmm.dll");

                local JOYINFOEX alien.defstruct{
                    {
                'dwSize''ulong'},
                    {
                'dwFlags''ulong'},
                    {
                'dwXpos''ulong'},
                    {
                'dwYpos''ulong'},
                    {
                'dwZpos''ulong'},
                    {
                'dwRpos''ulong'},
                    {
                'dwUpos''ulong'},
                    {
                'dwVpos''ulong'},
                    {
                'dwButtons''ulong'},
                    {
                'dwButtonNumber''ulong'},
                    {
                'dwPOV''ulong'},
                    {
                'dwReserved1''ulong'},
                    {
                'dwReserved2''ulong'}
                }

                winmm.joyGetPosEx:types{'uint''pointer'abi="stdcall"ret="uint"}
                winmm.joyGetNumDevs:types{abi="stdcall"ret="uint"}

                local joyGetNumDevs winmm.joyGetNumDevs
                local joyGetPosEx 
                winmm.joyGetPosEx;

                local nDevs joyGetNumDevs();
                local joyEx JOYINFOEX:new();

                joyEx.dwSize JOYINFOEX.size;
                joyEx.dwFlags 255; --JOY_RETURNALL
                JOYSTICKID 
                = -1;

                for 
                uJoyID 0,nDevs-do
                    
                local a =joyGetPosEx(uJoyIDjoyEx());
                    if (
                tbl[a] == 'JOYERR_NOERROR'then
                        JOYSTICKID 
                uJoyID;
                        print(
                JOYSTICKIDjoyEx.dwZposjoyEx.dwXposjoyEx.dwYpos);
                        break;
                    
                end
                end 

                Comment


                • #9
                  Based on your code with the Alien module, this should be the equivalent with MemoryEx:

                  Code:
                  local winmm = Library.Load("Winmm.dll");
                  if(not winmm)then
                  	error("Winmm.dll could not be loaded");
                  end
                  
                  -- the info structure
                  JOYINFOEX = MemoryEx.DefineStruct{
                  	UDWORD "dwSize";
                  	UDWORD "dwFlags";
                  	UDWORD "dwXpos";
                  	UDWORD "dwYpos";
                  	UDWORD "dwZpos";
                  	UDWORD "dwRpos";
                  	UDWORD "dwUpos";
                  	UDWORD "dwVpos";
                  	UDWORD "dwButtons";
                  	UDWORD "dwButtonNumber";
                  	UDWORD "dwPOV";
                  	UDWORD "dwReserved1";
                  	UDWORD "dwReserved2";
                  };
                  
                  -- joyGetPosEx can return any of these values
                  JOYERR_NOERROR 			= 0x00000000;
                  MMSYSERR_NODRIVER 		= 0x00000006;
                  MMSYSERR_INVALPARAM 	= 0x0000000B;
                  MMSYSERR_BADDEVICEID	= 0x00000002;
                  JOYERR_UNPLUGGED		= 0x000000A7;
                  JOYERR_PARMS			= 0x000000A5;
                  
                  -- simple wrapper for the call, returns a new buffer - don't forget to free it! 
                  JoyInfoEx = function(uJoyID) 
                  	local info = JOYINFOEX:New();
                  	if(info)then
                  		info.dwSize  = MemoryEx.StructSize(JOYINFOEX);
                  		info.dwFlags = 255;
                  		local result = winmm.JoyInfoEx(uJoyID, info:GetPointer());
                  		
                  		return result, info;
                  	end
                  end;
                  
                  --[[ Usage: 
                  	local res, info = JoyInfoEx(7);
                  	if(info)then
                  		if(res == JOYERR_NOERROR)then
                  			Dialog.Message("test", tostring(info.dwXpos));
                  		end
                  		
                  		info:Free();
                  	end
                  ]]
                  Bas Groothedde
                  Imagine Programming :: Blog

                  AMS8 Plugins
                  IMXLH Compiler

                  Comment


                  • #10
                    Originally posted by Imagine Programming View Post
                    Based on your code with the Alien module, this should be the equivalent with MemoryEx:
                    Small error fixed below - thanks to both Imagine Programming and webultra for getting me the outcome I needed.

                    -- Original
                    Code:
                    local result = winmm.JoyInfoEx(uJoyID, info:GetPointer());
                    -- Corrected
                    Code:
                    local result = winmm.joyGetPosEx(uJoyID, info:GetPointer());

                    Comment


                    • #11
                      Originally posted by Desrat View Post
                      Small error fixed below - thanks to both Imagine Programming and webultra for getting me the outcome I needed.
                      Ah of course, that's a logical fix! So I assume you have it working now? :yes
                      Bas Groothedde
                      Imagine Programming :: Blog

                      AMS8 Plugins
                      IMXLH Compiler

                      Comment


                      • #12
                        Originally posted by Imagine Programming View Post
                        So I assume you have it working now? :yes
                        Yep, working great, however would you be able to explain what this entails though, as I do intend to call the function repeatedly.

                        I suggest you allocate an instance of the info structure once and simply modify it via the call.

                        Comment


                        • #13
                          A structure is really just a table and when you call the function it updates the table with new information that you can then use, simple as that really.

                          If your doing this repetitively then change the function so that JOYINFOEX:New() is called just once on Global and info.dwSize with info.dwFlags are set just once as well as you don't need to keep creating a new structure object as you can just update the existing one and that will speed things up.

                          Comment


                          • #14
                            Something like this:

                            Code:
                            -- Global
                            
                            winmm = Library.Load("Winmm.dll");
                            
                            JOYINFOEX = MemoryEx.DefineStruct{
                            	UDWORD "dwSize";
                            	UDWORD "dwFlags";
                            	UDWORD "dwXpos";
                            	UDWORD "dwYpos";
                            	UDWORD "dwZpos";
                            	UDWORD "dwRpos";
                            	UDWORD "dwUpos";
                            	UDWORD "dwVpos";
                            	UDWORD "dwButtons";
                            	UDWORD "dwButtonNumber";
                            	UDWORD "dwPOV";
                            	UDWORD "dwReserved1";
                            	UDWORD "dwReserved2";
                            };
                            
                            JOYERR_NOERROR        = 0x00000000;
                            MMSYSERR_NODRIVER     = 0x00000006;
                            MMSYSERR_INVALPARAM   = 0x0000000B;
                            MMSYSERR_BADDEVICEID  = 0x00000002;
                            JOYERR_UNPLUGGED      = 0x000000A7;
                            JOYERR_PARMS          = 0x000000A5;
                            
                            Jinfo = JOYINFOEX:New();
                            Jinfo.dwSize  = JOYINFOEX:Size();
                            Jinfo.dwFlags = 255;
                            
                            JoyInfoEx = function(uJoyID)
                              return winmm.JoyInfoEx(uJoyID, Jinfo:GetPointer());
                            end;
                            
                            --[[ Usage:
                            	local res = JoyInfoEx(7);
                            	if(res == JOYERR_NOERROR)then
                            	 Dialog.Message("test", tostring(Jinfo.dwXpos));
                            	end
                            ]]

                            Comment


                            • #15
                              ok cool, I get the idea - cheers

                              Comment

                              Working...
                              X