Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions linux/src/open_file_dialog.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "open_file_dialog.h"
#include "compat.h"
#include <gtk/gtk.h>
#include <cstring>
#include <vector>
Expand Down Expand Up @@ -68,11 +69,11 @@ std::vector<std::string> open_file_dialog(const std::string &title, const std::s
return result;
}

int open_file_dialog(char *rom_name) {
int open_file_dialog(char *rom_filepath) {
std::vector<std::string> fileTypes = {"ch8", "CH8", "chip-8", "CHIP-8", "Chip-8"};
const char* defaultDir = ""; // unify behavior: let OS choose last-used/home
std::vector<std::string> files = open_file_dialog("Chip8", defaultDir, fileTypes);
if (files.empty()) return 1;
snprintf(rom_name, 256, "%s", files[0].c_str());
snprintf(rom_filepath, PATH_MAX, "%s", files[0].c_str());
return 0;
}
5 changes: 3 additions & 2 deletions macos/src/open_file_dialog.mm
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import "open_file_dialog.h"
#import "compat.h"
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#include <string>
Expand Down Expand Up @@ -43,11 +44,11 @@
return fileList;
}

int open_file_dialog(char *rom_name) {
int open_file_dialog(char *rom_filepath) {
std::vector<std::string> fileTypes = {"ch8", "CH8", "chip-8", "CHIP-8", "Chip-8"};
const char* defaultDir = ""; // unify behavior: let OS choose last-used/home
std::vector<std::string> files = open_file_dialog("Chip8", defaultDir, fileTypes);
if (files.empty()) return 1;
snprintf(rom_name, 256, "%s", files[0].c_str());
snprintf(rom_filepath, PATH_MAX, "%s", files[0].c_str());
return 0;
}
7 changes: 4 additions & 3 deletions shared/chip8.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "profiles.h"
#include "sha256.h"
#include "toast.h"
#include "compat.h"
#include "open_file_dialog.h"
#include <SDL2/SDL.h>
#include <stdio.h>
Expand Down Expand Up @@ -193,10 +194,10 @@ int chip8_load_rom(const char *rom_filepath) {

} else {
/* load ROM from GUI */
char new_rom_name[PATH_MAX];
open_file_dialog(new_rom_name) ?
char new_rom_filepath[PATH_MAX];
open_file_dialog(new_rom_filepath) ?
printf("User aborted the open file dialog.\n") :
chip8_load_rom(new_rom_name);
chip8_load_rom(new_rom_filepath);

/* flip GUI toggle */
gui.load_rom_flag = 0;
Expand Down
3 changes: 2 additions & 1 deletion shared/chip8.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
extern "C" {
#endif

#include "compat.h"
#include "bootrom.h" // Generated at build time from roms/Kiwi8_logo_2.ch8
#include "quirks.h"
#include <stdlib.h>
Expand Down Expand Up @@ -62,7 +63,7 @@ struct chip8 {
unsigned int rom_size;

/* rom profile tracking */
char rom_filename[256]; /* basename of currently loaded ROM */
char rom_filename[FILENAME_MAX]; /* basename of currently loaded ROM */
int rom_loaded; /* 1 if user ROM loaded, 0 if bootrom */

/* registers */
Expand Down
28 changes: 28 additions & 0 deletions shared/compat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef COMPAT_H
#define COMPAT_H

#include <limits.h>

// Handle missing PATH_MAX on some systems
#if defined(__APPLE__)
#include <sys/syslimits.h>
#elif defined(__linux__)
#include <linux/limits.h>
#elif defined(_WIN32)
#include <windows.h>
#ifndef PATH_MAX
#define PATH_MAX MAX_PATH
#endif
#endif

#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
#ifndef FILENAME_MAX
#define FILENAME_MAX 256
#endif
#ifndef LINE_MAX
#define LINE_MAX 2048
#endif

#endif // COMPAT_H
17 changes: 2 additions & 15 deletions shared/open_file_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,10 @@
extern "C" {
#endif

#ifdef __APPLE__
#include <limits.h> /* PATH_MAX */
#endif

#ifdef _WIN32
#include <windows.h> /* MAX_PATH */
#define PATH_MAX MAX_PATH
#endif

#ifdef __linux__
#include <limits.h> /* PATH_MAX */
#endif

int open_file_dialog(char *rom_name);
int open_file_dialog(char *rom_filepath);

#ifdef __cplusplus
}
#endif

#endif
#endif // OPEN_FILE_DIALOG_H
7 changes: 4 additions & 3 deletions shared/profiles.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "profiles.h"
#include "compat.h"
#define STB_DS_IMPLEMENTATION
#include "stb_ds.h"
#include "sha256.h"
Expand All @@ -13,7 +14,7 @@
static struct { sha256_hash_t key; struct profile value; } *profile_map = NULL;

/* Track which path we loaded profiles.ini from */
static char loaded_profiles_path[512] = "";
static char loaded_profiles_path[FILENAME_MAX] = "";

/* Quirk field descriptor table — generated from QUIRK_FIELDS X-macro in quirks.h */
static const struct {
Expand Down Expand Up @@ -58,7 +59,7 @@ static void parse_profiles_ini(void) {
FILE *file = fopen(loaded_profiles_path, "r");
if (!file) return;

char line[512];
char line[LINE_MAX];
sha256_hash_t current_sha256 = {0};
int has_current = 0;
struct profile current_profile;
Expand Down Expand Up @@ -164,7 +165,7 @@ static int resolve_profiles_path(void) {
}

/* Build search paths relative to executable */
char search_paths[2][512];
char search_paths[2][LINE_MAX];
char *base_path = SDL_GetBasePath();
if (!base_path) {
printf("Warning: Could not determine executable path. Trying current directory.\n");
Expand Down
3 changes: 2 additions & 1 deletion shared/profiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ extern "C" {
#include <stdint.h>
#include "quirks.h"
#include "sha256.h"
#include "compat.h"

typedef struct { uint8_t bytes[32]; } sha256_hash_t;

struct profile {
sha256_hash_t sha256;
char rom_name[256];
char rom_name[FILENAME_MAX];
struct quirks quirks;
};

Expand Down
8 changes: 8 additions & 0 deletions shared/usage.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef USAGE_H
#define USAGE_H

#ifdef __cplusplus
extern "C" {
#endif

/* Centralized usage/help text for CLI and GUI */
static const char *USAGE_TEXT =
"Usage: Kiwi8 [options] [rom_file]\n"
Expand All @@ -12,4 +16,8 @@ static const char *USAGE_TEXT =
"\n"
"Note: Quirks are configured per-ROM via profiles.ini or GUI.\n";

#ifdef __cplusplus
}
#endif

#endif /* USAGE_H */
15 changes: 8 additions & 7 deletions windows/src/open_file_dialog.cc
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#include "open_file_dialog.h"
#include "compat.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int open_file_dialog(char *rom_name, char *filters) {
int open_file_dialog(char *rom_filepath, char *filters) {
/* open file dialogue */
char cwd[MAX_PATH];
GetCurrentDirectory(MAX_PATH, cwd);
char cwd[PATH_MAX];
GetCurrentDirectory(PATH_MAX, cwd);

OPENFILENAME ofn;

char szFile[MAX_PATH];
char szFile[PATH_MAX];

/* open a file name */
ZeroMemory( &ofn , sizeof( ofn));
Expand All @@ -35,10 +36,10 @@ int open_file_dialog(char *rom_name, char *filters) {
return 1;
}

strcpy(rom_name, szFile);
strcpy(rom_filepath, szFile);
return 0;
}

int open_file_dialog(char *rom_name) {
return open_file_dialog(rom_name, "Chip8\0*.ch8\0All\0*.*\0");
int open_file_dialog(char *rom_filepath) {
return open_file_dialog(rom_filepath, "Chip8\0*.ch8\0All\0*.*\0");
}
Loading