summaryrefslogtreecommitdiff
path: root/lib/wayland-glib/wl-source.vala
blob: 57b986630b3adf9aecf950f1387516cad3b5cbec (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
namespace WlSource {
public class WlSource : Source {

  public Wl.Display display;
  public void* fd;
  public int error;

  public override bool dispatch(SourceFunc callback) {
    IOCondition revents = this.query_unix_fd(this.fd);
    if(this.error > 0 || (revents & (IOCondition.ERR | IOCondition.HUP)) != 0) {
      errno = this.error;
      if(callback != null) return callback();
      return Source.REMOVE;
    }
    if(this.display.dispatch_pending() < 0) {
      if(callback != null) return callback();
      return Source.REMOVE;
    }
    return Source.CONTINUE;
  }

  public override bool check() {
    if(this.error > 0) return true;
    IOCondition revents = this.query_unix_fd(this.fd);
    if((revents & IOCondition.IN) != 0) {
      if(this.display.read_events() < 0) {
        this.error = errno;
      }
    }
    else {
      this.display.cancel_read();
    }
    return revents > 0;
  }

  public override bool prepare(out int timeout) {
    timeout = 0;
    if(this.display.prepare_read() != 0) return true;
    else if (this.display.flush() < 0 ){
      this.error = errno;
      return true;
    }
    timeout = -1;
    return false;
  }

  public WlSource() {
    base();
    this.display = Wl.Display.connect(null);
    if(this.display == null) return;
    this.fd = this.add_unix_fd(this.display.get_fd(), IOCondition.IN | IOCondition.ERR | IOCondition.HUP);
    this.attach(null);
  }

}
}