Ticket #7429: usbjoy_v2.patch

File usbjoy_v2.patch, 14.0 KB (added by Disreali, 9 years ago)
  • src/add-ons/kernel/drivers/joystick/usb_joy/driver.h

     
    7777    void *buffer;
    7878    bool buffer_valid;
    7979
    80     const usb_device *dev;
     80    usb_device dev;
    8181    int number;
    8282
    8383    bool active;
     
    107107/* devmgmt.c */
    108108
    109109my_device_info *
    110 create_device (const usb_device *dev, const usb_interface_info *ii);
     110create_device (usb_device dev, const usb_interface_info *ii);
    111111
    112112void
    113113remove_device (my_device_info *my_dev);
  • src/add-ons/kernel/drivers/joystick/usb_joy/devmgmt.c

     
    1212static int device_number = 0;
    1313
    1414my_device_info *create_device (
    15     const usb_device *dev,
     15    usb_device dev,
    1616    const usb_interface_info *ii)
    1717{
    1818    my_device_info *my_dev = NULL;
     
    2121    sem_id sem;
    2222    char    area_name [32];
    2323
    24     assert (usb != NULL && dev != NULL);
     24    assert (usb != NULL);
    2525
    2626    number = device_number++;
    2727
  • src/add-ons/kernel/drivers/joystick/usb_joy/driver.c

     
    3636_EXPORT int32   api_version = B_CUR_DRIVER_API_VERSION;
    3737
    3838const char *my_driver_name = "usb_joy";
    39 const char *my_base_name = "joystick/usb/";
     39const char *my_base_name = "joystick/usb_haiku/";
    4040
    4141usb_module_info *usb;
    4242
     
    154154
    155155static void usb_callback (
    156156    void *cookie,
    157     uint32 status,
     157    status_t status,
    158158    void *data,
    159159    uint32 actual_len)
    160160{
     
    215215*/
    216216
    217217static status_t my_device_added (
    218     const usb_device *dev,
     218    usb_device dev,
    219219    void **cookie)
    220220{
    221221    my_device_info *my_dev;
     
    225225    status_t st;
    226226    usb_hid_descriptor *hid_desc;
    227227    uint8 *rep_desc = NULL;
     228    report_insn *reallocated_insns = NULL;
    228229    size_t desc_len, actual;
    229230    decomp_item *items;
    230231    size_t num_items;
    231232    int i, ifno, fd, report_id;
    232233
    233     assert (dev != NULL && cookie != NULL);
     234    assert (cookie != NULL);
    234235    DPRINTF_INFO ((MY_ID "device_added()\n"));
    235236
    236237    dev_desc = usb->get_device_descriptor (dev);
     
    353354    parse_report_descriptor (items, num_items, my_dev->insns,
    354355        &my_dev->num_insns, &my_dev->total_report_size, &report_id);
    355356    free (items);
    356     realloc (my_dev->insns, sizeof (report_insn) * my_dev->num_insns);
     357    reallocated_insns = realloc (my_dev->insns, sizeof (report_insn) * my_dev->num_insns);
     358    my_dev->insns = reallocated_insns;
    357359    DPRINTF_INFO ((MY_ID "%d items, %d insns, %d bytes\n",
    358360        (int)num_items, (int)my_dev->num_insns, (int)my_dev->total_report_size));
    359361
     
    661663            my_dev->insns, my_dev->num_insns,
    662664            my_dev->prod_info, my_dev->flags, &joy_data);
    663665    }
    664 
    665     if (cookie->enhanced && *num_bytes >= sizeof (extended_joystick))
     666   
     667    // always work in enhanced mode, the joystick caller will handle legacy mode
     668    if (*num_bytes >= sizeof (extended_joystick))
    666669    {
    667670        /* enhanced mode */
    668671        joy_data.timestamp = my_dev->timestamp;
     
    671674        err = B_OK;
    672675    }
    673676
    674     if (!cookie->enhanced && *num_bytes >= sizeof (joystick))
    675     {
    676         /* standard mode */
    677         joystick std_joy_data;
    678 
    679         std_joy_data.timestamp = my_dev->timestamp;
    680         std_joy_data.horizontal = joy_data.axes [0];
    681         std_joy_data.vertical   = joy_data.axes [1];
    682         /* false if pressed */
    683         std_joy_data.button1 = (joy_data.buttons & 1) ? false : true;
    684         std_joy_data.button2 = (joy_data.buttons & 2) ? false : true;
    685         memcpy (buf, &std_joy_data, sizeof (joystick));
    686         *num_bytes = sizeof (joystick);
    687         err = B_OK;
    688     }
    689 
    690677    return err;
    691678}
    692679
     
    735722            the joystick product may be connected
    736723        */
    737724        assert (arg != NULL);
    738         if (!cookie->enhanced &&
    739             strstr (((joystick_module_info*)arg)->device_name, "USB") != NULL)  /* XXX */
     725        if (!cookie->enhanced)// &&
     726            //strstr (((joystick_module_info*)arg)->device_name, "USB") != NULL)    /* XXX */
    740727        {
    741728            cookie->joy_mod_info = *(joystick_module_info *)arg;
    742729
     
    744731
    745732            /* set actual # of controls detected */
    746733            /* this will be returned in B_JOYSTICK_GET_DEVICE_MODULE */
     734            cookie->joy_mod_info.num_sticks = 1;
    747735            cookie->joy_mod_info.num_axes = my_dev->num_axes;
    748736            cookie->joy_mod_info.num_hats = my_dev->num_hats;
    749737            cookie->joy_mod_info.num_buttons = my_dev->num_buttons;
  • src/kits/device/Joystick.cpp

     
    44 * Distributed under the terms of the MIT License.
    55 */
    66
     7#include <JoystickTweaker.h>
    78
    89#include <List.h>
    9 #include "Joystick.h"
     10#include <Joystick.h>
    1011
    1112#include <errno.h>
    1213#include <fcntl.h>
     
    1920#include <String.h>
    2021#include <Debug.h>
    2122
     23#define JOYSTICK_READ 'jrea'
    2224
    2325#if DEBUG
    2426static FILE *sLogFile = NULL;
     
    4345
    4446#define CALLED() LOG("%s\n", __PRETTY_FUNCTION__)
    4547
    46 #include "JoystickTweaker.h"
    47 
    48 
    4948BJoystick::BJoystick()
    5049    :
     50    horizontal(0),
     51    vertical(0),
     52    button1(0),
     53    button2(0),
    5154    fBeBoxMode(false),
    5255    ffd(-1),
    53     fDevices(new BList),
    54     fJoystickInfo(new _joystick_info())
     56    fDevices(NULL),
     57    fJoystickInfo(new _joystick_info()),
     58    fDevName(0)
    5559{
    5660#if DEBUG
    5761    sLogFile = fopen("/var/log/libdevice.log", "a");
    5862#endif
    59     //ScanDevices();
     63
     64    memset(&fJoystickInfo->module_info, 0,
     65        sizeof(_joystick_module_info));
     66    memset(&fJoystickInfo->sticks, 0,
     67        sizeof(fJoystickInfo->sticks));
    6068}
    6169
    6270
     
    6573    if (ffd >= 0)
    6674        close(ffd);
    6775
    68     for (int32 count = fDevices->CountItems() - 1; count >= 0; count--) {
    69         free(fDevices->RemoveItem(count));
     76    if(fDevices)
     77    {
     78        for (int32 count = fDevices->CountItems() - 1; count >= 0; count--)
     79            free(fDevices->RemoveItem(count));
    7080    }
    7181
     82    free(fDevName);
    7283    delete fDevices;
    7384    delete fJoystickInfo;
    7485}
     
    98109        snprintf(buf, 64, DEVICEPATH"/%s", portName);
    99110    else
    100111        snprintf(buf, 64, "%s", portName);
    101 
     112   
    102113    if (ffd >= 0)
    103114        close(ffd);
    104115
     
    118129        _BJoystickTweaker jt(*this);
    119130        jt.GetInfo(fJoystickInfo, portName);
    120131
    121         LOG("ioctl - %d\n", fJoystickInfo->num_buttons);
    122         ioctl(ffd, B_JOYSTICK_SET_DEVICE_MODULE, fJoystickInfo);
    123         ioctl(ffd, B_JOYSTICK_GET_DEVICE_MODULE, fJoystickInfo);
    124         LOG("ioctl - %d\n", fJoystickInfo->num_buttons);
     132        int result = ioctl(ffd, B_JOYSTICK_SET_DEVICE_MODULE, &fJoystickInfo->module_info);
     133        LOG("ioctl B_JOYSTICK_SET_DEVICE_MODULE: %d\n", result);
     134        result = ioctl(ffd, B_JOYSTICK_GET_DEVICE_MODULE, &fJoystickInfo->module_info);
     135        LOG("ioctl B_JOYSTICK_GET_DEVICE_MODULE: %d\n", result);
     136        LOG("NumberOfButtons - %d\n", fJoystickInfo->module_info.num_buttons);
    125137
    126138        return ffd;
    127139    } else
     
    144156BJoystick::ScanDevices(bool useDisabled)
    145157{
    146158    CALLED();
     159    if(!fDevices) fDevices = new BList();
     160   
    147161    if (useDisabled) {
    148162        _BJoystickTweaker temp(*this);
    149163        temp.scan_including_disabled();
     
    157171    CALLED();
    158172
    159173    // Refresh devices list
    160     ScanDevices(true);
     174    if(!fDevices) ScanDevices(true);
    161175
    162176    int32 count = 0;
    163177    if (fDevices != NULL)
     
    173187{
    174188    CALLED();
    175189    BString *temp = NULL;
     190   
     191    if(!fDevices) ScanDevices(true);
     192
    176193    if (fDevices != NULL && fDevices->CountItems() > n)
    177194        temp = static_cast<BString*>(fDevices->ItemAt(n));
    178195    else
     
    203220BJoystick::CountSticks()
    204221{
    205222    CALLED();
    206     return fJoystickInfo->num_sticks;
     223    return fJoystickInfo->module_info.num_sticks;
    207224}
    208225
    209226
     
    211228BJoystick::CountAxes()
    212229{
    213230    CALLED();
    214     return fJoystickInfo->num_axes;
     231    return fJoystickInfo->module_info.num_axes;
    215232}
    216233
    217234
     
    219236BJoystick::CountHats()
    220237{
    221238    CALLED();
    222     return fJoystickInfo->num_hats;
     239    return fJoystickInfo->module_info.num_hats;
    223240}
    224241
    225242
     
    227244BJoystick::CountButtons()
    228245{
    229246    CALLED();
    230     return fJoystickInfo->num_buttons;
     247    return fJoystickInfo->module_info.num_buttons;
    231248}
    232249
    233250
     
    236253{
    237254    CALLED();
    238255    if (fJoystickInfo != NULL && ffd >= 0) {
    239         out_name->SetTo(fJoystickInfo->module_name);
     256        out_name->SetTo(fJoystickInfo->module_info.module_name);
     257        //out_name->SetTo("Module");
    240258        return B_OK;
    241259    } else
    242260        return B_ERROR;
     
    249267{
    250268    CALLED();
    251269    if (fJoystickInfo != NULL && ffd >= 0) {
    252         out_name->SetTo(fJoystickInfo->controller_name);
     270        //out_name->SetTo("Joystick");
     271        out_name->SetTo(fJoystickInfo->module_info.device_name);
    253272        return B_OK;
    254273    } else
    255274        return B_ERROR;
     
    280299BJoystick::SetMaxLatency(bigtime_t max_latency)
    281300{
    282301    CALLED();
    283     fJoystickInfo->max_latency = max_latency;
    284      //else B_ERROR (when?)
    285     return B_OK;
     302    if(ffd >= 0) {
     303        return ioctl(ffd, B_JOYSTICK_SET_MAX_LATENCY,
     304            &max_latency, sizeof(bigtime_t));
     305    }
     306    return B_ERROR;
    286307}
    287308
    288309//--------- not done -------------------
     
    291312BJoystick::GetAxisNameAt(int32 index, BString *out_name)
    292313{
    293314    CALLED();
    294     return B_BAD_INDEX;
     315    BString axisName = "Axis ";
     316    axisName << index + 1;
     317    out_name->SetTo(axisName);
     318    return B_OK;
    295319}
    296320
    297321
     
    299323BJoystick::GetHatNameAt(int32 index, BString *out_name)
    300324{
    301325    CALLED();
    302     return B_BAD_INDEX;
     326    BString hatName = "Hat ";
     327    hatName << index + 1;
     328    out_name->SetTo(hatName);
     329    return B_OK;
    303330}
    304331
    305332
     
    307334BJoystick::GetButtonNameAt(int32 index, BString *out_name)
    308335{
    309336    CALLED();
    310     return B_BAD_INDEX;
     337    BString buttonName = "Button ";
     338    buttonName << index + 1;
     339    out_name->SetTo(buttonName);
     340    return B_OK;
    311341}
    312342
    313343
     
    315345BJoystick::GetAxisValues(int16 *out_values, int32 for_stick)
    316346{
    317347    CALLED();
    318     return B_BAD_VALUE;
     348    if(for_stick < 0 || for_stick >= fJoystickInfo->module_info.num_sticks)
     349        return B_BAD_VALUE;
     350       
     351    memcpy(out_values, fJoystickInfo->sticks[0].axes,
     352        fJoystickInfo->module_info.num_axes * sizeof(int16));
     353    return B_OK;
    319354}
    320355
    321356
     
    323358BJoystick::GetHatValues(uint8 *out_hats, int32 for_stick)
    324359{
    325360    CALLED();
    326     return B_BAD_VALUE;
     361    if(for_stick < 0 || for_stick >= fJoystickInfo->module_info.num_sticks)
     362        return B_BAD_VALUE;
     363   
     364    memcpy(out_hats, fJoystickInfo->sticks[0].hats,
     365        fJoystickInfo->module_info.num_hats);
     366    return B_OK;
    327367}
    328368
    329369
     
    331371BJoystick::ButtonValues(int32 for_stick)
    332372{
    333373    CALLED();
    334     return 0;
     374    if(for_stick < 0 || for_stick >= fJoystickInfo->module_info.num_sticks)
     375        return B_BAD_VALUE;
     376   
     377    return fJoystickInfo->sticks[0].buttons;
    335378}
    336379
    337380
     
    340383{
    341384    CALLED();
    342385    if (ffd >= 0) {
     386        // Always read in enhanced mode,  then assign legacy axes and buttons their values
     387        extended_joystick joystick;
     388        memset(&joystick, 0, sizeof(joystick));
     389       
     390        status_t length = read(ffd, &joystick, sizeof(extended_joystick));
     391        if(length == sizeof(joystick)) fJoystickInfo->sticks[0] = joystick;
     392       
     393        // legacy mode buttons are true when 0
     394        horizontal = fJoystickInfo->sticks[0].axes[0];
     395        vertical = fJoystickInfo->sticks[0].axes[1];
     396        button1 = !(fJoystickInfo->sticks[0].buttons & 1);
     397        button2 = !(fJoystickInfo->sticks[0].buttons & 2);
     398        //printf("hor: %d ver:%d buttons: %d\n", horizontal, vertical, fJoystickInfo->sticks[0].buttons);
    343399        return B_OK;
    344400    } else
    345401        return B_ERROR;
  • src/kits/device/JoystickTweaker.cpp

     
    125125}
    126126
    127127
    128 status_t
     128_joystick_info *
    129129_BJoystickTweaker::get_info()
    130130{
    131131    CALLED();
    132     return B_ERROR;
     132    return fJoystick->fJoystickInfo;
    133133}
    134134
    135135
     
    141141    status_t err = B_ERROR;
    142142    BString str(JOYSTICKPATH);
    143143    str.Append(ref);
    144 
    145144    FILE *file = fopen(str.String(), "r");
    146145    if (file != NULL) {
    147146        char line [STRINGLENGTHCPY];
     
    167166
    168167    if (str.IFindFirst("module") != -1) {
    169168        str.RemoveFirst("module = ");
    170         strlcpy(info->module_name, str.String(), STRINGLENGTHCPY);
     169        strlcpy(info->module_info.module_name, str.String(), STRINGLENGTHCPY);
    171170    } else if (str.IFindFirst("gadget") != -1) {
    172171        str.RemoveFirst("gadget = ");
    173         strlcpy(info->controller_name, str.String(), STRINGLENGTHCPY);
     172        strlcpy(info->module_info.device_name, str.String(), STRINGLENGTHCPY);
    174173    } else if (str.IFindFirst("num_axes") != -1) {
    175174        str.RemoveFirst("num_axes = ");
    176         info->num_axes = atoi(str.String());       
     175        info->module_info.num_axes = atoi(str.String());       
    177176    } else if (str.IFindFirst("num_hats") != -1) {
    178177        str.RemoveFirst("num_hats = ");
    179         info->num_hats = atoi(str.String());
     178        info->module_info.num_hats = atoi(str.String());
    180179    } else if (str.IFindFirst("num_buttons") != -1) {
    181180        str.RemoveFirst("num_buttons = ");
    182         info->num_buttons = atoi(str.String());
     181        info->module_info.num_buttons = atoi(str.String());
    183182    } else if (str.IFindFirst("num_sticks") != -1) {
    184183        str.RemoveFirst("num_sticks = ");
    185         info->num_sticks = atoi(str.String());
     184        info->module_info.num_sticks = atoi(str.String());
    186185    } else {
    187186        LOG("Path = %s\n", str.String());
    188187    }
  • headers/private/device/JoystickTweaker.h

     
    2121class BJoystick;
    2222
    2323typedef struct _joystick_info {
     24    _joystick_module_info module_info;
     25    _extended_joystick sticks[4];
     26    BList           name_axis;
     27    BList           name_hat;
     28    BList           name_button;
     29    bool            calibration_enable;
     30} joystick_info;
     31
     32#if 0
     33typedef struct _joystick_info {
    2434        char            module_name[64];
    2535        char            controller_name[64];
    2636        int16           num_axes;
     
    3444        BList           name_button;
    3545//      BList           name_
    3646} joystick_info;
     47#endif
    3748
    3849class _BJoystickTweaker {
    3950
     
    4758        // BeOS R5's joystick pref need these
    4859        status_t    save_config(const entry_ref * ref = NULL);
    4960        void        scan_including_disabled();
    50         status_t    get_info();
     61        _joystick_info *    get_info();
    5162       
    5263private:
    5364        void        _BuildFromJoystickDesc(char *string, _joystick_info* info);