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