#ifndef QNX_SCANCODE_FIX_H
#define QNX_SCANCODE_FIX_H

#include <SDL2/SDL.h>

/*
 * QNX 8.0 Scancode Remapping for SDL2 (Screen driver)
 * Fixes incorrect raw key values coming from QNX Screen → standard SDL scancodes
 * Especially important for arrow keys, WASD, strafe, fire, etc. in Chocolate-Doom / prBoom
 */

static const SDL_Scancode qnx_scancode_fix[SDL_NUM_SCANCODES] = {
    [0] = SDL_SCANCODE_UNKNOWN,

    // === Navigation keys (most critical for Doom) ===
    [89] = SDL_SCANCODE_LEFT,
    [90] = SDL_SCANCODE_RIGHT,
    [98] = SDL_SCANCODE_UP,
    [91] = SDL_SCANCODE_DOWN,

    // === Movement / Action keys ===
    [47] = SDL_SCANCODE_W,      // forward
    [24] = SDL_SCANCODE_S,      // backward
    [32] = SDL_SCANCODE_A,      // strafe left
    [35] = SDL_SCANCODE_D,      // strafe right

    [44] = SDL_SCANCODE_SPACE,  // use / open / jump
    [224] = SDL_SCANCODE_LCTRL, // fire (common)
    [226] = SDL_SCANCODE_LALT,  // strafe / alt fire (common)

    // === Basic keys that are often wrong on QNX ===
    [53] = SDL_SCANCODE_GRAVE,      // ` ~
    [4]  = SDL_SCANCODE_1,
    [22] = SDL_SCANCODE_2,
    [7]  = SDL_SCANCODE_3,
    [9]  = SDL_SCANCODE_4,
    [10] = SDL_SCANCODE_5,
    [11] = SDL_SCANCODE_6,
    [13] = SDL_SCANCODE_7,
    [14] = SDL_SCANCODE_8,
    [15] = SDL_SCANCODE_9,
    [51] = SDL_SCANCODE_0,

    [27] = SDL_SCANCODE_MINUS,
    [6]  = SDL_SCANCODE_EQUALS,

    [23] = SDL_SCANCODE_Q,
    [36] = SDL_SCANCODE_E,
    [28] = SDL_SCANCODE_R,
    [12] = SDL_SCANCODE_T,
    [40] = SDL_SCANCODE_Y,
    [18] = SDL_SCANCODE_U,
    [45] = SDL_SCANCODE_I,
    [8]  = SDL_SCANCODE_O,
    [21] = SDL_SCANCODE_P,

    [25] = SDL_SCANCODE_LEFTBRACKET,
    [5]  = SDL_SCANCODE_RIGHTBRACKET,
    [17] = SDL_SCANCODE_BACKSLASH,

    [29] = SDL_SCANCODE_Z,
    [48] = SDL_SCANCODE_X,
    [34] = SDL_SCANCODE_C,
    [19] = SDL_SCANCODE_V,
    [33] = SDL_SCANCODE_B,
    [26] = SDL_SCANCODE_N,
    [20] = SDL_SCANCODE_M,

    [54] = SDL_SCANCODE_SEMICOLON,
    [55] = SDL_SCANCODE_APOSTROPHE,
    [52] = SDL_SCANCODE_RETURN,     // Enter

    [225] = SDL_SCANCODE_LSHIFT,
    [229] = SDL_SCANCODE_RSHIFT,    // if you have right shift issues

    [42] = SDL_SCANCODE_BACKSPACE,
    [43] = SDL_SCANCODE_TAB,
    [53] = SDL_SCANCODE_ESCAPE,     // Escape (sometimes collides with grave)

    // Function keys (if needed)
    [57] = SDL_SCANCODE_F1,
    [58] = SDL_SCANCODE_F2,
    [59] = SDL_SCANCODE_F3,
    [60] = SDL_SCANCODE_F4,
    [61] = SDL_SCANCODE_F5,
    [62] = SDL_SCANCODE_F6,
    [63] = SDL_SCANCODE_F7,
    [64] = SDL_SCANCODE_F8,
    [65] = SDL_SCANCODE_F9,
    [66] = SDL_SCANCODE_F10,
    [67] = SDL_SCANCODE_F11,
    [68] = SDL_SCANCODE_F12,
};

static inline SDL_Scancode QNX_FixScancode(SDL_Scancode original)
{
    if (original < SDL_NUM_SCANCODES && qnx_scancode_fix[original] != 0) {
        return qnx_scancode_fix[original];
    }
    return original;   // fallback: keep original if no mapping
}

#endif /* QNX_SCANCODE_FIX_H */
