Пример #1
0
void fs_ml_input_init() {
    fs_emu_log("fs_ml_input_init\n");
    fs_ml_initialize_keymap();
    /*
    // reset all input mappings
    for (int i = 0; i < (FS_ML_MAX_DEVICES * FS_ML_SLOTS); i++) {
        g_input_action_table[i] = 0;
        g_menu_action_table[i] = 0;
    }
    */
    //g_fs_ml_input_devices[FS_ML_KEYBOARD].name = g_strdup("KEYBOARD");
    //g_fs_ml_input_devices[FS_ML_KEYBOARD].alias = g_strdup("");

    GHashTable *device_counts = g_hash_table_new_full(g_str_hash, g_str_equal,
            g_free, NULL);

    int num_joysticks = SDL_NumJoysticks();
    for (int i = 0; i < num_joysticks; i++) {
        if (i == FS_ML_MAX_JOYSTICKS) {
            fs_emu_log("WARNING: reached max num joysticks");
            break;
        }
        SDL_Joystick *joystick = SDL_JoystickOpen(i);
        gchar* name = g_ascii_strup(SDL_JoystickName(i), -1);
        name = g_strstrip(name);
        int count = GPOINTER_TO_INT(g_hash_table_lookup(device_counts, name));
        count++;
        // hash table now owns name pointer
        g_hash_table_replace(device_counts, g_strdup(name),
                GINT_TO_POINTER(count));
        if (count > 1) {
            gchar *result = g_strdup_printf("%s #%d", name, count);
            g_free(name);
            name = result;
        }
        g_fs_ml_input_devices[i].index = i;
        g_fs_ml_input_devices[i].name = name;
        //g_input_devices[i].alias = g_strdup_printf("JOYSTICK #%d", i);
        if (i == 0) {
        	g_fs_ml_input_devices[i].alias = g_strdup("JOYSTICK");
        }
        else {
        	g_fs_ml_input_devices[i].alias = g_strdup_printf("JOYSTICK #%d",
        			i + 1);
        }
        g_fs_ml_input_devices[i].hats = SDL_JoystickNumHats(joystick);
        g_fs_ml_input_devices[i].buttons = SDL_JoystickNumButtons(joystick);
        g_fs_ml_input_devices[i].axes = SDL_JoystickNumAxes(joystick);
        g_fs_ml_input_devices[i].balls = SDL_JoystickNumBalls(joystick);
        /*
        for (int j = 0; j < MAX_STATES; j++) {
            g_input_devices[i].actions[j] = 0;
        }
        */
        fs_emu_log("device #%02d found: %s\n", i + 1, name);
        fs_emu_log("- %d buttons %d hats %d axes %d balls\n",
                g_fs_ml_input_devices[i].buttons,
                g_fs_ml_input_devices[i].hats,
                g_fs_ml_input_devices[i].axes,
                g_fs_ml_input_devices[i].balls);
    }

    g_fs_ml_input_devices[num_joysticks].name = g_strdup("KEYBOARD");
    g_fs_ml_input_devices[num_joysticks].alias = g_strdup("");

    g_fs_ml_input_device_count = num_joysticks + 1;
    g_hash_table_destroy(device_counts);

    fs_ml_initialize_keymap();
}
Пример #2
0
void fs_ml_input_init() {
    FS_ML_INIT_ONCE;

    SDL_Init(SDL_INIT_JOYSTICK);

    fs_log("fs_ml_input_init\n");

    if (fs_config_get_boolean(OPTION_MOUSE_INTEGRATION) == 1) {
        g_mouse_integration = 1;
    }

    g_cursor_mode = fs_config_get_boolean(OPTION_CURSOR);
    if (fs_config_check_auto(OPTION_CURSOR, FS_CONFIG_AUTO)) {
        if (fs_emu_mouse_integration()) {
            g_cursor_mode = 0;
        } else {
            g_cursor_mode = -1;
        }
    }

    g_input_queue = g_queue_new();
    g_input_mutex = fs_mutex_create();

    fs_log("calling fs_ml_video_init\n");
    fs_ml_video_init();

    int size = sizeof(fs_ml_input_device) * FS_ML_INPUT_DEVICES_MAX;
    // allocate zeroed memory
    g_fs_ml_input_devices = g_malloc0(size);

    fs_ml_initialize_keymap();

    int k = 0;
    g_fs_ml_first_joystick_index = 0;

    g_fs_ml_input_devices[k].type = FS_ML_KEYBOARD;
    g_fs_ml_input_devices[k].index = k;
    g_fs_ml_input_devices[k].name = g_strdup("KEYBOARD");
    g_fs_ml_input_devices[k].alias = g_strdup("KEYBOARD");
    k += 1;

    g_fs_ml_input_device_count = k;
    fs_ml_mouse_init();
    k = g_fs_ml_input_device_count;

    g_fs_ml_first_joystick_index = g_fs_ml_input_device_count;

    int num_joysticks = SDL_NumJoysticks();
    fs_log("num joystick devices: %d\n", num_joysticks);
    if (SDL_WasInit(SDL_INIT_JOYSTICK) == 0) {
        fs_log("WARNING: Joystick module not initialized\n");
    }
    for (int i = 0; i < num_joysticks; i++) {
        if (k == FS_ML_INPUT_DEVICES_MAX) {
            fs_log("WARNING: reached max num devices\n");
            break;
        }
        SDL_Joystick *joystick = SDL_JoystickOpen(i);

#ifdef USE_SDL2
        char* name = g_ascii_strup(SDL_JoystickName(joystick), -1);
#else
        char* name = g_ascii_strup(SDL_JoystickName(i), -1);
#endif
        name = g_strstrip(name);
        if (name[0] == '\0') {
            g_free(name);
            name = g_ascii_strup("Unnamed", -1);
        }

        // fs_ml_input_unique_device_name either returns name, or frees it
        // and return another name, so name must be malloced and owned by
        // caller
        name = fs_ml_input_unique_device_name(name);

        g_fs_ml_input_devices[k].type = FS_ML_JOYSTICK;
        g_fs_ml_input_devices[k].index = k;
        g_fs_ml_input_devices[k].name = name;
        if (i == 0) {
            g_fs_ml_input_devices[k].alias = g_strdup("JOYSTICK");
        }
        else {
            g_fs_ml_input_devices[k].alias = g_strdup_printf("JOYSTICK #%d",
                    i + 1);
        }

        g_fs_ml_input_devices[k].hats = SDL_JoystickNumHats(joystick);
        g_fs_ml_input_devices[k].buttons = SDL_JoystickNumButtons(joystick);
        g_fs_ml_input_devices[k].axes = SDL_JoystickNumAxes(joystick);
        g_fs_ml_input_devices[k].balls = SDL_JoystickNumBalls(joystick);

        fs_log("joystick device #%02d found: %s\n", i + 1, name);
        fs_log("- %d buttons %d hats %d axes %d balls\n",
                g_fs_ml_input_devices[k].buttons,
                g_fs_ml_input_devices[k].hats,
                g_fs_ml_input_devices[k].axes,
                g_fs_ml_input_devices[k].balls);
        k += 1;
    }

    g_fs_ml_input_device_count = k;

    fs_ml_initialize_keymap();
}