summaryrefslogtreecommitdiff
path: root/src/text.c
blob: e20fa10129c0d8ede9dc87bef017bd8101bb93e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "text.h"

#include "signal.h"

#include "modules/pulse.h"
#include "modules/time.h"
#include "modules/battery.h"
#include "modules/cpu.h"
#include "modules/memory.h"
#include "modules/brightness.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <tgmath.h>

#define VALUE_SIZE TEXT_SIZE

struct text {
    char value[VALUE_SIZE];

    struct battery * battery;
    struct pulse * pulse;
    struct time * time;
    struct cpu * cpu;
    struct memory * memory;
    struct brightness * brightness;
};

struct text *
text_new()
{
    struct text * t = malloc(sizeof(struct text));

    memset(t->value, '\0', VALUE_SIZE);

    t->battery = battery_new();
    t->time = time_new();
    t->pulse = pulse_new();
    t->cpu = cpu_new();
    t->memory = memory_new();
    t->brightness = brightness_new();

    return t;
}

void
text_free(struct text * t)
{
    time_free(t->time);
    pulse_free(t->pulse);
    battery_free(t->battery);
    cpu_free(t->cpu);
    memory_free(t->memory);
    brightness_free(t->brightness);

    free(t);
}

void
text_update(struct text * t)
{
    time_t now_time = time(NULL);
    bool should_update = signal_should_update();

    time_update(t->time);

    cpu_update(t->cpu);
    memory_update(t->memory);

    if(should_update || pulse_should_update(t->pulse, now_time, 4)) {
        pulse_update(t->pulse);
    }

    if(should_update || battery_should_update(t->battery, now_time, 10)) {
        battery_update(t->battery);
    }

    if(should_update) {
        brightness_update(t->brightness);
    }

    snprintf(
        t->value, VALUE_SIZE,
        "%s | %s | %s | %s | %s",
        pulse_get_val(t->pulse),
        cpu_get_val(t->cpu),
        memory_get_val(t->memory),
        battery_get_val(t->battery),
        time_get_val(t->time));
}

const char *
text_get_val(struct text * t)
{
    return t->value;
}