Skip to content

Commit

Permalink
add spawning commands via pthread
Browse files Browse the repository at this point in the history
  • Loading branch information
yehuthi committed Jan 11, 2024
1 parent 7a1aa8a commit 19eeb70
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,20 @@ find_package(PkgConfig REQUIRED)
pkg_search_module(LIBINPUT REQUIRED libinput)
pkg_search_module(LIBUDEV REQUIRED libudev)

set(SPAWN PTHREAD CACHE STRING "Spawn method")
set_property(CACHE SPAWN PROPERTY STRINGS PTHREAD FORK)

add_executable(linuxflip main.c cli.c)
target_compile_definitions(linuxflip PRIVATE LINUXFLIP_VERSION=${PROJECT_VERSION})
if (NOT SPAWN)
set(SPAWN PTHREAD)
endif()
if (SPAWN STREQUAL "PTHREAD")
target_compile_definitions(linuxflip PRIVATE LINUXFLIP_SPAWN_PTHREAD=1)
elseif(SPAWN STREQUAL "FORK")
target_compile_definitions(linuxflip PRIVATE LINUXFLIP_SPAWN_FORK=1)
else()
message(FATAL_ERROR "invalid SPAWN option \"${SPAWN}\" (must be PTHREAD or FORK, or unset for default)")
endif()
target_include_directories(linuxflip PRIVATE ${LIBINPUT_INCLUDE_DIRS} ${LIBUDEV_INCLUDE_DIRS})
target_link_libraries(linuxflip ${LIBINPUT_LIBRARIES} ${LIBUDEV_LIBRARIES})
27 changes: 26 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#if !defined(LINUXFLIP_SPAWN_PTHREAD) && !defined(LINUXFLIP_SPAWN_FORK)
#warning "No spawn method; using default"
#define LINUXFLIP_SPAWN_PTHREAD 1
#endif

#include <stdio.h>
#include <errno.h>
#include <string.h>
Expand All @@ -7,6 +12,9 @@
#include <fcntl.h>
#include <poll.h>
#include <stdbool.h>
#if LINUXFLIP_SPAWN_PTHREAD
#include <pthread.h>
#endif
#include "cli.h"

#define FATAL(...) { \
Expand All @@ -27,14 +35,25 @@ static void close_restricted(int fd, void *user_data) {
close(fd);
}

static void* system_routine(void* command) {
system((const char*)command);
return 0;
}

int main(int argc, char *argv[]) {
if (argc >= 2 && strcmp(argv[1],"--help") == 0) {
print_help(argv[0], isatty(STDOUT_FILENO));
return 0;
}
#ifdef LINUXFLIP_VERSION
if (argc >= 2 && strcmp(argv[1],"--version") == 0) {
printf("linuxflip %s\n", TO_STRING(LINUXFLIP_VERSION));
const char* spawn =
#if LINUXFLIP_SPAWN_PTHREAD
"pthread";
#else
"fork";
#endif
printf("linuxflip %s (%s)\n", TO_STRING(LINUXFLIP_VERSION), spawn);
return 0;
}
#endif
Expand Down Expand Up @@ -77,8 +96,14 @@ int main(int argc, char *argv[]) {
struct libinput_event_switch *switch_event = libinput_event_get_switch_event(event);
if (libinput_event_switch_get_switch(switch_event) != LIBINPUT_SWITCH_TABLET_MODE) continue;
const bool tablet_mode = libinput_event_switch_get_switch_state(switch_event) == LIBINPUT_SWITCH_STATE_ON;
#if LINUXFLIP_SPAWN_PTHREAD
pthread_t thread;
pthread_create(&thread, NULL, system_routine, tablet_mode ? cmd_tablet : cmd_laptop);
#else
if (!fork())
exit(system(tablet_mode ? cmd_tablet : cmd_laptop));
#endif

libinput_event_destroy(event);
}
}
Expand Down

0 comments on commit 19eeb70

Please sign in to comment.