Xamarin.Mac and Joysticks

Andreas_GAndreas_G DEUniversity ✭✭

Hi everybody,

I’m looking for the best practise to connect and poll common USB joysticks (their axis values in particular, no gamepads) with Xamarin.Mac for a Mac OS desktop app.

I have done quite a lot of research for about 1 week but have not found a robust and well running solution yet, unfortunately. What I have tried so far, with Logitech Extreme 3D Pro joystick connected (well detected by Controllers Lite tool).

  • UrhoSharp3D: Getting axis data works well but focus has always to be on an UrhoSharp3D application window otherwise joystick cannot be polled. But I do not want to have an extra window besides my Xamarin.Mac app.

  • MonoGame: No connected joystick found

  • MonoTK / OpenTK: There seems to be no compatible SDL2.dll

  • Sharpex2D: Sharpex2D.Game.Input is always null

  • Xamarin.Apple.iOSTVMac: Apple API GameController.GCController does not detect the joystick

Has anybody a good approach how to get the joystick working with a Xamarin.Mac app, or is there any different useful package/component available e.g. at Nuget?

Kind regards,

Andreas

Posts

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    I'm honestly surprised GController doesn't have anything. That's the Apple API.

    Have you tried using it directly, and not through Xamarin.Apple.iOSTVMac? Not that I'd expect any change.

    If that does not work, you are very likely going to have to dig into IOKit. Which is a HUGE library that we do not have bindings for, nor any short term plans to add.

    Something like: https://delog.wordpress.com/2012/04/27/access-usb-device-on-mac-os-x-using-io-kit/

    You could write p/invokes for every IOKit API used, but that might be rather painful.

    If I was tackling this, I'd write a small objective-c shared library in Xcode which uses IOKit and exposes a few entry points (Setup, Read Value, Close). Then you can bind those to C# using a binding library, and consume that from C#.

    Depending on how comfortable you are with C/Objective-C/C#, this may be challenging.

  • Andreas_GAndreas_G DEUniversity ✭✭
    edited March 2017

    Thank you very much for your help, Chris.

    It seems that GameController API is not available for Xamarin.Mac (MonoMac), only for iOS? This is why I tried Xamarin.Apple.iOSTVMac instead (through PCL).

    I will do some more research… If anybody knows a class/component for accessing (real) joysticks which is quite simple to use, any hint would be highly appreciated.

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    MonoMac? What assembly are you referencing?

    Any new work should be on Xamarin.Mac Unified (Xamarin.Mac.dll).

    I can access GameController.GCController just fine.

  • Andreas_GAndreas_G DEUniversity ✭✭

    Ouch… You are absolutely right. I used Xamarin.Mac.dll but had selected target „Framework.Mac Mobile“ instead of „Xamarin.Mac .NET 4.5“ for this attempt. „Xamarin.Mac .NET 4.5“ allows to access GameController. I will try this way…

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    Hmm. Both Mobile and XM 4.5 should have GameController available. Could you double check?

    You can read some details about the target frameworks here:

    https://developer.xamarin.com/guides/mac/advanced_topics/target-framework/

    And some history here if you are curious:

    https://medium.com/@donblas/a-history-lesson-on-the-xamarin-mac-target-frameworks-and-their-new-names-473d2731d887

  • Andreas_GAndreas_G DEUniversity ✭✭
    edited March 2017

    Finally I could access GameController.GCController.Controllers directly (with Nimbus Wireless Controller). But this seems to work for gamecontrollers with Apple MFi support only.

    For all other gamecontrollers IOKit bindings seem to be required. If somebody knows some binding libraries for IOKit/HID which have already been implemented, any feedback is welcome.

  • pratamabayupratamabayu USMember ✭✭

    Hi @Andreas_G

    Let me to introduce Impression, a C# cross platform game toolkit to create 2D and 3D games.

    Impression can be deployed to Mac platform. It has various input such as Mouse, Keyboard, GamePad, Touch, Gestures, Accelerometer, Magnetometer, and Gyrometer. Maybe this is useful for you.

    Please visit http://impression.id

    Kind regards,
    pratamabayu

  • Andreas_GAndreas_G DEUniversity ✭✭

    Hi pratamabayu,

    this sounds very interesting. I will check it out.

    I'm still looking for a solution to integrate general USB joysticks to my Xamarin.Mac solutions like the Logitech Extreme 3D. At the moment I can only use MFi (Apple certified) devices like the SteelSeries Nimbus gamepad with GCController class. Do you maybe have a solution for general joystick usage?

    Best regards,

    Andreas

  • Pierre-ChristopheDusPierre-ChristopheDus FRUniversity ✭✭✭

    Hi @Andreas_G did you found a solution to use a joystick in a Xamarin.Forms app on Mac?

  • Andreas_GAndreas_G DEUniversity ✭✭

    Hi Pierre-ChristopheDus,

    I'm working on Xamarin Classic projects, not Forms. I tried quite a lot of ways to read out the joysticks natively but had no success. Maybe there has become a solution available since March 2017?

    However, I build my own quite simple solution in meanwhile. Add a hidden webview containing a javascript webpage, polling the HTML5 Gamecontroller object all the time. In your C# code you can evaluate JS functions of the webpage to get the joystick data.

    This works for many apps which need the standard x/y axis and buttons only. One big disadvantage is that there is a short delay because of JS evaluation etc of 100-200ms. Hope that there will become support available directly in Xamarin soon.

    BR, Andreas

  • Pierre-ChristopheDusPierre-ChristopheDus FRUniversity ✭✭✭

    Hi @Andreas_G
    Thanks for your feedback.
    I found some samples where GCController.Controllers are used.
    I've made tests with 2 controllers, that are connected in USB:

    • 1 XBox 360 controller, after having installed a driver
    • 1 PS4 controller, without having installed a driver

    The XBox 360 controller is not recognized in the Xamarin app, but the PS4 controller is well found. However, the directional keys don't seem to work, whereas other buttons work well (jump or shoot).
    => Would you have an explanation about this?

    For my case, the latence should be a problem, so I'm looking to do it with Unity if it's not possible to fix the directional keys issue.

  • Pierre-ChristopheDusPierre-ChristopheDus FRUniversity ✭✭✭

    @Andreas_G
    Would you have a sample of your architecture implementation?

  • Andreas_GAndreas_G DEUniversity ✭✭

    Hi Pierre-Christophe,

    I tried to point you into the right direction by giving you some hints how such a joystick application can be build with Xamarin.Mac only, without any additional components.

    But it took quite a lot of time to build the class which interprets all the HTML5 Gamecontroller information provided by JavaScript in the WebView continously, working for as many joysticks as possible, so we cannot provide more information on this just on the fly. Thank you for understanding.

  • Pierre-ChristopheDusPierre-ChristopheDus FRUniversity ✭✭✭

    Hi @Andreas_G
    Thanks again for your feedback.
    Finally the client has chosen a native Mac OS development.

Sign In or Register to comment.