Skip to content

Commit

Permalink
Merge pull request #47 from d3m3vilurr/refactor-input-and-config
Browse files Browse the repository at this point in the history
Refactor input, config & Fix some crashes
  • Loading branch information
Vasiliy Horbachenko authored Oct 14, 2016
2 parents bd019a8 + b92bf2c commit 5f7137e
Show file tree
Hide file tree
Showing 9 changed files with 451 additions and 423 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[submodule "third_party/enet"]
path = third_party/enet
url = https://github.com/cgutman/enet.git
[submodule "third_party/inih"]
path = third_party/inih
url = https://github.com/benhoyt/inih
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ include_directories(
third_party/libuuid/src/
third_party/h264bitstream/
third_party/enet/include/
third_party/inih/
)

add_executable(${PROJECT_NAME}.elf
Expand Down Expand Up @@ -84,6 +85,8 @@ add_executable(${PROJECT_NAME}.elf
third_party/enet/peer.c
third_party/enet/protocol.c
third_party/enet/vita.c

third_party/inih/ini.c
)

target_link_libraries(${PROJECT_NAME}.elf
Expand Down
8 changes: 4 additions & 4 deletions mappings/vita.conf
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ btn_thumbl = 300100
btn_thumbr = 300200

# LT
btn_tl = 500258
btn_tl = 500001
# RT
btn_tr = 500259
btn_tr = 500002
# LS
btn_tl2 = 50025a
btn_tl2 = 500004
# RS
btn_tr2 = 50025b
btn_tr2 = 500008

# not used
btn_mode = -1
Expand Down
239 changes: 88 additions & 151 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include <ini.h>
#include "graphics.h"
#include "input/vita.h"

Expand All @@ -36,7 +37,9 @@

#define write_config_string(fd, key, value) fprintf(fd, "%s = %s\n", key, value)
#define write_config_int(fd, key, value) fprintf(fd, "%s = %d\n", key, value)
#define write_config_hex(fd, key, value) fprintf(fd, "%s = %X\n", key, value)
#define write_config_bool(fd, key, value) fprintf(fd, "%s = %s\n", key, value?"true":"false");
#define write_config_section(fd, key) fprintf(fd, "\n[%s]\n", key)

CONFIGURATION config;
char *config_path;
Expand All @@ -46,80 +49,34 @@ static bool mapped = true;
const char* audio_device = NULL;

static struct option long_options[] = {
{"720", no_argument, NULL, 'a'},
{"1080", no_argument, NULL, 'b'},
{"width", required_argument, NULL, 'c'},
{"height", required_argument, NULL, 'd'},
{"30fps", no_argument, NULL, 'e'},
{"60fps", no_argument, NULL, 'f'},
{"bitrate", required_argument, NULL, 'g'},
{"packetsize", required_argument, NULL, 'h'},
{"app", required_argument, NULL, 'i'},
{"input", required_argument, NULL, 'j'},
{"mapping", required_argument, NULL, 'k'},
{"nosops", no_argument, NULL, 'l'},
{"audio", required_argument, NULL, 'm'},
{"localaudio", no_argument, NULL, 'n'},
{"config", required_argument, NULL, 'o'},
{"platform", required_argument, 0, 'p'},
{"save", required_argument, NULL, 'q'},
{"keydir", required_argument, NULL, 'r'},
{"remote", no_argument, NULL, 's'},
{"windowed", no_argument, NULL, 't'},
{"surround", no_argument, NULL, 'u'},
{"fps", required_argument, NULL, 'v'},
{"forcehw", no_argument, NULL, 'w'},
{"forcehevc", no_argument, NULL, 'x'},
{"unsupported", no_argument, NULL, 'y'},
{"720", no_argument, NULL, 'a'},
{"1080", no_argument, NULL, 'b'},
{"width", required_argument, NULL, 'c'},
{"height", required_argument, NULL, 'd'},
{"30fps", no_argument, NULL, 'e'},
{"60fps", no_argument, NULL, 'f'},
{"bitrate", required_argument, NULL, 'g'},
{"packetsize", required_argument, NULL, 'h'},
{"app", required_argument, NULL, 'i'},
{"input", required_argument, NULL, 'j'},
{"mapping", required_argument, NULL, 'k'},
{"nosops", no_argument, NULL, 'l'},
{"audio", required_argument, NULL, 'm'},
{"localaudio", no_argument, NULL, 'n'},
{"config", required_argument, NULL, 'o'},
{"platform", required_argument, 0, 'p'},
{"save", required_argument, NULL, 'q'},
{"keydir", required_argument, NULL, 'r'},
{"remote", no_argument, NULL, 's'},
{"windowed", no_argument, NULL, 't'},
{"surround", no_argument, NULL, 'u'},
{"fps", required_argument, NULL, 'v'},
{"forcehw", no_argument, NULL, 'w'},
{"forcehevc", no_argument, NULL, 'x'},
{"unsupported", no_argument, NULL, 'y'},
{0, 0, 0, 0},
};

char* __get_path(char* name, char* extra_data_dirs) {
const char *xdg_config_dir = getenv("XDG_CONFIG_DIR");
const char *home_dir = getenv("HOME");

if (access(name, R_OK) != -1) {
return name;
}

if (!extra_data_dirs)
extra_data_dirs = "/usr/share:/usr/local/share";
if (!xdg_config_dir)
xdg_config_dir = home_dir;

char *data_dirs = malloc(strlen(USER_PATHS) + 1 + strlen(xdg_config_dir) + 1 + strlen(home_dir) + 1 + strlen(DEFAULT_CONFIG_DIR) + 1 + strlen(extra_data_dirs) + 2);
sprintf(data_dirs, USER_PATHS ":%s:%s/" DEFAULT_CONFIG_DIR ":%s/", xdg_config_dir, home_dir, extra_data_dirs);

char *path = malloc(strlen(data_dirs)+strlen(MOONLIGHT_PATH)+strlen(name)+2);
if (path == NULL) {
fprintf(stderr, "Not enough memory\n");
exit(-1);
}

char* data_dir = data_dirs;
char* end;
do {
end = strstr(data_dir, ":");
int length = end != NULL?end - data_dir:strlen(data_dir);
memcpy(path, data_dir, length);
if (path[0] == '/')
sprintf(path+length, MOONLIGHT_PATH "/%s", name);
else
sprintf(path+length, "/%s", name);

if(access(path, R_OK) != -1) {
free(data_dirs);
return path;
}

data_dir = end + 1;
} while (end != NULL);

free(data_dirs);
free(path);
return NULL;
}

static void parse_argument(int c, char* value, PCONFIGURATION config) {
switch (c) {
case 'a':
Expand Down Expand Up @@ -226,56 +183,55 @@ static void parse_argument(int c, char* value, PCONFIGURATION config) {
}
}

bool config_file_parse(char* filename, PCONFIGURATION config) {
FILE* fd = fopen(filename, "r");
if (fd == NULL) {
printf("Can't open configuration file: %s\n", filename);
return false;
}

char *line = NULL;
size_t len = 0;

while (__getline(&line, &len, fd) != -1) {
char key[256], scan_value[256];
if (sscanf(line, "%s = %s\n", &key, &scan_value) == 2) {
char *value = malloc(sizeof(char) * strlen(scan_value));
strcpy(value, scan_value);
if (strcmp(key, "address") == 0) {
config->address = malloc(sizeof(char)*strlen(value));
strcpy(config->address, value);
} else if (strcmp(key, "sops") == 0) {
config->sops = strcmp("true", value) == 0;
} else if (strcmp(key, "localaudio") == 0) {
config->localaudio = strcmp("true", value) == 0;
} else if (strcmp(key, "backtouchscreen_deadzone") == 0) {
sscanf(value,
"%d,%d,%d,%d",
&config->back_deadzone.top,
&config->back_deadzone.right,
&config->back_deadzone.bottom,
&config->back_deadzone.left);
} else if (strcmp(key, "special_keys") == 0) {
sscanf(value,
"%x,%x,%x,%x,%d,%d",
&config->special_keys.nw,
&config->special_keys.ne,
&config->special_keys.sw,
&config->special_keys.se,
&config->special_keys.offset,
&config->special_keys.size);
} else if (strcmp(key, "disable_powersave") == 0) {
config->disable_powersave = strcmp("true", value) == 0;
} else {
for (int i=0;long_options[i].name != NULL;i++) {
if (long_options[i].has_arg == required_argument && strcmp(long_options[i].name, key) == 0) {
parse_argument(long_options[i].val, value, config);
}
}
}
static int ini_handle(void *out, const char *section, const char *name,
const char *value) {
#define HEX(v) strtol((v), NULL, 16)
#define INT(v) atoi((v))
#define BOOL(v) strcmp((v), "true") == 0
#define STR(v) strdup((v))

PCONFIGURATION config = (PCONFIGURATION)out;
if (strcmp(section, "backtouchscreen_deadzone") == 0) {
if (strcmp(name, "top") == 0) {
config->back_deadzone.top = INT(value);
} else if (strcmp(name, "right") == 0) {
config->back_deadzone.right = INT(value);
} else if (strcmp(name, "bottom") == 0) {
config->back_deadzone.bottom = INT(value);
} else if (strcmp(name, "left") == 0) {
config->back_deadzone.left = INT(value);
}
} else if (strcmp(section, "special_keys") == 0) {
if (strcmp(name, "nw") == 0) {
config->special_keys.nw = HEX(value);
} else if (strcmp(name, "ne") == 0) {
config->special_keys.ne = HEX(value);
} else if (strcmp(name, "sw") == 0) {
config->special_keys.sw = HEX(value);
} else if (strcmp(name, "se") == 0) {
config->special_keys.se = HEX(value);
} else if (strcmp(name, "offset") == 0) {
config->special_keys.offset = INT(value);
} else if (strcmp(name, "size") == 0) {
config->special_keys.size = INT(value);
}
} else {
if (strcmp(name, "address") == 0) {
config->address = STR(value);
} else if (strcmp(name, "bitrate") == 0) {
config->stream.bitrate = INT(value);
} else if (strcmp(name, "sops") == 0) {
config->sops = BOOL(value);
} else if (strcmp(name, "localaudio") == 0) {
config->localaudio = BOOL(value);
} else if (strcmp(name, "disable_powersave") == 0) {
config->disable_powersave = BOOL(value);
}
}
return true;
}

bool config_file_parse(char* filename, PCONFIGURATION config) {
return ini_parse(filename, ini_handle, config);
}

void config_save(char* filename, PCONFIGURATION config) {
Expand Down Expand Up @@ -311,26 +267,19 @@ void config_save(char* filename, PCONFIGURATION config) {

write_config_bool(fd, "disable_powersave", config->disable_powersave);

char value[256];
sprintf(
value,
"%d,%d,%d,%d",
config->back_deadzone.top,
config->back_deadzone.right,
config->back_deadzone.bottom,
config->back_deadzone.left);
write_config_string(fd, "backtouchscreen_deadzone", value);
write_config_section(fd, "backtouchscreen_deadzone");
write_config_int(fd, "top", config->back_deadzone.top);
write_config_int(fd, "right", config->back_deadzone.right);
write_config_int(fd, "bottom", config->back_deadzone.bottom);
write_config_int(fd, "left", config->back_deadzone.left);

sprintf(
value,
"%x,%x,%x,%x,%d,%d",
config->special_keys.nw,
config->special_keys.ne,
config->special_keys.sw,
config->special_keys.se,
config->special_keys.offset,
config->special_keys.size);
write_config_string(fd, "special_keys", value);
write_config_section(fd, "special_keys");
write_config_hex(fd, "nw", config->special_keys.nw);
write_config_hex(fd, "ne", config->special_keys.ne);
write_config_hex(fd, "sw", config->special_keys.sw);
write_config_hex(fd, "se", config->special_keys.se);
write_config_int(fd, "offset", config->special_keys.offset);
write_config_int(fd, "size", config->special_keys.size);

fclose(fd);
}
Expand All @@ -347,7 +296,7 @@ void config_parse(int argc, char* argv[], PCONFIGURATION config) {
config->stream.audioConfiguration = AUDIO_CONFIGURATION_STEREO;
config->stream.supportsHevc = false;

config->platform = "default";
config->platform = "vita";
config->app = "Steam";
config->action = NULL;
config->address = NULL;
Expand All @@ -372,18 +321,6 @@ void config_parse(int argc, char* argv[], PCONFIGURATION config) {
if (config_file) {
config_file_parse(config_file, config);
}
if (argc == 2 && access(argv[1], F_OK) == 0) {
config->action = "stream";
if (!config_file_parse(argv[1], config))
exit(EXIT_FAILURE);

} else {
int option_index = 0;
int c;
while ((c = getopt_long_only(argc, argv, "-abc:d:efg:h:i:j:k:lm:no:p:q:r:stuv:w:xy", long_options, &option_index)) != -1) {
parse_argument(c, optarg, config);
}
}

if (config->config_file != NULL)
config_save(config->config_file, config);
Expand Down
18 changes: 15 additions & 3 deletions src/gui/ui_connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,17 @@ int ui_connect(char *address) {
server_connected = true;
}

struct menu_entry menu[48];
int app_count = 0;
if (server_applist != NULL) {
PAPP_LIST list = server_applist;
while (list) {
list = list->next;
app_count += 1;
}
}

// current menu = 11 + app_count. but little more alloc ;)
struct menu_entry menu[app_count + 16];

int idx = 0;

Expand Down Expand Up @@ -257,7 +267,7 @@ int ui_connect(char *address) {
}
}

assert(idx < 48);
//assert(idx < 48);
return display_menu(menu, idx, NULL, &ui_connect_loop, NULL, NULL, NULL);
}

Expand All @@ -270,6 +280,8 @@ void ui_connect_ip() {
switch (ime_dialog(&ip, "Enter IP:", "192.168.")) {
case 0:
server_connected = false;
if (config.address)
free(config.address);
config.address = malloc(sizeof(char) * strlen(ip));
strcpy(config.address, ip);
ui_settings_save_config();
Expand All @@ -285,5 +297,5 @@ bool ui_connect_connected() {
}

void ui_connect_address(char *addr) {
strcpy(addr, server.address);
strcpy(addr, server.serverInfo.address);
}
Loading

0 comments on commit 5f7137e

Please sign in to comment.