summaryrefslogtreecommitdiff
path: root/src/astal-river.c
blob: 6596eb445ee1233d3834d129dcc2a248dcc1d407 (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
#include "river.h"
#include "gio/gio.h"
#include <getopt.h>
#include <json-glib/json-glib.h>

GMainLoop *loop;

void print_json(AstalRiverRiver *river) {
    JsonNode *json = json_gobject_serialize (G_OBJECT(river));

    gchar* json_str = json_to_string (json, FALSE);
    g_print("%s\n", json_str);
    g_free(json);
}

int main(int argc, char **argv) {

    gboolean daemon = FALSE;

    int opt;
    const char *optstring = "d";

    static struct option long_options[] = {
            {"daemon",  no_argument, NULL, 'd'},
            {NULL, 0, NULL, 0}
    };

    while ((opt = getopt_long(argc, argv, optstring, long_options, NULL)) != -1) {
        switch (opt) {
            case 'd':
                daemon = TRUE;
                break;
            default:
                g_print("Usage: %s [-d]\n", argv[0]);
                exit(EXIT_FAILURE);
        }
    }

    AstalRiverRiver *river = astal_river_river_new ();
    if(daemon) {
        loop = g_main_loop_new (NULL, FALSE);
        g_signal_connect(river, "changed", G_CALLBACK(print_json), NULL);
        g_main_loop_run (loop);
    }
    else {
        print_json (river);
        g_object_unref(river);
    }

}