Gtk: Stringify some things.

This commit is contained in:
BearOso 2022-04-28 18:46:26 -05:00
parent a32d391483
commit 375a263961
8 changed files with 62 additions and 105 deletions

View File

@ -278,6 +278,9 @@ list(APPEND SOURCES
src/background_particles.cpp
src/background_particles.h)
list(APPEND SOURCES ../external/fmt/src/format.cc)
list(APPEND INCLUDES ../external/fmt/include)
set(LIBJMA_SOURCES
../jma/s9x-jma.cpp
../jma/7zlzma.cpp

View File

@ -9,6 +9,7 @@
#include <string.h>
#include "gtk_s9x.h"
#include "gtk_binding.h"
#include "fmt/format.h"
Binding::Binding()
{
@ -228,66 +229,53 @@ Binding::Binding(const char *raw_string)
std::string Binding::as_string()
{
char buf[PATH_MAX];
to_string(buf, false);
return std::string(buf);
return to_string(false);
}
void Binding::to_string(char *str, bool translate)
std::string Binding::to_string(bool translate)
{
char buf[256];
std::string str;
#undef _
#define _(String) translate ? gettext(String) : (String)
str[0] = '\0';
if (is_key())
{
char *keyval_name = NULL;
unsigned int keyval = gdk_keyval_to_lower(get_key());
keyval_name = gdk_keyval_name(keyval);
char *keyval_name = gdk_keyval_name(keyval);
if (keyval_name == NULL)
{
sprintf(buf, _("Unknown"));
}
if (keyval_name == nullptr)
str = _("Unknown");
else
{
memset(buf, 0, 256);
strncpy(buf,
keyval_name,
255);
}
str = keyval_name;
if (translate)
for (int i = 0; buf[i]; i++)
if (buf[i] == '_')
buf[i] = ' ';
for (char &c : str)
if (c == '_')
c = ' ';
sprintf(str, _("Keyboard %s%s%s%s"),
(value & BINDING_SHIFT) ? "Shift+" : "",
(value & BINDING_CTRL) ? "Ctrl+" : "",
(value & BINDING_ALT) ? "Alt+" : "",
buf);
str = fmt::format(_("Keyboard {}{}{}{}"),
(value & BINDING_SHIFT) ? "Shift+" : "",
(value & BINDING_CTRL) ? "Ctrl+" : "",
(value & BINDING_ALT) ? "Alt+" : "",
str);
}
else if (is_joy())
{
if ((get_key()) >= 512)
sprintf(buf,
_("Axis %u %s %u%%"),
get_axis(),
is_positive() ? "+" : "-",
get_threshold());
str = fmt::format(_("Axis {} {} {}%"),
get_axis(),
is_positive() ? "+" : "-",
get_threshold());
else
sprintf(buf, _("Button %u"), get_key());
str = fmt::format(_("Button {}"), get_key());
sprintf(str, _("Joystick %u %s"), get_device(), buf);
str = fmt::format(_("Joystick {} {}"), get_device(), str);
}
else
{
sprintf(str, _("Unset"));
}
str = _("Unset");
return str;
}

View File

@ -44,7 +44,7 @@ class Binding
Binding(unsigned int);
Binding();
Binding(const char *str);
void to_string(char *str, bool translate = true);
std::string to_string(bool translate = true);
std::string as_string();
unsigned int hex();
unsigned int base_hex();

View File

@ -93,6 +93,11 @@ void GtkBuilderWindow::set_entry_text(const char *name, const char *text)
get_object<Gtk::Entry>(name)->set_text(text);
}
void GtkBuilderWindow::set_entry_text(const char *name, const std::string &text)
{
get_object<Gtk::Entry>(name)->set_text(text);
}
float GtkBuilderWindow::get_slider(const char *name)
{
return get_object<Gtk::Range>(name)->get_value();

View File

@ -49,6 +49,7 @@ class GtkBuilderWindow
void set_check(const char *name, bool value);
void set_entry_value(const char *name, unsigned int value);
void set_entry_text(const char *name, const char *text);
void set_entry_text(const char *name, const std::string &text);
void set_combo(const char *name, unsigned char value);
void set_spin(const char *name, double value);
void set_slider(const char *name, float value);

View File

@ -188,8 +188,6 @@ static void swap_controllers_1_2()
static void change_slot(int difference)
{
static char buf[256];
gui_config->current_save_slot += difference;
gui_config->current_save_slot %= 1000;
if (gui_config->current_save_slot < 0)
@ -197,8 +195,8 @@ static void change_slot(int difference)
if (!gui_config->rom_loaded)
return;
snprintf(buf, 256, "State Slot: %d", gui_config->current_save_slot);
S9xSetInfoString(buf);
auto info_string = "State Slot: " + std::to_string(gui_config->current_save_slot);
S9xSetInfoString(info_string.c_str());
GFX.InfoStringTimeout = 60;
}

View File

@ -19,12 +19,13 @@
namespace fs = std::filesystem;
using namespace std::literals;
static std::string info_string;
// TODO:
// Snes9x core requires persistent memory for const char * returns, but won't
// manage them. This is used to hold the data. Functions here are Non-reentrant.
static std::string filename_common;
const char *S9xGetFilenameInc(const char *e, enum s9x_getdirtype dirtype)
{
static std::string filename;
fs::path rom_filename(Memory.ROMFilename);
fs::path filename_base(S9xGetDirectory(dirtype));
@ -46,13 +47,13 @@ const char *S9xGetFilenameInc(const char *e, enum s9x_getdirtype dirtype)
i++;
} while (fs::exists(new_filename));
filename = new_filename;
return filename.c_str();
filename_common = new_filename;
return filename_common.c_str();
}
const char *S9xGetDirectory(enum s9x_getdirtype dirtype)
{
static std::string dirname;
std::string dirname;
switch (dirtype)
{
@ -113,24 +114,17 @@ const char *S9xGetDirectory(enum s9x_getdirtype dirtype)
dirname = path;
}
return dirname.c_str();
filename_common = dirname;
return filename_common.c_str();
}
const char *S9xGetFilename(const char *ex, enum s9x_getdirtype dirtype)
{
static std::string filename;
fs::path path(S9xGetDirectory(dirtype));
path /= fs::path(Memory.ROMFilename).filename();
//Fixes issue with MSU-1 on linux
if(ex[0] == '-') {
path.replace_extension("");
filename = path.string();
filename.append(ex);
} else {
path.replace_extension(ex);
filename = path.string();
}
return filename.c_str();
path.replace_extension(ex);
filename_common = path.string();
return filename_common.c_str();
}
const char *S9xBasename(const char *f)
@ -145,22 +139,18 @@ const char *S9xBasename(const char *f)
const char *S9xBasenameNoExt(const char *f)
{
static std::string filename;
filename = fs::path(f).stem();
return filename.c_str();
filename_common = fs::path(f).stem();
return filename_common.c_str();
}
bool8 S9xOpenSnapshotFile(const char *filename, bool8 read_only, STREAM *file)
{
#ifdef ZLIB
if (read_only)
{
if ((*file = OPEN_STREAM(filename, "rb")))
return (true);
else
fprintf(stderr,
"Failed to open file stream for reading. (%s)\n",
zError(errno));
fprintf(stderr, "Failed to open file stream for reading.\n");
}
else
{
@ -170,44 +160,18 @@ bool8 S9xOpenSnapshotFile(const char *filename, bool8 read_only, STREAM *file)
}
else
{
fprintf(stderr,
"Couldn't open stream with zlib. (%s)\n",
zError(errno));
fprintf(stderr, "Couldn't open stream with zlib.\n");
}
}
fprintf(stderr, "zlib: Couldn't open snapshot file:\n%s\n", filename);
fprintf(stderr, "Couldn't open snapshot file:\n%s\n", filename);
#else
char command[PATH_MAX];
if (read_only)
{
sprintf(command, "gzip -d <\"%s\"", filename);
if (*file = popen(command, "r"))
return (true);
}
else
{
sprintf(command, "gzip --best >\"%s\"", filename);
if (*file = popen(command, "wb"))
return (true);
}
fprintf(stderr, "gzip: Couldn't open snapshot file:\n%s\n", filename);
#endif
return (false);
return false;
}
void S9xCloseSnapshotFile(STREAM file)
{
#ifdef ZLIB
CLOSE_STREAM(file);
#else
pclose(file);
#endif
}
void S9xAutoSaveSRAM()
@ -222,7 +186,7 @@ void S9xLoadState(const char *filename)
if (S9xUnfreezeGame(filename))
{
info_string = filename + " loaded"s;
auto info_string = filename + " loaded"s;
S9xSetInfoString(info_string.c_str());
}
else
@ -235,7 +199,7 @@ void S9xSaveState(const char *filename)
{
if (S9xFreezeGame(filename))
{
info_string = filename + " saved"s;
auto info_string = filename + " saved"s;
S9xSetInfoString(info_string.c_str());
}
else
@ -265,7 +229,7 @@ void S9xQuickSaveSlot(int slot)
if (S9xFreezeGame(filename.c_str()))
{
info_string = filename.filename().string() + " saved";
auto info_string = filename.filename().string() + " saved";
S9xSetInfoString(info_string.c_str());
}
}
@ -282,7 +246,7 @@ void S9xQuickLoadSlot(int slot)
if (S9xUnfreezeGame(filename.c_str()))
{
info_string = filename.filename().string() + " loaded";
auto info_string = filename.filename().string() + " loaded";
S9xSetInfoString(info_string.c_str());
return;
}

View File

@ -917,9 +917,7 @@ void Snes9xPreferences::clear_binding(const char *name)
if (b_links[i].button_name)
{
char buf[256];
unset.to_string(buf);
set_entry_text(b_links[i].button_name, buf);
set_entry_text(b_links[i].button_name, unset.to_string(true));
}
}
@ -931,14 +929,14 @@ void Snes9xPreferences::bindings_to_dialog(int joypad)
for (int i = 0; i < NUM_JOYPAD_LINKS; i++)
{
set_entry_text(b_links[i].button_name, bindings[i].as_string().c_str());
set_entry_text(b_links[i].button_name, bindings[i].as_string());
}
auto shortcut_names = &b_links[NUM_JOYPAD_LINKS];
for (int i = 0; shortcut_names[i].button_name; i++)
{
set_entry_text(shortcut_names[i].button_name, shortcut[i].as_string().c_str());
set_entry_text(shortcut_names[i].button_name, shortcut[i].as_string());
}
}