2020-03-23 22:02:42 +00:00
|
|
|
#include <chrono>
|
2020-03-16 15:48:12 +00:00
|
|
|
#include <unistd.h>
|
2020-03-23 22:02:42 +00:00
|
|
|
#include <fcntl.h>
|
2020-03-16 15:48:12 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/inotify.h>
|
2021-07-16 00:28:46 +00:00
|
|
|
#include <spdlog/spdlog.h>
|
2020-03-16 15:48:12 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "notify.h"
|
|
|
|
|
|
|
|
#define EVENT_SIZE ( sizeof (struct inotify_event) )
|
|
|
|
#define EVENT_BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) )
|
|
|
|
|
2022-03-24 11:27:12 +00:00
|
|
|
static void fileChanged(notify_thread *nt) {
|
2020-03-16 15:48:12 +00:00
|
|
|
int length, i = 0;
|
|
|
|
char buffer[EVENT_BUF_LEN];
|
2020-03-28 21:32:58 +00:00
|
|
|
overlay_params local_params = *nt->params;
|
2020-03-23 21:17:18 +00:00
|
|
|
|
2020-03-16 16:35:01 +00:00
|
|
|
while (!nt->quit) {
|
2020-03-23 22:02:42 +00:00
|
|
|
length = read( nt->fd, buffer, EVENT_BUF_LEN );
|
2020-03-16 16:35:01 +00:00
|
|
|
while (i < length) {
|
|
|
|
struct inotify_event *event =
|
|
|
|
(struct inotify_event *) &buffer[i];
|
|
|
|
i += EVENT_SIZE + event->len;
|
2020-06-29 11:38:47 +00:00
|
|
|
if (event->mask & IN_MODIFY || event->mask & IN_DELETE_SELF) {
|
2022-03-24 11:27:12 +00:00
|
|
|
// In the case of IN_DELETE_SELF, some editors may do a save-to-temp-file/delete-original/move-temp-file
|
2020-06-29 11:38:47 +00:00
|
|
|
// so sleep a little to let file to be replaced
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
2020-03-28 21:32:58 +00:00
|
|
|
parse_overlay_config(&local_params, getenv("MANGOHUD_CONFIG"));
|
2020-06-29 11:38:47 +00:00
|
|
|
if ((event->mask & IN_DELETE_SELF) || (nt->params->config_file_path != local_params.config_file_path)) {
|
2021-07-21 16:48:45 +00:00
|
|
|
SPDLOG_DEBUG("Watching config file: {}", local_params.config_file_path.c_str());
|
2020-06-08 16:03:20 +00:00
|
|
|
inotify_rm_watch(nt->fd, nt->wd);
|
2020-06-29 11:38:47 +00:00
|
|
|
nt->wd = inotify_add_watch(nt->fd, local_params.config_file_path.c_str(), IN_MODIFY | IN_DELETE_SELF);
|
|
|
|
}
|
|
|
|
std::lock_guard<std::mutex> lk(nt->mutex);
|
2020-03-28 21:32:58 +00:00
|
|
|
*nt->params = local_params;
|
2020-03-16 16:52:03 +00:00
|
|
|
}
|
2020-03-16 16:35:01 +00:00
|
|
|
}
|
|
|
|
i = 0;
|
2020-06-08 16:08:01 +00:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
2020-03-16 15:48:12 +00:00
|
|
|
}
|
2020-03-23 21:17:18 +00:00
|
|
|
}
|
2020-03-23 22:02:42 +00:00
|
|
|
|
|
|
|
bool start_notifier(notify_thread& nt)
|
|
|
|
{
|
2020-06-29 11:38:47 +00:00
|
|
|
nt.fd = inotify_init1(IN_NONBLOCK);
|
|
|
|
if (nt.fd < 0) {
|
2021-07-21 16:48:45 +00:00
|
|
|
SPDLOG_ERROR("inotify_init1 failed: {}", strerror(errno));
|
2020-06-29 11:38:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-03-23 22:02:42 +00:00
|
|
|
|
2020-06-29 11:38:47 +00:00
|
|
|
nt.wd = inotify_add_watch(nt.fd, nt.params->config_file_path.c_str(), IN_MODIFY | IN_DELETE_SELF);
|
2020-03-23 22:02:42 +00:00
|
|
|
if (nt.wd < 0) {
|
|
|
|
close(nt.fd);
|
|
|
|
nt.fd = -1;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-06-08 16:08:01 +00:00
|
|
|
if (nt.thread.joinable())
|
|
|
|
nt.thread.join();
|
|
|
|
nt.thread = std::thread(fileChanged, &nt);
|
2020-03-23 22:02:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void stop_notifier(notify_thread& nt)
|
|
|
|
{
|
|
|
|
if (nt.fd < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
nt.quit = true;
|
2020-06-08 16:08:01 +00:00
|
|
|
if (nt.thread.joinable())
|
|
|
|
nt.thread.join();
|
2020-03-23 22:02:42 +00:00
|
|
|
inotify_rm_watch(nt.fd, nt.wd);
|
|
|
|
close(nt.fd);
|
|
|
|
nt.fd = -1;
|
|
|
|
}
|