Skip to content
Open
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
19 changes: 19 additions & 0 deletions src/main/ptapi/io/piubtn/joystick/joystick.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ bool ptapi_io_piubtn_open()
struct io_util_joystick_util_device_info device_info[MAX_NUM_JOYSTICKS];
size_t devices_connected;

if (util_proc_get_folder_path_shared_object(
(void *) ptapi_io_piubtn_open, path, sizeof(path))) {
config_path = util_str_merge(path, CONFIG_FILENAME);

log_info("Loading configuration from emu lib path: %s", config_path);

if (ptapi_io_piubtn_joystick_util_conf_read_from_file(
&ptapi_io_piubtn_joystick_util_conf, config_path)) {
free(config_path);
goto conf_loaded;
}

free(config_path);
}

// The game changes the working directory to the 'game' sub-folder. Therefore,
// ./my-config does not work here.
if (!util_proc_get_folder_path_executable_no_ld_linux(path, sizeof(path))) {
Expand All @@ -69,6 +84,8 @@ bool ptapi_io_piubtn_open()

config_path = util_str_merge(path, CONFIG_FILENAME);

log_info("Loading configuration from executable path: %s", config_path);

if (!ptapi_io_piubtn_joystick_util_conf_read_from_file(
&ptapi_io_piubtn_joystick_util_conf, config_path)) {
log_error("Loading joystick config file %s failed.", CONFIG_FILENAME);
Expand All @@ -78,6 +95,8 @@ bool ptapi_io_piubtn_open()

free(config_path);

conf_loaded:

devices_connected =
io_util_joystick_util_scan(device_info, MAX_NUM_JOYSTICKS);

Expand Down
18 changes: 18 additions & 0 deletions src/main/ptapi/io/piubtn/keyboard/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ bool ptapi_io_piubtn_open()
char path[PATH_MAX];
char *config_path;

if (util_proc_get_folder_path_shared_object(
(void *) ptapi_io_piubtn_open, path, sizeof(path))) {
config_path = util_str_merge(path, CONFIG_FILENAME);

log_info("Loading configuration from emu lib path: %s", config_path);

if (ptapi_io_piubtn_keyboard_util_conf_read_from_file(
&ptapi_io_piubtn_keyboard_conf, config_path)) {
free(config_path);
memset(ptapi_io_piubtn_keyboard_buffer, 0, sizeof(bool) * KEY_MAP_SIZE);
return true;
}

free(config_path);
}

// The game changes the working directory to the 'game' sub-folder. Therefore,
// ./my-config does not work here.
if (!util_proc_get_folder_path_executable_no_ld_linux(path, sizeof(path))) {
Expand All @@ -57,6 +73,8 @@ bool ptapi_io_piubtn_open()

config_path = util_str_merge(path, CONFIG_FILENAME);

log_info("Loading configuration from executable path: %s", config_path);

if (!ptapi_io_piubtn_keyboard_util_conf_read_from_file(
&ptapi_io_piubtn_keyboard_conf, config_path)) {
log_error("Loading keyboard config file %s failed.", config_path);
Expand Down
19 changes: 18 additions & 1 deletion src/main/ptapi/io/piuio/joystick/joystick.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ bool ptapi_io_piuio_open()
struct io_util_joystick_util_device_info device_info[MAX_NUM_JOYSTICKS];
size_t devices_connected;

if (util_proc_get_folder_path_shared_object(
(void *) ptapi_io_piuio_open, path, sizeof(path))) {
config_path = util_str_merge(path, CONFIG_FILENAME);

log_info("Loading configuration from emu lib path: %s", config_path);

if (ptapi_io_piuio_joystick_util_conf_read_from_file(
&ptapi_io_piuio_joystick_util_conf, config_path)) {
free(config_path);
goto conf_loaded;
}

free(config_path);
}

// The game changes the working directory to the 'game' sub-folder. Therefore,
// ./my-config does not work here.
if (!util_proc_get_folder_path_executable_no_ld_linux(path, sizeof(path))) {
Expand All @@ -69,7 +84,7 @@ bool ptapi_io_piuio_open()

config_path = util_str_merge(path, CONFIG_FILENAME);

log_info("Loading configuration: ", config_path);
log_info("Loading configuration from executable path: %s", config_path);

if (!ptapi_io_piuio_joystick_util_conf_read_from_file(
&ptapi_io_piuio_joystick_util_conf, config_path)) {
Expand All @@ -80,6 +95,8 @@ bool ptapi_io_piuio_open()

free(config_path);

conf_loaded:

devices_connected =
io_util_joystick_util_scan(device_info, MAX_NUM_JOYSTICKS);

Expand Down
18 changes: 17 additions & 1 deletion src/main/ptapi/io/piuio/keyboard/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ bool ptapi_io_piuio_open()
char path[PATH_MAX];
char *config_path;

if (util_proc_get_folder_path_shared_object(
(void *) ptapi_io_piuio_open, path, sizeof(path))) {
config_path = util_str_merge(path, CONFIG_FILENAME);

log_info("Loading configuration from emu lib path: %s", config_path);

if (ptapi_io_piuio_keyboard_util_conf_read_from_file(
&ptapi_io_piuio_keyboard_conf, config_path)) {
free(config_path);
memset(ptapi_io_piuio_keyboard_buffer, 0, sizeof(bool) * KEY_MAP_SIZE);
return true;
}

free(config_path);
}

// The game changes the working directory to the 'game' sub-folder. Therefore,
// ./my-config does not work here.
if (!util_proc_get_folder_path_executable_no_ld_linux(path, sizeof(path))) {
Expand All @@ -56,7 +72,7 @@ bool ptapi_io_piuio_open()

config_path = util_str_merge(path, CONFIG_FILENAME);

log_info("Loading configuration: ", config_path);
log_info("Loading configuration from executable path: %s", config_path);

if (!ptapi_io_piuio_keyboard_util_conf_read_from_file(
&ptapi_io_piuio_keyboard_conf, config_path)) {
Expand Down
26 changes: 25 additions & 1 deletion src/main/util/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,39 @@ char *util_fs_get_filename(const char *path)

char *util_fs_get_abs_path(const char *path)
{
size_t path_len;
char *real_path;

if (!path) {
return NULL;
}

path_len = strlen(path);

if (path_len == 0) {
real_path = (char *) util_xmalloc(1);
real_path[0] = '\0';
return real_path;
}

/* Keep explicit absolute paths untouched to support external mount points. */
if (path[0] == '/') {
real_path = (char *) util_xmalloc(path_len + 1);
memcpy(real_path, path, path_len + 1);
return real_path;
}

real_path = realpath(path, NULL);

if (!real_path) {
log_error(
log_warn(
"Resolving path '%s' to absolute path failed: %s",
path,
strerror(errno));

/* Fall back to the original path and let the dynamic loader resolve it. */
real_path = (char *) util_xmalloc(path_len + 1);
memcpy(real_path, path, path_len + 1);
}

return real_path;
Expand Down
35 changes: 35 additions & 0 deletions src/main/util/proc.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#define LOG_MODULE "util-proc"

#include <dlfcn.h>
#include <errno.h>
#include <linux/limits.h>
#include <string.h>
Expand Down Expand Up @@ -205,6 +206,40 @@ bool util_proc_get_folder_path_executable_no_ld_linux(char *buffer, size_t size)
}
}

bool util_proc_get_folder_path_shared_object(
void *symbol, char *buffer, size_t size)
{
Dl_info info;

if (!symbol || !buffer || size == 0) {
return false;
}

if (!dladdr(symbol, &info) || !info.dli_fname) {
return false;
}

if (strlen(info.dli_fname) >= size) {
return false;
}

strcpy(buffer, info.dli_fname);

// If shared object in the root folder, keep the single /
size_t pos = strlen(buffer) - 1;
while (pos > 0 && buffer[pos] != '/') {
buffer[pos] = '\0';
pos--;
}

// delete /
if (pos > 0) {
buffer[pos] = '\0';
}

return true;
}

void util_proc_log_info()
{
log_info(
Expand Down
12 changes: 12 additions & 0 deletions src/main/util/proc.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ bool util_proc_get_folder_path_executable(char *buffer, size_t size);
bool util_proc_get_folder_path_executable_no_ld_linux(
char *buffer, size_t size);

/**
* Get the full path of the folder of the shared object that contains the
* supplied symbol.
*
* @param symbol Pointer to any symbol in the target shared object.
* @param buffer Buffer to read the path into.
* @param size Size of the buffer.
* @return True on success, false on failure.
*/
bool util_proc_get_folder_path_shared_object(
void *symbol, char *buffer, size_t size);

/**
* Log information about the current process to the console.
*/
Expand Down