diff --git a/src/gl/inject.cpp b/src/gl/inject.cpp index 64dfdb79..67bd3586 100644 --- a/src/gl/inject.cpp +++ b/src/gl/inject.cpp @@ -73,7 +73,7 @@ static std::string deviceName; // seems to quit by itself though static std::unique_ptr> - stop_notifier(¬ifier, [](notify_thread *n){ n->quit = true; }); + stop_it(¬ifier, [](notify_thread *n){ stop_notifier(*n); }); void imgui_init() { @@ -81,7 +81,7 @@ void imgui_init() return; parse_overlay_config(¶ms, getenv("MANGOHUD_CONFIG")); notifier.params = ¶ms; - pthread_create(&fileChange, NULL, &fileChanged, ¬ifier); + start_notifier(notifier); window_size = ImVec2(params.width, params.height); init_system_info(); cfg_inited = true; diff --git a/src/notify.cpp b/src/notify.cpp index 6ef2648b..130322c3 100644 --- a/src/notify.cpp +++ b/src/notify.cpp @@ -1,4 +1,7 @@ +#include +#include #include +#include #include #include #include "config.h" @@ -9,22 +12,13 @@ pthread_t fileChange; #define EVENT_SIZE ( sizeof (struct inotify_event) ) #define EVENT_BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) ) -void *fileChanged(void *params_void){ +static void *fileChanged(void *params_void) { notify_thread *nt = reinterpret_cast(params_void); int length, i = 0; - int fd; - int wd; char buffer[EVENT_BUF_LEN]; - fd = inotify_init(); - wd = inotify_add_watch( fd, nt->params->config_file_path.c_str(), IN_MODIFY); - - if (wd < 0) { - close(fd); - return nullptr; - } while (!nt->quit) { - length = read( fd, buffer, EVENT_BUF_LEN ); + length = read( nt->fd, buffer, EVENT_BUF_LEN ); while (i < length) { struct inotify_event *event = (struct inotify_event *) &buffer[i]; @@ -35,9 +29,39 @@ void *fileChanged(void *params_void){ } } i = 0; - printf("File Changed\n"); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); } - inotify_rm_watch(fd, wd); - close(fd); + printf("%s quit\n", __func__); return nullptr; } + +bool start_notifier(notify_thread& nt) +{ + nt.fd = inotify_init(); + nt.wd = inotify_add_watch( nt.fd, nt.params->config_file_path.c_str(), IN_MODIFY); + + int flags = fcntl(nt.fd, F_GETFL, 0); + if (fcntl(nt.fd, F_SETFL, flags | O_NONBLOCK)) + perror("Set non-blocking failed"); + + if (nt.wd < 0) { + close(nt.fd); + nt.fd = -1; + return false; + } + + pthread_create(&fileChange, NULL, &fileChanged, &nt); + + return true; +} + +void stop_notifier(notify_thread& nt) +{ + if (nt.fd < 0) + return; + + nt.quit = true; + inotify_rm_watch(nt.fd, nt.wd); + close(nt.fd); + nt.fd = -1; +} diff --git a/src/notify.h b/src/notify.h index ee09e74c..7dd8197a 100644 --- a/src/notify.h +++ b/src/notify.h @@ -1,13 +1,13 @@ -#include #include #include "overlay_params.h" struct notify_thread { + int fd = -1, wd = -1; overlay_params *params = nullptr; bool quit = false; std::mutex mutex; }; -extern pthread_t fileChange; -extern void *fileChanged(void *params_void); \ No newline at end of file +bool start_notifier(notify_thread& nt); +void stop_notifier(notify_thread& nt); diff --git a/src/overlay.cpp b/src/overlay.cpp index e5ef5f1b..9a4bbfad 100644 --- a/src/overlay.cpp +++ b/src/overlay.cpp @@ -2659,7 +2659,7 @@ static VkResult overlay_CreateInstance( parse_overlay_config(&instance_data->params, getenv("MANGOHUD_CONFIG")); instance_data->notifier.params = &instance_data->params; - pthread_create(&fileChange, NULL, &fileChanged, &instance_data->notifier); + start_notifier(instance_data->notifier); init_cpu_stats(instance_data->params); @@ -2685,7 +2685,7 @@ static void overlay_DestroyInstance( struct instance_data *instance_data = FIND(struct instance_data, instance); instance_data_map_physical_devices(instance_data, false); instance_data->vtable.DestroyInstance(instance, pAllocator); - instance_data->notifier.quit = true; + stop_notifier(instance_data->notifier); destroy_instance_data(instance_data); }