Skip to content

Commit

Permalink
Add kiosk mode - resolves #18
Browse files Browse the repository at this point in the history
you're welcome @Conan-Kudo
  • Loading branch information
serebit committed Jan 21, 2024
1 parent debf42d commit 5142055
Showing 1 changed file with 40 additions and 8 deletions.
48 changes: 40 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include "server.hpp"

#include <csignal>
#include <cstdio>
#include <optional>
#include <string>
#include <thread>
#include <unistd.h>
#include <utility>

Expand All @@ -10,28 +13,50 @@
#include <wlr/util/log.h>
#include "wlr-wrap-end.hpp"

static pthread_t main_thread;

static void kiosk_run(const int32_t argc, char** argv) {
if (argc != 1)
return;
system(argv[0]);
pthread_kill(main_thread, SIGINT);
}

int32_t main(const int32_t argc, char** argv) {
std::optional<std::string> kiosk_cmd;
std::vector<std::string> startup_cmds;

int32_t c;
while ((c = getopt(argc, argv, "s:h")) != -1) {
while ((c = getopt(argc, argv, "s:k:h")) != -1) {
switch (c) {
case 's':
if (kiosk_cmd.has_value()) {
std::printf("-s and -k options are mutually exclusive\n");
return 1;
}
startup_cmds.emplace_back(optarg);
break;
case 'k':
if (!startup_cmds.empty()) {
std::printf("-s and -k options are mutually exclusive\n");
return 1;
}
kiosk_cmd = optarg;
break;
default:
std::printf("Usage: %s [-s startup command]\n", argv[0]);
std::printf("Usage: %s [-s <startup command>] [-k <kiosk command>]\n", argv[0]);
return 0;
}
}

if (optind < argc) {
std::printf("Usage: %s [-s startup command]\n", argv[0]);
std::printf("Usage: %s [-s <startup command>] [-k <kiosk command>]\n", argv[0]);
return 0;
}

wlr_log_init(WLR_INFO, nullptr);

const Server server = Server();
const auto server = Server();

/* Add a Unix socket to the Wayland display. */
const char* socket = wl_display_add_socket_auto(server.display);
Expand All @@ -49,11 +74,18 @@ int32_t main(const int32_t argc, char** argv) {

setenv("WAYLAND_DISPLAY", socket, true);

for (const auto& cmd : std::as_const(startup_cmds)) {
if (fork() == 0) {
execl("/bin/sh", "/bin/sh", "-c", cmd.c_str(), nullptr);
if (kiosk_cmd.has_value()) {
wlr_log(WLR_INFO, "Running in kiosk mode with command '%s'.", kiosk_cmd->c_str());
main_thread = pthread_self();
char* kiosk_argv[1] = {strdup(kiosk_cmd.value().c_str())};
auto kiosk_thread = std::thread(kiosk_run, 1, kiosk_argv);
kiosk_thread.detach();
} else
for (const auto& cmd : std::as_const(startup_cmds)) {
if (fork() == 0) {
execl("/bin/sh", "/bin/sh", "-c", cmd.c_str(), nullptr);
}
}
}

/* Run the Wayland event loop. This does not return until you exit the
* compositor. Starting the backend rigged up all of the necessary event
Expand Down

0 comments on commit 5142055

Please sign in to comment.