Skip to content

Commit

Permalink
Cleanup source formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
kgilmer committed Oct 5, 2021
1 parent 96090d5 commit f72a575
Show file tree
Hide file tree
Showing 8 changed files with 1,903 additions and 250 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
files
build
.vscode
builddir
74 changes: 37 additions & 37 deletions src/arg_parser.vala
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,51 @@
using Gee;

errordomain ArgParser {
PARSE_ERROR
PARSE_ERROR
}

/**
* Convert ["-v", "-s", "asdf", "-f", "qwe"] => {("-v", null), ("-s", "adsf"), ("-f", "qwe")}
* Convert ["-v", "-s", "asdf", "-f", "qwe"] => {("-v", null), ("-s", "adsf"), ("-f", "qwe")}
* Populates key of "cmd" with first arg.
* NOTE: Currently does not support quoted parameter values.
*/
Map<string, string?> parse_args(string[] args) throws ArgParser.PARSE_ERROR {
var argMap = new HashMap<string, string?>();
Map<string, string ? > parse_args (string[] args) throws ArgParser.PARSE_ERROR {
var argMap = new HashMap<string, string ? >();

if (args == null || args.length == 0) {
return argMap;
}

string lastKey = null;
foreach (string token in args) {
if (!argMap.has_key("cmd")) {
argMap.set("cmd", token);
} else if (isKey(token)) {
if (lastKey != null) {
argMap.set(lastKey, null);
}
lastKey = token;
} else if (lastKey != null) {
argMap.set(lastKey, token);
lastKey = null;
} else {
throw new ArgParser.PARSE_ERROR(@"Unexpected literal: $token\n");
if (args == null || args.length == 0) {
return argMap;
}

string lastKey = null;
foreach (string token in args) {
if (!argMap.has_key ("cmd")) {
argMap.set ("cmd", token);
} else if (isKey (token)) {
if (lastKey != null) {
argMap.set (lastKey, null);
}
lastKey = token;
} else if (lastKey != null) {
argMap.set (lastKey, token);
lastKey = null;
} else {
throw new ArgParser.PARSE_ERROR (@"Unexpected literal: $token\n");
}
}

if (lastKey != null) { // Trailing single param
argMap.set (lastKey, null);
}
}

if (lastKey != null) { // Trailing single param
argMap.set(lastKey, null);
}

/*
foreach (var entry in argMap.entries) {
stdout.printf ("%s => %s\n", entry.key, entry.value);
}
*/

return argMap;

/*
foreach (var entry in argMap.entries) {
stdout.printf ("%s => %s\n", entry.key, entry.value);
}
*/

return argMap;
}

bool isKey(string inval) {
return inval.has_prefix("-");
bool isKey (string inval) {
return inval.has_prefix ("-");
}
218 changes: 109 additions & 109 deletions src/grelier.vala
Original file line number Diff line number Diff line change
@@ -1,138 +1,138 @@
/**
* A client library for i3-wm that deserializes into idomatic Vala response objects.
/**
* A client library for i3-wm that deserializes into idomatic Vala response objects.
*/
namespace Grelier {
enum I3_COMMAND {
RUN_COMMAND,
GET_WORKSPACES,
SUBSCRIBE,
GET_OUTPUTS,
GET_TREE,
GET_MARKS,
GET_BAR_CONFIG,
GET_VERSION,
GET_BINDING_MODES,
GET_CONFIG,
SEND_TICK,
SYNC
}

public errordomain I3_ERROR {
RPC_ERROR
}

// https://i3wm.org/docs/ipc.html#_version_reply
public class VersionReply {
public string human_readable { get; private set; }
public string loaded_config_file_name { get; private set; }
public string minor { get; private set; }
public string patch { get; private set; }
public string major { get; private set; }

internal VersionReply(Json.Node responseJson) {
human_readable = responseJson.get_object().get_string_member("human_readable");
loaded_config_file_name = responseJson.get_object().get_string_member("loaded_config_file_name");
minor = responseJson.get_object().get_string_member("minor");
patch = responseJson.get_object().get_string_member("patch");
major = responseJson.get_object().get_string_member("major");
}
}

// https://i3wm.org/docs/ipc.html#_config_reply
public class ConfigReply {
public string config { get; private set; }

internal ConfigReply(Json.Node responseJson) {
config = responseJson.get_object().get_string_member("config");
}
}

public class Client {
private Socket socket;
private uint8[] magic_number = "i3-ipc".data;
private uint8[] terminator = { '\0' };
private int bytes_to_payload = 14;
private int buffer_size = 1024 * 128;

public Client(string i3Socket) throws GLib.Error {
var socketAddress = new UnixSocketAddress(i3Socket);

socket = new Socket (SocketFamily.UNIX, SocketType.STREAM, SocketProtocol.DEFAULT);
assert (socket != null);

socket.connect (socketAddress);
socket.set_blocking(true);
enum I3_COMMAND {
RUN_COMMAND,
GET_WORKSPACES,
SUBSCRIBE,
GET_OUTPUTS,
GET_TREE,
GET_MARKS,
GET_BAR_CONFIG,
GET_VERSION,
GET_BINDING_MODES,
GET_CONFIG,
SEND_TICK,
SYNC
}

~Client() {
if (socket != null) {
socket.close();
}
public errordomain I3_ERROR {
RPC_ERROR
}

private uint8[] int32_to_uint8_array(int32 input) {
Variant val = new Variant.int32(input);
return val.get_data_as_bytes().get_data();
// https://i3wm.org/docs/ipc.html#_version_reply
public class VersionReply {
public string human_readable { get; private set; }
public string loaded_config_file_name { get; private set; }
public string minor { get; private set; }
public string patch { get; private set; }
public string major { get; private set; }

internal VersionReply (Json.Node responseJson) {
human_readable = responseJson.get_object ().get_string_member ("human_readable");
loaded_config_file_name = responseJson.get_object ().get_string_member ("loaded_config_file_name");
minor = responseJson.get_object ().get_string_member ("minor");
patch = responseJson.get_object ().get_string_member ("patch");
major = responseJson.get_object ().get_string_member ("major");
}
}

private string terminate_string(uint8[] rawString) {
ByteArray b = new ByteArray();
b.append(rawString);
b.append(terminator);
// https://i3wm.org/docs/ipc.html#_config_reply
public class ConfigReply {
public string config { get; private set; }

return (string) b.data;
internal ConfigReply (Json.Node responseJson) {
config = responseJson.get_object ().get_string_member ("config");
}
}

private uint8[] generate_request(I3_COMMAND cmd) {
ByteArray np = new ByteArray();
public class Client {
private Socket socket;
private uint8[] magic_number = "i3-ipc".data;
private uint8[] terminator = { '\0' };
private int bytes_to_payload = 14;
private int buffer_size = 1024 * 128;

np.append(magic_number);
np.append(int32_to_uint8_array(0)); // payloadSize.get_data_as_bytes().get_data());
np.append(int32_to_uint8_array(cmd)); // command.get_data_as_bytes().get_data());

Bytes message = ByteArray.free_to_bytes(np);
public Client (string i3Socket) throws GLib.Error {
var socketAddress = new UnixSocketAddress (i3Socket);

return message.get_data();
}
socket = new Socket (SocketFamily.UNIX, SocketType.STREAM, SocketProtocol.DEFAULT);
assert (socket != null);

private Json.Node? i3_ipc(I3_COMMAND command) throws GLib.Error {
ssize_t sent = socket.send(generate_request(command));
socket.connect (socketAddress);
socket.set_blocking (true);
}

debug("Sent " + sent.to_string() + " bytes to i3.\n");
uint8[] buffer = new uint8[buffer_size];
~Client () {
if (socket != null) {
socket.close ();
}
}

ssize_t len = socket.receive (buffer);
private uint8[] int32_to_uint8_array (int32 input) {
Variant val = new Variant.int32 (input);
return val.get_data_as_bytes ().get_data ();
}

debug("Received " + len.to_string() + " bytes from i3.\n");
private string terminate_string (uint8[] rawString) {
ByteArray b = new ByteArray ();
b.append (rawString);
b.append (terminator);

Bytes responseBytes = new Bytes.take(buffer[0:len]);
return (string) b.data;
}

string payload = terminate_string(responseBytes.slice(bytes_to_payload, responseBytes.length).get_data());
private uint8[] generate_request (I3_COMMAND cmd) {
ByteArray np = new ByteArray ();

Json.Parser parser = new Json.Parser();
parser.load_from_data(payload);
np.append (magic_number);
np.append (int32_to_uint8_array (0)); // payloadSize.get_data_as_bytes().get_data());
np.append (int32_to_uint8_array (cmd)); // command.get_data_as_bytes().get_data());

return parser.get_root();
}
Bytes message = ByteArray.free_to_bytes (np);

public VersionReply getVersion() throws I3_ERROR, GLib.Error {
var response = i3_ipc(I3_COMMAND.GET_VERSION);
return message.get_data ();
}

if (response == null) {
throw new I3_ERROR.RPC_ERROR("No Response");
}
private Json.Node ? i3_ipc (I3_COMMAND command) throws GLib.Error {
ssize_t sent = socket.send (generate_request (command));

return new VersionReply(response);
}
debug ("Sent " + sent.to_string () + " bytes to i3.\n");
uint8[] buffer = new uint8[buffer_size];

ssize_t len = socket.receive (buffer);

debug ("Received " + len.to_string () + " bytes from i3.\n");

Bytes responseBytes = new Bytes.take (buffer[0 : len]);

string payload = terminate_string (responseBytes.slice (bytes_to_payload, responseBytes.length).get_data ());

Json.Parser parser = new Json.Parser ();
parser.load_from_data (payload);

return parser.get_root ();
}

public VersionReply getVersion () throws I3_ERROR, GLib.Error {
var response = i3_ipc (I3_COMMAND.GET_VERSION);

if (response == null) {
throw new I3_ERROR.RPC_ERROR ("No Response");
}

return new VersionReply (response);
}

public ConfigReply getConfig() throws I3_ERROR, GLib.Error {
var response = i3_ipc(I3_COMMAND.GET_CONFIG);
public ConfigReply getConfig () throws I3_ERROR, GLib.Error {
var response = i3_ipc (I3_COMMAND.GET_CONFIG);

if (response == null) {
throw new I3_ERROR.RPC_ERROR("No Response");
}
if (response == null) {
throw new I3_ERROR.RPC_ERROR ("No Response");
}

return new ConfigReply(response);
return new ConfigReply (response);
}
}
}
}
8 changes: 4 additions & 4 deletions src/helper.vala
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ class Helper {
/**
* Return the screen dimentions in pixels that containes the passed-in window.
*/
public static Gdk.Rectangle getScreenSizeForWindow(Gtk.Window window) {
var display = Gdk.Display.get_default();
var monitor = display.get_monitor_at_window(window.get_window());
public static Gdk.Rectangle getScreenSizeForWindow (Gtk.Window window) {
var display = Gdk.Display.get_default ();
var monitor = display.get_monitor_at_window (window.get_window ());

return monitor.get_geometry();
return monitor.get_geometry ();
}
}
Loading

0 comments on commit f72a575

Please sign in to comment.