-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core: Add start_application() to replace the vull::main target
It is better for the application to define int main(). Also switch to the new ArgsParser class.
- Loading branch information
Showing
8 changed files
with
111 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#pragma once | ||
|
||
#include <vull/support/function.hh> | ||
|
||
namespace vull { | ||
|
||
class ArgsParser; | ||
|
||
int start_application(int argc, char **argv, ArgsParser &args_parser, Function<void()> start_fn); | ||
|
||
} // namespace vull |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#include <vull/core/application.hh> | ||
|
||
#include <vull/core/log.hh> | ||
#include <vull/platform/file.hh> | ||
#include <vull/support/args_parser.hh> | ||
#include <vull/tasklet/scheduler.hh> | ||
#include <vull/vpak/file_system.hh> | ||
|
||
#include <dirent.h> | ||
#include <errno.h> | ||
#include <fcntl.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <unistd.h> | ||
|
||
namespace vull { | ||
|
||
static int vpak_select(const struct dirent *entry) { | ||
return StringView(static_cast<const char *>(entry->d_name)).ends_with(".vpak") ? 1 : 0; | ||
} | ||
|
||
int start_application(int argc, char **argv, ArgsParser &args_parser, Function<void()> start_fn) { | ||
String vpak_directory_path; | ||
args_parser.add_option(vpak_directory_path, "Vpak directory path", "vpak-dir"); | ||
if (auto result = args_parser.parse_args(argc, argv); result != ArgsParseResult::Continue) { | ||
return result == ArgsParseResult::ExitSuccess ? EXIT_SUCCESS : EXIT_FAILURE; | ||
} | ||
|
||
// Default to directory containing the executable. | ||
if (vpak_directory_path.empty()) { | ||
char *last_slash = argv[0]; | ||
for (char *path = argv[0]; *path != '\0'; path++) { | ||
if (*path == '/') { | ||
last_slash = path; | ||
} | ||
} | ||
vpak_directory_path = String::copy_raw(argv[0], static_cast<size_t>(last_slash - argv[0])); | ||
} | ||
|
||
vull::open_log(); | ||
vull::set_log_colours_enabled(isatty(STDOUT_FILENO) == 1); | ||
|
||
struct dirent **entry_list; | ||
int entry_count = scandir(vpak_directory_path.data(), &entry_list, &vpak_select, &alphasort); | ||
if (entry_count < 0) { | ||
vull::error("[main] Failed to scan vpak directory '{}': {}", vpak_directory_path, strerror(errno)); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
int dir_fd = open(vpak_directory_path.data(), O_DIRECTORY); | ||
if (dir_fd < 0) { | ||
vull::error("[main] Failed to open vpak directory '{}': {}", vpak_directory_path, strerror(errno)); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
for (int i = 0; i < entry_count; i++) { | ||
const dirent *entry = entry_list[i]; | ||
|
||
String name(static_cast<const char *>(entry->d_name)); | ||
vull::info("[main] Found vpak {}", name); | ||
|
||
int fd = openat(dir_fd, name.data(), 0); | ||
if (fd < 0) { | ||
vull::error("[main] Failed to open vpak '{}': {}", name, strerror(errno)); | ||
return EXIT_FAILURE; | ||
} | ||
|
||
vpak::load_vpak(File::from_fd(fd)); | ||
free(entry_list[i]); | ||
} | ||
free(entry_list); | ||
close(dir_fd); | ||
|
||
Scheduler scheduler; | ||
scheduler.start([start_fn = vull::move(start_fn)] { | ||
start_fn(); | ||
Scheduler::current().stop(); | ||
vull::close_log(); | ||
}); | ||
return EXIT_SUCCESS; | ||
} | ||
|
||
} // namespace vull |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
vull_executable(vull-sandbox vull::core vull::graphics vull::main vull::physics vull::ui vull::x11-window) | ||
vull_executable(vull-sandbox vull::core vull::graphics vull::physics vull::ui vull::x11-window) | ||
target_sources(vull-sandbox PRIVATE free_camera.cc main.cc orbit_camera.cc) | ||
vull_depend_builtin(vull-sandbox) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters