Using the new way of writing lxpanel plugins (wiki was outdated)

This commit is contained in:
Fabio Manganiello 2022-01-25 16:18:18 +01:00
parent 5172a60c80
commit 1d3d7d84f7
3 changed files with 58 additions and 234 deletions

2
.gitignore vendored
View File

@ -67,10 +67,12 @@ depcomp
install-sh
missing
Makefile.in
Makefile.in.in
Makefile
intltool-extract.in
intltool-merge.in
intltool-update.in
libtool
.deps/
.libs/

View File

@ -23,6 +23,9 @@ LXPANEL_DATADIR=$(pkg-config --variable=datadir lxpanel)/lxpanel
AC_SUBST(LXPANEL_LIBDIR)
AC_SUBST(LXPANEL_DATADIR)
LXPANEL_CPPFLAGS=$(pkg-config --cflags --libs lxpanel)
AC_SUBST(LXPANEL_CPPFLAGS)
GETTEXT_PACKAGE=$PACKAGE
AC_SUBST(GETTEXT_PACKAGE)
AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE,"$GETTEXT_PACKAGE", [Gettext package.])

View File

@ -1,6 +1,6 @@
/**
*
* Copyright (c) 2009 LxDE Developers, see the file AUTHORS for details.
* Copyright (c) 2022 Fabio Manganiello, 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
@ -18,242 +18,61 @@
*
*/
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <glib/gi18n.h>
#include <string.h>
#include <lxpanel/plugin.h>
typedef struct {
unsigned int timer;
Plugin * plugin;
GtkWidget *main;
GtkWidget *widget;
GtkTooltips *tip;
gchar* text;
gint speed;
gint wait;
gint cnt;
gint color;
gboolean bool;
gchar* file;
gchar* file2;
} Example;
#define LabelSize 32
gint colors[] =
GtkWidget *constructor(LXPanel *panel, config_setting_t *settings)
{
0xFF0000,
0x00FF00,
0xFF00FF,
0xFFFF00
};
gboolean button_press_event( GtkWidget *widget, GdkEventButton* event, Plugin* plugin)
{
ENTER2;
if( event->button == 1 ) /* left button */
{
/* write here */
}
else if( event->button == 2 ) /* middle button */
{
/* write here */
}
else if( event->button == 3 ) /* right button */
{
GtkMenu* popup = (GtkMenu*)lxpanel_get_panel_menu( plugin->panel, plugin, FALSE ); /* lxpanel_menu, can be reimplemented */
gtk_menu_popup( popup, NULL, NULL, NULL, NULL, event->button, event->time );
return TRUE;
}
RET2(TRUE);
}
gboolean scroll_event (GtkWidget *widget, GdkEventScroll *event, Plugin* plugin)
{
ENTER2;
RET2(TRUE);
}
static gint timer_event(Example *egz)
{
char buffer [60];
if(egz->bool) return TRUE;
if(--egz->wait) return TRUE;
egz->wait = egz->speed;
egz->cnt++;
if( egz->cnt >= strlen(egz->text) ) egz->cnt = 0;
sprintf(buffer, "<span color=\"#%06x\"><b>%c</b></span>", colors[egz->color], egz->text[egz->cnt]);
gtk_label_set_markup (GTK_LABEL(egz->widget), buffer) ;
egz->color++;
if( egz->color > 3 ) egz->color = 0;
return TRUE;
}
static gint update_tooltip(Example *egz)
{
char *tooltip;
ENTER;
tooltip = g_strdup_printf(egz->text);
gtk_tooltips_set_tip(egz->tip, egz->main, tooltip, NULL);
g_free(tooltip);
RET(TRUE);
}
static int example_constructor(Plugin *p, char** fp)
{
Example *egz;
char buffer [60];
ENTER;
/* initialization */
egz = g_new0(Example, 1);
egz->plugin = p;
egz->text = g_strdup("Example");
egz->wait = egz->speed = 1;
p->priv = egz;
p->pwid = gtk_event_box_new();
GTK_WIDGET_SET_FLAGS( p->pwid, GTK_NO_WINDOW );
gtk_container_set_border_width( GTK_CONTAINER(p->pwid), 2 );
egz->widget = gtk_label_new(" ");
sprintf(buffer, "<span color=\"#%06x\"><b>%c</b></span>", colors[3], egz->text[0]);
gtk_label_set_markup (GTK_LABEL(egz->widget), buffer) ;
gtk_container_add(GTK_CONTAINER(p->pwid), egz->widget);
egz->main = p->pwid;
egz->tip = gtk_tooltips_new();
update_tooltip(egz);
g_signal_connect (G_OBJECT (p->pwid), "button_press_event", G_CALLBACK (button_press_event), (gpointer) p);
g_signal_connect (G_OBJECT (p->pwid), "scroll_event", G_CALLBACK (scroll_event), (gpointer) p);
line s;
s.len = 256;
if (fp)
{
while (lxpanel_get_line(fp, &s) != LINE_BLOCK_END)
{
if (s.type == LINE_NONE) {
ERR( "example: illegal token %s\n", s.str);
goto error;
}
if (s.type == LINE_VAR) {
if (!g_ascii_strcasecmp(s.t[0], "text")){
g_free(egz->text);
egz->text = g_strdup(s.t[1]);
}else if (!g_ascii_strcasecmp(s.t[0], "speed")){
egz->speed = atoi(s.t[1]);
}else if (!g_ascii_strcasecmp(s.t[0], "bool")){
egz->bool = atoi(s.t[1]); /* 0=false, 1=true */
}else if (!g_ascii_strcasecmp(s.t[0], "file1")){
egz->file = g_strdup(s.t[1]);
}else if (!g_ascii_strcasecmp(s.t[0], "file2")){
egz->file2 = g_strdup(s.t[1]);
}else {
ERR( "example: unknown var %s\n", s.t[0]);
continue;
}
}
else {
ERR( "example: illegal in this context %s\n", s.str);
goto error;
}
}
}
egz->timer = g_timeout_add(500, (GSourceFunc) timer_event, (gpointer)egz); /* set timer */
gtk_widget_show(egz->widget); /* show plugin on panel */
RET(TRUE);
error:
destructor( p );
RET(FALSE);
}
static void applyConfig(Plugin* p)
{
ENTER;
Example *egz = (Example *) p->priv;
gchar buffer [60];
egz->cnt = 0;
egz->color = 0;
if(egz->speed == 0) egz->speed = 1;
egz->wait = egz->speed;
sprintf(buffer, "<span color=\"#%06x\"><b>%c</b></span>", colors[3], egz->text[0]);
update_tooltip(egz);
RET();
}
static void config(Plugin *p, GtkWindow* parent) {
ENTER;
GtkWidget *dialog;
Example *egz = (Example *) p->priv;
dialog = create_generic_config_dlg(_(p->class->name),
GTK_WIDGET(parent),
(GSourceFunc) applyConfig, (gpointer) p,
_("Text"), &egz->text, CONF_TYPE_STR,
_("Speed"), &egz->speed, CONF_TYPE_INT,
_("Stop"), &egz->bool, CONF_TYPE_BOOL,
_("File"), &egz->file, CONF_TYPE_FILE,
_("File"), &egz->file2, CONF_TYPE_FILE_ENTRY,
NULL);
gtk_window_present(GTK_WINDOW(dialog));
RET();
}
static void example_destructor(Plugin *p)
{
ENTER;
Example *egz = (Example *)p->priv;
g_source_remove(egz->timer);
g_free(egz->text);
g_free(egz);
RET();
}
static void save_config( Plugin* p, FILE* fp )
{
ENTER;
Example *egz = (Example *)p->priv;
lxpanel_put_str( fp, "text", egz->text );
lxpanel_put_int( fp, "speed", egz->speed );
lxpanel_put_bool( fp, "bool", egz->bool );
lxpanel_put_str( fp, "file1", egz->file );
lxpanel_put_str( fp, "file2", egz->file2 );
RET();
}
PluginClass example_plugin_class = {
PLUGINCLASS_VERSIONING,
type : "cutiepi-battery",
name : N_("CutiePi battery indicator"),
version: "0.1",
description : N_("Shows the current state of the CutiePi tablet battery"),
constructor : example_constructor,
destructor : example_destructor,
config : config,
save : save_config,
panel_configuration_changed : NULL
/* panel is a pointer to the panel and
settings is a pointer to the configuration data
since we don't use it, we'll make sure it doesn't
give us an error at compilation time */
(void)panel;
(void)settings;
// make a label out of the hostname
char cIdBuf[LabelSize+1] = {'\0'};
FILE *fp;
fp = fopen("/etc/hostname", "r");
fgets(cIdBuf, LabelSize, fp);
fclose(fp);
// create a label widget instance
GtkWidget *pLabel = gtk_label_new(cIdBuf);
// set the label to be visible
gtk_widget_show(pLabel);
// need to create a container to be able to set a border
GtkWidget *p = gtk_event_box_new();
// our widget doesn't have a window...
// it is usually illegal to call gtk_widget_set_has_window() from application but for GtkEventBox it doesn't hurt
gtk_widget_set_has_window(p, FALSE);
// set border width
gtk_container_set_border_width(GTK_CONTAINER(p), 1);
// add the label to the container
gtk_container_add(GTK_CONTAINER(p), pLabel);
// set the size we want
// gtk_widget_set_size_request(p, 100, 25);
// success!!!
return p;
}
FM_DEFINE_MODULE(lxpanel_gtk, test)
/* Plugin descriptor. */
LXPanelPluginInit fm_module_init_lxpanel_gtk = {
.name = "CutiePi battery",
.description = "Displays the current state of the CutiePi battery",
.one_per_system = 1,
// assigning our functions to provided pointers.
.new_instance = constructor
};