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

Client can now parse multiple commands (#580)

parent 9339e454
...@@ -133,15 +133,17 @@ private void print_subscribe_waybar () { ...@@ -133,15 +133,17 @@ private void print_subscribe_waybar () {
} }
} }
public int command_line (string[] args) { public int command_line (ref string[] args) {
bool skip_wait = "--skip-wait" in args || "-sw" in args; bool skip_wait = "--skip-wait" in args || "-sw" in args;
// Used to know how many args the current command consumed
int used_args = 1;
try { try {
if (args.length < 2) { if (args.length < 1) {
print_help (args); print_help (args);
Process.exit (1); Process.exit (1);
} }
switch (args[1]) { switch (args[0]) {
case "--help": case "--help":
case "-h": case "-h":
print_help (args); print_help (args);
...@@ -209,8 +211,9 @@ public int command_line (string[] args) { ...@@ -209,8 +211,9 @@ public int command_line (string[] args) {
case "--action": case "--action":
case "-a": case "-a":
int action_index = 0; int action_index = 0;
if ( args.length >= 3 ) { if (args.length >= 2) {
action_index = int.parse (args[2]); used_args++;
action_index = int.parse (args[1]);
} }
cc_daemon.latest_invoke_action ((uint32) action_index); cc_daemon.latest_invoke_action ((uint32) action_index);
break; break;
...@@ -224,27 +227,29 @@ public int command_line (string[] args) { ...@@ -224,27 +227,29 @@ public int command_line (string[] args) {
break; break;
case "--inhibitor-add": case "--inhibitor-add":
case "-Ia": case "-Ia":
if (args.length < 3) { if (args.length < 2) {
stderr.printf ("Application ID needed!"); stderr.printf ("Application ID needed!");
Process.exit (1); Process.exit (1);
} }
if (cc_daemon.add_inhibitor (args[2])) { used_args++;
print ("Added inhibitor: \"%s\"", args[2]); if (cc_daemon.add_inhibitor (args[1])) {
print ("Added inhibitor: \"%s\"", args[1]);
break; break;
} }
stderr.printf ("Inhibitor: \"%s\" already added!...", args[2]); stderr.printf ("Inhibitor: \"%s\" already added!...", args[1]);
break; break;
case "--inhibitor-remove": case "--inhibitor-remove":
case "-Ir": case "-Ir":
if (args.length < 3) { if (args.length < 2) {
stderr.printf ("Application ID needed!"); stderr.printf ("Application ID needed!");
Process.exit (1); Process.exit (1);
} }
if (cc_daemon.remove_inhibitor (args[2])) { used_args++;
print ("Removed inhibitor: \"%s\"", args[2]); if (cc_daemon.remove_inhibitor (args[1])) {
print ("Removed inhibitor: \"%s\"", args[1]);
break; break;
} }
stderr.printf ("Inhibitor: \"%s\" does not exist!...", args[2]); stderr.printf ("Inhibitor: \"%s\" does not exist!...", args[1]);
break; break;
case "inhibitors-clear": case "inhibitors-clear":
case "-Ic": case "-Ic":
...@@ -284,26 +289,28 @@ public int command_line (string[] args) { ...@@ -284,26 +289,28 @@ public int command_line (string[] args) {
loop.run (); loop.run ();
break; break;
case "--change-cc-monitor": case "--change-cc-monitor":
if (args.length < 3) { if (args.length < 2) {
stderr.printf ("Monitor connector name needed!"); stderr.printf ("Monitor connector name needed!");
Process.exit (1); Process.exit (1);
} }
if (cc_daemon.set_cc_monitor (args[2])) { used_args++;
print ("Changed monitor to: \"%s\"", args[2]); if (cc_daemon.set_cc_monitor (args[1])) {
print ("Changed monitor to: \"%s\"", args[1]);
break; break;
} }
stderr.printf ("Could not find monitor: \"%s\"!", args[2]); stderr.printf ("Could not find monitor: \"%s\"!", args[1]);
break; break;
case "--change-noti-monitor": case "--change-noti-monitor":
if (args.length < 3) { if (args.length < 2) {
stderr.printf ("Monitor connector name needed!"); stderr.printf ("Monitor connector name needed!");
Process.exit (1); Process.exit (1);
} }
if (cc_daemon.set_noti_window_monitor (args[2])) { used_args++;
print ("Changed monitor to: \"%s\"", args[2]); if (cc_daemon.set_noti_window_monitor (args[1])) {
print ("Changed monitor to: \"%s\"", args[1]);
break; break;
} }
stderr.printf ("Could not find monitor: \"%s\"!", args[2]); stderr.printf ("Could not find monitor: \"%s\"!", args[1]);
break; break;
default: default:
print_help (args); print_help (args);
...@@ -314,6 +321,8 @@ public int command_line (string[] args) { ...@@ -314,6 +321,8 @@ public int command_line (string[] args) {
if (skip_wait) Process.exit (1); if (skip_wait) Process.exit (1);
return 1; return 1;
} }
args = args[used_args:];
return 0; return 0;
} }
...@@ -322,16 +331,24 @@ void print_connection_error () { ...@@ -322,16 +331,24 @@ void print_connection_error () {
"Could not connect to CC service. Will wait for connection...\n"); "Could not connect to CC service. Will wait for connection...\n");
} }
int try_connect (string[] args) { int try_connect (owned string[] args) {
try { try {
cc_daemon = Bus.get_proxy_sync ( cc_daemon = Bus.get_proxy_sync (
BusType.SESSION, BusType.SESSION,
"org.erikreider.swaync.cc", "org.erikreider.swaync.cc",
"/org/erikreider/swaync/cc"); "/org/erikreider/swaync/cc");
if (command_line (args) == 1) { bool one_arg = true;
while (args.length > 0) {
if (!one_arg) {
// Separate each command output
print ("\n");
}
if (command_line (ref args) == 1) {
print_connection_error (); print_connection_error ();
return 1; return 1;
} }
one_arg = false;
}
return 0; return 0;
} catch (Error e) { } catch (Error e) {
print_connection_error (); print_connection_error ();
...@@ -340,14 +357,14 @@ int try_connect (string[] args) { ...@@ -340,14 +357,14 @@ int try_connect (string[] args) {
} }
public int main (string[] args) { public int main (string[] args) {
if (try_connect (args) == 1) { if (try_connect (args[1:]) == 1) {
MainLoop loop = new MainLoop (); MainLoop loop = new MainLoop ();
Bus.watch_name ( Bus.watch_name (
BusType.SESSION, BusType.SESSION,
"org.erikreider.swaync.cc", "org.erikreider.swaync.cc",
BusNameWatcherFlags.NONE, BusNameWatcherFlags.NONE,
(conn, name, name_owner) => { (conn, name, name_owner) => {
if (try_connect (args) == 0) loop.quit (); if (try_connect (args[1:]) == 0) loop.quit ();
}, },
null); null);
loop.run (); loop.run ();
......
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