Lint the daemon directory via vala-lint (#606)

This commit is contained in:
Keli Grubb 2019-09-24 13:33:52 -04:00 committed by Daniel Foré
parent 49d1d8ffab
commit 8444dd4119
2 changed files with 248 additions and 259 deletions

View File

@ -15,132 +15,132 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// //
namespace Gala namespace Gala {
{ [DBus (name = "org.gnome.SessionManager")]
[DBus (name = "org.gnome.SessionManager")] public interface SessionManager : Object {
public interface SessionManager : Object public abstract async ObjectPath register_client (
{ string app_id,
public abstract async ObjectPath register_client (string app_id, string client_start_id) throws DBusError, IOError; string client_start_id
} ) throws DBusError, IOError;
}
[DBus (name = "org.gnome.SessionManager.ClientPrivate")] [DBus (name = "org.gnome.SessionManager.ClientPrivate")]
public interface SessionClient : Object public interface SessionClient : Object {
{ public abstract void end_session_response (bool is_ok, string reason) throws DBusError, IOError;
public abstract void end_session_response (bool is_ok, string reason) throws DBusError, IOError;
public signal void stop () ; public signal void stop () ;
public signal void query_end_session (uint flags); public signal void query_end_session (uint flags);
public signal void end_session (uint flags); public signal void end_session (uint flags);
public signal void cancel_end_session (); public signal void cancel_end_session ();
} }
public class Daemon public class Daemon {
{ SessionClient? sclient = null;
SessionClient? sclient = null;
public Daemon () public Daemon () {
{ register.begin ((o, res)=> {
register.begin ((o, res)=> { bool success = register.end (res);
bool success = register.end (res); if (!success) {
if (!success) { message ("Failed to register with Session manager");
message ("Failed to register with Session manager"); }
} });
});
var menu_daemon = new MenuDaemon (); var menu_daemon = new MenuDaemon ();
menu_daemon.setup_dbus (); menu_daemon.setup_dbus ();
} }
public void run () { public void run () {
Gtk.main (); Gtk.main ();
} }
public static async SessionClient? register_with_session (string app_id) public static async SessionClient? register_with_session (string app_id) {
{ ObjectPath? path = null;
ObjectPath? path = null; string? msg = null;
string? msg = null; string? start_id = null;
string? start_id = null;
SessionManager? session = null; SessionManager? session = null;
SessionClient? session_client = null; SessionClient? session_client = null;
start_id = Environment.get_variable ("DESKTOP_AUTOSTART_ID"); start_id = Environment.get_variable ("DESKTOP_AUTOSTART_ID");
if (start_id != null) { if (start_id != null) {
Environment.unset_variable ("DESKTOP_AUTOSTART_ID"); Environment.unset_variable ("DESKTOP_AUTOSTART_ID");
} else { } else {
start_id = ""; start_id = "";
warning ("DESKTOP_AUTOSTART_ID not set, session registration may be broken (not running via session?)"); warning (
} "DESKTOP_AUTOSTART_ID not set, session registration may be broken (not running via session?)"
);
}
try { try {
session = yield Bus.get_proxy (BusType.SESSION, "org.gnome.SessionManager", "/org/gnome/SessionManager"); session = yield Bus.get_proxy (
} catch (Error e) { BusType.SESSION,
warning ("Unable to connect to session manager: %s", e.message); "org.gnome.SessionManager",
return null; "/org/gnome/SessionManager"
} );
} catch (Error e) {
warning ("Unable to connect to session manager: %s", e.message);
return null;
}
try { try {
path = yield session.register_client (app_id, start_id); path = yield session.register_client (app_id, start_id);
} catch (Error e) { } catch (Error e) {
msg = e.message; msg = e.message;
warning ("Error registering with session manager: %s", e.message); warning ("Error registering with session manager: %s", e.message);
return null; return null;
} }
try { try {
session_client = yield Bus.get_proxy (BusType.SESSION, "org.gnome.SessionManager", path); session_client = yield Bus.get_proxy (BusType.SESSION, "org.gnome.SessionManager", path);
} catch (Error e) { } catch (Error e) {
warning ("Unable to get private sessions client proxy: %s", e.message); warning ("Unable to get private sessions client proxy: %s", e.message);
return null; return null;
} }
return session_client; return session_client;
} }
async bool register () async bool register () {
{ sclient = yield register_with_session ("org.pantheon.gala.daemon");
sclient = yield register_with_session ("org.pantheon.gala.daemon");
sclient.query_end_session.connect (() => end_session (false)); sclient.query_end_session.connect (() => end_session (false));
sclient.end_session.connect (() => end_session (false)); sclient.end_session.connect (() => end_session (false));
sclient.stop.connect (() => end_session (true)); sclient.stop.connect (() => end_session (true));
return true; return true;
} }
void end_session (bool quit) void end_session (bool quit) {
{ if (quit) {
if (quit) { Gtk.main_quit ();
Gtk.main_quit (); return;
return; }
}
try { try {
sclient.end_session_response (true, ""); sclient.end_session_response (true, "");
} catch (Error e) { } catch (Error e) {
warning ("Unable to respond to session manager: %s", e.message); warning ("Unable to respond to session manager: %s", e.message);
} }
} }
} }
public static int main (string[] args) public static int main (string[] args) {
{ Gtk.init (ref args);
Gtk.init (ref args);
var ctx = new OptionContext ("Gala Daemon"); var ctx = new OptionContext ("Gala Daemon");
ctx.set_help_enabled (true); ctx.set_help_enabled (true);
ctx.add_group (Gtk.get_option_group (false)); ctx.add_group (Gtk.get_option_group (false));
try { try {
ctx.parse (ref args); ctx.parse (ref args);
} catch (Error e) { } catch (Error e) {
stderr.printf ("Error: %s\n", e.message); stderr.printf ("Error: %s\n", e.message);
return 0; return 0;
} }
var daemon = new Daemon (); var daemon = new Daemon ();
daemon.run (); daemon.run ();
return 0; return 0;
} }
} }

View File

@ -15,185 +15,174 @@
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
// //
namespace Gala namespace Gala {
{ const string DBUS_NAME = "org.pantheon.gala";
const string DBUS_NAME = "org.pantheon.gala"; const string DBUS_OBJECT_PATH = "/org/pantheon/gala";
const string DBUS_OBJECT_PATH = "/org/pantheon/gala";
const string DAEMON_DBUS_NAME = "org.pantheon.gala.daemon"; const string DAEMON_DBUS_NAME = "org.pantheon.gala.daemon";
const string DAEMON_DBUS_OBJECT_PATH = "/org/pantheon/gala/daemon"; const string DAEMON_DBUS_OBJECT_PATH = "/org/pantheon/gala/daemon";
[DBus (name = "org.pantheon.gala")] [DBus (name = "org.pantheon.gala")]
public interface WMDBus : GLib.Object public interface WMDBus : GLib.Object {
{ public abstract void perform_action (Gala.ActionType type) throws DBusError, IOError;
public abstract void perform_action (Gala.ActionType type) throws DBusError, IOError; }
}
[DBus (name = "org.pantheon.gala.daemon")] [DBus (name = "org.pantheon.gala.daemon")]
public class MenuDaemon : Object public class MenuDaemon : Object {
{ Gtk.Menu? window_menu = null;
Gtk.Menu? window_menu = null; Gtk.MenuItem minimize;
Gtk.MenuItem minimize; Gtk.MenuItem maximize;
Gtk.MenuItem maximize; Gtk.MenuItem move;
Gtk.MenuItem move; Gtk.MenuItem resize;
Gtk.MenuItem resize; Gtk.CheckMenuItem always_on_top;
Gtk.CheckMenuItem always_on_top; Gtk.CheckMenuItem on_visible_workspace;
Gtk.CheckMenuItem on_visible_workspace; Gtk.MenuItem move_left;
Gtk.MenuItem move_left; Gtk.MenuItem move_right;
Gtk.MenuItem move_right; Gtk.MenuItem close;
Gtk.MenuItem close;
WMDBus? wm_proxy = null; WMDBus? wm_proxy = null;
ulong always_on_top_sid = 0U;
ulong on_visible_workspace_sid = 0U;
[DBus (visible = false)] ulong always_on_top_sid = 0U;
public void setup_dbus () ulong on_visible_workspace_sid = 0U;
{
var flags = BusNameOwnerFlags.ALLOW_REPLACEMENT | BusNameOwnerFlags.REPLACE;
Bus.own_name (BusType.SESSION, DAEMON_DBUS_NAME, flags, on_bus_acquired, () => {}, null);
Bus.watch_name (BusType.SESSION, DBUS_NAME, BusNameWatcherFlags.NONE, gala_appeared, lost_gala); [DBus (visible = false)]
} public void setup_dbus () {
var flags = BusNameOwnerFlags.ALLOW_REPLACEMENT | BusNameOwnerFlags.REPLACE;
Bus.own_name (BusType.SESSION, DAEMON_DBUS_NAME, flags, on_bus_acquired, () => {}, null);
void on_gala_get (GLib.Object? o, GLib.AsyncResult? res) Bus.watch_name (BusType.SESSION, DBUS_NAME, BusNameWatcherFlags.NONE, gala_appeared, lost_gala);
{ }
try {
wm_proxy = Bus.get_proxy.end (res);
} catch (Error e) {
warning ("Failed to get Gala proxy: %s", e.message);
}
}
void lost_gala () void on_gala_get (GLib.Object? o, GLib.AsyncResult? res) {
{ try {
wm_proxy = null; wm_proxy = Bus.get_proxy.end (res);
} } catch (Error e) {
warning ("Failed to get Gala proxy: %s", e.message);
}
}
void gala_appeared () void lost_gala () {
{ wm_proxy = null;
if (wm_proxy == null) { }
Bus.get_proxy.begin<WMDBus> (BusType.SESSION, DBUS_NAME, DBUS_OBJECT_PATH, 0, null, on_gala_get);
}
}
void on_bus_acquired (DBusConnection conn) void gala_appeared () {
{ if (wm_proxy == null) {
try { Bus.get_proxy.begin<WMDBus> (BusType.SESSION, DBUS_NAME, DBUS_OBJECT_PATH, 0, null, on_gala_get);
conn.register_object (DAEMON_DBUS_OBJECT_PATH, this); }
} catch (Error e) { }
stderr.printf ("Error registering MenuDaemon: %s\n", e.message);
}
}
void perform_action (Gala.ActionType type) void on_bus_acquired (DBusConnection conn) {
{ try {
if (wm_proxy != null) { conn.register_object (DAEMON_DBUS_OBJECT_PATH, this);
try { } catch (Error e) {
wm_proxy.perform_action (type); stderr.printf ("Error registering MenuDaemon: %s\n", e.message);
} catch (Error e) { }
warning ("Failed to perform Gala action over DBus: %s", e.message); }
}
}
}
void init_window_menu () void perform_action (Gala.ActionType type) {
{ if (wm_proxy != null) {
window_menu = new Gtk.Menu (); try {
wm_proxy.perform_action (type);
} catch (Error e) {
warning ("Failed to perform Gala action over DBus: %s", e.message);
}
}
}
minimize = new Gtk.MenuItem.with_label (_("Minimize")); void init_window_menu () {
minimize.activate.connect (() => { window_menu = new Gtk.Menu ();
perform_action (Gala.ActionType.MINIMIZE_CURRENT);
});
window_menu.append (minimize);
maximize = new Gtk.MenuItem.with_label (""); minimize = new Gtk.MenuItem.with_label (_("Minimize"));
maximize.activate.connect (() => { minimize.activate.connect (() => {
perform_action (Gala.ActionType.MAXIMIZE_CURRENT); perform_action (Gala.ActionType.MINIMIZE_CURRENT);
}); });
window_menu.append (maximize); window_menu.append (minimize);
move = new Gtk.MenuItem.with_label (_("Move")); maximize = new Gtk.MenuItem.with_label ("");
move.activate.connect (() => { maximize.activate.connect (() => {
perform_action (Gala.ActionType.START_MOVE_CURRENT); perform_action (Gala.ActionType.MAXIMIZE_CURRENT);
}); });
window_menu.append (move); window_menu.append (maximize);
resize = new Gtk.MenuItem.with_label (_("Resize")); move = new Gtk.MenuItem.with_label (_("Move"));
resize.activate.connect (() => { move.activate.connect (() => {
perform_action (Gala.ActionType.START_RESIZE_CURRENT); perform_action (Gala.ActionType.START_MOVE_CURRENT);
}); });
window_menu.append (resize); window_menu.append (move);
always_on_top = new Gtk.CheckMenuItem.with_label (_("Always on Top")); resize = new Gtk.MenuItem.with_label (_("Resize"));
always_on_top_sid = always_on_top.activate.connect (() => { resize.activate.connect (() => {
perform_action (Gala.ActionType.TOGGLE_ALWAYS_ON_TOP_CURRENT); perform_action (Gala.ActionType.START_RESIZE_CURRENT);
}); });
window_menu.append (always_on_top); window_menu.append (resize);
on_visible_workspace = new Gtk.CheckMenuItem.with_label (_("Always on Visible Workspace")); always_on_top = new Gtk.CheckMenuItem.with_label (_("Always on Top"));
on_visible_workspace_sid = on_visible_workspace.activate.connect (() => { always_on_top_sid = always_on_top.activate.connect (() => {
perform_action (Gala.ActionType.TOGGLE_ALWAYS_ON_VISIBLE_WORKSPACE_CURRENT); perform_action (Gala.ActionType.TOGGLE_ALWAYS_ON_TOP_CURRENT);
}); });
window_menu.append (on_visible_workspace); window_menu.append (always_on_top);
move_left = new Gtk.MenuItem.with_label (_("Move to Workspace Left")); on_visible_workspace = new Gtk.CheckMenuItem.with_label (_("Always on Visible Workspace"));
move_left.activate.connect (() => { on_visible_workspace_sid = on_visible_workspace.activate.connect (() => {
perform_action (Gala.ActionType.MOVE_CURRENT_WORKSPACE_LEFT); perform_action (Gala.ActionType.TOGGLE_ALWAYS_ON_VISIBLE_WORKSPACE_CURRENT);
}); });
window_menu.append (move_left); window_menu.append (on_visible_workspace);
move_right = new Gtk.MenuItem.with_label (_("Move to Workspace Right")); move_left = new Gtk.MenuItem.with_label (_("Move to Workspace Left"));
move_right.activate.connect (() => { move_left.activate.connect (() => {
perform_action (Gala.ActionType.MOVE_CURRENT_WORKSPACE_RIGHT); perform_action (Gala.ActionType.MOVE_CURRENT_WORKSPACE_LEFT);
}); });
window_menu.append (move_right); window_menu.append (move_left);
close = new Gtk.MenuItem.with_label (_("Close")); move_right = new Gtk.MenuItem.with_label (_("Move to Workspace Right"));
close.activate.connect (() => { move_right.activate.connect (() => {
perform_action (Gala.ActionType.CLOSE_CURRENT); perform_action (Gala.ActionType.MOVE_CURRENT_WORKSPACE_RIGHT);
}); });
window_menu.append (close); window_menu.append (move_right);
window_menu.show_all (); close = new Gtk.MenuItem.with_label (_("Close"));
} close.activate.connect (() => {
perform_action (Gala.ActionType.CLOSE_CURRENT);
});
window_menu.append (close);
public void show_window_menu (Gala.WindowFlags flags, int x, int y) throws DBusError, IOError window_menu.show_all ();
{ }
if (window_menu == null) {
init_window_menu ();
}
minimize.visible = Gala.WindowFlags.CAN_MINIMIZE in flags; public void show_window_menu (Gala.WindowFlags flags, int x, int y) throws DBusError, IOError {
maximize.visible = Gala.WindowFlags.CAN_MAXIMIZE in flags; if (window_menu == null) {
maximize.label = Gala.WindowFlags.IS_MAXIMIZED in flags ? _("Unmaximize") : _("Maximize"); init_window_menu ();
move.visible = Gala.WindowFlags.ALLOWS_MOVE in flags; }
resize.visible = Gala.WindowFlags.ALLOWS_RESIZE in flags;
// Setting active causes signal fires on activate so
// we temporarily block those signals from emissions
SignalHandler.block (always_on_top, always_on_top_sid);
SignalHandler.block (on_visible_workspace, on_visible_workspace_sid);
always_on_top.active = Gala.WindowFlags.ALWAYS_ON_TOP in flags; minimize.visible = Gala.WindowFlags.CAN_MINIMIZE in flags;
on_visible_workspace.active = Gala.WindowFlags.ON_ALL_WORKSPACES in flags; maximize.visible = Gala.WindowFlags.CAN_MAXIMIZE in flags;
maximize.label = Gala.WindowFlags.IS_MAXIMIZED in flags ? _("Unmaximize") : _("Maximize");
SignalHandler.unblock (always_on_top, always_on_top_sid); move.visible = Gala.WindowFlags.ALLOWS_MOVE in flags;
SignalHandler.unblock (on_visible_workspace, on_visible_workspace_sid); resize.visible = Gala.WindowFlags.ALLOWS_RESIZE in flags;
move_right.visible = !on_visible_workspace.active; // Setting active causes signal fires on activate so
move_left.visible = !on_visible_workspace.active; // we temporarily block those signals from emissions
close.visible = Gala.WindowFlags.CAN_CLOSE in flags; SignalHandler.block (always_on_top, always_on_top_sid);
SignalHandler.block (on_visible_workspace, on_visible_workspace_sid);
window_menu.popup (null, null, (m, ref px, ref py, out push_in) => { always_on_top.active = Gala.WindowFlags.ALWAYS_ON_TOP in flags;
var scale = m.scale_factor; on_visible_workspace.active = Gala.WindowFlags.ON_ALL_WORKSPACES in flags;
px = x / scale;
// Move the menu 1 pixel outside of the pointer or else it closes instantly SignalHandler.unblock (always_on_top, always_on_top_sid);
// on the mouse up event SignalHandler.unblock (on_visible_workspace, on_visible_workspace_sid);
py = (y / scale) + 1;
push_in = true; move_right.visible = !on_visible_workspace.active;
}, 3, Gdk.CURRENT_TIME); move_left.visible = !on_visible_workspace.active;
} close.visible = Gala.WindowFlags.CAN_CLOSE in flags;
}
window_menu.popup (null, null, (m, ref px, ref py, out push_in) => {
var scale = m.scale_factor;
px = x / scale;
// Move the menu 1 pixel outside of the pointer or else it closes instantly
// on the mouse up event
py = (y / scale) + 1;
push_in = true;
}, 3, Gdk.CURRENT_TIME);
}
}
} }