abse/gameboy-advance

A tscircuit package created by abse.

Version
0.0.45
License
unset
Stars
0

firmware/game-console/doomgeneric/i_input.c

//
// Copyright(C) 1993-1996 Id Software, Inc.
// Copyright(C) 2005-2014 Simon Howard
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//


#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
#include <fcntl.h>
#include <stdio.h>

#include "config.h"
#include "deh_str.h"
#include "doomtype.h"
#include "doomkeys.h"
#include "i_joystick.h"
#include "i_system.h"
#include "i_swap.h"
#include "i_timer.h"
#include "i_video.h"
#include "i_scale.h"
#include "m_argv.h"
#include "m_config.h"
#include "m_misc.h"
#include "tables.h"
#include "v_video.h"
#include "w_wad.h"
#include "z_zone.h"

#include "doomgeneric.h"

int vanilla_keyboard_mapping = 1;

// Is the shift key currently down?

static int shiftdown = 0;

// Lookup table for mapping AT keycodes to their doom keycode
static const char at_to_doom[] =
{
    /* 0x00 */ 0x00,
    /* 0x01 */ KEY_ESCAPE,
    /* 0x02 */ '1',
    /* 0x03 */ '2',
    /* 0x04 */ '3',
    /* 0x05 */ '4',
    /* 0x06 */ '5',
    /* 0x07 */ '6',
    /* 0x08 */ '7',
    /* 0x09 */ '8',
    /* 0x0a */ '9',
    /* 0x0b */ '0',
    /* 0x0c */ '-',
    /* 0x0d */ '=',
    /* 0x0e */ KEY_BACKSPACE,
    /* 0x0f */ KEY_TAB,
    /* 0x10 */ 'q',
    /* 0x11 */ 'w',
    /* 0x12 */ 'e',
    /* 0x13 */ 'r',
    /* 0x14 */ 't',
    /* 0x15 */ 'y',
    /* 0x16 */ 'u',
    /* 0x17 */ 'i',
    /* 0x18 */ 'o',
    /* 0x19 */ 'p',
    /* 0x1a */ '[',
    /* 0x1b */ ']',
    /* 0x1c */ KEY_ENTER,
    /* 0x1d */ KEY_FIRE, /* KEY_RCTRL, */
    /* 0x1e */ 'a',
    /* 0x1f */ 's',
    /* 0x20 */ 'd',
    /* 0x21 */ 'f',
    /* 0x22 */ 'g',
    /* 0x23 */ 'h',
    /* 0x24 */ 'j',
    /* 0x25 */ 'k',
    /* 0x26 */ 'l',
    /* 0x27 */ ';',
    /* 0x28 */ '\'',
    /* 0x29 */ '`',
    /* 0x2a */ KEY_RSHIFT,
    /* 0x2b */ '\\',
    /* 0x2c */ 'z',
    /* 0x2d */ 'x',
    /* 0x2e */ 'c',
    /* 0x2f */ 'v',
    /* 0x30 */ 'b',
    /* 0x31 */ 'n',
    /* 0x32 */ 'm',
    /* 0x33 */ ',',
    /* 0x34 */ '.',
    /* 0x35 */ '/',
    /* 0x36 */ KEY_RSHIFT,
    /* 0x37 */ KEYP_MULTIPLY,
    /* 0x38 */ KEY_LALT,
    /* 0x39 */ KEY_USE,
    /* 0x3a */ KEY_CAPSLOCK,
    /* 0x3b */ KEY_F1,
    /* 0x3c */ KEY_F2,
    /* 0x3d */ KEY_F3,
    /* 0x3e */ KEY_F4,
    /* 0x3f */ KEY_F5,
    /* 0x40 */ KEY_F6,
    /* 0x41 */ KEY_F7,
    /* 0x42 */ KEY_F8,
    /* 0x43 */ KEY_F9,
    /* 0x44 */ KEY_F10,
    /* 0x45 */ KEY_NUMLOCK,
    /* 0x46 */ 0x0,
    /* 0x47 */ 0x0, /* 47 (Keypad-7/Home) */
    /* 0x48 */ 0x0, /* 48 (Keypad-8/Up) */
    /* 0x49 */ 0x0, /* 49 (Keypad-9/PgUp) */
    /* 0x4a */ 0x0, /* 4a (Keypad--) */
    /* 0x4b */ 0x0, /* 4b (Keypad-4/Left) */
    /* 0x4c */ 0x0, /* 4c (Keypad-5) */
    /* 0x4d */ 0x0, /* 4d (Keypad-6/Right) */
    /* 0x4e */ 0x0, /* 4e (Keypad-+) */
    /* 0x4f */ 0x0, /* 4f (Keypad-1/End) */
    /* 0x50 */ 0x0, /* 50 (Keypad-2/Down) */
    /* 0x51 */ 0x0, /* 51 (Keypad-3/PgDn) */
    /* 0x52 */ 0x0, /* 52 (Keypad-0/Ins) */
    /* 0x53 */ 0x0, /* 53 (Keypad-./Del) */
    /* 0x54 */ 0x0, /* 54 (Alt-SysRq) on a 84+ key keyboard */
    /* 0x55 */ 0x0,
    /* 0x56 */ 0x0,
    /* 0x57 */ 0x0,
    /* 0x58 */ 0x0,
    /* 0x59 */ 0x0,
    /* 0x5a */ 0x0,
    /* 0x5b */ 0x0,
    /* 0x5c */ 0x0,
    /* 0x5d */ 0x0,
    /* 0x5e */ 0x0,
    /* 0x5f */ 0x0,
    /* 0x60 */ 0x0,
    /* 0x61 */ 0x0,
    /* 0x62 */ 0x0,
    /* 0x63 */ 0x0,
    /* 0x64 */ 0x0,
    /* 0x65 */ 0x0,
    /* 0x66 */ 0x0,
    /* 0x67 */ KEY_UPARROW,
    /* 0x68 */ 0x0,
    /* 0x69 */ KEY_LEFTARROW,
    /* 0x6a */ KEY_RIGHTARROW,
    /* 0x6b */ 0x0,
    /* 0x6c */ KEY_DOWNARROW,
    /* 0x6d */ 0x0,
    /* 0x6e */ 0x0,
    /* 0x6f */ 0x0,
    /* 0x70 */ 0x0,
    /* 0x71 */ 0x0,
    /* 0x72 */ 0x0,
    /* 0x73 */ 0x0,
    /* 0x74 */ 0x0,
    /* 0x75 */ 0x0,
    /* 0x76 */ 0x0,
    /* 0x77 */ 0x0,
    /* 0x78 */ 0x0,
    /* 0x79 */ 0x0,
    /* 0x7a */ 0x0,
    /* 0x7b */ 0x0,
    /* 0x7c */ 0x0,
    /* 0x7d */ 0x0,
    /* 0x7e */ 0x0,
    /* 0x7f */ KEY_FIRE, //KEY_RCTRL,
};

