diff options
author | Aylur <[email protected]> | 2024-09-01 14:17:36 +0200 |
---|---|---|
committer | Aylur <[email protected]> | 2024-09-01 14:17:36 +0200 |
commit | 3e3f045d650a839d21f7b649da7aa5c19bd2e38b (patch) | |
tree | 9a974eb0d38932d474940288c662bd1f01ea3088 /lib/tray | |
parent | 408faee16911ccfaa3e7dad69f9938fd4a696704 (diff) |
monorepo structuring
Diffstat (limited to 'lib/tray')
-rw-r--r-- | lib/tray/cli.vala | 54 | ||||
-rw-r--r-- | lib/tray/config.vala.in | 6 | ||||
-rw-r--r-- | lib/tray/meson.build | 118 | ||||
-rw-r--r-- | lib/tray/meson_options.txt | 11 | ||||
-rw-r--r-- | lib/tray/tray.vala | 135 | ||||
-rw-r--r-- | lib/tray/trayItem.vala | 363 | ||||
-rw-r--r-- | lib/tray/version | 1 | ||||
-rw-r--r-- | lib/tray/watcher.vala | 59 |
8 files changed, 747 insertions, 0 deletions
diff --git a/lib/tray/cli.vala b/lib/tray/cli.vala new file mode 100644 index 0000000..3147fb5 --- /dev/null +++ b/lib/tray/cli.vala @@ -0,0 +1,54 @@ +static bool version; +static bool daemonize; + +const OptionEntry[] options = { + { "version", 'v', OptionFlags.NONE, OptionArg.NONE, ref version, "Print version number", null }, + { "daemonize", 'd', OptionFlags.NONE, OptionArg.NONE, ref daemonize, "Monitor the systemtray", null }, + { null }, +}; + +int main(string[] argv) { + try { + var opts = new OptionContext(); + opts.add_main_entries(options, null); + opts.set_help_enabled(true); + opts.set_ignore_unknown_options(false); + opts.parse(ref argv); + } catch (OptionError err) { + printerr (err.message); + return 1; + } + + if (version) { + print(AstalTray.VERSION); + return 0; + } + + if (daemonize) { + var loop = new MainLoop(); + var tray = new AstalTray.Tray(); + + tray.item_added.connect((id) => { + AstalTray.TrayItem item = tray.get_item(id); + + stdout.printf("{\"event\":\"item_added\",\"id\":\"%s\",\"item\":%s}\n", + id, item.to_json_string()); + stdout.flush(); + + item.changed.connect(() => { + stdout.printf("{\"event\":\"item_changed\",\"id\":\"%s\",\"item\":%s}\n", + id, item.to_json_string()); + stdout.flush(); + }); + }); + + tray.item_removed.connect((id) => { + stdout.printf("{\"event\":\"item_removed\",\"id\":\"%s\"}\n", id); + stdout.flush(); + }); + + loop.run(); + } + + return 0; +} diff --git a/lib/tray/config.vala.in b/lib/tray/config.vala.in new file mode 100644 index 0000000..8ef8498 --- /dev/null +++ b/lib/tray/config.vala.in @@ -0,0 +1,6 @@ +namespace AstalTray { + public const int MAJOR_VERSION = @MAJOR_VERSION@; + public const int MINOR_VERSION = @MINOR_VERSION@; + public const int MICRO_VERSION = @MICRO_VERSION@; + public const string VERSION = "@VERSION@"; +} diff --git a/lib/tray/meson.build b/lib/tray/meson.build new file mode 100644 index 0000000..421f33d --- /dev/null +++ b/lib/tray/meson.build @@ -0,0 +1,118 @@ +project( + 'astal-tray', + 'vala', + 'c', + version: run_command('cat', join_paths(meson.project_source_root(), 'version')).stdout().strip(), + meson_version: '>= 0.62.0', + default_options: [ + 'warning_level=2', + 'werror=false', + 'c_std=gnu11', + ], +) + +assert( + get_option('lib') or get_option('cli'), + 'Either lib or cli option must be set to true.', +) + +version_split = meson.project_version().split('.') +api_version = version_split[0] + '.' + version_split[1] +gir = 'AstalTray-' + api_version + '.gir' +typelib = 'AstalTray-' + api_version + '.typelib' + +config = configure_file( + input: 'config.vala.in', + output: 'config.vala', + configuration: { + 'VERSION': meson.project_version(), + 'MAJOR_VERSION': version_split[0], + 'MINOR_VERSION': version_split[1], + 'MICRO_VERSION': version_split[2], + }, +) + +deps = [ + dependency('glib-2.0'), + dependency('gobject-2.0'), + dependency('gio-2.0'), + dependency('json-glib-1.0'), + dependency('gdk-pixbuf-2.0'), + dependency('gtk+-3.0'), +] + +dbusmenu_cflags = run_command( + find_program('pkg-config', required: true), + '--cflags', 'dbusmenu-gtk3-0.4', + 'gobject-introspection-1.0', + 'gobject-2.0', + 'glib-2.0', + capture: true, + check: true, +).stdout().strip() + +dbusmenu_libs = run_command( + find_program('pkg-config', required: true), + '--libs', 'dbusmenu-gtk3-0.4', + 'gobject-introspection-1.0', + 'gobject-2.0', + 'glib-2.0', + capture: true, + check: true, +).stdout().strip() + +sources = [config, 'tray.vala', 'watcher.vala', 'trayItem.vala'] + +if get_option('lib') + lib = library( + meson.project_name(), + sources, + dependencies: deps, + vala_header: meson.project_name() + '.h', + vala_vapi: meson.project_name() + '-' + api_version + '.vapi', + vala_gir: gir, + vala_args: ['--pkg', 'DbusmenuGtk3-0.4', '--pkg', 'Dbusmenu-0.4'], + version: meson.project_version(), + c_args: dbusmenu_cflags.split(' '), + link_args: dbusmenu_libs.split(' '), + install: true, + install_dir: [true, true, true, true], + ) + + import('pkgconfig').generate( + lib, + name: meson.project_name(), + filebase: meson.project_name() + '-' + api_version, + version: meson.project_version(), + subdirs: meson.project_name(), + requires: deps, + install_dir: get_option('libdir') / 'pkgconfig', + ) + + custom_target( + typelib, + command: [ + find_program('g-ir-compiler'), + '--output', '@OUTPUT@', + '--shared-library', get_option('prefix') / get_option('libdir') / '@PLAINNAME@', + meson.current_build_dir() / gir, + ], + input: lib, + output: typelib, + depends: lib, + install: true, + install_dir: get_option('libdir') / 'girepository-1.0', + ) +endif + +if get_option('cli') + executable( + meson.project_name(), + ['cli.vala', sources], + dependencies: deps, + vala_args: ['--pkg', 'DbusmenuGtk3-0.4', '--pkg', 'Dbusmenu-0.4'], + c_args: dbusmenu_cflags.split(' '), + link_args: dbusmenu_libs.split(' '), + install: true, + ) +endif diff --git a/lib/tray/meson_options.txt b/lib/tray/meson_options.txt new file mode 100644 index 0000000..f110242 --- /dev/null +++ b/lib/tray/meson_options.txt @@ -0,0 +1,11 @@ +option( + 'lib', + type: 'boolean', + value: true, +) + +option( + 'cli', + type: 'boolean', + value: true, +) diff --git a/lib/tray/tray.vala b/lib/tray/tray.vala new file mode 100644 index 0000000..09b0643 --- /dev/null +++ b/lib/tray/tray.vala @@ -0,0 +1,135 @@ +namespace AstalTray { +[DBus (name="org.kde.StatusNotifierWatcher")] +internal interface IWatcher : Object { + public abstract string[] RegisteredStatusNotifierItems { owned get; } + public abstract int ProtocolVersion { owned get; } + + public abstract void RegisterStatusNotifierItem(string service, BusName sender) throws DBusError, IOError; + public abstract void RegisterStatusNotifierHost(string service) throws DBusError, IOError; + + public signal void StatusNotifierItemRegistered(string service); + public signal void StatusNotifierItemUnregistered(string service); + public signal void StatusNotifierHostRegistered(); + public signal void StatusNotifierHostUnregistered(); +} + +public Tray get_default() { + return Tray.get_default(); +} + +public class Tray : Object { + private static Tray? instance; + public static unowned Tray get_default() { + if (instance == null) + instance = new Tray(); + + return instance; + } + + private StatusNotifierWatcher watcher; + private IWatcher proxy; + + private HashTable<string, TrayItem> _items = + new HashTable<string, TrayItem>(str_hash, str_equal); + + public List<weak TrayItem> items { owned get { return _items.get_values(); }} + + public signal void item_added(string service) { + notify_property("items"); + } + + public signal void item_removed(string service) { + notify_property("items"); + } + + construct { + try { + Bus.own_name( + BusType.SESSION, + "org.kde.StatusNotifierWatcher", + BusNameOwnerFlags.NONE, + start_watcher, + () => { + if (proxy != null) { + proxy = null; + } + }, + start_host + ); + } catch (Error err) { + critical(err.message); + } + + } + + private void start_watcher(DBusConnection conn) { + try { + watcher = new StatusNotifierWatcher(); + conn.register_object("/StatusNotifierWatcher", watcher); + watcher.StatusNotifierItemRegistered.connect(on_item_register); + watcher.StatusNotifierItemUnregistered.connect(on_item_unregister); + } catch (Error err) { + critical(err.message); + } + } + + private void start_host() { + if (proxy != null) + return; + + try { + proxy = Bus.get_proxy_sync(BusType.SESSION, + "org.kde.StatusNotifierWatcher", + "/StatusNotifierWatcher"); + + proxy.StatusNotifierItemRegistered.connect(on_item_register); + proxy.StatusNotifierItemUnregistered.connect(on_item_unregister); + + proxy.notify["g-name-owner"].connect(() => { + _items.foreach((service, _) => { + item_removed(service); + }); + + _items.remove_all(); + + if(proxy != null) { + foreach (string item in proxy.RegisteredStatusNotifierItems) { + on_item_register(item); + } + } else { + foreach (string item in watcher.RegisteredStatusNotifierItems) { + on_item_register(item); + } + } + }); + + foreach (string item in proxy.RegisteredStatusNotifierItems) { + on_item_register(item); + } + } catch (Error err) { + critical("cannot get proxy: %s", err.message); + } + } + + private void on_item_register(string service) { + if (_items.contains(service)) + return; + + var parts = service.split("/", 2); + TrayItem item = new TrayItem(parts[0], "/" + parts[1]); + item.ready.connect(() => { + _items.set(service, item); + item_added(service); + }); + } + + private void on_item_unregister(string service) { + _items.remove(service); + item_removed(service); + } + + public TrayItem get_item(string service) { + return _items.get(service); + } +} +} diff --git a/lib/tray/trayItem.vala b/lib/tray/trayItem.vala new file mode 100644 index 0000000..b6b9da0 --- /dev/null +++ b/lib/tray/trayItem.vala @@ -0,0 +1,363 @@ +using DbusmenuGtk; + +namespace AstalTray { +public struct Pixmap { + int width; + int height; + uint8[] bytes; +} + +public struct Tooltip { + string icon_name; + Pixmap[] icon; + string title; + string description; +} + +[DBus (use_string_marshalling = true)] +public enum Category { + [DBus (value = "ApplicationStatus"), Description (nick = "ApplicationStatus")] + APPLICATION, + + [DBus (value = "Communications"), Description (nick = "Communications")] + COMMUNICATIONS, + + [DBus (value = "SystemServices"), Description (nick = "SystemServices")] + SYSTEM, + + [DBus (value = "Hardware"), Description (nick = "Hardware")] + HARDWARE; + + public string to_nick () { + var enumc = (EnumClass)typeof (Category).class_ref(); + unowned var eval = enumc.get_value(this); + return eval.value_nick; + } +} + + +[DBus (use_string_marshalling = true)] +public enum Status { + [DBus (value = "Passive"), Description (nick = "Passive")] + PASSIVE, + + [DBus (value = "Active"), Description (nick = "Active")] + ACTIVE, + + [DBus (value = "NeedsAttention"), Description (nick = "NeedsAttention")] + NEEDS_ATTENTION; + + public string to_nick () { + var enumc = (EnumClass)typeof (Status).class_ref(); + unowned var eval = enumc.get_value(this); + return eval.value_nick; + } +} + +[DBus (name="org.kde.StatusNotifierItem")] +internal interface IItem : DBusProxy { + public abstract string Title { owned get; } + public abstract Category Category { owned get; } + public abstract Status Status { owned get; } + public abstract Tooltip? ToolTip { owned get; } + public abstract string Id { owned get; } + public abstract string? IconThemePath { owned get; } + public abstract bool ItemIsMenu { owned get; } + public abstract ObjectPath? Menu { owned get; } + public abstract string IconName { owned get; } + public abstract Pixmap[] IconPixmap { owned get; } + public abstract string AttentionIconName { owned get; } + public abstract Pixmap[] AttentionIconPixmap { owned get; } + public abstract string OverlayIconName { owned get; } + public abstract Pixmap[] OverlayIconPixmap { owned get; } + + public abstract void ContexMenu(int x, int y) throws DBusError, IOError; + public abstract void Activate(int x, int y) throws DBusError, IOError; + public abstract void SecondaryActivate(int x, int y) throws DBusError, IOError; + public abstract void Scroll(int delta, string orientation) throws DBusError, IOError; + + public signal void NewTitle(); + public signal void NewIcon(); + public signal void NewAttentionIcon(); + public signal void NewOverlayIcon(); + public signal void NewToolTip(); + public signal void NewStatus(string status); +} + +public class TrayItem : Object { + private IItem proxy; + private List<ulong> connection_ids; + + public string title { owned get { return proxy.Title; } } + public Category category { get { return proxy.Category; } } + public Status status { get { return proxy.Status; } } + public Tooltip? tooltip { owned get { return proxy.ToolTip; } } + + public string tooltip_markup { + owned get { + if (proxy.ToolTip == null) + return ""; + + var tt = proxy.ToolTip.title; + if (proxy.ToolTip.description != "") + tt += "\n" + proxy.ToolTip.description; + + return tt; + } + } + + public string id { owned get { return proxy.Id ;} } + public string icon_theme_path { owned get { return proxy.IconThemePath ;} } + public bool is_menu { get { return proxy.ItemIsMenu ;} } + + public string icon_name { + owned get { + return proxy.Status == Status.NEEDS_ATTENTION + ? proxy.AttentionIconName + : proxy.IconName; + } + } + + public Gdk.Pixbuf icon_pixbuf { owned get { return _get_icon_pixbuf(); } } + + public GLib.Icon gicon { get; private set; } + + public string item_id { get; private set; } + + public signal void changed(); + public signal void ready(); + + public TrayItem(string service, string path) { + connection_ids = new List<ulong>(); + item_id = service + path; + setup_proxy.begin(service, path, (_, res) => setup_proxy.end(res)); + } + + private async void setup_proxy(string service, string path) { + try { + proxy = yield Bus.get_proxy( + BusType.SESSION, + service, + path); + + connection_ids.append(proxy.NewStatus.connect(refresh_all_properties)); + connection_ids.append(proxy.NewToolTip.connect(refresh_all_properties)); + connection_ids.append(proxy.NewTitle.connect(refresh_all_properties)); + connection_ids.append(proxy.NewIcon.connect(refresh_all_properties)); + + proxy.notify["g-name-owner"].connect(() => { + if (proxy.g_name_owner == null) { + foreach (var id in connection_ids) + SignalHandler.disconnect(proxy, id); + } + }); + + update_gicon(); + + ready(); + } catch (Error err) { + critical(err.message); + } + } + + private void _notify() { + string[] props = { "category", "id", "title", "status", "is-menu", "tooltip-markup", "icon-name", "icon-pixbuf" }; + + foreach (string prop in props) + notify_property(prop); + + changed(); + } + + private void update_gicon() { + if(icon_name != null && icon_name != "") { + if(icon_theme_path != null && icon_theme_path != "") { + + Gtk.IconTheme icon_theme = new Gtk.IconTheme(); + string[] paths = {icon_theme_path}; + icon_theme.set_search_path(paths); + + int size = icon_theme.get_icon_sizes(icon_name)[0]; + Gtk.IconInfo icon_info = icon_theme.lookup_icon( + icon_name, size, Gtk.IconLookupFlags.FORCE_SIZE); + + if (icon_info != null) + gicon = new GLib.FileIcon(GLib.File.new_for_path(icon_info.get_filename())); + } else { + gicon = new GLib.ThemedIcon(icon_name); + } + } + else { + Pixmap[] pixmaps = proxy.Status == Status.NEEDS_ATTENTION + ? proxy.AttentionIconPixmap + : proxy.IconPixmap; + gicon = pixmap_to_pixbuf(pixmaps); + } + } + + + private void refresh_all_properties() { + proxy.g_connection.call.begin( + proxy.g_name, + proxy.g_object_path, + "org.freedesktop.DBus.Properties", + "GetAll", + new Variant("(s)", proxy.g_interface_name), + new VariantType("(a{sv})"), + DBusCallFlags.NONE, + -1, + null, + (_, result) => { + try { + Variant parameters = proxy.g_connection.call.end(result); + VariantIter prop_iter; + parameters.get("(a{sv})", out prop_iter); + + string prop_key; + Variant prop_value; + + while (prop_iter.next ("{sv}", out prop_key, out prop_value)) { + proxy.set_cached_property(prop_key, prop_value); + } + + update_gicon(); + + _notify(); + } catch(Error e) { + //silently ignore + } + } + ); + } + + public void activate(int x, int y) { + try { + proxy.Activate(x, y); + } catch (Error e) { + if(e.domain != DBusError.quark() || e.code != DBusError.UNKNOWN_METHOD) + warning(e.message); + } + } + + public void secondary_activate(int x, int y) { + try { + proxy.SecondaryActivate(x, y); + } catch (Error e) { + if(e.domain != DBusError.quark() || e.code != DBusError.UNKNOWN_METHOD) + warning(e.message); + } + } + + public void scroll(int delta, string orientation) { + try { + proxy.Scroll(delta, orientation); + } catch (Error e) { + if(e.domain != DBusError.quark() || e.code != DBusError.UNKNOWN_METHOD) + warning("%s\n", e.message); + } + } + + + public DbusmenuGtk.Menu? create_menu() { + if (proxy.Menu == null) + return null; + + return new DbusmenuGtk.Menu( + proxy.get_name_owner(), + proxy.Menu); + } + + public Gdk.Pixbuf? _get_icon_pixbuf() { + Pixmap[] pixmaps = proxy.Status == Status.NEEDS_ATTENTION + ? proxy.AttentionIconPixmap + : proxy.IconPixmap; + + + string icon_name = proxy.Status == Status.NEEDS_ATTENTION + ? proxy.AttentionIconName + : proxy.IconName; + + Gdk.Pixbuf pixbuf = null; + + if (icon_name != null && proxy.IconThemePath != null) + pixbuf = load_from_theme(icon_name, proxy.IconThemePath); + + if (pixbuf == null) + pixbuf = pixmap_to_pixbuf(pixmaps); + + return pixbuf; + } + + private Gdk.Pixbuf? load_from_theme(string icon_name, string theme_path) { + if (theme_path == "" || theme_path == null) + return null; + + if (icon_name == "" || icon_name == null) + return null; + + Gtk.IconTheme icon_theme = new Gtk.IconTheme(); + string[] paths = {theme_path}; + icon_theme.set_search_path(paths); + + int size = icon_theme.get_icon_sizes(icon_name)[0]; + Gtk.IconInfo icon_info = icon_theme.lookup_icon( + icon_name, size, Gtk.IconLookupFlags.FORCE_SIZE); + + if (icon_info != null) + return icon_info.load_icon(); + + return null; + } + + private Gdk.Pixbuf? pixmap_to_pixbuf(Pixmap[] pixmaps) { + if (pixmaps == null || pixmaps.length == 0) + return null; + + Pixmap pixmap = pixmaps[0]; + uint8[] image_data = pixmap.bytes.copy(); + + for (int i = 0; i < pixmap.width * pixmap.height * 4; i += 4) { + uint8 alpha = image_data[i]; + image_data[i] = image_data[i + 1]; + image_data[i + 1] = image_data[i + 2]; + image_data[i + 2] = image_data[i + 3]; + image_data[i + 3] = alpha; + } + + return new Gdk.Pixbuf.from_bytes( + new Bytes(image_data), + Gdk.Colorspace.RGB, + true, + 8, + (int)pixmap.width, + (int)pixmap.height, + (int)(pixmap.width * 4) + ); + } + + public string to_json_string() { + var generator = new Json.Generator(); + generator.set_root(to_json()); + return generator.to_data(null); + } + + internal Json.Node to_json() { + return new Json.Builder() + .begin_object() + .set_member_name("item_id").add_string_value(item_id) + .set_member_name("id").add_string_value(id) + .set_member_name("bus_name").add_string_value(proxy.g_name) + .set_member_name("object_path").add_string_value(proxy.g_object_path) + .set_member_name("title").add_string_value(title) + .set_member_name("status").add_string_value(status.to_nick()) + .set_member_name("category").add_string_value(category.to_nick()) + .set_member_name("tooltip").add_string_value(tooltip_markup) + .set_member_name("icon_theme_path").add_string_value(proxy.IconThemePath) + .set_member_name("icon_name").add_string_value(icon_name) + .set_member_name("menu_path").add_string_value(proxy.Menu) + .set_member_name("is_menu").add_boolean_value(is_menu) + .end_object() + .get_root(); + } +} +} diff --git a/lib/tray/version b/lib/tray/version new file mode 100644 index 0000000..6e8bf73 --- /dev/null +++ b/lib/tray/version @@ -0,0 +1 @@ +0.1.0 diff --git a/lib/tray/watcher.vala b/lib/tray/watcher.vala new file mode 100644 index 0000000..974cd02 --- /dev/null +++ b/lib/tray/watcher.vala @@ -0,0 +1,59 @@ +namespace AstalTray { +[DBus (name="org.kde.StatusNotifierWatcher")] +internal class StatusNotifierWatcher : Object { + private HashTable<string, string> _items = + new HashTable<string, string>(str_hash, str_equal); + + public string[] RegisteredStatusNotifierItems { owned get { return _items.get_values_as_ptr_array().data; } } + public bool IsStatusNotifierHostRegistered { get; default = true; } + public int ProtocolVersion { get; default = 0; } + + public signal void StatusNotifierItemRegistered(string service); + public signal void StatusNotifierItemUnregistered(string service); + public signal void StatusNotifierHostRegistered(); + public signal void StatusNotifierHostUnregistered(); + + public void RegisterStatusNotifierItem(string service, BusName sender) throws DBusError, IOError { + string busName; + string path; + if (service[0] == '/') { + path = service; + busName = sender; + } else { + busName = service; + path = "/StatusNotifierItem"; + } + + Bus.get_sync(BusType.SESSION).signal_subscribe( + null, + "org.freedesktop.DBus", + "NameOwnerChanged", + null, + null, + DBusSignalFlags.NONE, + (connection, sender_name, path, interface_name, signal_name, parameters) => { + string name = null; + string new_owner = null; + string old_owner = null; + parameters.get("(sss)", &name, &old_owner, &new_owner); + if (new_owner == "" && _items.contains(old_owner)) { + string full_path = _items.take(old_owner); + StatusNotifierItemUnregistered(full_path); + } + } + ); + + _items.set(busName, busName+path); + StatusNotifierItemRegistered(busName+path); + } + + public void RegisterStatusNotifierHost(string service) throws DBusError, IOError { + /* NOTE: + usually the watcher should keep track of registered host + but some tray applications do net register their trayitem properly + when hosts register/deregister. This is fixed by setting isHostRegistered + always to true, this also make host handling logic unneccessary. + */ + } +} +} |