aboutsummaryrefslogtreecommitdiff
path: root/src/comet.c
blob: 98beb74330b117931593db6d62647bded95b21cd (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <math.h>
#include <glib.h>
#include <glib-unix.h>
#include <time.h>
#include <unistd.h>

#include "window.h"
#include "log.h"
#include "draw.h"
#include "connect.h"
#include "state.h"

#define EVEN(n) ((int)(n) - ((int)(n) % 2 != 0))

static gboolean mainloop_quit(gpointer data)
{
    g_main_loop_quit(data);
    return G_SOURCE_CONTINUE;
}

//static gboolean mainloop_draw(gpointer data)
//{
//    State *state = data;
//    draw_compute_layout(state->draw, state->win, state->btns);
//    draw_paint(state->draw, state->win);
//    return G_SOURCE_CONTINUE;
//}

static void log_handler(const char *log_domain,
                        GLogLevelFlags level,
                        const char *message,
                        gpointer log_level)
{
    GLogLevelFlags message_level = level & G_LOG_LEVEL_MASK;

    if ((GLogLevelFlags)log_level < message_level)
        return;

    if (message_level <= G_LOG_LEVEL_WARNING)
        g_printerr("%s\n", message);
    else
        g_print("%s\n", message);
}

typedef struct {
    State *state;
    Button *btn;
    timer_t timer;
} Date_Handler;

static gboolean date_update(Date_Handler *date_ctx)
{
    g_free(date_ctx->btn->text);
    GDateTime *time = g_date_time_new_now_local();
    char *date = g_date_time_format(time, "%A %d    %H:%M");
    g_assert(date != NULL);
    date_ctx->btn->text = date;

    log_debug("Updated date");
    state_redraw(date_ctx->state);

    struct timespec current;
    clock_gettime(CLOCK_REALTIME, &current);

    struct itimerspec its = { 0 };
    its.it_value.tv_sec = 60 - (current.tv_sec % 60);
    timer_settime(date_ctx->timer, 0, &its, NULL);

    return G_SOURCE_CONTINUE;
}


static void show_action(Button *btn)
{
    log_info("Called action: %s", btn->text);
}

static void quit_action(Button *btn)
{
    log_info("Quit button pressed");
    g_main_loop_quit(btn->action_data);
}

int main(int argc, char **argv)
{
    g_log_set_default_handler(log_handler, (gpointer)G_LOG_LEVEL_DEBUG);

    GMainLoop *mainloop = g_main_loop_new(NULL, FALSE);

    Connection *con = connect_create();

    Window *win = window_create(con);

    int screen_width = con->screen_size->width;
    int screen_height = con->screen_size->height;

    int height = EVEN(round(screen_height * 0.021));
    int x_padding = EVEN(round(screen_width * 0.005));
    int y_padding = EVEN(round(screen_height * 0.004));

    log_debug("Calculated dimensions [height=%d, x_pad=%d, y_pad=%d]", height, x_padding, y_padding);

    Color background_all = { 0.3, 0.3, 0.3, 1 };
    Drawable *draw = draw_create("Hack 13 Bold", height, x_padding, x_padding, y_padding, 0);
    draw_set_background(draw, background_all);

    State *state = state_create(win, draw);

    Color background = { 0.4, 0.4, 0.4, 1 };
    Color foreground = { 0.8, 0.8, 0.8, 1 };
    Color stroke = { 0.8, 0.8, 0.8, 1 };

    for (int i = 0; i < 9; ++i) {
        char text[] = { '1' + i, '\0' };
        Button *btn = button_create(text, PANGO_ALIGN_LEFT);
        button_set_colors(btn, background, foreground, stroke);
        button_set_action(btn, show_action, NULL);
        state_add_button(state, btn);
    }

    Button *long_btn = button_create("long button", PANGO_ALIGN_RIGHT);
    button_set_colors(long_btn, background, foreground, stroke);
    button_set_action(long_btn, show_action, NULL);
    state_add_button(state, long_btn);

    Button *date_btn = button_create("date", PANGO_ALIGN_CENTER);
    button_set_colors(date_btn, background, foreground, stroke);
    button_set_action(date_btn, show_action, NULL);
    state_add_button(state, date_btn);

    struct sigevent sev = { 0 };
    sev.sigev_notify = SIGEV_SIGNAL;
    sev.sigev_signo = SIGUSR1;

    Date_Handler date_ctx = { state, date_btn, 0 };
    g_assert(timer_create(CLOCK_REALTIME, &sev, &date_ctx.timer) == 0);
    date_update(&date_ctx);

    // purple
    Color background2 = { 0.502, 0.168, 0.886, 1 };
    Button *q_btn = button_create("Q", PANGO_ALIGN_RIGHT);
    button_set_colors(q_btn, background2, foreground, stroke);
    button_set_action(q_btn, quit_action, mainloop);
    state_add_button(state, q_btn);

    connect_attach_state(con, state);
    connect_attach_source(con);

    guint source_alrm = g_unix_signal_add(SIGUSR1, (void *)date_update, &date_ctx);
    guint source_term = g_unix_signal_add(SIGTERM, mainloop_quit, mainloop);
    guint source_int  = g_unix_signal_add(SIGINT,  mainloop_quit, mainloop);

    // TODO: Redraw only when needed (dirty flag)
    //guint id = g_timeout_add(1000, mainloop_draw, state);
    state_redraw(state);

    log_debug("Starting main loop");
    g_main_loop_run(mainloop);

    log_debug("Cleaning main loop");
    g_clear_pointer(&mainloop, g_main_loop_unref);

    g_source_remove(source_alrm);
    g_source_remove(source_term);
    g_source_remove(source_int);

    timer_delete(date_ctx.timer);

    state_destroy(state);
    draw_destroy(draw);
    window_destroy(win);

    return 0;
}

// vim: ts=4 sw=4 et