summaryrefslogtreecommitdiff
path: root/src/cli.vala
blob: c22e096a820d4486e1a24cb4b726ccff6943a1de (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
static bool help;
static bool version;
static bool daemonize;

const OptionEntry[] options = {
  { "version", 'v', OptionFlags.NONE, OptionArg.NONE, ref version, "Print version number", null },
  { "daemon", 'd', OptionFlags.NONE, OptionArg.NONE, ref daemonize, "Monitor the systemtray", null },
  { null },
};

int main(string[] argv) {
  try {
    var opts = new OptionContext();
    opts.add_main_entries(options, null);
    opts.set_help_enabled(true);
    opts.set_ignore_unknown_options(false);
    opts.parse(ref argv);
  } catch (OptionError err) {
    printerr (err.message);
    return 1;
  }

  var loop = new MainLoop();
  var tray = new AstalTray.Tray();

  if (version) {
    print(AstalTray.VERSION);
    return 0;
  }

  if (daemonize) {
    tray.item_added.connect((id) => {

      AstalTray.TrayItem item = tray.get_item(id);

      string item_json = item.to_json_string();
      stdout.printf("{\"event\":\"item_added\",\"id\":\"%s\",\"item\":%s}\n",
        id, item_json);
      stdout.flush();

      item.changed.connect(() => {
        stdout.printf("{\"event\":\"item_changed\",\"id\":\"%s\",\"item\":%s}\n",
          id, item_json);
        stdout.flush();
      });
    });
    tray.item_removed.connect((id) => {
      stdout.printf("{\"event\":\"item_removed\",\"id\":\"%s\"}\n", id);
      stdout.flush();
    });
  }

  loop.run();
  return 0;
}