Skip to content

Commit

Permalink
Fix #40
Browse files Browse the repository at this point in the history
  • Loading branch information
gnunn1 committed Jan 20, 2016
1 parent e7016df commit bb014d3
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 5 deletions.
16 changes: 15 additions & 1 deletion source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import std.stdio;

import std.experimental.logger;
import std.format;
import std.process;

import gtk.Main;
import gtk.Version;
Expand Down Expand Up @@ -49,7 +50,20 @@ int main(string[] args) {
int result;
try {
trace("Running application...");
result = terminixApp.run(tempArgs);
if (cp.command.length > 0) {
string id = environment["TERMINIX_ID"];
if (id.length == 0) {
writeln("You must execute a command within a running instance of terminix");
return 2;
} else {
trace("Sending command");
terminixApp.register(null);
terminixApp.executeCommand(cp.command, id);
return 0;
}
} else {
result = terminixApp.run(tempArgs);
}
} catch (Exception e) {
error("Unexpected exception occurred");
error("Error: " ~ e.msg);
Expand Down
55 changes: 55 additions & 0 deletions source/gx/terminix/application.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import std.format;
import std.path;
import std.variant;

import gio.ActionGroupIF;
import gio.ActionMapIF;
import gio.Menu;
import gio.MenuModel;
Expand Down Expand Up @@ -36,6 +37,7 @@ import gx.gtk.util;
import gx.i18n.l10n;
import gx.terminix.appwindow;
import gx.terminix.cmdparams;
import gx.terminix.common;
import gx.terminix.constants;
import gx.terminix.preferences;
import gx.terminix.prefwindow;
Expand All @@ -58,6 +60,7 @@ private:
enum ACTION_PREFERENCES = "preferences";
enum ACTION_ABOUT = "about";
enum ACTION_QUIT = "quit";
enum ACTION_COMMAND = "command";

GSettings gsShortcuts;
GSettings gsGeneral;
Expand Down Expand Up @@ -236,6 +239,41 @@ private:
void applyPreferences() {
Settings.getDefault().setProperty(GTK_APP_PREFER_DARK_THEME, (SETTINGS_THEME_VARIANT_DARK_VALUE == gsGeneral.getString(SETTINGS_THEME_VARIANT_KEY)));
}

void executeCommand(GVariant value, SimpleAction sa) {
ulong l;
string command = value.getChildValue(0).getString(l);
if (command.length == 0) {
error("No command was received");
return;
}
string terminalUUID = value.getChildValue(1).getString(l);
if (terminalUUID.length == 0) {
error("Terminal UUID was not sent for command, cannot resolve");
return;
}
trace(format("Command Received, command=%s, terminalID=%s", command, terminalUUID));
//Get action name
string prefix;
string actionName;
getActionNameFromKey(command, prefix, actionName);
Widget widget = findWidgetForUUID(terminalUUID);
while (widget !is null) {
ActionGroupIF group = widget.getActionGroup(prefix);
if (group !is null && group.hasAction(actionName)) {
trace(format("Activating action for prefix=%s and action=%s", prefix, actionName));
group.activateAction(actionName, null);
return;
}
widget = widget.getParent();
}
//Check if the action belongs to the app
if (prefix == ACTION_PREFIX) {
activateAction(actionName, null);
return;
}
trace(format("Could not find action for prefix=%s and action=%s", prefix, actionName));
}

public:

Expand All @@ -245,8 +283,25 @@ public:
this.addOnActivate(&onAppActivate);
this.addOnStartup(&onAppStartup);
this.addOnShutdown(&onAppShutdown);
GVariant param = new GVariant([new GVariant("None"), new GVariant("None")]);
trace("Registering command action with type " ~ param.getType().peekString());
registerAction(this, ACTION_PREFIX, ACTION_COMMAND, null, &executeCommand, param.getType(), param);

terminix = this;
}

/**
* Executes a command by invoking the command action.
* This is used to invoke a command on a remote instance of
* the GTK Application leveraging the ability for the remote
* instance to trigger actions on the primary instance.
*
* See https://wiki.gnome.org/HowDoI/GtkApplication
*/
void executeCommand(string command, string terminalID) {
GVariant[] param = [new GVariant(command), new GVariant(terminalID)];
activateAction(ACTION_COMMAND, new GVariant(param));
}

void addAppWindow(AppWindow window) {
appWindows ~= window;
Expand Down
10 changes: 7 additions & 3 deletions source/gx/terminix/cmdparams.d
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ struct CommandParameters {
string workingDir;
string profileName;
string session;
string command;

bool exit = false;
int exitCode = 0;

this(string[] args) {
auto results = getopt(args, "working-directory|w", _("Set the working directory of the terminal"), &workingDir, "profile|p",
_("Set the starting profile"), &profileName, "session|s", _("Open the specified session"), &session);

auto results = getopt(args,
"working-directory|w", _("Set the working directory of the terminal"), &workingDir,
"profile|p", _("Set the starting profile"), &profileName,
"session|s", _("Open the specified session"), &session,
"command|c",_("Send a command to current instance"), &command);

if (results.helpWanted) {
defaultGetoptPrinter("Terminix Usage:\n\tterminix [OPTIONS]\n\nAvailable options are:\n", results.options);
writeln("Note that the session option is not compatible with profile and working-directory options");
Expand Down
2 changes: 1 addition & 1 deletion source/gx/terminix/terminal/terminal.d
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ private:
flags = flags | GSpawnFlags.FILE_AND_ARGV_ZERO;
}
}
string[] envv = [""];
string[] envv = ["TERMINIX_ID="~terminalUUID];
foreach(arg; args) trace("Argument: " ~ arg);
try {
bool result = vte.spawnSync(VtePtyFlags.DEFAULT, initialPath, args, envv, flags, null, null, gpid, null);
Expand Down

0 comments on commit bb014d3

Please sign in to comment.