Skip to content

Commit

Permalink
Merge pull request #88 from regolith-linux/chore-api-cleanup
Browse files Browse the repository at this point in the history
Chore api cleanup
  • Loading branch information
kgilmer authored Sep 7, 2024
2 parents 3d1a6a8 + c5bee48 commit a5d642c
Show file tree
Hide file tree
Showing 15 changed files with 135 additions and 121 deletions.
24 changes: 20 additions & 4 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,27 @@ namespace Ilia {
}

protected override void activate () {
var window = new Ilia.DialogWindow (this.arg_map);
// Get session type (wayland or x11) and set the flag
string session_type = Environment.get_variable ("XDG_SESSION_TYPE");
string gdk_backend = Environment.get_variable ("GDK_BACKEND");
var is_wayland_session = session_type == "wayland" && gdk_backend != "x11";

// Set window manager
string sway_sock = Environment.get_variable ("SWAYSOCK");
string i3_sock = Environment.get_variable ("I3SOCK");

string wm_name = "Unknown";
if (sway_sock != null) {
wm_name = "sway";
} else if (i3_sock != null) {
wm_name = "i3";
}

var window = new Ilia.DialogWindow (this.arg_map, is_wayland_session, wm_name);
window.set_application (this);

// Grab inputs from wayland backend before showing window
if (IS_SESSION_WAYLAND) {
if (is_wayland_session) {
bool is_layer_shell_supported = GtkLayerShell.is_supported ();
if (!is_layer_shell_supported) {
stderr.printf ("The wayland compositor does not support the layer-shell protocol, aborting.");
Expand All @@ -39,8 +55,8 @@ namespace Ilia {
window.show_all ();

// Grab inputs from X11 backend after showing window
if (!IS_SESSION_WAYLAND) {
Gdk.Window gdkwin = window.get_window ();
if (!is_wayland_session) {
Gdk.Window gdkwin = window.get_window ();
var seat = grab_inputs (gdkwin);
if (seat == null) {
stderr.printf ("Failed to aquire access to input devices, aborting.");
Expand Down
36 changes: 21 additions & 15 deletions src/DialogWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,16 @@ namespace Ilia {

private Gtk.TreeView keybinding_view;

public DialogWindow (HashTable<string, string? > arg_map) {
private string wm_name;
private bool is_wayland;

public DialogWindow (HashTable<string, string? > arg_map, bool is_wayland_session, string wm_name) {
Object(type: Gtk.WindowType.POPUP); // Window is unmanaged
window_position = WindowPosition.CENTER_ALWAYS;

this.wm_name = wm_name;
this.is_wayland = is_wayland_session;

settings = new GLib.Settings ("org.regolith-linux.ilia");

entry = new Gtk.Entry ();
Expand Down Expand Up @@ -79,7 +85,7 @@ namespace Ilia {
grid.attach (notebook, 0, 1, 1, 1);
add (grid);

if (IS_SESSION_WAYLAND) {
if (is_wayland_session) {
set_size_request (settings.get_int ("window-width"), settings.get_int ("window-height"));
} else {
set_default_size (settings.get_int ("window-width"), settings.get_int ("window-height"));
Expand Down Expand Up @@ -241,31 +247,31 @@ namespace Ilia {
switch (focus_page.down ()) {
case "apps":
dialog_pages[0] = new DesktopAppPage ();
dialog_pages[0].initialize.begin (settings, arg_map, entry, this);
dialog_pages[0].initialize.begin (settings, arg_map, entry, this, this.wm_name, this.is_wayland);
break;
case "terminal":
dialog_pages[0] = new CommandPage ();
dialog_pages[0].initialize.begin (settings, arg_map, entry, this);
dialog_pages[0].initialize.begin (settings, arg_map, entry, this, this.wm_name, this.is_wayland);
break;
case "notifications":
dialog_pages[0] = new RoficationPage ();
dialog_pages[0].initialize.begin (settings, arg_map, entry, this);
dialog_pages[0].initialize.begin (settings, arg_map, entry, this, this.wm_name, this.is_wayland);
break;
case "keybindings":
dialog_pages[0] = new KeybingingsPage ();
dialog_pages[0].initialize.begin (settings, arg_map, entry, this);
dialog_pages[0].initialize.begin (settings, arg_map, entry, this, this.wm_name, this.is_wayland);
break;
case "textlist":
dialog_pages[0] = new TextListPage ();
dialog_pages[0].initialize.begin (settings, arg_map, entry, this);
dialog_pages[0].initialize.begin (settings, arg_map, entry, this, this.wm_name, this.is_wayland);
break;
case "windows":
dialog_pages[0] = new WindowPage ();
dialog_pages[0].initialize.begin (settings, arg_map, entry, this);
dialog_pages[0].initialize.begin (settings, arg_map, entry, this, this.wm_name, this.is_wayland);
break;
case "tracker":
dialog_pages[0] = new TrackerPage ();
dialog_pages[0].initialize.begin (settings, arg_map, entry, this);
dialog_pages[0].initialize.begin (settings, arg_map, entry, this, this.wm_name, this.is_wayland);
break;
default:
stderr.printf ("Unknown page type: %s\n", focus_page);
Expand All @@ -281,17 +287,17 @@ namespace Ilia {
dialog_pages = new DialogPage[page_count];

dialog_pages[0] = new DesktopAppPage ();
dialog_pages[0].initialize.begin (settings, arg_map, entry, this);
dialog_pages[0].initialize.begin (settings, arg_map, entry, this, this.wm_name, this.is_wayland);
dialog_pages[1] = new CommandPage ();
dialog_pages[1].initialize.begin (settings, arg_map, entry, this);
dialog_pages[1].initialize.begin (settings, arg_map, entry, this, this.wm_name, this.is_wayland);
dialog_pages[2] = new RoficationPage ();
dialog_pages[2].initialize.begin (settings, arg_map, entry, this);
dialog_pages[2].initialize.begin (settings, arg_map, entry, this, this.wm_name, this.is_wayland);
dialog_pages[3] = new KeybingingsPage ();
dialog_pages[3].initialize.begin (settings, arg_map, entry, this);
dialog_pages[3].initialize.begin (settings, arg_map, entry, this, this.wm_name, this.is_wayland);
dialog_pages[4] = new WindowPage ();
dialog_pages[4].initialize.begin (settings, arg_map, entry, this);
dialog_pages[4].initialize.begin (settings, arg_map, entry, this, this.wm_name, this.is_wayland);
dialog_pages[5] = new TrackerPage ();
dialog_pages[5].initialize.begin (settings, arg_map, entry, this);
dialog_pages[5].initialize.begin (settings, arg_map, entry, this, this.wm_name, this.is_wayland);
// last page, help, will be initialized later in init

switch (focus_page.down ()) {
Expand Down
34 changes: 0 additions & 34 deletions src/Main.vala
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using Gtk;
using GtkLayerShell;

// Globals
bool IS_SESSION_WAYLAND;
string WM_NAME;
// Default style
char* default_css = """
.root_box {
Expand Down Expand Up @@ -37,41 +34,10 @@ char* default_css = """
public static int main (string[] args) {
Gtk.Application app = new Ilia.Application ();

// Get session type (wayland or x11) and set the flag
string session_type = Environment.get_variable ("XDG_SESSION_TYPE");
string gdk_backend = Environment.get_variable ("GDK_BACKEND");
IS_SESSION_WAYLAND = session_type == "wayland" && gdk_backend != "x11";

// Set window manager
string sway_sock = Environment.get_variable ("SWAYSOCK");
string i3_sock = Environment.get_variable ("I3SOCK");

if (sway_sock != null) {
WM_NAME = "sway";
} else if (i3_sock != null) {
WM_NAME = "i3";
} else {
WM_NAME = "Unknown";
}

app.run (args);
return 0;
}


/*
Get the location for swaymsg or i3-msg as per the current session type
*/
string? get_wm_cli() {
if(WM_NAME == "i3") {
return "/usr/bin/i3-msg ";
} else if (WM_NAME == "sway") {
return "/usr/bin/swaymsg ";
}
return null;
}


/* Get AppInfo object used to run a command */
public AppInfo get_runner_app_info (AppInfo app_info) throws GLib.Error {
string systemd_run_path = GLib.Environment.find_program_in_path ("systemd-run");
Expand Down
50 changes: 13 additions & 37 deletions src/DialogPage.vala → src/ModuleApi.vala
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
using Gtk;

/**
* Ilia module contracts
*/
namespace Ilia {
/**
* Represents actions and state that DialogPage types may access from the active session.
*/
public interface SessionContoller : GLib.Object {

// Exit the app
public abstract void quit ();
}

/**
* A DialogPage represents a filtered, sorted view for the global search entry upon some domain.
*/
interface DialogPage : GLib.Object {
/**
* Initialize the page. Create widgets, load model data, etc.
*/
public async abstract void initialize (GLib.Settings settings, HashTable<string, string ? > arg_map, Gtk.Entry entry, SessionContoller sessionController) throws GLib.Error;
public async abstract void initialize (GLib.Settings settings, HashTable<string, string ? > arg_map, Gtk.Entry entry, SessionContoller sessionController, string wm_name, bool is_wayland) throws GLib.Error;

/**
* Return the root widget of the page
Expand Down Expand Up @@ -60,38 +70,4 @@ namespace Ilia {
*/
public abstract void show ();
}

/**
* Implement prev/next item for emacs and vim bindings
*
* returns true if key entry was handled
*/
public static bool handle_emacs_vim_nav(Gtk.TreeView item_view, Gtk.TreePath path, Gdk.EventKey key) {
if ((key.state & Gdk.ModifierType.CONTROL_MASK) == Gdk.ModifierType.CONTROL_MASK) { //CTRL
bool is_last = selection_is_last (item_view.get_selection ());

if (key.keyval == 'p' || key.keyval == 'k') {
path.prev ();
item_view.get_selection ().select_path (path);
item_view.set_cursor (path, null, false);
} else if ((key.keyval == 'n' || key.keyval == 'j') && !is_last) {
path.next ();
item_view.get_selection ().select_path (path);
item_view.set_cursor (path, null, false);
}

return true;
}

return false;
}

public static bool selection_is_last (TreeSelection selection) {
TreeModel model;
TreeIter iter;
if (selection.get_selected(out model, out iter)) {
return !model.iter_next(ref iter);
}
return false;
}
}
8 changes: 0 additions & 8 deletions src/SessionController.vala

This file was deleted.

47 changes: 47 additions & 0 deletions src/Util.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace Ilia {
/**
* Implement prev/next item for emacs and vim bindings
*
* returns true if key entry was handled
*/
public static bool handle_emacs_vim_nav(Gtk.TreeView item_view, Gtk.TreePath path, Gdk.EventKey key) {
if ((key.state & Gdk.ModifierType.CONTROL_MASK) == Gdk.ModifierType.CONTROL_MASK) { //CTRL
bool is_last = selection_is_last (item_view.get_selection ());

if (key.keyval == 'p' || key.keyval == 'k') {
path.prev ();
item_view.get_selection ().select_path (path);
item_view.set_cursor (path, null, false);
} else if ((key.keyval == 'n' || key.keyval == 'j') && !is_last) {
path.next ();
item_view.get_selection ().select_path (path);
item_view.set_cursor (path, null, false);
}

return true;
}

return false;
}

public static bool selection_is_last (Gtk.TreeSelection selection) {
Gtk.TreeModel model;
Gtk.TreeIter iter;
if (selection.get_selected(out model, out iter)) {
return !model.iter_next(ref iter);
}
return false;
}

/*
* Get the location for swaymsg or i3-msg as per the current session type
*/
public static string? get_wm_cli(string wm_name) {
if (wm_name == "i3") {
return "/usr/bin/i3-msg ";
} else if (wm_name == "sway") {
return "/usr/bin/swaymsg ";
}
return null;
}
}
2 changes: 1 addition & 1 deletion src/apps/DesktopAppPage.vala
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ namespace Ilia {
return keybindings;
}

public async void initialize (GLib.Settings settings, HashTable<string, string ? > arg_map, Gtk.Entry entry, SessionContoller sessionController) throws GLib.Error {
public async void initialize (GLib.Settings settings, HashTable<string, string ? > arg_map, Gtk.Entry entry, SessionContoller sessionController, string wm_name, bool is_wayland) throws GLib.Error {
this.settings = settings;
this.entry = entry;
this.session_controller = sessionController;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/CommandPage.vala
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace Ilia {
return keybindings;
}

public async void initialize (GLib.Settings settings, HashTable<string, string ? > arg_map, Gtk.Entry entry, SessionContoller sessionController) throws GLib.Error {
public async void initialize (GLib.Settings settings, HashTable<string, string ? > arg_map, Gtk.Entry entry, SessionContoller sessionController, string wm_name, bool is_wayland) throws GLib.Error {
this.entry = entry;
this.session_controller = sessionController;

Expand Down
16 changes: 9 additions & 7 deletions src/keybindings/I3Ipc.vala
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ namespace Ilia {
return baseConfig + walked_configs;
}

internal ConfigReply (Json.Node responseJson) throws GLib.FileError {
if (WM_NAME == "sway") {
internal ConfigReply (Json.Node responseJson, string wm_name) throws GLib.FileError {
if (wm_name == "sway") {
config = get_sway_config (responseJson);
} else {
config = get_i3_config (responseJson);
Expand Down Expand Up @@ -261,8 +261,10 @@ namespace Ilia {
private uint8[] terminator = { '\0' };
private int bytes_to_payload = 14;
private int buffer_size = 1024 * 128;
private string wm_name;

public IPCClient () throws GLib.Error {
public IPCClient (string wm_name) throws GLib.Error {
this.wm_name = wm_name;
var socket_path = Environment.get_variable ("I3SOCK");

var socketAddress = new UnixSocketAddress (socket_path);
Expand All @@ -280,7 +282,7 @@ namespace Ilia {
socket.close ();
} catch (GLib.Error err) {
// TODO consistent error handling
stderr.printf ("Failed to close %s socket: %s\n", WM_NAME, err.message);
stderr.printf ("Failed to close %s socket: %s\n", wm_name, err.message);
}
}
}
Expand Down Expand Up @@ -313,12 +315,12 @@ namespace Ilia {
private Json.Node ? wm_ipc (WM_COMMAND command) throws GLib.Error {
ssize_t sent = socket.send (generate_request (command));

debug ("Sent " + sent.to_string () + " bytes to " + WM_NAME + ".\n");
debug ("Sent " + sent.to_string () + " bytes to " + wm_name + ".\n");
uint8[] buffer = new uint8[buffer_size];

ssize_t len = socket.receive (buffer);

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

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

Expand Down Expand Up @@ -347,7 +349,7 @@ namespace Ilia {
throw new WM_ERROR.RPC_ERROR ("No Response");
}

return new ConfigReply (response);
return new ConfigReply (response, this.wm_name);
}

public TreeReply getTree () throws WM_ERROR, GLib.Error {
Expand Down
Loading

0 comments on commit a5d642c

Please sign in to comment.