diff options
author | kotontrion <[email protected]> | 2024-05-23 16:03:04 +0200 |
---|---|---|
committer | kotontrion <[email protected]> | 2024-05-23 16:03:04 +0200 |
commit | f54981f20f6418848fa86fbd6d56642906ef0f17 (patch) | |
tree | aec13c058e33d2cc12c4cb4b8adb65279ab42300 | |
parent | fc09b269240ed2716c7949b1f73133a9c01ae107 (diff) |
code formatting
-rw-r--r-- | src/meson.build | 2 | ||||
-rw-r--r-- | src/tray.vala | 107 | ||||
-rw-r--r-- | src/trayItem.vala | 228 | ||||
-rw-r--r-- | src/watcher.vala | 95 |
4 files changed, 215 insertions, 217 deletions
diff --git a/src/meson.build b/src/meson.build index 56042aa..f89f3fc 100644 --- a/src/meson.build +++ b/src/meson.build @@ -42,7 +42,7 @@ sources = files( libtray = library( meson.project_name(), sources, - dependencies: deps + dependencies: deps, vala_header: meson.project_name() + '.h', vala_vapi: meson.project_name() + '.vapi', vala_gir: tray_gir, diff --git a/src/tray.vala b/src/tray.vala index 942d230..d1dfdd2 100644 --- a/src/tray.vala +++ b/src/tray.vala @@ -1,29 +1,29 @@ namespace AstalTray { -[DBus (name="org.kde.StatusNotifierWatcher")] -internal interface IWatcher : Object { + [DBus (name="org.kde.StatusNotifierWatcher")] + internal interface IWatcher : Object { - public abstract string[] RegisteredStatusNotifierItems { owned get; } - public abstract int ProtocolVersion { owned get; } + 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 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 class Tray : Object { + public class Tray : Object { private StatusNotifierWatcher watcher; private IWatcher proxy; private HashTable<string, TrayItem> _items; public List<weak TrayItem> items { owned get { return _items.get_values(); }} - + public signal void item_added(string service); public signal void item_removed(string service); @@ -31,54 +31,52 @@ public class Tray : Object { _items = new HashTable<string, TrayItem>(GLib.str_hash, GLib.str_equal); try { - Bus.own_name( - BusType.SESSION, - "org.kde.StatusNotifierWatcher", - BusNameOwnerFlags.NONE, - start_watcher, - () => { - if (proxy != null) { - //foreach (string item in proxy.RegisteredStatusNotifierItems) { - // on_item_unregister(item); - //} - proxy = null; - } - }, - start_host - ); - + Bus.own_name( + BusType.SESSION, + "org.kde.StatusNotifierWatcher", + BusNameOwnerFlags.NONE, + start_watcher, + () => { + if (proxy != null) { + proxy = null; + } + }, + start_host); + } catch (Error err) { - critical("%s", err.message); + critical("%s", err.message); } } private void start_watcher(DBusConnection conn) { - watcher = new StatusNotifierWatcher(); - conn.register_object("/StatusNotifierWatcher", watcher); - watcher.StatusNotifierItemRegistered.connect(on_item_register); - watcher.StatusNotifierItemUnregistered.connect(on_item_unregister); + 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; - - 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( - () => { + + 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); @@ -88,12 +86,13 @@ public class Tray : Object { on_item_register(item); } } - } - ); - + }); - foreach (string item in proxy.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); } } @@ -116,7 +115,7 @@ public class Tray : Object { return _items.get(service); } -} + } } diff --git a/src/trayItem.vala b/src/trayItem.vala index 4261e52..11840d5 100644 --- a/src/trayItem.vala +++ b/src/trayItem.vala @@ -7,7 +7,7 @@ namespace AstalTray { int height; uint8[] bytes; } - + public struct Tooltip { string icon_name; Pixmap[] icon; @@ -16,64 +16,64 @@ namespace AstalTray { } [DBus (use_string_marshalling = true)] - public enum Category { - [DBus (value = "ApplicationStatus")] - APPLICATION, - [DBus (value = "Communications")] - COMMUNICATIONS, - [DBus (value = "SystemServices")] - SYSTEM, - [DBus (value = "Hardware")] - HARDWARE - } + public enum Category { + [DBus (value = "ApplicationStatus")] + APPLICATION, + [DBus (value = "Communications")] + COMMUNICATIONS, + [DBus (value = "SystemServices")] + SYSTEM, + [DBus (value = "Hardware")] + HARDWARE + } [DBus (use_string_marshalling = true)] - public enum Status { - [DBus (value = "Passive")] - PASSIVE, - [DBus (value = "Active")] - ACTIVE, - [DBus (value = "NeedsAttention")] - NEEDS_ATTENTION - } + public enum Status { + [DBus (value = "Passive")] + PASSIVE, + [DBus (value = "Active")] + ACTIVE, + [DBus (value = "NeedsAttention")] + NEEDS_ATTENTION + } -[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); + [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 { + 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; } } @@ -84,7 +84,7 @@ public class TrayItem : Object { string tt = proxy.ToolTip.title; if (proxy.ToolTip.description != "") - tt += "\n" + proxy.ToolTip.description; + tt += "\n" + proxy.ToolTip.description; return tt; } @@ -108,41 +108,42 @@ public class TrayItem : Object { public signal void ready(); public TrayItem(string service, string path) { - + connection_ids = new List<ulong>(); setup_proxy(service, path); - + } private async void setup_proxy(string service, string path) { - - proxy = yield Bus.get_proxy( - BusType.SESSION, - service, - path - ); - - if(proxy.Menu != null) { - menu = new DbusmenuGtk.Menu( - proxy.get_name_owner(), - proxy.Menu); - } - 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())); + try { + proxy = yield Bus.get_proxy( + BusType.SESSION, + service, + path); + + if(proxy.Menu != null) { + menu = new DbusmenuGtk.Menu( + proxy.get_name_owner(), + proxy.Menu); + } + + 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( - () => { + proxy.notify["g-name-owner"].connect(() => { if (proxy.g_name_owner == null) { foreach (var id in connection_ids) SignalHandler.disconnect(proxy, id); - } - } - ); + } + }); - ready(); + ready(); + } catch (Error err) { + critical("%s", err.message); + } } @@ -151,7 +152,7 @@ public class TrayItem : Object { "category", "id", "title", "status", "is-menu", "tooltip-markup" }; - + foreach (string prop in props) notify_property(prop); @@ -160,49 +161,49 @@ public class TrayItem : Object { 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); + 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); + } + _notify(); + + } catch(Error e) { + //silently ignore } - _notify(); - - } catch(Error e) { - //silently ignore - } - }); + }); } - + 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); @@ -214,14 +215,14 @@ public class TrayItem : Object { 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); + icon_name, size, Gtk.IconLookupFlags.FORCE_SIZE); if (icon_info != null) return icon_info.load_icon(); @@ -232,7 +233,7 @@ public class TrayItem : Object { 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]; @@ -240,7 +241,7 @@ public class TrayItem : Object { 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, @@ -250,9 +251,8 @@ public class TrayItem : Object { (int)pixmap.height, (int)(pixmap.width * 4) ); - } -} + } } diff --git a/src/watcher.vala b/src/watcher.vala index a7ba3d5..cf06c7d 100644 --- a/src/watcher.vala +++ b/src/watcher.vala @@ -1,68 +1,67 @@ namespace AstalTray { -[DBus (name="org.kde.StatusNotifierWatcher")] -internal class StatusNotifierWatcher : Object { + [DBus (name="org.kde.StatusNotifierWatcher")] + internal class StatusNotifierWatcher : Object { - private HashTable<string, string> _items; + private HashTable<string, string> _items; - 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 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(); - - construct { - _items = new HashTable<string, string>(GLib.str_hash, GLib.str_equal); - } + 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"; + construct { + _items = new HashTable<string, string>(GLib.str_hash, GLib.str_equal); } - 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); - } + 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); + _items.set(busName, busName+path); - StatusNotifierItemRegistered(busName+path); - } + StatusNotifierItemRegistered(busName+path); + } - public void RegisterStatusNotifierHost(string service) throws DBusError, IOError { - /* + 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. - */ + */ + } } - -} } |