Skip to content

Commit

Permalink
Add AUTOTYPE to DOSBox's programs
Browse files Browse the repository at this point in the history
AUTOTYPE performs scripted keyboard entry into the running
DOS program.

It can be used to reliably skip intros, answer Q&A style questions
that some games ask on startup, or conduct a simple demo.

It allows for delaying input by any number of fractional seconds,
as well defining the pacing between keystrokes. It uses the
comma character "," to insert additional delays similar to modern
phone numbers.

It uses key_* names as defined by the mapper to avoid using SDL
scancodes[1], which are unstable across platforms. This approach
also allows the triggering of custom key bindings the use has
defined.

[1] https://wiki.libsdl.org/SDL_GetScancodeName

"Warning: The returned name is by design not stable across
platforms, e.g. the name for SDL_SCANCODE_LGUI is "Left GUI" under
Linux but "Left Windows" under Microsoft Windows, and some
scancodes like SDL_SCANCODE_NONUSBACKSLASH don't have any name at
all. There are even scancodes that share names, e.g.
SDL_SCANCODE_RETURN and SDL_SCANCODE_RETURN2 (both called
"Return"). This function is therefore unsuitable for creating a
stable cross-platform two-way mapping between strings and
scancodes."
  • Loading branch information
krcroft authored and dreamer committed Apr 4, 2020
1 parent ee71074 commit 239396f
Show file tree
Hide file tree
Showing 11 changed files with 377 additions and 29 deletions.
8 changes: 8 additions & 0 deletions .pvs-suppress
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
{
"version": 1,
"warnings": [
{
"CodeCurrent": 432032057,
"CodeNext": 36027430,
"CodePrev": 0,
"ErrorCode": "V801",
"FileName": "sdl_mapper.cpp",
"Message": "Decreased performance. It is better to redefine the first function argument as a reference. Consider replacing 'const .. sequence' with 'const .. &sequence'."
},
{
"CodeCurrent": 4109900279,
"CodeNext": 355817,
Expand Down
28 changes: 28 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,34 @@ KEYB [keyboardlayoutcode [codepage [codepagefile]]]
keyb


AUTOTYPE [-list] [-w WAIT] [-p PACE] button_1 [button_2 [...]]

Types the button sequence on your behalf, as if entered manually.

It can be used to reliably skip intros, answer Q&A style questions that
some games ask on startup, or to script a simple demo.

Typing is initially delayed by the WAIT time, which defaults to 2 seconds.
The delay between keystrokes is defined by the PACE time, which defaults
to 0.5 seconds.

The comma character "," adds an extra delay similar to modern phone numbers.

-list: prints all available button names.

Examples:
autotype -w 3 -p 0.7 up enter , right enter
autotype -w 1.3 esc esc esc enter p l a y e r enter
autotype -p 1 e x i t enter

Sample batch file for Microprose F-19 Steath Fighter:

autotype n 1
f19.com

It types 'n' when asked if you have a joystick and '1' to select VGA mode.
The game then proceeds to load with these settings applied.


For more information use the /? command line switch with the programs.

Expand Down
10 changes: 9 additions & 1 deletion include/mapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
#ifndef DOSBOX_MAPPER_H
#define DOSBOX_MAPPER_H

#include <string>
#include <vector>
#include "setup.h"
#include "types.h"

enum MapKeys {
MK_f1,MK_f2,MK_f3,MK_f4,MK_f5,MK_f6,MK_f7,MK_f8,MK_f9,MK_f10,MK_f11,MK_f12,
MK_return,MK_kpminus,MK_scrolllock,MK_printscreen,MK_pause,MK_home
Expand All @@ -32,7 +37,10 @@ void MAPPER_StartUp(Section * sec);
void MAPPER_Run(bool pressed);
void MAPPER_DisplayUI();
void MAPPER_LosingFocus(void);

std::vector<std::string> MAPPER_GetEventNames(const std::string &prefix);
void MAPPER_AutoType(std::vector<std::string> &sequence,
const uint32_t wait_ms,
const uint32_t pacing_ms);

#define MMOD1 0x1
#define MMOD2 0x2
Expand Down
3 changes: 3 additions & 0 deletions include/support.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
#define strncasecmp(a, b, n) _strnicmp(a, b, n)
#endif

// Convert a string to double, returning true or false depending on susccess
bool str_to_double(const std::string& input, double &value);

// Returns the filename with the prior path stripped.
// Works with both \ and / directory delimeters.
std::string get_basename(const std::string& filename);
Expand Down
2 changes: 1 addition & 1 deletion src/dos/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ AM_CPPFLAGS = -I$(top_srcdir)/include
noinst_LIBRARIES = libdos.a
EXTRA_DIST = dos_codepages.h dos_keyboard_layout_data.h
libdos_a_SOURCES = dos.cpp dos_devices.cpp dos_execute.cpp dos_files.cpp dos_ioctl.cpp dos_memory.cpp \
dos_misc.cpp dos_classes.cpp dos_programs.cpp dos_tables.cpp \
dos_misc.cpp dos_classes.cpp program_autotype.cpp dos_programs.cpp dos_tables.cpp \
drives.cpp drive_virtual.cpp drive_local.cpp drive_cache.cpp drive_fat.cpp \
drive_iso.cpp dev_con.h dos_mscdex.cpp dos_keyboard_layout.cpp \
cdrom.h cdrom.cpp cdrom_image.cpp \
Expand Down
3 changes: 2 additions & 1 deletion src/dos/dos_programs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "inout.h"
#include "dma.h"
#include "shell.h"
#include "program_autotype.h"

#if defined(WIN32)
#ifndef S_ISDIR
Expand Down Expand Up @@ -1564,7 +1565,6 @@ static void KEYB_ProgramStart(Program * * make) {
*make=new KEYB;
}


void DOS_SetupPrograms(void) {
/*Add Messages */

Expand Down Expand Up @@ -1771,6 +1771,7 @@ void DOS_SetupPrograms(void) {
MSG_Add("PROGRAM_KEYB_INVCPFILE","None or invalid codepage file for layout %s\n\n");

/*regular setup*/
PROGRAMS_MakeFile("AUTOTYPE.COM", AUTOTYPE_ProgramStart);
PROGRAMS_MakeFile("MOUNT.COM",MOUNT_ProgramStart);
PROGRAMS_MakeFile("MEM.COM",MEM_ProgramStart);
PROGRAMS_MakeFile("LOADFIX.COM",LOADFIX_ProgramStart);
Expand Down
158 changes: 158 additions & 0 deletions src/dos/program_autotype.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <sstream>

#include "support.h"
#include "mapper.h"
#include "dosbox.h"
#include "programs.h"
#include "program_autotype.h"

void AUTOTYPE::PrintUsage() {
WriteOut(
"\033[32;1mAUTOTYPE\033[0m [-list] [-w WAIT] [-p PACE] button_1 [button_2 [...]] \n\n"
"Where:\n"
" -list: prints all available button names.\n"
" -w WAIT: seconds before typing begins. Two second default; max of 30.\n"
" -p PACE: seconds between each keystroke. Half-second default; max of 10.\n"
"\n"
" The sequence is comprised of one or more space-separated buttons.\n"
" Autotyping begins after WAIT seconds, and each button is entered \n"
" every PACE seconds. The , character inserts an extra PACE delay.\n"
"\n"
"Some examples:\n"
" \033[32;1mAUTOTYPE\033[0m -w 1 -p 0.3 up enter , right enter\n"
" \033[32;1mAUTOTYPE\033[0m -p 0.2 1 3 , , enter\n"
" \033[32;1mAUTOTYPE\033[0m -w 1.3 esc esc esc enter p l a y e r enter\n");
}

// Prints the key-names for the mapper's currently-bound events.
void AUTOTYPE::PrintKeys() {
const std::vector<std::string> names = MAPPER_GetEventNames("key_");

// Keep track of the longest key name
size_t max_length = 0;
for (const auto &name : names)
max_length = (std::max)(name.length(), max_length);

// Sanity check to avoid dividing by 0
if (!max_length) {
WriteOut("AUTOTYPE: The mapper has no key bindings\n");
return;
}

// Setup our rows and columns
const size_t console_width = 72;
const size_t columns = console_width / max_length;
const size_t rows = ceil_udivide(names.size(), columns);

// Build the string output by rows and columns
std::stringstream ss;
auto name = names.begin();
for (size_t row = 0; row < rows; ++row) {
for (size_t i = row; i < names.size(); i += rows)
ss << std::setw(max_length + 1) << name[i];
ss << std::endl;
}

WriteOut(ss.str().c_str());
}

/*
* Reads a floating point argument from command line, where:
* - name is a human description for the flag, ie: DELAY
* - flag is the command-line flag, ie: -d or -delay
* - default is the default value if the flag doesn't exist
* - value will be populated with the default or provided value
*
* Returns:
* true if 'value' is set to the default or read from the arg.
* false if the argument was used but could not be parsed.
*/
bool AUTOTYPE::ReadDoubleArg(const std::string &name,
const char *flag,
const double &def_value,
const double &min_value,
const double &max_value,
double &value) {
bool result = false;
std::string str_value;
// Is the user trying to set this flag?
if (cmd->FindString(flag, str_value, true)) {
double user_value;

// Can the user's value be parsed?
if (str_to_double(str_value, user_value)) {
result = true;
// Clamp the user's value if needed
value = clamp(user_value, min_value, max_value);

// If we had to clamp the users value, then inform them
if (std::fabs(user_value - value) > std::numeric_limits<double>::epsilon())
WriteOut("AUTOTYPE: bounding %s value of %.2f to %.2f\n",
name.c_str(), user_value, value);
// Otherwise inform them we couldn't parse their value
} else {
WriteOut("AUTOTYPE: %s value '%s' is not a valid floating point number\n",
name.c_str(), str_value.c_str());
}
// Otherwise the user hasn't set this flag, so use the default
} else {
value = def_value;
result = true;
}
return result;
}

void AUTOTYPE::Run() {

//Hack To allow long commandlines
ChangeToLongCmd();

// Usage
if (!cmd->GetCount()) {
PrintUsage();
return;
}

// Print available keys
if (cmd->FindExist("-list", false)) {
PrintKeys();
return;
}

// Get the wait delay in milliseconds
double wait_s;
constexpr double def_wait_s = 2.0;
constexpr double min_wait_s = 0.0;
constexpr double max_wait_s = 30.0;
if (!ReadDoubleArg("WAIT", "-w", def_wait_s, min_wait_s,max_wait_s, wait_s))
return;
const auto wait_ms = static_cast<uint32_t>(wait_s * 1000);

// Get the inter-key pacing in milliseconds
double pace_s;
constexpr double def_pace_s = 0.5;
constexpr double min_pace_s = 0.0;
constexpr double max_pace_s = 10.0;
if (!ReadDoubleArg("PACE", "-p", def_pace_s, min_pace_s, max_pace_s, pace_s))
return;
const auto pace_ms = static_cast<uint32_t>(pace_s * 1000);

// Get the button sequence
std::vector<std::string> sequence;
cmd->FillVector(sequence);
if (sequence.empty()) {
WriteOut("AUTOTYPE: button sequence is empty\n");
return;
}
MAPPER_AutoType(sequence, wait_ms, pace_ms);
}

void AUTOTYPE_ProgramStart(Program * *make) {
*make = new AUTOTYPE;
}
17 changes: 17 additions & 0 deletions src/dos/program_autotype.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "programs.h"

class AUTOTYPE : public Program {
public:
void Run();
private:
void PrintUsage();
void PrintKeys();
bool ReadDoubleArg(const std::string &name,
const char *flag,
const double &def_value,
const double &min_value,
const double &max_value,
double &value);
};

void AUTOTYPE_ProgramStart(Program * *make);
Loading

0 comments on commit 239396f

Please sign in to comment.