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