Allow -Wl,-z,defs to work - create a liblxpanel.so.0.0.0
[lxde/lxpanel.git] / src / plugin.c
1 /**
2 * Copyright (c) 2006-2014 LxDE Developers, see the file AUTHORS for details.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include "private.h"
24
25 #include <gdk-pixbuf/gdk-pixbuf.h>
26 #include <gdk-pixbuf-xlib/gdk-pixbuf-xlib.h>
27 #include <gdk/gdk.h>
28 #include <string.h>
29 #include <stdlib.h>
30
31 #include "misc.h"
32 #include "bg.h"
33
34 #include <glib-object.h>
35 #include <glib/gi18n.h>
36 #include <libfm/fm-gtk.h>
37
38 //#define DEBUG
39 #include "dbg.h"
40
41 static void plugin_class_unref(PluginClass * pc);
42
43 GQuark lxpanel_plugin_qinit;
44 GQuark lxpanel_plugin_qconf;
45 GQuark lxpanel_plugin_qdata;
46 GQuark lxpanel_plugin_qsize;
47 static GHashTable *_all_types = NULL;
48
49 /* Dynamic parameter for static (built-in) plugins must be FALSE so we will not try to unload them */
50 #define REGISTER_STATIC_PLUGIN_CLASS(pc) \
51 do {\
52 extern PluginClass pc;\
53 register_plugin_class(&pc, FALSE);\
54 } while (0)
55
56 static inline const LXPanelPluginInit *_find_plugin(const char *name)
57 {
58 return g_hash_table_lookup(_all_types, name);
59 }
60
61 static GtkWidget *_old_plugin_config(LXPanel *panel, GtkWidget *instance)
62 {
63 const LXPanelPluginInit *init = PLUGIN_CLASS(instance);
64 Plugin * plugin;
65
66 g_return_val_if_fail(init != NULL && init->new_instance == NULL, NULL);
67 plugin = lxpanel_plugin_get_data(instance);
68 if (plugin->class->config)
69 plugin->class->config(plugin, GTK_WINDOW(panel));
70 return NULL;
71 }
72
73 static void _old_plugin_reconfigure(LXPanel *panel, GtkWidget *instance)
74 {
75 const LXPanelPluginInit *init = PLUGIN_CLASS(instance);
76 Plugin * plugin;
77
78 g_return_if_fail(init != NULL && init->new_instance == NULL);
79 plugin = lxpanel_plugin_get_data(instance);
80 if (plugin->class->panel_configuration_changed)
81 plugin->class->panel_configuration_changed(plugin);
82 }
83
84 /* Register a PluginClass. */
85 static void register_plugin_class(PluginClass * pc, gboolean dynamic)
86 {
87 LXPanelPluginInit *init = g_new0(LXPanelPluginInit, 1);
88 init->_reserved1 = pc;
89 init->name = pc->name;
90 init->description = pc->description;
91 if (pc->config)
92 init->config = _old_plugin_config;
93 if (pc->panel_configuration_changed)
94 init->reconfigure = _old_plugin_reconfigure;
95 init->one_per_system = pc->one_per_system;
96 init->expand_available = pc->expand_available;
97 init->expand_default = pc->expand_default;
98 pc->dynamic = dynamic;
99 g_hash_table_insert(_all_types, g_strdup(pc->type), init);
100 }
101
102 /* Load a dynamic plugin. */
103 static void plugin_load_dynamic(const char * type, const gchar * path)
104 {
105 PluginClass * pc = NULL;
106
107 /* Load the external module. */
108 GModule * m = g_module_open(path, G_MODULE_BIND_LAZY);
109 if (m != NULL)
110 {
111 /* Formulate the name of the expected external variable of type PluginClass. */
112 char class_name[128];
113 g_snprintf(class_name, sizeof(class_name), "%s_plugin_class", type);
114
115 /* Validate that the external variable is of type PluginClass. */
116 gpointer tmpsym;
117 if (( ! g_module_symbol(m, class_name, &tmpsym)) /* Ensure symbol is present */
118 || ((pc = tmpsym) == NULL)
119 || (pc->structure_size != sizeof(PluginClass)) /* Then check versioning information */
120 || (pc->structure_version != PLUGINCLASS_VERSION)
121 || (strcmp(type, pc->type) != 0)) /* Then and only then access other fields; check name */
122 {
123 g_module_close(m);
124 g_warning("%s.so is not a lxpanel plugin", type);
125 return;
126 }
127
128 /* Register the newly loaded and valid plugin. */
129 pc->gmodule = m;
130 register_plugin_class(pc, TRUE);
131 pc->count = 1;
132 }
133 }
134
135 /* Unreference a dynamic plugin. */
136 static void plugin_class_unref(PluginClass * pc)
137 {
138 pc->count -= 1;
139
140 /* If the reference count drops to zero, unload the plugin if it is dynamic and has declared itself unloadable. */
141 if ((pc->count == 0)
142 && (pc->dynamic)
143 && ( ! pc->not_unloadable))
144 {
145 g_module_close(pc->gmodule);
146 }
147 }
148
149 /* Loads all available old type plugins. Should be removed in future releases. */
150 static void plugin_get_available_classes(void)
151 {
152 #ifndef DISABLE_PLUGINS_LOADING
153 GDir * dir = g_dir_open(PACKAGE_LIB_DIR "/lxpanel/plugins", 0, NULL);
154 if (dir != NULL)
155 {
156 const char * file;
157 while ((file = g_dir_read_name(dir)) != NULL)
158 {
159 if (g_str_has_suffix(file, ".so"))
160 {
161 char * type = g_strndup(file, strlen(file) - 3);
162 if (_find_plugin(type) == NULL)
163 {
164 /* If it has not been loaded, do it. If successful, add it to the result. */
165 char * path = g_build_filename(PACKAGE_LIB_DIR "/lxpanel/plugins", file, NULL );
166 plugin_load_dynamic(type, path);
167 g_free(path);
168 }
169 g_free(type);
170 }
171 }
172 g_dir_close(dir);
173 }
174 #endif
175 }
176
177 /* Recursively set the background of all widgets on a panel background configuration change. */
178 void plugin_widget_set_background(GtkWidget * w, LXPanel * panel)
179 {
180 if (w != NULL)
181 {
182 Panel *p = panel->priv;
183 if (gtk_widget_get_has_window(w))
184 {
185 if ((p->background) || (p->transparent))
186 {
187 #if GTK_CHECK_VERSION(2, 20, 0)
188 if (gtk_widget_get_realized(w))
189 #else
190 if (GTK_WIDGET_REALIZED(w))
191 #endif
192 {
193 _panel_determine_background_pixmap(panel, w);
194 gdk_window_invalidate_rect(gtk_widget_get_window(w), NULL, TRUE);
195 }
196 }
197 else
198 {
199 /* Set background according to the current GTK style. */
200 gtk_widget_set_app_paintable(w, FALSE);
201 #if GTK_CHECK_VERSION(2, 20, 0)
202 if (gtk_widget_get_realized(w))
203 #else
204 if (GTK_WIDGET_REALIZED(w))
205 #endif
206 {
207 gdk_window_set_back_pixmap(gtk_widget_get_window(w), NULL, TRUE);
208 gtk_style_set_background(gtk_widget_get_style(w),
209 gtk_widget_get_window(w),
210 GTK_STATE_NORMAL);
211 }
212 }
213 }
214
215 /* Special handling to get tray icons redrawn. */
216 if (GTK_IS_SOCKET(w))
217 {
218 gtk_widget_hide(w);
219 gdk_window_process_all_updates();
220 gtk_widget_show(w);
221 gdk_window_process_all_updates();
222 }
223
224 /* Recursively process all children of a container. */
225 if (GTK_IS_CONTAINER(w))
226 gtk_container_foreach(GTK_CONTAINER(w), (GtkCallback) plugin_widget_set_background, panel);
227 }
228 }
229
230 /* Handler for "button_press_event" signal with Plugin as parameter.
231 * External so can be used from a plugin. */
232 static gboolean lxpanel_plugin_button_press_event(GtkWidget *plugin, GdkEventButton *event, LXPanel *panel)
233 {
234 if (event->button == 3 && /* right button */
235 (event->state & gtk_accelerator_get_default_mod_mask()) == 0) /* no key */
236 {
237 GtkMenu* popup = (GtkMenu*)lxpanel_get_plugin_menu(panel, plugin, FALSE);
238 gtk_menu_popup(popup, NULL, NULL, NULL, NULL, event->button, event->time);
239 return TRUE;
240 }
241 return FALSE;
242 }
243
244 /* for old plugins compatibility */
245 gboolean plugin_button_press_event(GtkWidget *widget, GdkEventButton *event, Plugin *plugin)
246 {
247 return lxpanel_plugin_button_press_event(plugin->pwid, event, PLUGIN_PANEL(plugin->pwid));
248 }
249
250 /* Helper for position-calculation callback for popup menus. */
251 void lxpanel_plugin_popup_set_position_helper(LXPanel * p, GtkWidget * near, GtkWidget * popup, gint * px, gint * py)
252 {
253 gint x, y;
254 GtkAllocation allocation;
255 GtkAllocation popup_req;
256
257 /* Get the allocation of the popup menu. */
258 gtk_widget_realize(popup);
259 gtk_widget_get_allocation(popup, &popup_req);
260 if (gtk_widget_is_toplevel(popup))
261 {
262 GdkRectangle extents;
263 /* FIXME: can we wait somehow for WM drawing decorations? */
264 gdk_window_process_all_updates();
265 gdk_window_get_frame_extents(gtk_widget_get_window(popup), &extents);
266 popup_req.width = extents.width;
267 popup_req.height = extents.height;
268 }
269
270 /* Get the origin of the requested-near widget in screen coordinates. */
271 gtk_widget_get_allocation(near, &allocation);
272 gdk_window_get_origin(gtk_widget_get_window(near), &x, &y);
273 if (!gtk_widget_get_has_window(near))
274 {
275 /* For non-window widgets allocation is given within the screen */
276 x += allocation.x;
277 y += allocation.y;
278 }
279
280 /* Dispatch on edge to lay out the popup menu with respect to the button.
281 * Also set "push-in" to avoid any case where it might flow off screen. */
282 switch (p->priv->edge)
283 {
284 case EDGE_TOP: y += allocation.height; break;
285 case EDGE_BOTTOM: y -= popup_req.height; break;
286 case EDGE_LEFT: x += allocation.width; break;
287 case EDGE_RIGHT: x -= popup_req.width; break;
288 }
289
290 /* Push onscreen. */
291 int screen_width = gdk_screen_width();
292 int screen_height = gdk_screen_height();
293 x = CLAMP(x, 0, screen_width - popup_req.width);
294 y = CLAMP(y, 0, screen_height - popup_req.height);
295 /* FIXME: take monitor area into account not just screen */
296
297 *px = x;
298 *py = y;
299 }
300
301 /* for old plugins compatibility -- popup_req is ignored here */
302 void plugin_popup_set_position_helper(Plugin * p, GtkWidget * near, GtkWidget * popup, GtkRequisition * popup_req, gint * px, gint * py)
303 {
304 lxpanel_plugin_popup_set_position_helper(p->panel->topgwin, near, popup, px, py);
305 }
306
307 /* Adjust the position of a popup window to ensure that it is not hidden by the panel.
308 * It is observed that some window managers do not honor the strut that is set on the panel. */
309 void lxpanel_plugin_adjust_popup_position(GtkWidget * popup, GtkWidget * parent)
310 {
311 gint x, y;
312
313 /* Calculate desired position for the popup. */
314 lxpanel_plugin_popup_set_position_helper(PLUGIN_PANEL(parent), parent,
315 popup, &x, &y);
316 /* Move the popup to position. */
317 gdk_window_move(gtk_widget_get_window(popup), x, y);
318 }
319
320 /* for old plugins compatibility */
321 void plugin_adjust_popup_position(GtkWidget * popup, Plugin * plugin)
322 {
323 lxpanel_plugin_adjust_popup_position(popup, plugin->pwid);
324 }
325
326 /* Open a specified path in a file manager. */
327 static gboolean _open_dir_in_file_manager(GAppLaunchContext* ctx, GList* folder_infos,
328 gpointer user_data, GError** err)
329 {
330 FmFileInfo *fi = folder_infos->data; /* only first is used */
331 GAppInfo *app = g_app_info_get_default_for_type("inode/directory", TRUE);
332 GFile *gf;
333 gboolean ret;
334
335 if (app == NULL)
336 {
337 g_set_error_literal(err, G_SHELL_ERROR, G_SHELL_ERROR_EMPTY_STRING,
338 _("No file manager is configured."));
339 return FALSE;
340 }
341 gf = fm_path_to_gfile(fm_file_info_get_path(fi));
342 folder_infos = g_list_prepend(NULL, gf);
343 ret = fm_app_info_launch(app, folder_infos, ctx, err);
344 g_list_free(folder_infos);
345 g_object_unref(gf);
346 g_object_unref(app);
347 return ret;
348 }
349
350 gboolean lxpanel_launch_path(LXPanel *panel, FmPath *path)
351 {
352 return fm_launch_path_simple(NULL, NULL, path, _open_dir_in_file_manager, NULL);
353 }
354
355 void lxpanel_plugin_show_config_dialog(GtkWidget* plugin)
356 {
357 const LXPanelPluginInit *init = PLUGIN_CLASS(plugin);
358 LXPanel *panel = PLUGIN_PANEL(plugin);
359 GtkWidget *dlg = panel->priv->plugin_pref_dialog;
360
361 if (dlg && g_object_get_data(G_OBJECT(dlg), "generic-config-plugin") == plugin)
362 return; /* configuration dialog is already shown for this widget */
363 g_return_if_fail(panel != NULL);
364 dlg = init->config(panel, plugin);
365 if (dlg)
366 _panel_show_config_dialog(panel, plugin, dlg);
367 }
368
369 #if GLIB_CHECK_VERSION(2, 32, 0)
370 static GRecMutex _mutex;
371 #else
372 static GStaticRecMutex _mutex = G_STATIC_REC_MUTEX_INIT;
373 #endif
374
375 #ifndef DISABLE_PLUGINS_LOADING
376 FM_MODULE_DEFINE_TYPE(lxpanel_gtk, LXPanelPluginInit, 1)
377
378 static gboolean fm_module_callback_lxpanel_gtk(const char *name, gpointer init, int ver)
379 {
380 /* ignore ver for now, only 1 exists */
381 return lxpanel_register_plugin_type(name, init);
382 }
383 #endif
384
385 static gboolean old_plugins_loaded = FALSE;
386
387 void _prepare_modules(void)
388 {
389 _all_types = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
390 lxpanel_plugin_qdata = g_quark_from_static_string("LXPanel::plugin-data");
391 lxpanel_plugin_qinit = g_quark_from_static_string("LXPanel::plugin-init");
392 lxpanel_plugin_qconf = g_quark_from_static_string("LXPanel::plugin-conf");
393 lxpanel_plugin_qsize = g_quark_from_static_string("LXPanel::plugin-size");
394 #ifndef DISABLE_PLUGINS_LOADING
395 fm_modules_add_directory(PACKAGE_LIB_DIR "/lxpanel/plugins");
396 fm_module_register_lxpanel_gtk();
397 #endif
398 }
399
400 void _unload_modules(void)
401 {
402 GHashTableIter iter;
403 gpointer key, val;
404
405 g_hash_table_iter_init(&iter, _all_types);
406 while(g_hash_table_iter_next(&iter, &key, &val))
407 {
408 register const LXPanelPluginInit *init = val;
409 if (init->new_instance == NULL) /* old type of plugin */
410 {
411 plugin_class_unref(init->_reserved1);
412 g_free(val);
413 }
414 }
415 g_hash_table_destroy(_all_types);
416 #ifndef DISABLE_PLUGINS_LOADING
417 fm_module_unregister_type("lxpanel_gtk");
418 #endif
419 old_plugins_loaded = FALSE;
420 }
421
422 gboolean lxpanel_register_plugin_type(const char *name, const LXPanelPluginInit *init)
423 {
424 const LXPanelPluginInit *data;
425
426 /* validate it */
427 if (init->new_instance == NULL || name == NULL || name[0] == '\0')
428 return FALSE;
429 #if GLIB_CHECK_VERSION(2, 32, 0)
430 g_rec_mutex_lock(&_mutex);
431 #else
432 g_static_rec_mutex_lock(&_mutex);
433 #endif
434 /* test if it's registered already */
435 data = _find_plugin(name);
436 if (data == NULL)
437 {
438 if (init->init)
439 init->init();
440 g_hash_table_insert(_all_types, g_strdup(name), (gpointer)init);
441 }
442 #if GLIB_CHECK_VERSION(2, 32, 0)
443 g_rec_mutex_unlock(&_mutex);
444 #else
445 g_static_rec_mutex_unlock(&_mutex);
446 #endif
447 return (data == NULL);
448 }
449
450 static void _old_plugin_save_hook(const config_setting_t * setting, FILE * f, gpointer user_data)
451 {
452 Plugin *pl = user_data;
453 PluginClass *pc = pl->class;
454 if (pc->save)
455 pc->save(pl, f);
456 }
457
458 /* This is called right before Plugin instance is destroyed */
459 static void _old_plugin_destroy(gpointer data)
460 {
461 Plugin *pl = data;
462
463 plugin_class_unref(pl->class);
464
465 /* Free the Plugin structure. */
466 g_free(pl);
467 }
468
469 static void _on_old_widget_destroy(GtkWidget *widget, Plugin *pl)
470 {
471 /* Never let run it again. */
472 g_signal_handlers_disconnect_by_func(widget, _on_old_widget_destroy, pl);
473 /* Run the destructor before destroying the top level widget.
474 * This prevents problems with the plugin destroying child widgets. */
475 pl->class->destructor(pl);
476 }
477
478 static void on_size_allocate(GtkWidget *widget, GdkRectangle *allocation, LXPanel *p)
479 {
480 GdkRectangle *alloc;
481
482 alloc = g_object_get_qdata(G_OBJECT(widget), lxpanel_plugin_qsize);
483 if (alloc->x == allocation->x && alloc->y == allocation->y &&
484 alloc->width == allocation->width && alloc->height == allocation->height)
485 return; /* not changed */
486 *alloc = *allocation;
487 /* g_debug("size-allocate on %s", PLUGIN_CLASS(widget)->name); */
488 _panel_queue_update_background(p);
489 // _queue_panel_calculate_size(p);
490 }
491
492 GtkWidget *lxpanel_add_plugin(LXPanel *p, const char *name, config_setting_t *cfg, gint at)
493 {
494 const LXPanelPluginInit *init;
495 GtkWidget *widget;
496 config_setting_t *s, *pconf;
497 gint expand, padding = 0, border = 0, i;
498
499 CHECK_MODULES();
500 if (!old_plugins_loaded)
501 plugin_get_available_classes();
502 old_plugins_loaded = TRUE;
503 init = _find_plugin(name);
504 if (init == NULL)
505 return NULL;
506 /* prepare widget settings */
507 if (!init->expand_available)
508 expand = 0;
509 else if ((s = config_setting_get_member(cfg, "expand")))
510 expand = config_setting_get_int(s);
511 else
512 expand = init->expand_default;
513 s = config_setting_get_member(cfg, "padding");
514 if (s)
515 padding = config_setting_get_int(s);
516 s = config_setting_get_member(cfg, "border");
517 if (s)
518 border = config_setting_get_int(s);
519 /* prepare config and create it if need */
520 s = config_setting_add(cfg, "", PANEL_CONF_TYPE_LIST);
521 for (i = 0; (pconf = config_setting_get_elem(s, i)); i++)
522 if (strcmp(config_setting_get_name(pconf), "Config") == 0)
523 break;
524 if (!pconf)
525 pconf = config_setting_add(s, "Config", PANEL_CONF_TYPE_GROUP);
526 /* If this plugin can only be instantiated once, count the instantiation.
527 * This causes the configuration system to avoid displaying the plugin as one that can be added. */
528 if (init->new_instance) /* new style of plugin */
529 {
530 widget = init->new_instance(p, pconf);
531 if (widget == NULL)
532 return widget;
533 /* always connect lxpanel_plugin_button_press_event() */
534 g_signal_connect(widget, "button-press-event",
535 G_CALLBACK(lxpanel_plugin_button_press_event), p);
536 if (init->button_press_event)
537 g_signal_connect(widget, "button-press-event",
538 G_CALLBACK(init->button_press_event), p);
539 }
540 else
541 {
542 Plugin *pl = g_new0(Plugin, 1);
543 PluginClass *pc = init->_reserved1;
544 char *conf = config_setting_to_string(pconf), *fp;
545
546 pl->class = pc;
547 pl->panel = p->priv;
548 widget = NULL;
549 fp = &conf[9]; /* skip "Config {\n" */
550 /* g_debug("created conf: %s",conf); */
551 /* Call the constructor.
552 * It is responsible for parsing the parameters, and setting "pwid" to the top level widget. */
553 if (pc->constructor(pl, &fp))
554 widget = pl->pwid;
555 g_free(conf);
556
557 if (widget == NULL) /* failed */
558 {
559 g_free(pl);
560 return widget;
561 }
562
563 pc->count += 1;
564 g_signal_connect(widget, "destroy", G_CALLBACK(_on_old_widget_destroy), pl);
565 config_setting_set_save_hook(pconf, _old_plugin_save_hook, pl);
566 lxpanel_plugin_set_data(widget, pl, _old_plugin_destroy);
567 }
568 gtk_widget_set_name(widget, name);
569 gtk_box_pack_start(GTK_BOX(p->priv->box), widget, expand, TRUE, padding);
570 gtk_container_set_border_width(GTK_CONTAINER(widget), border);
571 g_signal_connect(widget, "size-allocate", G_CALLBACK(on_size_allocate), p);
572 gtk_widget_show(widget);
573 g_object_set_qdata(G_OBJECT(widget), lxpanel_plugin_qconf, cfg);
574 g_object_set_qdata(G_OBJECT(widget), lxpanel_plugin_qinit, (gpointer)init);
575 g_object_set_qdata_full(G_OBJECT(widget), lxpanel_plugin_qsize,
576 g_new0(GdkRectangle, 1), g_free);
577 return widget;
578 }
579
580 /* transfer none - note that not all fields are valid there */
581 GHashTable *lxpanel_get_all_types(void)
582 {
583 return _all_types;
584 }