Skip to content
This repository has been archived by the owner on Nov 28, 2017. It is now read-only.

Commit

Permalink
Add "busy" and "unbusy" messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnchen902 committed Jan 17, 2017
1 parent e912729 commit 8b7fe51
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
17 changes: 16 additions & 1 deletion messages.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,22 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "messages.h"
#include <stdbool.h>
#include <string.h>
#include "tasks.h"
#include "timecalc.h"

static void handle_now(char *arg, struct Task *tasks, unsigned n);
static void handle_busy(char *arg, struct Task *tasks, unsigned n);
static void handle_unbusy(char *arg, struct Task *tasks, unsigned n);

struct {
const char *const command;
void (*const handler) (char *arg, struct Task *tasks, unsigned n);
} actions[] = {
{"now", handle_now}
{"now", handle_now},
{"busy", handle_busy},
{"unbusy", handle_unbusy},
};

void handle_messages(char *message, struct Task *tasks, unsigned n) {
Expand All @@ -46,3 +52,12 @@ static void handle_now(char *arg, struct Task *tasks, unsigned n) {
if(strcmp(tasks[i].name, arg) == 0 && tasks[i].pid == 0)
execute_task(tasks + i);
}

static void handle_busy(char *arg, struct Task *tasks, unsigned n) {
(void) arg, (void) tasks, (void) n;
timecalc_set_busy(true);
}
static void handle_unbusy(char *arg, struct Task *tasks, unsigned n) {
(void) arg, (void) tasks, (void) n;
timecalc_set_busy(false);
}
31 changes: 24 additions & 7 deletions timecalc.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <X11/Xlib.h>
#include <X11/extensions/scrnsaver.h>
#include <errno.h>
#include <stdbool.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
Expand All @@ -29,18 +30,22 @@

static struct timespec get_idle_time();

static const struct timespec very_long_time = {31536000, 0}; // 1 year
static const struct timespec activity_error = {0, 10000000}; // 10ms
static const struct timespec min_sleep_time = {0, 10000000}; // 10ms
// fire tasks in range (last, current - offset]
static struct timespec last, offset;
// last user activity
static struct timespec last_act;
// if busy, assume user is always active
static bool busy;

void timecalc_init() {
last = (const struct timespec) {0, 0};
if(clock_gettime(CLOCK_MONOTONIC, &offset) < 0)
die("clock_gettime() failed. Reason: %s\n", strerror(errno));
last_act = offset;
busy = false;
}

void timecalc_cycle(struct timespec *timeout,
Expand All @@ -55,6 +60,9 @@ void timecalc_cycle(struct timespec *timeout,
timespec_maxify(&running, tasks[i].time);

struct timespec idle = get_idle_time();
if(busy)
idle = (const struct timespec) {0, 0};

struct timespec activity = timespec_sub(cur, idle);

if(timespec_cmp(last, running) < 0) {
Expand All @@ -79,14 +87,23 @@ void timecalc_cycle(struct timespec *timeout,
}
last = end;

*timeout = (const struct timespec) {365 * 86400, 0};
for(unsigned i = 0; i < n; i++) {
if(timespec_cmp(tasks[i].time, last) > 0)
timespec_minify(timeout, timespec_sub(tasks[i].time, last));
if(timespec_cmp(tasks[i].time, running) > 0)
timespec_minify(timeout, timespec_sub(tasks[i].time, running));
*timeout = very_long_time;
if(!busy) {
for(unsigned i = 0; i < n; i++) {
if(timespec_cmp(tasks[i].time, last) > 0)
timespec_minify(timeout, timespec_sub(tasks[i].time, last));
if(timespec_cmp(tasks[i].time, running) > 0)
timespec_minify(timeout, timespec_sub(tasks[i].time, running));
}
timespec_maxify(timeout, min_sleep_time);
}
timespec_maxify(timeout, min_sleep_time);
}

void timecalc_set_busy(bool b) {
busy = b;
}
bool timecalc_is_busy() {
return busy;
}

static struct timespec get_idle_time() {
Expand Down
5 changes: 5 additions & 0 deletions timecalc.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ void timecalc_init();
*/
void timecalc_cycle(struct timespec *sleep_time,
struct Task *tasks, unsigned n);
/**
* If busy, assume user is always active.
*/
void timecalc_set_busy(_Bool busy);
_Bool timecalc_is_busy();
#endif // TIMECALC_H

0 comments on commit 8b7fe51

Please sign in to comment.