From 17cbfa9303daa96550ff9b510c548df42dff77a5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Guido=20G=C3=BCnther?= <agx@sigxcpu.org>
Date: Tue, 17 Jul 2018 15:10:57 +0200
Subject: [PATCH] PhoshShell: Use *_get_default pattern for shell singleton too

---
 src/background.c  |  4 ++--
 src/favorites.c   |  2 +-
 src/phosh.c       | 58 +++++++++++++++++++++--------------------------
 src/phosh.h       |  4 ++--
 src/settings.c    |  6 ++---
 tests/phoshstub.c |  2 +-
 6 files changed, 35 insertions(+), 41 deletions(-)

diff --git a/src/background.c b/src/background.c
index fa52bcc..f2c783e 100644
--- a/src/background.c
+++ b/src/background.c
@@ -130,7 +130,7 @@ background_draw_cb (PhoshBackground *self,
   PhoshBackgroundPrivate *priv = phosh_background_get_instance_private (self);
   gint x, y, width, height;
 
-  phosh_shell_get_usable_area (phosh(), &x, &y, &width, &height);
+  phosh_shell_get_usable_area (phosh_shell_get_default (), &x, &y, &width, &height);
   gdk_cairo_set_source_pixbuf (cr, priv->pixbuf, x, y);
   cairo_paint (cr);
   return TRUE;
@@ -179,7 +179,7 @@ phosh_background_constructed (GObject *object)
   gtk_window_set_decorated (GTK_WINDOW (self), FALSE);
   gtk_widget_realize (GTK_WIDGET (self));
 
-  g_signal_connect_swapped (phosh(),
+  g_signal_connect_swapped (phosh_shell_get_default (),
                             "notify::rotation",
                             G_CALLBACK (rotation_notify_cb),
                             self);
diff --git a/src/favorites.c b/src/favorites.c
index 70ecf2d..027b5a3 100644
--- a/src/favorites.c
+++ b/src/favorites.c
@@ -184,7 +184,7 @@ phosh_favorites_constructed (GObject *object)
   G_OBJECT_CLASS (phosh_favorites_parent_class)->constructed (object);
 
   /* window properties */
-  phosh_shell_get_usable_area (phosh(), NULL, NULL, &width, &height);
+  phosh_shell_get_usable_area (phosh_shell_get_default (), NULL, NULL, &width, &height);
   gtk_window_set_title (GTK_WINDOW (self), "phosh favorites");
   gtk_window_set_decorated (GTK_WINDOW (self), FALSE);
   gtk_window_resize (GTK_WINDOW (self), width, height);
diff --git a/src/phosh.c b/src/phosh.c
index 535e339..4c4c0ae 100644
--- a/src/phosh.c
+++ b/src/phosh.c
@@ -91,9 +91,6 @@ typedef struct _PhoshShell
 
 G_DEFINE_TYPE_WITH_PRIVATE (PhoshShell, phosh_shell, G_TYPE_OBJECT)
 
-/* Shell singleton */
-static PhoshShell *_phosh;
-
 
 static struct popup**
 get_popup_from_xdg_popup (PhoshShell *self, struct xdg_popup *xdg_popup)
@@ -383,7 +380,7 @@ lockscreen_create (PhoshShell *self)
   PhoshMonitor *monitor;
   PhoshWayland *wl = phosh_wayland_get_default ();
 
-  monitor = phosh_shell_get_primary_monitor ();
+  monitor = phosh_shell_get_primary_monitor (self);
   g_return_if_fail (monitor);
 
   lockscreen = g_malloc0 (sizeof *lockscreen);
@@ -508,22 +505,6 @@ lockscreen_prepare (PhoshShell *self)
 }
 
 
-PhoshMonitor *
-get_primary_monitor (PhoshShell *self)
-{
-  PhoshShellPrivate *priv;
-  PhoshMonitor *monitor;
-
-  g_return_val_if_fail (PHOSH_IS_SHELL (self), NULL);
-  priv = phosh_shell_get_instance_private (self);
-
-  monitor = phosh_monitor_manager_get_monitor (priv->monitor_manager, 0);
-  g_return_val_if_fail (monitor, NULL);
-
-  return monitor;
-}
-
-
 static void
 panel_create (PhoshShell *self)
 {
@@ -534,7 +515,7 @@ panel_create (PhoshShell *self)
   PhoshMonitor *monitor;
   PhoshWayland *wl = phosh_wayland_get_default ();
 
-  monitor = get_primary_monitor (self);
+  monitor = phosh_shell_get_primary_monitor (self);
   g_return_if_fail (monitor);
 
   panel = calloc (1, sizeof *panel);
@@ -585,7 +566,7 @@ background_create (PhoshShell *self)
   PhoshMonitor *monitor;
   gint width, height;
 
-  monitor = phosh_shell_get_primary_monitor ();
+  monitor = phosh_shell_get_primary_monitor (self);
   g_return_if_fail (monitor);
   phosh_shell_get_usable_area (self, NULL, NULL, &width, &height);
 
@@ -779,10 +760,18 @@ phosh_shell_rotate_display (PhoshShell *self,
 
 
 PhoshMonitor *
-phosh_shell_get_primary_monitor ()
+phosh_shell_get_primary_monitor (PhoshShell *self)
 {
-  g_return_val_if_fail (PHOSH_IS_SHELL (_phosh), NULL);
-  return get_primary_monitor (_phosh);
+  PhoshShellPrivate *priv;
+  PhoshMonitor *monitor;
+
+  g_return_val_if_fail (PHOSH_IS_SHELL (self), NULL);
+  priv = phosh_shell_get_instance_private (self);
+
+  monitor = phosh_monitor_manager_get_monitor (priv->monitor_manager, 0);
+  g_return_val_if_fail (monitor, NULL);
+
+  return monitor;
 }
 
 
@@ -797,7 +786,7 @@ phosh_shell_get_usable_area (PhoshShell *self, gint *x, gint *y, gint *width, gi
   gint panel_height = 0;
   gint w, h;
 
-  monitor = get_primary_monitor (self);
+  monitor = phosh_shell_get_primary_monitor (self);
   g_return_if_fail(monitor);
 
   w = monitor->width;
@@ -815,9 +804,15 @@ phosh_shell_get_usable_area (PhoshShell *self, gint *x, gint *y, gint *width, gi
 
 
 PhoshShell *
-phosh ()
+phosh_shell_get_default ()
 {
-  return _phosh;
+  static PhoshShell *instance;
+
+  if (instance == NULL) {
+    instance = g_object_new (PHOSH_TYPE_SHELL, NULL);
+    g_object_add_weak_pointer (G_OBJECT (instance), (gpointer *)&instance);
+  }
+  return instance;
 }
 
 
@@ -838,6 +833,7 @@ int main(int argc, char *argv[])
   GError *err = NULL;
   gboolean unlocked = FALSE;
   g_autoptr(PhoshWayland) wl;
+  g_autoptr(PhoshShell) shell;
 
   const GOptionEntry options [] = {
     {"unlocked", 'U', 0, G_OPTION_ARG_NONE, &unlocked,
@@ -861,13 +857,11 @@ int main(int argc, char *argv[])
   g_source_attach (sigterm, context);
 
   wl = phosh_wayland_get_default ();
-  _phosh = g_object_new (PHOSH_TYPE_SHELL, NULL);
+  shell = phosh_shell_get_default ();
   if (!unlocked)
-    phosh_shell_lock (_phosh);
+    phosh_shell_lock (shell);
 
   gtk_main ();
-  g_object_unref (_phosh);
-  _phosh = NULL;
 
   return EXIT_SUCCESS;
 }
diff --git a/src/phosh.h b/src/phosh.h
index 3144146..52acf42 100644
--- a/src/phosh.h
+++ b/src/phosh.h
@@ -16,7 +16,7 @@
 
 G_DECLARE_FINAL_TYPE (PhoshShell, phosh_shell, PHOSH, SHELL, GObject)
 
-PhoshShell          *phosh                       (void);
+PhoshShell          *phosh_shell_get_default     (void);
 void                 phosh_shell_rotate_display  (PhoshShell *self, guint degrees);
 int                  phosh_shell_get_rotation    (PhoshShell *self);
 void                 phosh_shell_get_usable_area (PhoshShell *self,
@@ -27,5 +27,5 @@ void                 phosh_shell_get_usable_area (PhoshShell *self,
 void                 phosh_shell_set_locked      (PhoshShell *self, gboolean locked);
 void                 phosh_shell_lock            (PhoshShell *self);
 void                 phosh_shell_unlock          (PhoshShell *self);
-PhoshMonitor        *phosh_shell_get_primary_monitor ();
+PhoshMonitor        *phosh_shell_get_primary_monitor (PhoshShell *self);
 #endif /* PHOSH_H */
diff --git a/src/settings.c b/src/settings.c
index a55a916..568c135 100644
--- a/src/settings.c
+++ b/src/settings.c
@@ -66,7 +66,7 @@ brightness_changed_cb (GtkAdjustment *adj_brightness, gpointer *unused)
 static void
 rotation_changed_cb (GtkSwitch *btn, GParamSpec *pspec, PhoshSettings *self)
 {
-  PhoshShell *shell = phosh();
+  PhoshShell *shell = phosh_shell_get_default ();
   gboolean rotate;
 
   rotate = gtk_switch_get_active(btn);
@@ -95,7 +95,7 @@ settings_clicked_cb (PhoshSettings *self, gpointer *unused)
 static void
 lock_screen_clicked_cb (PhoshSettings *self, gpointer *unused)
 {
-  phosh_shell_lock (phosh());
+  phosh_shell_lock (phosh_shell_get_default ());
   g_signal_emit (self, signals[SETTING_DONE], 0);
 }
 
@@ -126,7 +126,7 @@ phosh_settings_constructed (GObject *object)
   priv->adj_volume = gtk_adjustment_new (0, 0, 100, 1, 10, 10);
   gtk_range_set_adjustment (GTK_RANGE (priv->scale_volume), priv->adj_volume);
 
-  if (phosh_shell_get_rotation (phosh()))
+  if (phosh_shell_get_rotation (phosh_shell_get_default ()))
     gtk_switch_set_active (GTK_SWITCH (priv->btn_rotation), TRUE);
   g_signal_connect (priv->btn_rotation,
                     "notify::active",
diff --git a/tests/phoshstub.c b/tests/phoshstub.c
index ff88647..02191f0 100644
--- a/tests/phoshstub.c
+++ b/tests/phoshstub.c
@@ -3,7 +3,7 @@
 #include <glib.h>
 
 gpointer
-phosh ()
+phosh_shell_get_default ()
 {
   return NULL;
 }
-- 
GitLab