Skip to content

Commit a95c432

Browse files
authored
Port 'Session Monitor and Inhibit' to Vala (#176)
1 parent 1ff9605 commit a95c432

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#! /usr/bin/env -S vala workbench.vala --pkg libadwaita-1 --pkg libportal-gtk4
2+
private Xdp.Portal portal;
3+
private Gtk.Button button_start;
4+
private Gtk.Button button_stop;
5+
private Adw.SwitchRow switch_row_logout;
6+
private Adw.SwitchRow switch_row_idle;
7+
private Xdp.Parent parent;
8+
private Adw.EntryRow entry;
9+
private List<int> ids;
10+
11+
public void main () {
12+
portal = new Xdp.Portal ();
13+
parent = Xdp.parent_new_gtk (workbench.window);
14+
entry = (Adw.EntryRow) workbench.builder.get_object ("entry");
15+
switch_row_logout = (Adw.SwitchRow) workbench.builder.get_object ("switch_row_logout");
16+
switch_row_idle = (Adw.SwitchRow) workbench.builder.get_object ("switch_row_idle");
17+
button_start = (Gtk.Button) workbench.builder.get_object ("button_start");
18+
button_stop = (Gtk.Button) workbench.builder.get_object ("button_stop");
19+
ids = new List<int> ();
20+
21+
button_start.clicked.connect (() => {
22+
start_session.begin ();
23+
});
24+
25+
button_stop.clicked.connect (() => {
26+
stop_session.begin ();
27+
});
28+
29+
portal.session_state_changed.connect ((self, screensaver_active, session_state) => {
30+
31+
if (screensaver_active) {
32+
message (@"Screensaver is active");
33+
}
34+
switch (session_state) {
35+
case Xdp.LoginSessionState.RUNNING:
36+
message (@"Session: Running");
37+
break;
38+
case Xdp.LoginSessionState.QUERY_END:
39+
message (@"Session: Query End");
40+
portal.session_monitor_query_end_response ();
41+
break;
42+
case Xdp.LoginSessionState.ENDING:
43+
message (@"Session: Ending");
44+
break;
45+
}
46+
});
47+
}
48+
49+
async void start_session () {
50+
try {
51+
bool result = yield portal.session_monitor_start (parent, NONE, null);
52+
53+
if (result) {
54+
button_start.sensitive = false;
55+
button_stop.sensitive = true;
56+
if (switch_row_logout.active)inhibit_session.begin (Xdp.InhibitFlags.LOGOUT);
57+
if (switch_row_idle.active)inhibit_session.begin (Xdp.InhibitFlags.IDLE);
58+
/*
59+
Xdp Portal also supports inhibition of Suspend and User Switch
60+
using the flags SUSPEND and USER_SWITCH respectively. But these
61+
actions cannot be inhibited on GNOME as they do not end user's
62+
session.
63+
*/
64+
}
65+
} catch (Error e) {
66+
warning (@"$(e.message)");
67+
}
68+
}
69+
70+
async void stop_session () {
71+
foreach (int id in ids) {
72+
portal.session_uninhibit (id);
73+
}
74+
ids = new List<int> ();
75+
portal.session_monitor_stop ();
76+
button_start.sensitive = true;
77+
button_stop.sensitive = false;
78+
}
79+
80+
async void inhibit_session (Xdp.InhibitFlags flag) {
81+
string reason = entry.text;
82+
try {
83+
int id = yield portal.session_inhibit (parent, reason, flag, null);
84+
85+
ids.append (id);
86+
} catch (Error e) {
87+
warning (@"$(e.message)");
88+
}
89+
}

0 commit comments

Comments
 (0)