// Lookup table for mapping ASCII characters to their equivalent when
// shift is pressed on an American layout keyboard:
static const char shiftxform[] =
{
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
    11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
    21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
    31, ' ', '!', '"', '#', '$', '%', '&',
    '"', // shift-'
    '(', ')', '*', '+',
    '<', // shift-,
    '_', // shift--
    '>', // shift-.
    '?', // shift-/
    ')', // shift-0
    '!', // shift-1
    '@', // shift-2
    '#', // shift-3
    '$', // shift-4
    '%', // shift-5
    '^', // shift-6
    '&', // shift-7
    '*', // shift-8
    '(', // shift-9
    ':',
    ':', // shift-;
    '<',
    '+', // shift-=
    '>', '?', '@',
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
    'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
    '[', // shift-[
    '!', // shift-backslash - OH MY GOD DOES WATCOM SUCK
    ']', // shift-]
    '"', '_',
    '\'', // shift-`
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
    'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
    '{', '|', '}', '~', 127
};


static unsigned char TranslateKey(unsigned char key)
{
	return key;

	/*
    if (key < sizeof(at_to_doom))
        return at_to_doom[key];
    else
        return 0x0;
	*/

    //default:
    //  return tolower(key);
}

// Get the equivalent ASCII (Unicode?) character for a keypress.

static unsigned char GetTypedChar(unsigned char key)
{
    key = TranslateKey(key);

    // Is shift held down?  If so, perform a translation.

    if (shiftdown > 0)
    {
        if (key >= 0 && key < arrlen(shiftxform))
        {
            key = shiftxform[key];
        }
        else
        {
            key = 0;
        }
    }

    return key;
}

static void UpdateShiftStatus(int pressed, unsigned char key)
{
    int change;

    if (pressed) {
        change = 1;
    } else {
        change = -1;
    }

    if (key == KEY_RSHIFT) {
        shiftdown += change;
    }
}


