-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.vala
88 lines (78 loc) · 2.82 KB
/
main.vala
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
private Xdp.Portal portal;
private Gtk.Button button_start;
private Gtk.Button button_stop;
private Adw.SwitchRow switch_row_logout;
private Adw.SwitchRow switch_row_idle;
private Xdp.Parent parent;
private Adw.EntryRow entry;
private List<int> ids;
public void main () {
portal = new Xdp.Portal ();
parent = Xdp.parent_new_gtk (workbench.window);
entry = (Adw.EntryRow) workbench.builder.get_object ("entry");
switch_row_logout = (Adw.SwitchRow) workbench.builder.get_object ("switch_row_logout");
switch_row_idle = (Adw.SwitchRow) workbench.builder.get_object ("switch_row_idle");
button_start = (Gtk.Button) workbench.builder.get_object ("button_start");
button_stop = (Gtk.Button) workbench.builder.get_object ("button_stop");
ids = new List<int> ();
button_start.clicked.connect (() => {
start_session.begin ();
});
button_stop.clicked.connect (() => {
stop_session.begin ();
});
portal.session_state_changed.connect ((self, screensaver_active, session_state) => {
if (screensaver_active) {
message (@"Screensaver is active");
}
switch (session_state) {
case Xdp.LoginSessionState.RUNNING:
message (@"Session: Running");
break;
case Xdp.LoginSessionState.QUERY_END:
message (@"Session: Query End");
portal.session_monitor_query_end_response ();
break;
case Xdp.LoginSessionState.ENDING:
message (@"Session: Ending");
break;
}
});
}
async void start_session () {
try {
bool result = yield portal.session_monitor_start (parent, NONE, null);
if (result) {
button_start.sensitive = false;
button_stop.sensitive = true;
if (switch_row_logout.active)inhibit_session.begin (Xdp.InhibitFlags.LOGOUT);
if (switch_row_idle.active)inhibit_session.begin (Xdp.InhibitFlags.IDLE);
/*
Xdp Portal also supports inhibition of Suspend and User Switch
using the flags SUSPEND and USER_SWITCH respectively. But these
actions cannot be inhibited on GNOME as they do not end user's
session.
*/
}
} catch (Error e) {
warning (@"$(e.message)");
}
}
async void stop_session () {
foreach (int id in ids) {
portal.session_uninhibit (id);
}
ids = new List<int> ();
portal.session_monitor_stop ();
button_start.sensitive = true;
button_stop.sensitive = false;
}
async void inhibit_session (Xdp.InhibitFlags flag) {
string reason = entry.text;
try {
int id = yield portal.session_inhibit (parent, reason, flag, null);
ids.append (id);
} catch (Error e) {
warning (@"$(e.message)");
}
}