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