void I_GetEvent(void)
{
    event_t event;
    int pressed;
    unsigned char key;

    
	while (DG_GetKey(&pressed, &key))
    {
        UpdateShiftStatus(pressed, key);

        // process event
        
        if (pressed)
        {
            // data1 has the key pressed, data2 has the character
            // (shift-translated, etc)
            event.type = ev_keydown;
            event.data1 = TranslateKey(key);
            event.data2 = GetTypedChar(key);

            if (event.data1 != 0)
            {
                D_PostEvent(&event);
            }
        }
        else
        {
            event.type = ev_keyup;
            event.data1 = TranslateKey(key);

            // data2 is just initialized to zero for ev_keyup.
            // For ev_keydown it's the shifted Unicode character
            // that was typed, but if something wants to detect
            // key releases it should do so based on data1
            // (key ID), not the printable char.

            event.data2 = 0;

            if (event.data1 != 0)
            {
                D_PostEvent(&event);
            }
            break;
        }
    }


                /*
            case SDL_MOUSEMOTION:
                event.type = ev_mouse;
                event.data1 = mouse_button_state;
                event.data2 = AccelerateMouse(sdlevent.motion.xrel);
                event.data3 = -AccelerateMouse(sdlevent.motion.yrel);
                D_PostEvent(&event);
                break;
                */
}

void I_InitInput(void)
{
}

