Unverified Commit ea66e687 authored by Erik Reider's avatar Erik Reider Committed by GitHub

Simplified script and base_widget command execution (#318)

parent 237310c6
...@@ -237,44 +237,18 @@ namespace SwayNotificationCenter { ...@@ -237,44 +237,18 @@ namespace SwayNotificationCenter {
public ScriptRunOnType run_on { get; set; default = ScriptRunOnType.RECEIVE; } public ScriptRunOnType run_on { get; set; default = ScriptRunOnType.RECEIVE; }
public async bool run_script (NotifyParams param, out string msg) { public async bool run_script (NotifyParams param, out string msg) {
msg = ""; string[] spawn_env = {};
try { spawn_env += "SWAYNC_APP_NAME=%s".printf (param.app_name);
string[] spawn_env = Environ.get (); spawn_env += "SWAYNC_SUMMARY=%s".printf (param.summary);
// Export env variables spawn_env += "SWAYNC_BODY=%s".printf (param.body);
spawn_env += "SWAYNC_APP_NAME=%s".printf (param.app_name); spawn_env += "SWAYNC_URGENCY=%s".printf (param.urgency.to_string ());
spawn_env += "SWAYNC_SUMMARY=%s".printf (param.summary); spawn_env += "SWAYNC_CATEGORY=%s".printf (param.category);
spawn_env += "SWAYNC_BODY=%s".printf (param.body); spawn_env += "SWAYNC_ID=%s".printf (param.applied_id.to_string ());
spawn_env += "SWAYNC_URGENCY=%s".printf (param.urgency.to_string ()); spawn_env += "SWAYNC_REPLACES_ID=%s".printf (param.replaces_id.to_string ());
spawn_env += "SWAYNC_CATEGORY=%s".printf (param.category); spawn_env += "SWAYNC_TIME=%s".printf (param.time.to_string ());
spawn_env += "SWAYNC_ID=%s".printf (param.applied_id.to_string ()); spawn_env += "SWAYNC_DESKTOP_ENTRY=%s".printf (param.desktop_entry ?? "");
spawn_env += "SWAYNC_REPLACES_ID=%s".printf (param.replaces_id.to_string ());
spawn_env += "SWAYNC_TIME=%s".printf (param.time.to_string ()); return yield Functions.execute_command (exec, spawn_env, out msg);
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;
}
} }
public override bool matches_notification (NotifyParams param) { public override bool matches_notification (NotifyParams param) {
......
using Posix;
namespace SwayNotificationCenter.Widgets { namespace SwayNotificationCenter.Widgets {
public abstract class BaseWidget : Gtk.Box { public abstract class BaseWidget : Gtk.Box {
public abstract string widget_name { get; } public abstract string widget_name { get; }
...@@ -101,17 +99,9 @@ namespace SwayNotificationCenter.Widgets { ...@@ -101,17 +99,9 @@ namespace SwayNotificationCenter.Widgets {
return res; return res;
} }
protected void execute_command (string cmd) { protected async void execute_command (string cmd, string[] env_additions = {}) {
pid_t pid; string msg = "";
int status; yield Functions.execute_command (cmd, env_additions, out msg);
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);
} }
} }
} }
...@@ -28,7 +28,7 @@ namespace SwayNotificationCenter.Widgets { ...@@ -28,7 +28,7 @@ namespace SwayNotificationCenter.Widgets {
foreach (var act in actions) { foreach (var act in actions) {
Gtk.Button b = new Gtk.Button.with_label (act.label); 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); container.insert (b, -1);
} }
......
...@@ -76,7 +76,7 @@ namespace SwayNotificationCenter.Widgets { ...@@ -76,7 +76,7 @@ namespace SwayNotificationCenter.Widgets {
foreach (Action a in obj.actions) { foreach (Action a in obj.actions) {
Gtk.Button b = new Gtk.Button.with_label (a.label); 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); container.add (b);
} }
...@@ -111,7 +111,7 @@ namespace SwayNotificationCenter.Widgets { ...@@ -111,7 +111,7 @@ namespace SwayNotificationCenter.Widgets {
foreach (var a in obj.actions) { foreach (var a in obj.actions) {
Gtk.Button b = new Gtk.Button.with_label (a.label); 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); menu.pack_start (b, true, true, 0);
} }
......
...@@ -299,5 +299,43 @@ namespace SwayNotificationCenter { ...@@ -299,5 +299,43 @@ namespace SwayNotificationCenter {
} }
return result; 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;
}
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment