Merging upstream version 0.7.0 (Closes: #493243, #510888, #567617, #699414, #709777...
[debian/lxpanel.git] / src / plugin.c
index 6007702..1fd3e9b 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2006 LxDE Developers, see the file AUTHORS for details.
+ * Copyright (c) 2006-2014 LxDE Developers, see the file AUTHORS for details.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -20,7 +20,7 @@
 #include "config.h"
 #endif
 
-#include "plugin.h"
+#include "private.h"
 
 #include <gdk-pixbuf/gdk-pixbuf.h>
 #include <gdk-pixbuf-xlib/gdk-pixbuf-xlib.h>
 #include "bg.h"
 
 #include <glib-object.h>
+#include <glib/gi18n.h>
+#include <libfm/fm-gtk.h>
 
 //#define DEBUG
 #include "dbg.h"
 
-static GList * pcl = NULL;                     /* List of PluginClass structures */
-
-static void register_plugin_class(PluginClass * pc, gboolean dynamic);
 static void init_plugin_class_list(void);
-static PluginClass * plugin_find_class(const char * type);
-static PluginClass * plugin_load_dynamic(const char * type, const gchar * path);
 static void plugin_class_unref(PluginClass * pc);
 
+GQuark lxpanel_plugin_qinit;
+GQuark lxpanel_plugin_qconf;
+GQuark lxpanel_plugin_qdata;
+static GHashTable *_all_types = NULL;
+
 /* Dynamic parameter for static (built-in) plugins must be FALSE so we will not try to unload them */
 #define REGISTER_STATIC_PLUGIN_CLASS(pc) \
 do {\
@@ -51,74 +53,101 @@ do {\
     register_plugin_class(&pc, FALSE);\
 } while (0)
 
+/* The same for new plugins type - they will be not unloaded by FmModule */
+#define REGISTER_STATIC_MODULE(pc) do { \
+    extern LXPanelPluginInit lxpanel_static_plugin_##pc; \
+    lxpanel_register_plugin_type(#pc, &lxpanel_static_plugin_##pc); } while (0)
+
+static inline const LXPanelPluginInit *_find_plugin(const char *name)
+{
+    return g_hash_table_lookup(_all_types, name);
+}
+
+static GtkWidget *_old_plugin_config(LXPanel *panel, GtkWidget *instance)
+{
+    const LXPanelPluginInit *init = PLUGIN_CLASS(instance);
+    Plugin * plugin;
+
+    g_return_val_if_fail(init != NULL && init->new_instance == NULL, NULL);
+    plugin = lxpanel_plugin_get_data(instance);
+    if (plugin->class->config)
+        plugin->class->config(plugin, GTK_WINDOW(panel));
+    return NULL;
+}
+
+static void _old_plugin_reconfigure(LXPanel *panel, GtkWidget *instance)
+{
+    const LXPanelPluginInit *init = PLUGIN_CLASS(instance);
+    Plugin * plugin;
+
+    g_return_if_fail(init != NULL && init->new_instance == NULL);
+    plugin = lxpanel_plugin_get_data(instance);
+    if (plugin->class->panel_configuration_changed)
+        plugin->class->panel_configuration_changed(plugin);
+}
+
 /* Register a PluginClass. */
 static void register_plugin_class(PluginClass * pc, gboolean dynamic)
 {
-    pcl = g_list_append(pcl, pc);
+    LXPanelPluginInit *init = g_new0(LXPanelPluginInit, 1);
+    init->_reserved1 = pc;
+    init->name = pc->name;
+    init->description = pc->description;
+    if (pc->config)
+        init->config = _old_plugin_config;
+    if (pc->panel_configuration_changed)
+        init->reconfigure = _old_plugin_reconfigure;
+    init->one_per_system = pc->one_per_system;
+    init->expand_available = pc->expand_available;
+    init->expand_default = pc->expand_default;
     pc->dynamic = dynamic;
+    g_hash_table_insert(_all_types, g_strdup(pc->type), init);
 }
 
 /* Initialize the static plugins. */
 static void init_plugin_class_list(void)
 {
 #ifdef STATIC_SEPARATOR
-    REGISTER_STATIC_PLUGIN_CLASS(separator_plugin_class);
+    REGISTER_STATIC_MODULE(separator);
 #endif
 
-#ifdef STATIC_LAUNCHBAR
-    REGISTER_STATIC_PLUGIN_CLASS(launchbar_plugin_class);
+#ifdef STATIC_LAUNCHTASKBAR
+    REGISTER_STATIC_MODULE(launchtaskbar);
 #endif
 
 #ifdef STATIC_DCLOCK
-    REGISTER_STATIC_PLUGIN_CLASS(dclock_plugin_class);
+    REGISTER_STATIC_MODULE(dclock);
 #endif
 
 #ifdef STATIC_WINCMD
-    REGISTER_STATIC_PLUGIN_CLASS(wincmd_plugin_class);
+    REGISTER_STATIC_MODULE(wincmd);
 #endif
 
 #ifdef STATIC_DIRMENU
-    REGISTER_STATIC_PLUGIN_CLASS(dirmenu_plugin_class);
-#endif
-
-#ifdef STATIC_TASKBAR
-    REGISTER_STATIC_PLUGIN_CLASS(taskbar_plugin_class);
+    REGISTER_STATIC_MODULE(dirmenu);
 #endif
 
 #ifdef STATIC_PAGER
-    REGISTER_STATIC_PLUGIN_CLASS(pager_plugin_class);
+    REGISTER_STATIC_MODULE(pager);
 #endif
 
 #ifdef STATIC_TRAY
-    REGISTER_STATIC_PLUGIN_CLASS(tray_plugin_class);
+    REGISTER_STATIC_MODULE(tray);
 #endif
 
 #ifndef DISABLE_MENU
 #ifdef STATIC_MENU
-    REGISTER_STATIC_PLUGIN_CLASS(menu_plugin_class);
+    REGISTER_STATIC_MODULE(menu);
 #endif
 #endif
 
 #ifdef STATIC_SPACE
-    REGISTER_STATIC_PLUGIN_CLASS(space_plugin_class);
+    REGISTER_STATIC_MODULE(space);
 #endif
 }
 
-/* Look up a plugin class by name. */
-static PluginClass * plugin_find_class(const char * type)
-{
-    GList * tmp;
-    for (tmp = pcl; tmp != NULL; tmp = g_list_next(tmp))
-    {
-        PluginClass * pc = (PluginClass *) tmp->data;
-        if (g_ascii_strcasecmp(type, pc->type) == 0)
-            return pc;
-    }
-    return NULL;
-}
-
 /* Load a dynamic plugin. */
-static PluginClass * plugin_load_dynamic(const char * type, const gchar * path)
+static void plugin_load_dynamic(const char * type, const gchar * path)
 {
     PluginClass * pc = NULL;
 
@@ -139,104 +168,15 @@ static PluginClass * plugin_load_dynamic(const char * type, const gchar * path)
         || (strcmp(type, pc->type) != 0))                      /* Then and only then access other fields; check name */
         {
             g_module_close(m);
-            ERR("%s.so is not a lxpanel plugin\n", type);
-            return NULL;
+            g_warning("%s.so is not a lxpanel plugin", type);
+            return;
         }
 
         /* Register the newly loaded and valid plugin. */
         pc->gmodule = m;
         register_plugin_class(pc, TRUE);
+        pc->count = 1;
     }
-    return pc;
-}
-
-/* Create an instance of a plugin with a specified name, loading it if external. */
-Plugin * plugin_load(char * type)
-{
-    /* Initialize static plugins on first call. */
-    if (pcl == NULL)
-        init_plugin_class_list();
-
-    /* Look up the PluginClass. */
-    PluginClass * pc = plugin_find_class(type);
-
-#ifndef DISABLE_PLUGINS_LOADING
-    /* If not found and dynamic loading is available, try to locate an external plugin. */
-    if ((pc == NULL) && (g_module_supported()))
-    {
-        gchar path[PATH_MAX];
-        g_snprintf(path, sizeof(path), PACKAGE_LIB_DIR "/lxpanel/plugins/%s.so", type);
-        pc = plugin_load_dynamic(type, path);
-    }
-#endif  /* DISABLE_PLUGINS_LOADING */
-
-    /* If not found, return failure. */
-    if (pc == NULL)
-        return NULL;
-
-    /* Instantiate the plugin */
-    Plugin * plug = g_new0(Plugin, 1);
-    plug->class = pc;
-    pc->count += 1;
-    return plug;
-}
-
-/* Configure and start a plugin by calling its constructor. */
-int plugin_start(Plugin * pl, char ** fp)
-{
-    /* Call the constructor.
-     * It is responsible for parsing the parameters, and setting "pwid" to the top level widget. */
-    if ( ! pl->class->constructor(pl, fp))
-        return 0;
-
-    /* If this plugin can only be instantiated once, count the instantiation.
-     * This causes the configuration system to avoid displaying the plugin as one that can be added. */
-    if (pl->class->one_per_system)
-        pl->class->one_per_system_instantiated = TRUE;
-
-    /* If the plugin has a top level widget, add it to the panel's container. */
-    if (pl->pwid != NULL)
-    {
-        gtk_widget_set_name(pl->pwid, pl->class->type);
-        gtk_box_pack_start(GTK_BOX(pl->panel->box), pl->pwid, pl->expand, TRUE, pl->padding);
-        gtk_container_set_border_width(GTK_CONTAINER(pl->pwid), pl->border);
-        gtk_widget_show(pl->pwid);
-    }
-    return 1;
-}
-
-/* Unload a plugin if initialization fails. */
-void plugin_unload(Plugin * pl)
-{
-    plugin_class_unref(pl->class);
-    g_free(pl);
-}
-
-/* Delete a plugin. */
-void plugin_delete(Plugin * pl)
-{
-    Panel * p = pl->panel;
-    PluginClass * pc = pl->class;
-
-    /* If a plugin configuration dialog is open, close it. */
-    if (p->plugin_pref_dialog != NULL)
-    {
-        gtk_widget_destroy(p->plugin_pref_dialog);
-        p->plugin_pref_dialog = NULL;
-    }
-
-    /* Run the destructor and then destroy the top level widget.
-     * This prevents problems with the plugin destroying child widgets. */
-    pc->destructor(pl);
-    if (pl->pwid != NULL)
-        gtk_widget_destroy(pl->pwid);
-
-    /* Data structure bookkeeping. */
-    pc->one_per_system_instantiated = FALSE;
-    plugin_class_unref(pc);
-
-    /* Free the Plugin structure. */
-    g_free(pl);
 }
 
 /* Unreference a dynamic plugin. */
@@ -249,29 +189,15 @@ static void plugin_class_unref(PluginClass * pc)
     && (pc->dynamic)
     && ( ! pc->not_unloadable))
     {
-        pcl = g_list_remove(pcl, pc);
         g_module_close(pc->gmodule);
     }
 }
 
-/* Get a list of all available plugin classes.
- * Returns a newly allocated GList which should be freed with plugin_class_list_free(list). */
-GList * plugin_get_available_classes(void)
+/* Loads all available old type plugins. Should be removed in future releases. */
+static void plugin_get_available_classes(void)
 {
     /* Initialize static plugins on first call. */
-    if (pcl == NULL)
-        init_plugin_class_list();
-
-    /* Loop over all classes to formulate the result.
-     * Increase the reference count; it will be decreased in plugin_class_list_free. */
-    GList * classes = NULL;
-    GList * l;
-    for (l = pcl; l != NULL; l = l->next)
-    {
-        PluginClass * pc = (PluginClass *) l->data;
-        classes = g_list_prepend(classes, pc);
-        pc->count += 1;
-    }
+    init_plugin_class_list();
 
 #ifndef DISABLE_PLUGINS_LOADING
     GDir * dir = g_dir_open(PACKAGE_LIB_DIR "/lxpanel/plugins", 0, NULL);
@@ -283,16 +209,11 @@ GList * plugin_get_available_classes(void)
             if (g_str_has_suffix(file, ".so"))
             {
                 char * type = g_strndup(file, strlen(file) - 3);
-                if (plugin_find_class(type) == NULL)
+                if (_find_plugin(type) == NULL)
                 {
                     /* If it has not been loaded, do it.  If successful, add it to the result. */
                     char * path = g_build_filename(PACKAGE_LIB_DIR "/lxpanel/plugins", file, NULL );
-                    PluginClass * pc = plugin_load_dynamic(type, path);
-                    if (pc != NULL)
-                    {
-                        pc->count += 1;
-                        classes = g_list_prepend(classes, pc);
-                    }
+                    plugin_load_dynamic(type, path);
                     g_free(path);
                 }
                 g_free(type);
@@ -301,39 +222,42 @@ GList * plugin_get_available_classes(void)
         g_dir_close(dir);
     }
 #endif
-    return classes;
-}
-
-/* Free the list allocated by plugin_get_available_classes. */
-void plugin_class_list_free(GList * list)
-{
-   g_list_foreach(list, (GFunc) plugin_class_unref, NULL);
-   g_list_free(list);
 }
 
 /* Recursively set the background of all widgets on a panel background configuration change. */
-void plugin_widget_set_background(GtkWidget * w, Panel * p)
+void plugin_widget_set_background(GtkWidget * w, LXPanel * panel)
 {
     if (w != NULL)
     {
-        if ( ! GTK_WIDGET_NO_WINDOW(w))
+        Panel *p = panel->priv;
+        if (gtk_widget_get_has_window(w))
         {
             if ((p->background) || (p->transparent))
             {
+#if GTK_CHECK_VERSION(2, 20, 0)
+                if (gtk_widget_get_realized(w))
+#else
                 if (GTK_WIDGET_REALIZED(w))
+#endif
                 {
-                    panel_determine_background_pixmap(p, w, w->window);
-                    gdk_window_invalidate_rect(w->window, NULL, TRUE);
+                    _panel_determine_background_pixmap(panel, w);
+                    gdk_window_invalidate_rect(gtk_widget_get_window(w), NULL, TRUE);
                 }
             }
             else
             {
                 /* Set background according to the current GTK style. */
                 gtk_widget_set_app_paintable(w, FALSE);
+#if GTK_CHECK_VERSION(2, 20, 0)
+                if (gtk_widget_get_realized(w))
+#else
                 if (GTK_WIDGET_REALIZED(w))
+#endif
                 {
-                    gdk_window_set_back_pixmap(w->window, NULL, TRUE);
-                    gtk_style_set_background(w->style, w->window, GTK_STATE_NORMAL);
+                    gdk_window_set_back_pixmap(gtk_widget_get_window(w), NULL, TRUE);
+                    gtk_style_set_background(gtk_widget_get_style(w),
+                                             gtk_widget_get_window(w),
+                                             GTK_STATE_NORMAL);
                 }
             }
         }
@@ -349,80 +273,350 @@ void plugin_widget_set_background(GtkWidget * w, Panel * p)
 
         /* Recursively process all children of a container. */
         if (GTK_IS_CONTAINER(w))
-            gtk_container_foreach(GTK_CONTAINER(w), (GtkCallback) plugin_widget_set_background, p);
+            gtk_container_foreach(GTK_CONTAINER(w), (GtkCallback) plugin_widget_set_background, panel);
     }
 }
 
 /* Handler for "button_press_event" signal with Plugin as parameter.
  * External so can be used from a plugin. */
-gboolean plugin_button_press_event(GtkWidget *widget, GdkEventButton *event, Plugin *plugin)
+static gboolean lxpanel_plugin_button_press_event(GtkWidget *plugin, GdkEventButton *event, LXPanel *panel)
 {
-    if (event->button == 3)     /* right button */
+    if (event->button == 3 && /* right button */
+        (event->state & gtk_accelerator_get_default_mod_mask()) == 0) /* no key */
     {
-        GtkMenu* popup = (GtkMenu*) lxpanel_get_panel_menu(plugin->panel, plugin, FALSE);
+        GtkMenu* popup = (GtkMenu*)lxpanel_get_plugin_menu(panel, plugin, FALSE);
         gtk_menu_popup(popup, NULL, NULL, NULL, NULL, event->button, event->time);
         return TRUE;
-    }    
+    }
     return FALSE;
 }
 
+/* for old plugins compatibility */
+gboolean plugin_button_press_event(GtkWidget *widget, GdkEventButton *event, Plugin *plugin)
+{
+    return lxpanel_plugin_button_press_event(plugin->pwid, event, PLUGIN_PANEL(plugin->pwid));
+}
+
 /* Helper for position-calculation callback for popup menus. */
-void plugin_popup_set_position_helper(Plugin * p, GtkWidget * near, GtkWidget * popup, GtkRequisition * popup_req, gint * px, gint * py)
+void lxpanel_plugin_popup_set_position_helper(LXPanel * p, GtkWidget * near, GtkWidget * popup, gint * px, gint * py)
 {
-    /* Get the origin of the requested-near widget in screen coordinates. */
     gint x, y;
-    gdk_window_get_origin(GDK_WINDOW(near->window), &x, &y);
-    if (x != near->allocation.x) x += near->allocation.x;      /* Doesn't seem to be working according to spec; the allocation.x sometimes has the window origin in it */
-    if (y != near->allocation.y) y += near->allocation.y;
+    GtkAllocation allocation;
+    GtkAllocation popup_req;
+
+    /* Get the allocation of the popup menu. */
+    gtk_widget_realize(popup);
+    gtk_widget_get_allocation(popup, &popup_req);
+    if (gtk_widget_is_toplevel(popup))
+    {
+        GdkRectangle extents;
+        /* FIXME: can we wait somehow for WM drawing decorations? */
+        gdk_window_process_all_updates();
+        gdk_window_get_frame_extents(gtk_widget_get_window(popup), &extents);
+        popup_req.width = extents.width;
+        popup_req.height = extents.height;
+    }
+
+    /* Get the origin of the requested-near widget in screen coordinates. */
+    gtk_widget_get_allocation(near, &allocation);
+    gdk_window_get_origin(gtk_widget_get_window(near), &x, &y);
+    if (!gtk_widget_get_has_window(near))
+    {
+        /* For non-window widgets allocation is given within the screen */
+        x += allocation.x;
+        y += allocation.y;
+    }
 
     /* Dispatch on edge to lay out the popup menu with respect to the button.
      * Also set "push-in" to avoid any case where it might flow off screen. */
-    switch (p->panel->edge)
+    switch (p->priv->edge)
     {
-        case EDGE_TOP:          y += near->allocation.height;         break;
-        case EDGE_BOTTOM:       y -= popup_req->height;                break;
-        case EDGE_LEFT:         x += near->allocation.width;          break;
-        case EDGE_RIGHT:        x -= popup_req->width;                 break;
+        case EDGE_TOP:          y += allocation.height;         break;
+        case EDGE_BOTTOM:       y -= popup_req.height;                break;
+        case EDGE_LEFT:         x += allocation.width;          break;
+        case EDGE_RIGHT:        x -= popup_req.width;                 break;
     }
+
+    /* Push onscreen. */
+    int screen_width = gdk_screen_width();
+    int screen_height = gdk_screen_height();
+    x = CLAMP(x, 0, screen_width - popup_req.width);
+    y = CLAMP(y, 0, screen_height - popup_req.height);
+    /* FIXME: take monitor area into account not just screen */
+
     *px = x;
     *py = y;
 }
 
+/* for old plugins compatibility -- popup_req is ignored here */
+void plugin_popup_set_position_helper(Plugin * p, GtkWidget * near, GtkWidget * popup, GtkRequisition * popup_req, gint * px, gint * py)
+{
+    lxpanel_plugin_popup_set_position_helper(p->panel->topgwin, near, popup, px, py);
+}
+
 /* Adjust the position of a popup window to ensure that it is not hidden by the panel.
  * It is observed that some window managers do not honor the strut that is set on the panel. */
+void lxpanel_plugin_adjust_popup_position(GtkWidget * popup, GtkWidget * parent)
+{
+    gint x, y;
+
+    /* Calculate desired position for the popup. */
+    lxpanel_plugin_popup_set_position_helper(PLUGIN_PANEL(parent), parent,
+                                             popup, &x, &y);
+    /* Move the popup to position. */
+    gdk_window_move(gtk_widget_get_window(popup), x, y);
+}
+
+/* for old plugins compatibility */
 void plugin_adjust_popup_position(GtkWidget * popup, Plugin * plugin)
 {
-    /* Initialize. */
-    Panel * p = plugin->panel;
-    GtkWidget * parent = plugin->pwid;
+    lxpanel_plugin_adjust_popup_position(popup, plugin->pwid);
+}
 
-    /* Get the coordinates of the plugin top level widget. */
-    int x = p->cx + parent->allocation.x;
-    int y = p->cy + parent->allocation.y;
+/* Open a specified path in a file manager. */
+static gboolean _open_dir_in_file_manager(GAppLaunchContext* ctx, GList* folder_infos,
+                                          gpointer user_data, GError** err)
+{
+    FmFileInfo *fi = folder_infos->data; /* only first is used */
+    GAppInfo *app = g_app_info_get_default_for_type("inode/directory", TRUE);
+    GFile *gf;
+    gboolean ret;
 
-    /* Adjust these coordinates according to the panel edge. */
-    switch (p->edge)
+    if (app == NULL)
     {
-        case EDGE_TOP:
-           y += parent->allocation.height;
-            break;
-        case EDGE_BOTTOM:
-            y -= popup->allocation.height;
-            break;
-        case EDGE_LEFT:
-            x += parent->allocation.width;
-            break;
-        case EDGE_RIGHT:
-            x -= popup->allocation.width;
+        g_set_error_literal(err, G_SHELL_ERROR, G_SHELL_ERROR_EMPTY_STRING,
+                            _("No file manager is configured."));
+        return FALSE;
+    }
+    gf = fm_path_to_gfile(fm_file_info_get_path(fi));
+    folder_infos = g_list_prepend(NULL, gf);
+    ret = fm_app_info_launch(app, folder_infos, ctx, err);
+    g_list_free(folder_infos);
+    g_object_unref(gf);
+    g_object_unref(app);
+    return ret;
+}
+
+gboolean lxpanel_launch_path(LXPanel *panel, FmPath *path)
+{
+    return fm_launch_path_simple(NULL, NULL, path, _open_dir_in_file_manager, NULL);
+}
+
+void lxpanel_plugin_show_config_dialog(GtkWidget* plugin)
+{
+    const LXPanelPluginInit *init = PLUGIN_CLASS(plugin);
+    LXPanel *panel = PLUGIN_PANEL(plugin);
+    GtkWidget *dlg = panel->priv->plugin_pref_dialog;
+
+    if (dlg && g_object_get_data(G_OBJECT(dlg), "generic-config-plugin") == plugin)
+        return; /* configuration dialog is already shown for this widget */
+    g_return_if_fail(panel != NULL);
+    dlg = init->config(panel, plugin);
+    if (dlg)
+        _panel_show_config_dialog(panel, plugin, dlg);
+}
+
+#if GLIB_CHECK_VERSION(2, 32, 0)
+static GRecMutex _mutex;
+#else
+static GStaticRecMutex _mutex = G_STATIC_REC_MUTEX_INIT;
+#endif
+
+#ifndef DISABLE_PLUGINS_LOADING
+FM_MODULE_DEFINE_TYPE(lxpanel_gtk, LXPanelPluginInit, 1)
+
+static gboolean fm_module_callback_lxpanel_gtk(const char *name, gpointer init, int ver)
+{
+    /* ignore ver for now, only 1 exists */
+    return lxpanel_register_plugin_type(name, init);
+}
+#endif
+
+static gboolean old_plugins_loaded = FALSE;
+
+void _prepare_modules(void)
+{
+    _all_types = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
+    lxpanel_plugin_qdata = g_quark_from_static_string("LXPanel::plugin-data");
+    lxpanel_plugin_qinit = g_quark_from_static_string("LXPanel::plugin-init");
+    lxpanel_plugin_qconf = g_quark_from_static_string("LXPanel::plugin-conf");
+#ifndef DISABLE_PLUGINS_LOADING
+    fm_modules_add_directory(PACKAGE_LIB_DIR "/lxpanel/plugins");
+    fm_module_register_lxpanel_gtk();
+#endif
+}
+
+void _unload_modules(void)
+{
+    GHashTableIter iter;
+    gpointer key, val;
+
+    g_hash_table_iter_init(&iter, _all_types);
+    while(g_hash_table_iter_next(&iter, &key, &val))
+    {
+        register const LXPanelPluginInit *init = val;
+        if (init->new_instance == NULL) /* old type of plugin */
+        {
+            plugin_class_unref(init->_reserved1);
+            g_free(val);
+        }
+    }
+    g_hash_table_destroy(_all_types);
+#ifndef DISABLE_PLUGINS_LOADING
+    fm_module_unregister_type("lxpanel_gtk");
+#endif
+    old_plugins_loaded = FALSE;
+}
+
+gboolean lxpanel_register_plugin_type(const char *name, const LXPanelPluginInit *init)
+{
+    const LXPanelPluginInit *data;
+
+    /* validate it */
+    if (init->new_instance == NULL || name == NULL || name[0] == '\0')
+        return FALSE;
+#if GLIB_CHECK_VERSION(2, 32, 0)
+    g_rec_mutex_lock(&_mutex);
+#else
+    g_static_rec_mutex_lock(&_mutex);
+#endif
+    /* test if it's registered already */
+    data = _find_plugin(name);
+    if (data == NULL)
+    {
+        if (init->init)
+            init->init();
+        g_hash_table_insert(_all_types, g_strdup(name), (gpointer)init);
+    }
+#if GLIB_CHECK_VERSION(2, 32, 0)
+    g_rec_mutex_unlock(&_mutex);
+#else
+    g_static_rec_mutex_unlock(&_mutex);
+#endif
+    return (data == NULL);
+}
+
+static void _old_plugin_save_hook(const config_setting_t * setting, FILE * f, gpointer user_data)
+{
+    Plugin *pl = user_data;
+    PluginClass *pc = pl->class;
+    if (pc->save)
+        pc->save(pl, f);
+}
+
+/* This is called right before Plugin instance is destroyed */
+static void _old_plugin_destroy(gpointer data)
+{
+    Plugin *pl = data;
+
+    plugin_class_unref(pl->class);
+
+    /* Free the Plugin structure. */
+    g_free(pl);
+}
+
+static void _on_old_widget_destroy(GtkWidget *widget, Plugin *pl)
+{
+    /* Never let run it again. */
+    g_signal_handlers_disconnect_by_func(widget, _on_old_widget_destroy, pl);
+    /* Run the destructor before destroying the top level widget.
+     * This prevents problems with the plugin destroying child widgets. */
+    pl->class->destructor(pl);
+}
+
+//static void on_size_allocate(GtkWidget *widget, GdkRectangle *allocation, LXPanel *p)
+//{
+//    _queue_panel_calculate_size(p);
+//}
+
+GtkWidget *lxpanel_add_plugin(LXPanel *p, const char *name, config_setting_t *cfg, gint at)
+{
+    const LXPanelPluginInit *init;
+    GtkWidget *widget;
+    config_setting_t *s, *pconf;
+    gint expand, padding = 0, border = 0, i;
+
+    CHECK_MODULES();
+    if (!old_plugins_loaded)
+        plugin_get_available_classes();
+    old_plugins_loaded = TRUE;
+    init = _find_plugin(name);
+    if (init == NULL)
+        return NULL;
+    /* prepare widget settings */
+    if (!init->expand_available)
+        expand = 0;
+    else if ((s = config_setting_get_member(cfg, "expand")))
+        expand = config_setting_get_int(s);
+    else
+        expand = init->expand_default;
+    s = config_setting_get_member(cfg, "padding");
+    if (s)
+        padding = config_setting_get_int(s);
+    s = config_setting_get_member(cfg, "border");
+    if (s)
+        border = config_setting_get_int(s);
+    /* prepare config and create it if need */
+    s = config_setting_add(cfg, "", PANEL_CONF_TYPE_LIST);
+    for (i = 0; (pconf = config_setting_get_elem(s, i)); i++)
+        if (strcmp(config_setting_get_name(pconf), "Config") == 0)
             break;
+    if (!pconf)
+        pconf = config_setting_add(s, "Config", PANEL_CONF_TYPE_GROUP);
+    /* If this plugin can only be instantiated once, count the instantiation.
+     * This causes the configuration system to avoid displaying the plugin as one that can be added. */
+    if (init->new_instance) /* new style of plugin */
+    {
+        widget = init->new_instance(p, pconf);
+        if (widget == NULL)
+            return widget;
+        /* always connect lxpanel_plugin_button_press_event() */
+        g_signal_connect(widget, "button-press-event",
+                         G_CALLBACK(lxpanel_plugin_button_press_event), p);
+        if (init->button_press_event)
+            g_signal_connect(widget, "button-press-event",
+                             G_CALLBACK(init->button_press_event), p);
     }
+    else
+    {
+        Plugin *pl = g_new0(Plugin, 1);
+        PluginClass *pc = init->_reserved1;
+        char *conf = config_setting_to_string(pconf), *fp;
+
+        pl->class = pc;
+        pl->panel = p->priv;
+        widget = NULL;
+        fp = &conf[9]; /* skip "Config {\n" */
+        /* g_debug("created conf: %s",conf); */
+    /* Call the constructor.
+     * It is responsible for parsing the parameters, and setting "pwid" to the top level widget. */
+        if (pc->constructor(pl, &fp))
+            widget = pl->pwid;
+        g_free(conf);
 
-    /* Clip the coordinates to ensure that the popup remains on screen. */
-    int screen_width = gdk_screen_width();
-    int screen_height = gdk_screen_height();
-    if ((x + popup->allocation.width) > screen_width) x = screen_width - popup->allocation.width;
-    if ((y + popup->allocation.height) > screen_height) y = screen_height - popup->allocation.height;
+        if (widget == NULL) /* failed */
+        {
+            g_free(pl);
+            return widget;
+        }
 
-    /* Move the popup to position. */
-    gdk_window_move(popup->window, x, y);
+        pc->count += 1;
+        g_signal_connect(widget, "destroy", G_CALLBACK(_on_old_widget_destroy), pl);
+        config_setting_set_save_hook(pconf, _old_plugin_save_hook, pl);
+        lxpanel_plugin_set_data(widget, pl, _old_plugin_destroy);
+    }
+    gtk_widget_set_name(widget, name);
+    gtk_box_pack_start(GTK_BOX(p->priv->box), widget, expand, TRUE, padding);
+    gtk_container_set_border_width(GTK_CONTAINER(widget), border);
+//    g_signal_connect(widget, "size-allocate", G_CALLBACK(on_size_allocate), p);
+    gtk_widget_show(widget);
+    g_object_set_qdata(G_OBJECT(widget), lxpanel_plugin_qconf, cfg);
+    g_object_set_qdata(G_OBJECT(widget), lxpanel_plugin_qinit, (gpointer)init);
+    return widget;
+}
+
+/* transfer none - note that not all fields are valid there */
+GHashTable *lxpanel_get_all_types(void)
+{
+    return _all_types;
 }