diff --git a/CMakeLists.txt b/CMakeLists.txt index b71121c..56d8cd6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) diff --git a/main.c b/main.c index c6e66bd..2f6e0a6 100644 --- a/main.c +++ b/main.c @@ -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 #include #include @@ -7,6 +12,9 @@ #include #include #include +#if LINUXFLIP_SPAWN_PTHREAD +#include +#endif #include "cli.h" #define FATAL(...) { \ @@ -27,6 +35,11 @@ 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)); @@ -34,7 +47,13 @@ int main(int argc, char *argv[]) { } #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 @@ -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); } }