Skip to content

Commit

Permalink
legiond: daemon also watches the power-state or power-profile
Browse files Browse the repository at this point in the history
* removes the need for the acpid daemon

Signed-off-by: Gonçalo Negrier Duarte <[email protected]>
  • Loading branch information
MrDuartePT committed Aug 22, 2024
1 parent d960251 commit 4cdcc02
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
2 changes: 0 additions & 2 deletions extra/acpi/events/legion_ac

This file was deleted.

2 changes: 0 additions & 2 deletions extra/acpi/events/legion_ppd

This file was deleted.

4 changes: 2 additions & 2 deletions extra/service/legiond/README.org
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ Every power-state/power-profile change will reset the timer.

~legiond-cli~ send request to ~legiond~ via [[https://en.wikipedia.org/wiki/Unix_domain_socket][Unix domain socket]].

When the power-state or power-profile changed, ~acpid.service~ send a request to ~legiond~ via ~legiond-ctl~.
~legiond~ will call a series of utils after a variable delay(depend on ~legiond-ctl~ arguments).
~legiond~ also check if power-state or power-profile change and automatically load the appropriated fan curve

* Configuration
#+begin_src shell
sudo cp /usr/share/legion_linux/{*.yaml,legiond.ini} /etc/legion_linux/
Expand Down
35 changes: 35 additions & 0 deletions extra/service/legiond/legiond.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "modules/powerstate.h"
#include "modules/output.h"

#define BUF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1))

LEGIOND_CONFIG config;

int delayed = 0;
Expand Down Expand Up @@ -46,11 +48,38 @@ void set_timer(struct itimerspec *its, long delay_s, long delay_ns,
timer_settime(timerid, 0, its, NULL);
}

void* fancurve_change()
{
char buffer[BUF_LEN];
struct inotify_event *event = NULL;

while(1) {
int inotify_fd = inotify_init();
inotify_add_watch(inotify_fd, profile_path, IN_MODIFY);
inotify_add_watch(inotify_fd, ac_path, IN_MODIFY);

int lengh = read(inotify_fd, buffer, BUF_LEN);
char* p = buffer;
while(p < buffer + lengh) {
event = (struct inotify_event*)p;
if (event->mask & IN_MODIFY) {
pretty("set_fancurve start");
set_fancurve(get_powerstate(), &config);
pretty("set_fancurve end");
}
p += sizeof(struct inotify_event) + event->len;
}
close(inotify_fd);
}
return NULL;
}

int main()
{
// remove socket before create it
clear_socket();


parseconf(&config);

// calculate delay
Expand Down Expand Up @@ -93,12 +122,17 @@ int main()
action.sa_handler = term_handler;
sigaction(SIGTERM, &action, NULL);

// powerprofile thread
pthread_t thread_id;
pthread_create(&thread_id, NULL, fancurve_change, NULL);

// listen
while (1) {
int clientfd = accept(fd, NULL, NULL);
char ret[20];
memset(ret, 0, sizeof(ret));
recv(clientfd, ret, sizeof(ret), 0);

printf("cmd: \"%s\" received\n", ret);
if (ret[0] == 'A') {
// delayed means user use legiond-ctl fanset with a parameter
Expand Down Expand Up @@ -128,6 +162,7 @@ int main()
} else {
printf("do nothing\n");
}

close(clientfd);
}
}
4 changes: 4 additions & 0 deletions extra/service/legiond/public.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#include <limits.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/inotify.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <time.h>
#include <unistd.h>
#include <stdbool.h>
#include <pthread.h>

const char *socket_path = "/run/legiond.socket";
const double delay = 1.5;

0 comments on commit 4cdcc02

Please sign in to comment.