File: EventSource.cpp

package info (click to toggle)
keyman 18.0.245-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,316 kB
  • sloc: python: 52,784; cpp: 21,278; sh: 7,633; ansic: 4,823; xml: 3,617; perl: 959; makefile: 139; javascript: 138
file content (55 lines) | stat: -rw-r--r-- 1,387 bytes parent folder | download | duplicates (2)
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
#include "EventSource.h"

GSourceFuncs sourceFuncs = {
    /* prepare */
    [](GSource*, int* timeout) -> gboolean {
      *timeout = 10 /*ms*/;
      return FALSE;
    },
    /* check */
    [](GSource* gsource) -> gboolean {
      EventSource* source = (EventSource*)gsource;
      return source->Check();
    },
    /* dispatch */
    [](GSource* gsource, GSourceFunc, gpointer) -> gboolean {
      EventSource* source = (EventSource*)gsource;
      return source->Dispatch();
    },
    /* finalize */
    [](GSource*) {},
    nullptr,
    nullptr,
};

EventSource* EventSource::Create(sd_bus* bus) {
  EventSource* source = (EventSource*)g_source_new(&sourceFuncs, sizeof(EventSource));

  source->bus           = bus;
  source->pollFd.fd     = sd_bus_get_fd(bus);
  source->pollFd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;

  g_source_add_poll(&source->base, &source->pollFd);
  g_source_attach(&source->base, g_main_context_default());

  return source;
}

void EventSource::Destroy(EventSource* source) {
  g_source_remove_poll(&source->base, &source->pollFd);
  g_source_destroy(&source->base);
  g_source_unref(&source->base);
}

gboolean EventSource::Check() const {
  return pollFd.revents & (G_IO_IN | G_IO_HUP | G_IO_ERR);
}

gboolean EventSource::Dispatch() {
  int result;
  do {
    result = sd_bus_process(bus, nullptr);
  } while (result > 0);

  return result >= 0;
}