, '%', '\u0026',\n '\"', // shift-'\n '(', ')', '*', '+',\n '\u003c', // shift-,\n '_', // shift--\n '\u003e', // shift-.\n '?', // shift-/\n ')', // shift-0\n '!', // shift-1\n '@', // shift-2\n '#', // shift-3\n '
, // shift-4\n '%', // shift-5\n '^', // shift-6\n '\u0026', // shift-7\n '*', // shift-8\n '(', // shift-9\n ':',\n ':', // shift-;\n '\u003c',\n '+', // shift-=\n '\u003e', '?', '@',\n 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',\n 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',\n '[', // shift-[\n '!', // shift-backslash - OH MY GOD DOES WATCOM SUCK\n ']', // shift-]\n '\"', '_',\n '\\'', // shift-`\n 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',\n 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',\n '{', '|', '}', '~', 127\n};\n\n\nstatic unsigned char TranslateKey(unsigned char key)\n{\n\treturn key;\n\n\t/*\n if (key \u003c sizeof(at_to_doom))\n return at_to_doom[key];\n else\n return 0x0;\n\t*/\n\n //default:\n // return tolower(key);\n}\n\n// Get the equivalent ASCII (Unicode?) character for a keypress.\n\nstatic unsigned char GetTypedChar(unsigned char key)\n{\n key = TranslateKey(key);\n\n // Is shift held down? If so, perform a translation.\n\n if (shiftdown \u003e 0)\n {\n if (key \u003e= 0 \u0026\u0026 key \u003c arrlen(shiftxform))\n {\n key = shiftxform[key];\n }\n else\n {\n key = 0;\n }\n }\n\n return key;\n}\n\nstatic void UpdateShiftStatus(int pressed, unsigned char key)\n{\n int change;\n\n if (pressed) {\n change = 1;\n } else {\n change = -1;\n }\n\n if (key == KEY_RSHIFT) {\n shiftdown += change;\n }\n}\n\n\nvoid I_GetEvent(void)\n{\n event_t event;\n int pressed;\n unsigned char key;\n\n \n\twhile (DG_GetKey(\u0026pressed, \u0026key))\n {\n UpdateShiftStatus(pressed, key);\n\n // process event\n \n if (pressed)\n {\n // data1 has the key pressed, data2 has the character\n // (shift-translated, etc)\n event.type = ev_keydown;\n event.data1 = TranslateKey(key);\n event.data2 = GetTypedChar(key);\n\n if (event.data1 != 0)\n {\n D_PostEvent(\u0026event);\n }\n }\n else\n {\n event.type = ev_keyup;\n event.data1 = TranslateKey(key);\n\n // data2 is just initialized to zero for ev_keyup.\n // For ev_keydown it's the shifted Unicode character\n // that was typed, but if something wants to detect\n // key releases it should do so based on data1\n // (key ID), not the printable char.\n\n event.data2 = 0;\n\n if (event.data1 != 0)\n {\n D_PostEvent(\u0026event);\n }\n break;\n }\n }\n\n\n /*\n case SDL_MOUSEMOTION:\n event.type = ev_mouse;\n event.data1 = mouse_button_state;\n event.data2 = AccelerateMouse(sdlevent.motion.xrel);\n event.data3 = -AccelerateMouse(sdlevent.motion.yrel);\n D_PostEvent(\u0026event);\n break;\n */\n}\n\nvoid I_InitInput(void)\n{\n}\n\n","content_mimetype":"text/x-c","created_at":"2026-11-05T18:49:30.702Z"}; window.SSR_PACKAGE_BUILDS = []; window.SSR_RELATED_PACKAGES = [{"package_id":"1bb71080-c9d9-4806-8dad-9546e8b9913b","creator_account_id":"03854e77-22c2-44a0-9cfc-2d4ed6741105","latest_package_release_id":"92d10956-aa95-4eba-8e98-42e66c16f463","last_built_release_id":"92d10956-aa95-4eba-8e98-42e66c16f463","latest_package_release_fs_sha":"md5-73c7349eef4667e7cbc7beecb7e35036","latest_pcb_preview_image_url":"https://api.tscircuit.com/package_files/view?package_release_id=92d10956-aa95-4eba-8e98-42e66c16f463\u0026file_path=dist%2Findex%2Fpcb.svg","latest_sch_preview_image_url":"https://api.tscircuit.com/package_files/view?package_release_id=92d10956-aa95-4eba-8e98-42e66c16f463\u0026file_path=dist%2Findex%2Fschematic.svg","latest_cad_preview_image_url":"https://api.tscircuit.com/package_files/view?package_release_id=92d10956-aa95-4eba-8e98-42e66c16f463\u0026file_path=dist%2Findex%2F3d.png","latest_version":"1.0.5","license":"unset","owner_org_id":"7978967e-b956-4ac2-b813-667a08f6310a","org_owner_tscircuit_handle":"abse","owner_github_username":null,"github_repo_full_name":null,"is_source_from_github":false,"description":null,"name":"abse/boostxl-uln2003","unscoped_name":"boostxl-uln2003","star_count":0,"created_at":"2026-08-12T10:10:06.978Z","updated_at":"2026-08-12T22:29:23.783Z","ai_description":"This code defines a React component that models a through-hole or surface-mount LED with specified pin labels, footprint, silkscreen, and 3D CAD model for PCB layout.","ai_usage_instructions":null,"is_private":false,"is_unlisted":false,"is_public":true,"public_dist_enabled":false,"has_public_dist":true,"starred_at":null,"default_view":"files","website":null,"github_installation_id":null,"allow_pr_previews":false,"related_type":"same_author","thumbnail_url":"https://api.tscircuit.com/package_files/view?package_release_id=92d10956-aa95-4eba-8e98-42e66c16f463\u0026file_path=dist%2Findex%2Fpcb.svg"},{"package_id":"1a688437-24fc-4e9f-921c-1ddc4fc53cf5","creator_account_id":"84198f66-bb86-4dc1-97f0-cdf2cf2c843a","latest_package_release_id":"37e98b52-0b1d-4f91-8f06-8e5e321f5e45","last_built_release_id":"37e98b52-0b1d-4f91-8f06-8e5e321f5e45","latest_package_release_fs_sha":"md5-a234ce80dacb6ff214fc7c28c619fd53","latest_pcb_preview_image_url":"https://api.tscircuit.com/package_files/view?package_release_id=37e98b52-0b1d-4f91-8f06-8e5e321f5e45\u0026file_path=dist%2Fassembly%2Fpcb.svg","latest_sch_preview_image_url":"https://api.tscircuit.com/package_files/view?package_release_id=37e98b52-0b1d-4f91-8f06-8e5e321f5e45\u0026file_path=dist%2Fassembly%2Fschematic.svg","latest_cad_preview_image_url":"https://api.tscircuit.com/package_files/view?package_release_id=37e98b52-0b1d-4f91-8f06-8e5e321f5e45\u0026file_path=dist%2Fassembly%2F3d.png","latest_version":"1.12.2","license":"ISC","owner_org_id":"07e44f25-c5d3-4583-9c34-2c7bb568f85b","org_owner_tscircuit_handle":"AnasSarkiz","owner_github_username":"AnasSarkiz","github_repo_full_name":null,"is_source_from_github":false,"description":null,"name":"AnasSarkiz/NEMA14_CH32X035G8U6","unscoped_name":"NEMA14_CH32X035G8U6","star_count":0,"created_at":"2026-10-07T17:53:37.598Z","updated_at":"2026-10-16T19:05:31.590Z","ai_description":"A NEMA stepper motor is shown with proposed M3 screws/spacers, four wired motor leads and splices, and a JST SHR-04V-S connector.","ai_usage_instructions":null,"is_private":false,"is_unlisted":false,"is_public":true,"public_dist_enabled":true,"has_public_dist":true,"starred_at":null,"default_view":"files","website":null,"github_installation_id":null,"allow_pr_previews":false,"related_type":"random","thumbnail_url":"https://api.tscircuit.com/package_files/view?package_release_id=37e98b52-0b1d-4f91-8f06-8e5e321f5e45\u0026file_path=dist%2Fassembly%2Fpcb.svg"}]; window.SSR_PACKAGE_ROUTE = {"author":"abse","packageName":"gameboy-advance","packageNameWithScope":"abse/gameboy-advance","version":null,"isLegacyRoute":false,"kind":"file","filePath":"firmware/game-console/doomgeneric/i_input.c"};