/*
GKrellMouse- mouse odometer plugin to GKrellM
Copyright (C) 2001 paul cannon
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 the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
To contact the author try:
<paul@cannon.cs.usu.edu>
-------------------------------------------------------
* juicy bits taken from cpu.c in the gkrellm 1.0.8 source
* incorporates changes by bill wilson for new gkrellm 1.2.0!
*/
#include <gkrellm.h>
#include <math.h>
/* Use the new meter style under GKrellM 1.2.0 and later */
#if ((GKRELLM_VERSION_MAJOR == 1) && (GKRELLM_VERSION_MINOR >= 2))
# define GKRELLM_1_2_0
# define PLUGIN_VER "0.0.2/s2"
#else
# define PLUGIN_VER "0.0.2/s1"
#endif
#define PLUGIN_NAME "GKrellMouse"
#define PLUGIN_DESC "Mouse odometer for GKrellM"
#define PLUGIN_STYLE PLUGIN_NAME
#define PLUGIN_KEYWORD PLUGIN_NAME
#define PLUGIN_URL "http://ssl.usu.edu/paul/gkrellmouse/"
#define SCALE_MARK 1000
#define SCALE_MAX 2000
static char *gkmouse_info_text[] =
{
"<b>" PLUGIN_NAME " " PLUGIN_VER "\n\n",
"This plugin purports to show you how far your mouse has\n",
"travelled since GKrellM was started.\n\n",
"If you enable the option \"Show total distance on chart\",\n",
"then the total distance in pixels travelled by the mouse\n",
"will be shown. The option can be toggled in the configuration,\n",
"or simply by clicking on the chart. Middle-clicking on the\n",
"chart resets the counter.\n\n",
"The total distance is ",
"<b>not",
" saved when GKrellM quits.\n\n",
"For more info, see ",
"<b> " PLUGIN_URL
};
static gchar *gkmouse_about_text = _(
PLUGIN_NAME " " PLUGIN_VER
"\n" PLUGIN_DESC
"\n\nCopyright (C) 2001 paul cannon\n"
"paul@cannon.cs.usu.edu\n"
"space software lab/utah state university\n\n"
PLUGIN_NAME " comes with ABSOLUTELY NO WARRANTY;\n"
"see the file COPYING for details.\n\n"
PLUGIN_URL );
static Display *xdisp;
static Window xwin;
static gint style_id;
static Chart *cp = NULL;
static GtkWidget *infobutton = NULL;
#ifdef GKRELLM_1_2_0
static Monitor *monitor;
static ChartConfig *chart_config;
#else
static TextStyle *ts = NULL;
static gint ascent;
#endif
static gboolean draw_extra_info = TRUE;
static gulong total = 0;
static gulong sqr (gulong i)
{
return i * i;
}
static gulong get_travelled_distance (int prime)
{
static int lastx, lasty;
int curx, cury;
int childx, childy, mask_ret;
Window root_ret, child_ret;
gulong distance;
XQueryPointer (xdisp, xwin, &root_ret, &child_ret, &curx, &cury,
&childx, &childy, &mask_ret);
if (prime)
distance = 0;
else
distance = (gulong) sqrt ((double)(sqr (curx - lastx) + sqr (cury - lasty)));
lastx = curx;
lasty = cury;
return distance;
}
static void draw_chart_extra (Chart *cp, gulong n)
{
char buf[10];
if (!cp)
return;
snprintf (buf, sizeof (buf), "%ld", n);
#ifdef GKRELLM_1_2_0
gkrellm_draw_chart_text (cp, style_id, buf);
#else
gkrellm_draw_chart_label (cp, ts, 4, ascent, buf);
#endif
}
static void draw_chart (Chart *cp)
{
#ifdef GKRELLM_1_2_0
gkrellm_draw_chartdata (cp);
if (draw_extra_info)
draw_chart_extra (cp, total);
gkrellm_draw_chart_to_screen(cp);
#else
gkrellm_draw_chart (cp);
if (draw_extra_info)
draw_chart_extra (cp, total);
#endif
}
static void gkmouse_update_plugin ()
{
gulong distance;
Krell *krell;
distance = get_travelled_distance (FALSE);
total += distance;
if (GK.second_tick)
{
#ifdef GKRELLM_1_2_0
gkrellm_store_chartdata (cp, 0, total, 0);
#else
gkrellm_store_chart_data (cp, total, 0, 0);
#endif
draw_chart(cp);
}
krell = KRELL (cp->panel);
#ifdef GKRELLM_1_2_0
gkrellm_update_krell (cp->panel, krell, distance);
gkrellm_draw_panel_layers (cp->panel);
#else
krell->previous = 0;
gkrellm_update_krell (cp->panel, krell, distance);
gkrellm_draw_layers (cp->panel);
#endif
}
static gint gkmouse_clicked (GtkWidget *widget, GdkEventButton *event)
{
switch (event->button)
{
case 1:
draw_extra_info = !draw_extra_info;
gkrellm_config_modified ();
break;
case 2:
total = 0;
break;
#ifdef GKRELLM_1_2_0
case 3:
gkrellm_chartconfig_window_create(cp);
break;
#endif
}
return TRUE;
}
static gint gkmouse_expose_event (GtkWidget *widget, GdkEventExpose *ev)
{
GdkPixmap *pixmap = NULL;
if (widget == cp->drawing_area)
pixmap = cp->pixmap;
else if (widget == cp->panel->drawing_area)
pixmap = cp->panel->pixmap;
if (pixmap)
{
gdk_draw_pixmap (widget->window, GK.draw1_GC, pixmap,
ev->area.x, ev->area.y, ev->area.x, ev->area.y,
ev->area.width, ev->area.height);
}
return FALSE;
}
static void gkmouse_create_plugin (GtkWidget *vbox, gint first_create)
{
Style *style;
Krell *k;
if (first_create)
{
cp = gkrellm_chart_new0 ();
cp->panel = gkrellm_panel_new0 ();
#ifndef GKRELLM_1_2_0
cp->name = g_strdup (_("Mouse"));
ts = gkrellm_chart_textstyle (style_id);
ascent = gdk_char_height (ts->font, '8') + 4;
}
else
{
gkrellm_destroy_decal_list (cp->panel);
gkrellm_destroy_krell_list (cp->panel);
#endif
}
xdisp = GDK_WINDOW_XDISPLAY (vbox->window);
xwin = GDK_WINDOW_XWINDOW (vbox->window);
get_travelled_distance (TRUE);
style = gkrellm_panel_style (style_id);
#ifdef GKRELLM_1_2_0
gkrellm_chart_create (vbox, monitor, cp, &chart_config);
gkrellm_add_default_chartdata (cp, _("Mouse Distance"));
gkrellm_set_chartconfig_grid_resolution (chart_config,
SCALE_MAX / FULL_SCALE_GRIDS);
gkrellm_set_chartconfig_auto_grid_resolution (chart_config, FALSE);
gkrellm_alloc_chartdata (cp);
gkrellm_set_draw_chart_function (cp, draw_chart, cp);
k = gkrellm_create_krell (cp->panel,
gkrellm_krell_panel_image (style_id),
style);
gkrellm_monotonic_krell_values (k, FALSE);
gkrellm_set_krell_full_scale (k, SCALE_MARK, 1);
gkrellm_panel_configure (cp->panel, g_strdup (_("Mouse")), style);
gkrellm_panel_create (vbox, monitor, cp->panel);
#else
k = gkrellm_create_krell (cp->panel,
gkrellm_krell_panel_image (style_id),
style);
k->full_scale = SCALE_MARK;
cp->scale_min = SCALE_MAX / FULL_SCALE_GRIDS;
cp->scale_max = 0; /* Force a chart rescale */
cp->h = gkrellm_chart_height (MON_CPU);
gkrellm_create_chart (vbox, cp, style_id);
cp->panel->textstyle = gkrellm_panel_textstyle (style_id);
gkrellm_configure_panel (cp->panel, g_strdup (cp->name), style);
gkrellm_create_panel (vbox, cp->panel, gkrellm_bg_panel_image (style_id));
gkrellm_monitor_height_adjust (cp->h + cp->panel->h);
gkrellm_alloc_chart_data (cp);
#endif
if (first_create)
{
gtk_signal_connect (GTK_OBJECT (cp->drawing_area), "expose_event",
GTK_SIGNAL_FUNC (gkmouse_expose_event), NULL);
gtk_signal_connect (GTK_OBJECT (cp->panel->drawing_area), "expose_event",
GTK_SIGNAL_FUNC (gkmouse_expose_event), NULL);
gtk_signal_connect (GTK_OBJECT (cp->drawing_area), "button_press_event",
GTK_SIGNAL_FUNC (gkmouse_clicked), NULL);
}
}
static void gkmouse_save_config (FILE *f)
{
fprintf (f, "%s info %d\n", PLUGIN_KEYWORD, draw_extra_info);
#ifdef GKRELLM_1_2_0
gkrellm_save_chartconfig (f, chart_config, PLUGIN_KEYWORD, NULL);
#endif
}
static void gkmouse_load_config (gchar *arg)
{
gchar config_keyword[32], config_data[CFG_BUFSIZE];
if (sscanf(arg, "%31s %[^\n]", config_keyword, config_data) != 2)
return;
if (!strcmp (config_keyword, "info"))
sscanf(config_data, "%d", &draw_extra_info);
#ifdef GKRELLM_1_2_0
else if (!strcmp (config_keyword, GKRELLM_CHARTCONFIG_KEYWORD))
gkrellm_load_chartconfig (&chart_config, config_data, 1);
#endif
}
static void gkmouse_apply_config ()
{
draw_extra_info = GTK_TOGGLE_BUTTON (infobutton)->active;
}
static void gkmouse_create_tabs (GtkWidget *tab_vbox)
{
GtkWidget *tabs;
GtkWidget *vbox, *scrolled, *text, *about;
tabs = gtk_notebook_new ();
gtk_notebook_set_tab_pos (GTK_NOTEBOOK (tabs), GTK_POS_TOP);
gtk_box_pack_start (GTK_BOX (tab_vbox), tabs, TRUE, TRUE, 0);
/* Options tab */
vbox = gkrellm_create_tab (tabs, _("Options"));
gkrellm_check_button (vbox, &infobutton, draw_extra_info, TRUE, 0,
_("Show total distance on chart"));
/* Info tab */
vbox = gkrellm_create_tab (tabs, _("Info"));
scrolled = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_box_pack_start (GTK_BOX (vbox), scrolled, TRUE, TRUE, 0);
text = gtk_text_new (NULL, NULL);
gkrellm_add_info_text (text, gkmouse_info_text,
sizeof (gkmouse_info_text) / sizeof (gchar *));
gtk_text_set_editable (GTK_TEXT (text), FALSE);
gtk_container_add (GTK_CONTAINER (scrolled), text);
/* About tab */
vbox = gkrellm_create_tab (tabs, _("About"));
about = gtk_label_new (gkmouse_about_text);
gtk_box_pack_start (GTK_BOX (vbox), about, TRUE, TRUE, 0);
}
static Monitor gkmouse_plugin_mon =
{
PLUGIN_NAME, /* Name, for config tab. */
0, /* Id, 0 if a plugin */
gkmouse_create_plugin, /* The create function */
gkmouse_update_plugin, /* The update function */
gkmouse_create_tabs, /* The config tab create function */
gkmouse_apply_config, /* Apply the config function */
gkmouse_save_config, /* Save user config */
gkmouse_load_config, /* Load user config */
PLUGIN_KEYWORD, /* config keyword */
NULL, /* Undefined 2 */
NULL, /* Undefined 1 */
NULL, /* private */
MON_INET | MON_INSERT_AFTER, /* Insert plugin before this monitor */
NULL, /* Handle if a plugin, filled in by GKrellM */
NULL /* Path if a plugin, filled in by GKrellM */
};
Monitor *init_plugin ()
{
#ifdef GKRELLM_1_2_0
monitor = &gkmouse_plugin_mon;
#endif
style_id = gkrellm_add_chart_style (&gkmouse_plugin_mon, PLUGIN_NAME);
return &gkmouse_plugin_mon;
}
syntax highlighted by Code2HTML, v. 0.9.1