Page 1 of 1

apifunctions.txt

Posted: Tue Apr 10, 2012 7:14 am
by J. Baker
In the compilers folder of PB, there's an "apifunctions.txt". Are these available to use in PB? If so, how do you import them or call on them? As it says the function/api doesn't exist.

Reason being, is that the joystick has a bug and I don't want to have to wait for the Cocoa version before this gets fixed. So I thought if I could use the api's some how, I could just fix the issue myself. That is, if it could be solved in that manner. Any info would be great, thanks!

Re: apifunctions.txt

Posted: Tue Apr 10, 2012 8:43 am
by Fred
apifunctions.txt is just for the help text which is displayed in the IDE. To import carbon fonctions, the easiest is to use ImportC/EndImport

Re: apifunctions.txt

Posted: Tue Apr 10, 2012 6:01 pm
by J. Baker
Thanks Fred. ;)

Re: apifunctions.txt

Posted: Tue Apr 10, 2012 11:11 pm
by Shardik
J. Baker wrote:Reason being, is that the joystick has a bug and I don't want to have to wait for the Cocoa version before this gets fixed. So I thought if I could use the api's some how, I could just fix the issue myself. That is, if it could be solved in that manner. Any info would be great, thanks!
Joe,

take a look into Apple's HID Class Device Interface Guide. HID (=Human Interface Device) devices are supported by Apple with two types of interfaces: the low level I/O Kit and the easier HID Manager API (introduced with MacOS X 10.5 "Leopard"). For a further understanding you should also take a look into the Wikipedia article USB human interface device class and Apple's Accessing Hardware From Applications.

You may also download Apple's complete C code example project HID Calibrator. You can compile it with Xcode to an app, execute it and calibrate your joystick. It's similar to the Joystick and Gamepad Tester for Mac to which you posted a link in another thread. After testing and analyzing the C code you can try to port the C source code to PB. But be aware: it's very heavy stuff... :twisted:

To help you I have already started in trying to port it to PB but I am still in the beginning and my progress is quite slow. Maybe you are quicker than me... :wink:

Re: apifunctions.txt

Posted: Tue Apr 10, 2012 11:49 pm
by J. Baker
Thanks for the info. Just started reading through the HID earlier. You would have thought Apple would have made api specific to joystick but either way, it will be a learning experience. ;)

Re: apifunctions.txt

Posted: Wed Apr 11, 2012 7:15 am
by J. Baker
Ok, I got SDL joystick showing the number of joysticks connected. Still more work to do. You'll need to download the SDL framework from http://www.libsdl.org/ if you want to test it out. ;)

Code: Select all

ImportC "/System/Library/Frameworks/SDL.framework/SDL"
  SDL_Init(flags)
  SDL_Quit()
  SDL_InitSubSystem (flags)
  SDL_QuitSubSystem(flags)
  SDL_NumJoysticks()
  SDL_JoystickName(device_index)
  SDL_JoystickOpen(device_index)
  SDL_JoystickOpened(device_index)
  SDL_JoystickIndex(*joystick)
  SDL_JoystickNumAxes(*joystick)
  SDL_JoystickNumBalls(*joystick)
  SDL_JoystickNumHats(*joystick)
  SDL_JoystickNumButtons(*joystick)
  SDL_JoystickUpdate()
  SDL_JoystickEventState(state)
  SDL_JoystickGetAxis(*joystick, axis)
  SDL_JoystickGetHat(*joystick, hat)
  SDL_JoystickGetBall(*joystick, ball, *dx, *dy)
  SDL_JoystickGetButton(*joystick, button)
  SDL_JoystickClose(*joystick)
EndImport

#SDL_INIT_JOYSTICK = 512

