mirror of
https://github.com/ErikReider/SwayNotificationCenter.git
synced 2024-10-27 01:51:26 +03:00
Simplified script and base_widget command execution (#318)
This commit is contained in:
parent
237310c627
commit
ea66e68783
@ -237,44 +237,18 @@ namespace SwayNotificationCenter {
|
||||
public ScriptRunOnType run_on { get; set; default = ScriptRunOnType.RECEIVE; }
|
||||
|
||||
public async bool run_script (NotifyParams param, out string msg) {
|
||||
msg = "";
|
||||
try {
|
||||
string[] spawn_env = Environ.get ();
|
||||
// Export env variables
|
||||
spawn_env += "SWAYNC_APP_NAME=%s".printf (param.app_name);
|
||||
spawn_env += "SWAYNC_SUMMARY=%s".printf (param.summary);
|
||||
spawn_env += "SWAYNC_BODY=%s".printf (param.body);
|
||||
spawn_env += "SWAYNC_URGENCY=%s".printf (param.urgency.to_string ());
|
||||
spawn_env += "SWAYNC_CATEGORY=%s".printf (param.category);
|
||||
spawn_env += "SWAYNC_ID=%s".printf (param.applied_id.to_string ());
|
||||
spawn_env += "SWAYNC_REPLACES_ID=%s".printf (param.replaces_id.to_string ());
|
||||
spawn_env += "SWAYNC_TIME=%s".printf (param.time.to_string ());
|
||||
spawn_env += "SWAYNC_DESKTOP_ENTRY=%s".printf (param.desktop_entry ?? "");
|
||||
string[] spawn_env = {};
|
||||
spawn_env += "SWAYNC_APP_NAME=%s".printf (param.app_name);
|
||||
spawn_env += "SWAYNC_SUMMARY=%s".printf (param.summary);
|
||||
spawn_env += "SWAYNC_BODY=%s".printf (param.body);
|
||||
spawn_env += "SWAYNC_URGENCY=%s".printf (param.urgency.to_string ());
|
||||
spawn_env += "SWAYNC_CATEGORY=%s".printf (param.category);
|
||||
spawn_env += "SWAYNC_ID=%s".printf (param.applied_id.to_string ());
|
||||
spawn_env += "SWAYNC_REPLACES_ID=%s".printf (param.replaces_id.to_string ());
|
||||
spawn_env += "SWAYNC_TIME=%s".printf (param.time.to_string ());
|
||||
spawn_env += "SWAYNC_DESKTOP_ENTRY=%s".printf (param.desktop_entry ?? "");
|
||||
|
||||
Pid child_pid;
|
||||
Process.spawn_async (
|
||||
"/",
|
||||
exec.split (" "),
|
||||
spawn_env,
|
||||
SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD,
|
||||
null,
|
||||
out child_pid);
|
||||
|
||||
// Close the child when the spawned process is idling
|
||||
int end_status = 0;
|
||||
ChildWatch.add (child_pid, (pid, status) => {
|
||||
Process.close_pid (pid);
|
||||
end_status = status;
|
||||
run_script.callback ();
|
||||
});
|
||||
// Waits until `run_script.callback()` is called above
|
||||
yield;
|
||||
return end_status == 0;
|
||||
} catch (Error e) {
|
||||
stderr.printf ("Run_Script Error: %s\n", e.message);
|
||||
msg = e.message;
|
||||
return false;
|
||||
}
|
||||
return yield Functions.execute_command (exec, spawn_env, out msg);
|
||||
}
|
||||
|
||||
public override bool matches_notification (NotifyParams param) {
|
||||
|
@ -1,5 +1,3 @@
|
||||
using Posix;
|
||||
|
||||
namespace SwayNotificationCenter.Widgets {
|
||||
public abstract class BaseWidget : Gtk.Box {
|
||||
public abstract string widget_name { get; }
|
||||
@ -101,17 +99,9 @@ namespace SwayNotificationCenter.Widgets {
|
||||
return res;
|
||||
}
|
||||
|
||||
protected void execute_command (string cmd) {
|
||||
pid_t pid;
|
||||
int status;
|
||||
if ((pid = fork ()) < 0) {
|
||||
perror ("fork()");
|
||||
}
|
||||
if (pid == 0) { // Child process
|
||||
execl ("/bin/sh", "sh", "-c", cmd);
|
||||
exit (EXIT_FAILURE); // should not return from execl
|
||||
}
|
||||
waitpid (pid, out status, 1);
|
||||
protected async void execute_command (string cmd, string[] env_additions = {}) {
|
||||
string msg = "";
|
||||
yield Functions.execute_command (cmd, env_additions, out msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ namespace SwayNotificationCenter.Widgets {
|
||||
foreach (var act in actions) {
|
||||
Gtk.Button b = new Gtk.Button.with_label (act.label);
|
||||
|
||||
b.clicked.connect (() => execute_command (act.command));
|
||||
b.clicked.connect (() => execute_command.begin (act.command));
|
||||
|
||||
container.insert (b, -1);
|
||||
}
|
||||
|
@ -76,7 +76,7 @@ namespace SwayNotificationCenter.Widgets {
|
||||
foreach (Action a in obj.actions) {
|
||||
Gtk.Button b = new Gtk.Button.with_label (a.label);
|
||||
|
||||
b.clicked.connect (() => execute_command (a.command));
|
||||
b.clicked.connect (() => execute_command.begin (a.command));
|
||||
|
||||
container.add (b);
|
||||
}
|
||||
@ -111,7 +111,7 @@ namespace SwayNotificationCenter.Widgets {
|
||||
|
||||
foreach (var a in obj.actions) {
|
||||
Gtk.Button b = new Gtk.Button.with_label (a.label);
|
||||
b.clicked.connect (() => execute_command (a.command));
|
||||
b.clicked.connect (() => execute_command.begin (a.command));
|
||||
menu.pack_start (b, true, true, 0);
|
||||
}
|
||||
|
||||
|
@ -299,5 +299,43 @@ namespace SwayNotificationCenter {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static async bool execute_command (string cmd, string[] env_additions = {}, out string msg) {
|
||||
msg = "";
|
||||
try {
|
||||
string[] spawn_env = Environ.get ();
|
||||
// Export env variables
|
||||
foreach (string additions in env_additions) {
|
||||
spawn_env += additions;
|
||||
}
|
||||
|
||||
string[] argvp = {};
|
||||
Shell.parse_argv (cmd, out argvp);
|
||||
|
||||
Pid child_pid;
|
||||
Process.spawn_async (
|
||||
"/",
|
||||
argvp,
|
||||
spawn_env,
|
||||
SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD,
|
||||
null,
|
||||
out child_pid);
|
||||
|
||||
// Close the child when the spawned process is idling
|
||||
int end_status = 0;
|
||||
ChildWatch.add (child_pid, (pid, status) => {
|
||||
Process.close_pid (pid);
|
||||
end_status = status;
|
||||
execute_command.callback ();
|
||||
});
|
||||
// Waits until `run_script.callback()` is called above
|
||||
yield;
|
||||
return end_status == 0;
|
||||
} catch (Error e) {
|
||||
stderr.printf ("Run_Script Error: %s\n", e.message);
|
||||
msg = e.message;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user