BOOL CALLBACK EnumCallback(const DIDEVICEINSTANCE* instance,
                  void* context) {
  Context* ctxt = reinterpret_cast<Context*>(context);
  if (FAILED(ctxt->d_interface->CreateDevice(instance->guidInstance,
                                           &ctxt->device,
                                           NULL)))
    return DIENUM_CONTINUE;
  ctxt->device->Acquire();

#define MAKE_AXIS(i) \
  {0, FIELD_OFFSET(JoyData, axes) + 4 * i, \
   DIDFT_AXIS | DIDFT_MAKEINSTANCE(i) | DIDFT_OPTIONAL, 0}
#define MAKE_BUTTON(i) \
  {&GUID_Button, FIELD_OFFSET(JoyData, buttons) + i, \
   DIDFT_BUTTON | DIDFT_MAKEINSTANCE(i) | DIDFT_OPTIONAL, 0}
#define MAKE_POV() \
  {&GUID_POV, FIELD_OFFSET(JoyData, pov), DIDFT_POV | DIDFT_OPTIONAL, 0}
  DIOBJECTDATAFORMAT rgodf[] = {
    MAKE_AXIS(0),
    MAKE_AXIS(1),
    MAKE_AXIS(2),
    MAKE_AXIS(3),
    MAKE_AXIS(4),
    MAKE_AXIS(5),
    MAKE_AXIS(6),
    MAKE_AXIS(7),
    MAKE_AXIS(8),
    MAKE_AXIS(9),
    MAKE_BUTTON(0),
    MAKE_BUTTON(1),
    MAKE_BUTTON(2),
    MAKE_BUTTON(3),
    MAKE_BUTTON(4),
    MAKE_BUTTON(5),
    MAKE_BUTTON(6),
    MAKE_BUTTON(7),
    MAKE_BUTTON(8),
    MAKE_BUTTON(9),
    MAKE_BUTTON(10),
    MAKE_BUTTON(11),
    MAKE_BUTTON(12),
    MAKE_BUTTON(13),
    MAKE_BUTTON(14),
    MAKE_BUTTON(15),
    MAKE_BUTTON(16),
    MAKE_POV(),
  };
#undef MAKE_AXIS
#undef MAKE_BUTTON
#undef MAKE_POV

  DIDATAFORMAT df = {
    sizeof (DIDATAFORMAT),
    sizeof (DIOBJECTDATAFORMAT),
    DIDF_ABSAXIS,
    sizeof (JoyData),
    sizeof (rgodf) / sizeof (rgodf[0]),
    rgodf
  };
  
  // If we can't set the data format on the device, don't add it to our
  // list, since we won't know how to read data from it.
  if (FAILED(ctxt->device->SetDataFormat(&df))) {
    ctxt->device->Release();
    return DIENUM_CONTINUE;
  }
  printf("Product name: %ls\n", instance->tszProductName);
  if (!GetVendorProduct(ctxt))
	return DIENUM_CONTINUE;

  // Dead zone at 10%.
  SetDirectInputDeadZone(ctxt->device, 1000);

  // A hack to disallow "zero" values from our axis.
  // This allows us to tell the difference between an axis that isn't
  // present, and one that is at its minimum value.
  DIPROPRANGE range;
  range.diph.dwSize = sizeof(DIPROPRANGE);
  range.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  range.diph.dwHow = DIPH_DEVICE;
  range.diph.dwObj = 0;
  range.lMin = 1;
  range.lMax = 65535;
  if (SUCCEEDED(ctxt->device->SetProperty(DIPROP_RANGE, &range.diph)))
    ctxt->valid = true;
  return DIENUM_STOP;
}
Exemple #2
0
static int  Load_Inputs_Src_Parse_Var (int VarIdx, char *s, t_input_src *input_src)
{
    char w[256];

    if (!parse_getword(w, sizeof(w), &s, " \t", ';', PARSE_FLAGS_NONE))
        return MEKA_ERR_EMPTY;

    switch (VarIdx)
    {
    case  0: // type -----------------------------------------------------------
        if (!strcmp(w, "keyboard"))
        {
            input_src->type = INPUT_SRC_TYPE_KEYBOARD;
            input_src->flags = INPUT_SRC_FLAGS_DIGITAL;
            input_src->Connected_and_Ready = TRUE;
            return MEKA_ERR_OK;
        }
        if (!strcmp(w, "joypad"))
        {
            input_src->type = INPUT_SRC_TYPE_JOYPAD;
            input_src->flags = INPUT_SRC_FLAGS_DIGITAL;
            input_src->Connected_and_Ready = FALSE;
            return MEKA_ERR_OK;
        }
        if (!strcmp(w, "mouse"))
        {
            input_src->type = INPUT_SRC_TYPE_MOUSE;
            input_src->flags = INPUT_SRC_FLAGS_ANALOG;
            input_src->Connected_and_Ready = (g_Env.mouse_installed ? TRUE : FALSE);
            return MEKA_ERR_OK;
        }
        return MEKA_ERR_SYNTAX;

    case  1: // enabled --------------------------------------------------------
        if (!strcmp(w, "yes"))      { input_src->enabled = TRUE; return MEKA_ERR_OK; }
        if (!strcmp(w, "no"))       { input_src->enabled = FALSE;  return MEKA_ERR_OK; }
        return MEKA_ERR_SYNTAX;

    case  2: // player ---------------------------------------------------------
        input_src->player = atoi(w) - 1;
        if (input_src->player != 0 && input_src->player != 1)
            return MEKA_ERR_SYNTAX;
        return MEKA_ERR_OK;

    case  3: // driver ---------------------------------------------------------
        if (input_src->type == INPUT_SRC_TYPE_JOYPAD)
            input_src->Driver = Config_Driver_Joy_Str_to_Int(w);
        return MEKA_ERR_OK;

    case  4: // connection -----------------------------------------------------
        input_src->Connection_Port = atoi(w);
        if (input_src->Connection_Port > 0) input_src->Connection_Port -= 1;
        return MEKA_ERR_OK;

    case  5: // emulate_digital ------------------------------------------------
        if (!strcmp (w, "yes"))      { input_src->flags |= INPUT_SRC_FLAGS_EMULATE_DIGITAL; return MEKA_ERR_OK; }
        if (!strcmp (w, "no"))       { input_src->flags &= ~INPUT_SRC_FLAGS_EMULATE_DIGITAL; return MEKA_ERR_OK; }
        return MEKA_ERR_SYNTAX;

    case  6: // emulate_analog -------------------------------------------------
        if (!strcmp (w, "yes"))      { input_src->flags |= INPUT_SRC_FLAGS_EMULATE_ANALOG; return MEKA_ERR_OK; }
        if (!strcmp (w, "no"))       { input_src->flags &= ~INPUT_SRC_FLAGS_EMULATE_ANALOG; return MEKA_ERR_OK; }
        return MEKA_ERR_SYNTAX;

    case  7: // digital_falloff ------------------------------------------------
        input_src->Analog_to_Digital_FallOff = atof (w);
        return MEKA_ERR_OK;

    case  8: // player_up ------------------------------------------------------
    case  9: // player_down ----------------------------------------------------
    case 10: // player_left ----------------------------------------------------
    case 11: // player_right ---------------------------------------------------
    case 12: // player_button1 -------------------------------------------------
    case 13: // player_button2 -------------------------------------------------
    case 14: // player_start_pause ---------------------------------------------
    case 15: // player_reset ---------------------------------------------------
        {
            int MapIdx = INPUT_MAP_DIGITAL_UP + VarIdx - 8;

            // FIXME: ???
            //if (MapIdx >= INPUT_MAP_DIGITAL_UP && MapIdx <= INPUT_MAP_DOWN && !(input_src->Result_Type & DIGITAL))
            //   return MEKA_ERR_INCOHERENT;

            if (!strcmp (w, "key"))
            {
                const t_key_info *key_info;
                if (input_src->type != INPUT_SRC_TYPE_KEYBOARD)
                    return MEKA_ERR_INCOHERENT;
                //if (!Parse_LineGet (&w, &s))
                //  return MEKA_ERR_SYNTAX;
                parse_skip_spaces(&s);
                if (StrNull (s))
                    return MEKA_ERR_SYNTAX;
                key_info = KeyInfo_FindByName(s);
                if (key_info != NULL)
                {
                    input_src->Map [MapIdx].Type = INPUT_MAP_TYPE_KEY;
                    input_src->Map [MapIdx].Idx = key_info->scancode;
                }
                return MEKA_ERR_OK;
            }

            if (!strcmp (w, "joy"))
            {
                int stick, axis, dir;

                if (input_src->type != INPUT_SRC_TYPE_JOYPAD)
                    return MEKA_ERR_INCOHERENT;
                if (!parse_getword(w, sizeof(w), &s, " \t", ';', PARSE_FLAGS_NONE) || strcmp(w, "stick") || !parse_getword(w, sizeof(w), &s, " \t", ';', PARSE_FLAGS_NONE))
                    return MEKA_ERR_SYNTAX;
                stick = atoi(w);
                if (!parse_getword(w, sizeof(w), &s, " \t", ';', PARSE_FLAGS_NONE) || strcmp(w, "axis") || !parse_getword(w, sizeof(w), &s, " \t", ';', PARSE_FLAGS_NONE))
                    return MEKA_ERR_SYNTAX;
                axis = atoi(w);
                if (!parse_getword(w, sizeof(w), &s, " \t", ';', PARSE_FLAGS_NONE) || strcmp(w, "dir") || !parse_getword(w, sizeof(w), &s, " \t", ';', PARSE_FLAGS_NONE))
                    return MEKA_ERR_SYNTAX;
                dir = atoi(w);
                input_src->Map [MapIdx].Type = INPUT_MAP_TYPE_JOY_AXIS;
                input_src->Map [MapIdx].Idx = MAKE_STICK_AXIS_DIR (stick, axis, dir);
                return MEKA_ERR_OK;
            }

            if (!strcmp (w, "joy_button"))
            {
                if (input_src->type != INPUT_SRC_TYPE_JOYPAD)
                    return MEKA_ERR_INCOHERENT;
                if (!parse_getword(w, sizeof(w), &s, " \t", ';', PARSE_FLAGS_NONE))
                    return MEKA_ERR_SYNTAX;
                input_src->Map [MapIdx].Type = INPUT_MAP_TYPE_JOY_BUTTON;
                input_src->Map [MapIdx].Idx = atoi(w);
                return MEKA_ERR_OK;
            }

            if (!strcmp (w, "mouse_button"))
            {
                if (input_src->type != INPUT_SRC_TYPE_MOUSE)
                    return MEKA_ERR_INCOHERENT;
                if (!parse_getword(w, sizeof(w), &s, " \t", ';', PARSE_FLAGS_NONE))
                    return MEKA_ERR_SYNTAX;
                input_src->Map [MapIdx].Type = INPUT_MAP_TYPE_MOUSE_BUTTON;
                input_src->Map [MapIdx].Idx = atoi(w);
                return MEKA_ERR_OK;
            }

            return MEKA_ERR_SYNTAX;
        }

    case 16: // player_x_axis --------------------------------------------------
    case 17: // player_y_axis --------------------------------------------------
        {
            int MapIdx = INPUT_MAP_ANALOG_AXIS_X + VarIdx - 16;

            if (!(input_src->flags & INPUT_SRC_FLAGS_ANALOG))
                return MEKA_ERR_INCOHERENT;

            if (!strcmp (w, "mouse_axis"))
            {
                if (input_src->type != INPUT_SRC_TYPE_MOUSE)
                    return MEKA_ERR_INCOHERENT;
                if (!parse_getword(w, sizeof(w), &s, " \t", ';', PARSE_FLAGS_NONE))
                    return MEKA_ERR_SYNTAX;
                input_src->Map [MapIdx].Type = input_src->Map [MapIdx + 2].Type = INPUT_MAP_TYPE_MOUSE_AXIS;
                input_src->Map [MapIdx].Idx = input_src->Map [MapIdx + 2].Idx = MAKE_AXIS(atoi(w));
                return MEKA_ERR_OK;
            }

            return MEKA_ERR_SYNTAX;
        }

    case 18: // joy_driver -----------------------------------------------------
        {
            Inputs.Sources_Joy_Driver = Config_Driver_Joy_Str_to_Int (w);
            return MEKA_ERR_OK;
        }

    case 19: // mouse_speed_x --------------------------------------------------
        {
            Inputs.MouseSpeed_X = atoi (w);
            return MEKA_ERR_OK;
        }

    case 20: // mouse_speed_y --------------------------------------------------
        {
            Inputs.MouseSpeed_Y = atoi (w);
            return MEKA_ERR_OK;
        }

    case 21: // cabinet_mode ---------------------------------------------------
        {
            Inputs.Cabinet_Mode = atoi (w);
            return MEKA_ERR_OK;
        }
    }

    return MEKA_ERR_SYNTAX;
}