SDL_Init(#SDL_INIT_JOYSTICK)

 SDL_InitSubSystem(#SDL_INIT_JOYSTICK)

  Num = SDL_NumJoysticks()

  Debug Num
  
   SDL_JoystickOpen(0)
  
    JoyName = SDL_JoystickName(0)
  
    Debug JoyName
    
;   Repeat
;     
;     SDL_JoystickUpdate()
;     
;      B = SDL_JoystickGetButton(0, 1)
;     
;       Debug B
;     
;     Delay(10)
;     
;   ForEver
    
   SDL_JoystickClose(0)
  
 SDL_QuitSubSystem(#SDL_INIT_JOYSTICK)

SDL_Quit()

Re: apifunctions.txt

Posted: Wed Apr 11, 2012 8:22 pm
by Shardik
Joe,

thank you for your hint to use the SDL framework. It's so much easier
to use than the HID Class Device interface. But as a disadvantage you
have to deliver and install the SDL framework on your customer's Mac.
SDL 1.2 is licensed under GNU LGPL version 2 and may only be used
freely in commercial programs as long as it is linked with the dynamic
library...

I have found out that in PB on the Mac most of the SDL functions are
predefined with a trailing underscore but if I try to use for example
SDL_Init_(512) I obtain the error
PB IDE wrote:SDL_Init_() is not a function, array, macro or linked list
In your example you display only the pointer address of the joystick's
name. You should use PeekS() to read the name. Unfortunately I can't
test this because my Thrustmaster gamepad doesn't seem to have stored
a name.

I have programmed my own example which loops through all connected
joysticks and displays the queried informations. I have tried to catch most
of the possible errors and display an error message:

Code: Select all

ImportC "/System/Library/Frameworks/SDL.framework/SDL"
  SDL_Init(Flags.L)
  SDL_Quit()
  SDL_NumJoysticks()
  SDL_JoystickName(JoystickIndex.L)
  SDL_JoystickOpen(JoystickIndex.L)
  SDL_JoystickNumAxes(*JoystickID)
  SDL_JoystickNumBalls(*JoystickID)
  SDL_JoystickNumButtons(*JoystickID)
  SDL_JoystickClose(JoystickID)
EndImport

#SDL_INIT_JOYSTICK = 512

If SDL_Init(#SDL_INIT_JOYSTICK) <> 0
  Debug "SDL initialization failed"
Else
  NumJoysticks = SDL_NumJoysticks()
  
  Select NumJoysticks
    Case 0
      Debug "No joystick connected!"
    Case 1 To 8
      Debug "Connected joysticks: " + Str(NumJoysticks)
      Debug ""

      For i = 1 To NumJoysticks
        JoystickID = SDL_JoystickOpen(i - 1)
        
        If JoystickID = 0
          Debug "Error opening joystick #" + Str(i)
        Else
          Debug "Data of joystick #" + Str(i) + ":"
          *JoystickName = SDL_JoystickName(i - 1)

          If *JoystickName <> 0
            If PeekB(*JoystickName) <> 0
              Debug "- Joystick name: " + PeekS(*JoystickName)
            EndIf
          EndIf

          Debug "- Number of axes: " + Str(SDL_JoystickNumAxes(JoystickID))
          Debug "- Number of buttons: " + Str(SDL_JoystickNumButtons(JoystickID))
          Debug "- Number of balls: " + Str(SDL_JoystickNumBalls(JoystickID))
          
          SDL_JoystickClose(JoystickID)
        EndIf
      Next i
    Default
      Debug "Error querying connected joysticks"
  EndSelect      
  
  SDL_Quit()
EndIf

Re: apifunctions.txt

Posted: Wed Apr 11, 2012 8:51 pm
by J. Baker
Yeah, it was the easier way then HID. HID would take more time.

Your code works good. I have a ThrustMaster myself.
Connected joysticks: 1

Data of joystick #1:
- Joystick name: USB 2-Axis 8-Button Gamepad
- Number of axes: 2
- Number of buttons: 8
- Number of balls: 0
Thanks for the PeekS() info. I'll add that to my other sdl joystick post. ;)

My gamepad must be female. :lol: