Cleanup linux console code

Unify all commands into function interface, which simplifies code.
This commit is contained in:
Azamat H. Hackimov 2024-05-31 15:56:45 +03:00
parent 95caa7b6c3
commit 09823f9b40
3 changed files with 52 additions and 98 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Descent 3 * Descent 3
* Copyright (C) 2024 Parallax Software * Copyright (C) 2024 Parallax Software
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -57,22 +57,34 @@
*/ */
#include <cstdarg> #include <cstdarg>
#include <cstring>
#include <cstdio> #include <cstdio>
#include <cctype> #include <cctype>
#include "application.h" #include "application.h"
#include "AppConsole.h" #include "AppConsole.h"
////////////////////////////////////////////////// enum {
// Defines Console_null,
#define CON_MAX_STRINGLEN 768 Console_raw,
Console_total,
} Console_mode;
enum { Console_null, Console_raw } Console_mode; typedef bool (*Input_fp)(char *buf, int buflen);
typedef void (*Defer_fp)();
typedef bool (*Create_fp)();
typedef void (*Destroy_fp)();
typedef void (*Puts_fp)(int window, const char *str);
struct Console_Commands {
Create_fp con_Create;
Destroy_fp con_Destroy;
Defer_fp con_Defer;
Input_fp con_Input;
Puts_fp con_Puts;
};
//////////////////////////////////////////////// ////////////////////////////////////////////////
// NULL driver functions // NULL driver functions
void con_null_Printf(const char *fmt, ...);
bool con_null_Input(char *buf, int buflen); bool con_null_Input(char *buf, int buflen);
void con_null_Defer(); void con_null_Defer();
bool con_null_Create(); bool con_null_Create();
@ -81,17 +93,32 @@ void con_null_Puts(int window, const char *str);
//////////////////////////////////////////////// ////////////////////////////////////////////////
// raw driver functions // raw driver functions
void con_raw_Printf(const char *fmt, ...);
bool con_raw_Input(char *buf, int buflen); bool con_raw_Input(char *buf, int buflen);
void con_raw_Defer(); void con_raw_Defer();
bool con_raw_Create(); bool con_raw_Create();
void con_raw_Destroy(); void con_raw_Destroy();
void con_raw_Puts(int window, const char *str); void con_raw_Puts(int window, const char *str);
Console_Commands commands[Console_total] = {
{
.con_Create = con_null_Create,
.con_Destroy = con_null_Destroy,
.con_Defer = con_null_Defer,
.con_Input = con_null_Input,
.con_Puts = con_null_Puts,
},
{
.con_Create = con_raw_Create,
.con_Destroy = con_raw_Destroy,
.con_Defer = con_raw_Defer,
.con_Input = con_raw_Input,
.con_Puts = con_raw_Puts,
},
};
////////////////////////////////////////////////// //////////////////////////////////////////////////
// Global Variables // Global Variables
char *Con_read_buf = nullptr; // The next buffer of text from user input bool Con_init = false; // Console has been initialized
bool Con_init = false; // Console has been initialized
#ifdef mem_malloc #ifdef mem_malloc
#undef mem_malloc #undef mem_malloc
@ -102,9 +129,6 @@ bool Con_init = false; // Console has been initialized
#define mem_malloc(x) malloc(x) #define mem_malloc(x) malloc(x)
#define mem_free(x) free(x) #define mem_free(x) free(x)
//////////////////////////////////////////////////
// Prototypes
void con_Defer(); // Performs the actions for the frame
////////////////////////////////////////////////// //////////////////////////////////////////////////
// Functions // Functions
@ -139,16 +163,7 @@ void con_Printf(const char *fmt, ...) {
} }
*fp = '\0'; *fp = '\0';
switch (Console_mode) { commands[Console_mode].con_Puts(0, filter_buf);
case Console_null:
con_null_Puts(0, filter_buf);
break;
case Console_raw:
con_raw_Puts(0, filter_buf);
break;
default:
break;
}
} }
bool con_Input(char *buf, int buflen) { bool con_Input(char *buf, int buflen) {
@ -156,30 +171,7 @@ bool con_Input(char *buf, int buflen) {
*buf = '\0'; *buf = '\0';
return false; return false;
} }
return commands[Console_mode].con_Input(buf, buflen);
if (Console_mode == Console_null) {
*buf = '\0';
return false;
}
if (Console_mode == Console_raw) {
return con_raw_Input(buf, buflen);
}
if (!Con_read_buf) { // there is no read buffer...yipes
*buf = '\0';
return false;
}
if (Con_read_buf[0]) {
// we have a new buffer of input...send it away
strncpy(buf, Con_read_buf, buflen - 1);
buf[buflen - 1] = 0;
Con_read_buf[0] = 0;
return true;
}
return false;
} }
void con_Defer() { void con_Defer() {
@ -187,16 +179,7 @@ void con_Defer() {
// the console hasn't been initialized yet // the console hasn't been initialized yet
return; return;
} }
commands[Console_mode].con_Defer();
if (Console_mode == Console_null) {
con_null_Defer();
return;
}
if (Console_mode == Console_raw) {
con_raw_Defer();
return;
}
} }
bool con_Create(int flags) { bool con_Create(int flags) {
@ -204,23 +187,12 @@ bool con_Create(int flags) {
if (flags & APPFLAG_USESERVICE) { if (flags & APPFLAG_USESERVICE) {
// use the NULL driver! // use the NULL driver!
Console_mode = Console_null; Console_mode = Console_null;
Con_init = con_null_Create();
return Con_init;
} else { } else {
// use stdout driver // use stdout driver
Console_mode = Console_raw; Console_mode = Console_raw;
Con_init = con_raw_Create();
return Con_init;
} }
Con_init = commands[Console_mode].con_Create();
return Con_init;
} }
void con_Destroy() { void con_Destroy() { commands[Console_mode].con_Destroy(); }
if (Console_mode == Console_null) {
con_null_Destroy();
return;
}
if (Console_mode == Console_raw) {
con_raw_Destroy();
return;
}
}

View File

@ -45,7 +45,6 @@
* $NoKeywords: $ * $NoKeywords: $
*/ */
#include <cstdarg>
#include <cstdio> #include <cstdio>
#include "mono.h" #include "mono.h"
@ -54,17 +53,11 @@
// put some data up on the screen // put some data up on the screen
void con_null_Puts(int window, const char *str); void con_null_Puts(int window, const char *str);
void con_null_Printf(const char *fmt, ...) { bool con_null_Input(char *buf, int buflen) {
char buffer[1024]; *buf = '\0';
va_list args; return false;
va_start(args, fmt);
std::vsnprintf(buffer, sizeof(buffer), fmt, args);
va_end(args);
con_null_Puts(0, buffer);
} }
bool con_null_Input(char *buf, int buflen) { return false; }
void con_null_Defer() {} void con_null_Defer() {}
bool con_null_Create() { bool con_null_Create() {

View File

@ -1,5 +1,5 @@
/* /*
* Descent 3 * Descent 3
* Copyright (C) 2024 Parallax Software * Copyright (C) 2024 Parallax Software
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -40,34 +40,23 @@
*/ */
#include <cstdlib> #include <cstdlib>
#include <cstdarg>
#include <cstring> #include <cstring>
#include <cstdio> #include <cstdio>
#include <cctype> #include <cctype>
#include "AppConsole.h" #include "AppConsole.h"
#include "TaskSystem.h" #include "TaskSystem.h"
#include "ddio_common.h" #include "ddio_common.h"
static char *Con_raw_read_buf = NULL; // The next buffer of text from user input static char *Con_raw_read_buf = nullptr; // The next buffer of text from user input
static char *Con_raw_inp_buf = NULL, Con_raw_inp_pos = 0; // Currently updating input buffer of text (and it's position) static char *Con_raw_inp_buf = nullptr,
static char Con_raw_last_command[CON_MAX_STRINGLEN]; // The last command entered by the user Con_raw_inp_pos = 0; // Currently updating input buffer of text (and it's position)
static int Con_raw_cols = 0, Con_raw_rows = 0; // The size of the main window (input window is (1 row, Con_cols)) static char Con_raw_last_command[CON_MAX_STRINGLEN]; // The last command entered by the user
static bool Con_raw_newline = false; static int Con_raw_cols = 0, Con_raw_rows = 0; // The size of the main window (input window is (1 row, Con_cols))
// put some data up on the screen // put some data up on the screen
void con_raw_Puts(int window, const char *str); void con_raw_Puts(int window, const char *str);
void con_raw_Printf(const char *fmt, ...) {
char buffer[1024];
va_list args;
va_start(args, fmt);
std::vsnprintf(buffer, sizeof(buffer), fmt, args);
va_end(args);
con_raw_Puts(0, buffer);
}
bool con_raw_Input(char *buf, int buflen) { bool con_raw_Input(char *buf, int buflen) {
if (!Con_raw_read_buf) { // there is no read buffer...yipes if (!Con_raw_read_buf) { // there is no read buffer...yipes
*buf = '\0'; *buf = '\0';