/* iwqual.c - requires GKrellM 2.0.0 or better | | This is based on demo1.c. | | This looks up the wireless signal quality in /proc/net/wireless and | display it on a chart. */ #if !defined(WIN32) #include #else #include #include #endif #include static int get_link_quality(void) { #if 0 FILE *g = popen("/sbin/iwconfig eth1", "r"); char line[160]; int result = -1; if (g) { while (fgets(line, sizeof(line), g)) { sscanf(line, " Link Quality=%d", &result); } pclose(g); } return result; #endif FILE *f = fopen("/proc/net/wireless", "r"); char line[160]; int result = -1; int index, i; if (f && fgets(line, sizeof(line), f) && fgets(line, sizeof(line), f)) { for (index = 0; line[index] != 0; index++) { if (line[index+0] == 'l' && line[index+1] == 'i' && line[index+2] == 'n' && line[index+3] == 'k') break; } if (line[index] != 0 && fgets(line, sizeof(line), f)) { for (i=0; iwindow, widget->style->fg_gc[GTK_WIDGET_STATE (widget)], chart->pixmap, ev->area.x, ev->area.y, ev->area.x, ev->area.y, ev->area.width, ev->area.height); return FALSE; } static void cb_chart_click(GtkWidget *widget, GdkEventButton *event, gpointer data) { if (event->button == 3) gkrellm_chartconfig_window_create(chart); } /* This routine is needed only if you want to draw something extra on the | chart besides the raw data and grid lines that are drawn automatically | in gkrellm_draw_chartdata(). If you have nothing extra to draw, don't | connect to this routine with gkrellm_set_draw_chart_function(). */ static void draw_plugin_chart(void) { char text[32]; gkrellm_draw_chartdata(chart); sprintf(text, "\\n\\f\\c%d%%", link_quality); gkrellm_draw_chart_text(chart, plugin_style_id, text); gkrellm_draw_chart_to_screen(chart); } static void update_plugin(void) { gboolean chart_is_visible; if (pGK->second_tick) { if (--second_ticks <= 0) { second_ticks = 2; /* only update every 2 seconds */ link_quality = get_link_quality(); } gkrellm_store_chartdata(chart, 100, link_quality >= 0 ? link_quality : 0); chart_is_visible = gkrellm_is_chart_visible(chart); if (link_quality >= 0) { if (!chart_is_visible) gkrellm_chart_show(chart, TRUE); draw_plugin_chart(); } else { if (chart_is_visible) gkrellm_chart_hide(chart, TRUE); } } } static void setup_iw_scaling(GkrellmChartconfig *cf) { gint grids; grids = gkrellm_get_chartconfig_fixed_grids(cf); if (!grids) grids = FULL_SCALE_GRIDS; gkrellm_set_chartconfig_grid_resolution(cf, 100 / grids); } static void create_plugin(GtkWidget *vbox, gint first_create) { int previous_chart_config = chart_config != NULL; if (first_create) chart = gkrellm_chart_new0(); /* Chart heights initially default to 40 if this next call is not made. | Once the user configs the chart, this call has no effect. Make this | call before the chart is created. */ gkrellm_set_chart_height_default(chart, 23); /* The address of a ChartConfig struct pointer must be passed to the create | function. If the pointer is NULL, a ChartConfig struct will be | allocated and the pointer updated. But usually the pointer will not | be NULL if you load a saved chartconfig in load_plugin_config(). */ gkrellm_chart_create(vbox, mon, chart, &chart_config); /* Set "Number of Grids" to 1 by default. */ if (!previous_chart_config) gkrellm_set_chartconfig_fixed_grids(chart_config, 1); /* This chart will have only one data set drawn on it, but you may add | multiple data sets to be drawn. */ plugin_cd = gkrellm_add_default_chartdata(chart, "Wireless quality"); /* If the data to be charted monotonically increases, then don't make | this next call. The default is data is assumed to monotonically | increase and difference values are automatically charted. */ gkrellm_monotonic_chartdata(plugin_cd, FALSE); /* Setting the draw style default also has no effect once the user has | configured the chart. | If you set the CHARTDATA_ALLOW_HIDE flag, there will be a button in | the chart config window allowing the user to hide the data. | Make these calls after adding a chartdata. */ gkrellm_set_chartdata_draw_style_default(plugin_cd, CHARTDATA_LINE); gkrellm_set_chartdata_flags(plugin_cd, CHARTDATA_ALLOW_HIDE); /* Set your own chart draw function if you have extra info to draw */ gkrellm_set_draw_chart_function(chart, draw_plugin_chart, NULL); /* Stuff copied from cpu.c */ gkrellm_set_chartconfig_auto_grid_resolution(chart_config, FALSE); gkrellm_chartconfig_fixed_grids_connect(chart_config, setup_iw_scaling, NULL); setup_iw_scaling(chart_config); gkrellm_alloc_chartdata(chart); if (first_create) { g_signal_connect(G_OBJECT(chart->drawing_area), "expose_event", G_CALLBACK(chart_expose_event), NULL); g_signal_connect(G_OBJECT(chart->drawing_area), "button_press_event", G_CALLBACK(cb_chart_click), NULL); } else draw_plugin_chart(); } /* ---- User Config ---- */ /* Save any configuration data we have in config lines in the format: | MONITOR_CONFIG_KEYWORD config_keyword data */ static void save_plugin_config(FILE *f) { /* Save any chart config changes the user has made. */ gkrellm_save_chartconfig(f, chart_config, MONITOR_CONFIG_KEYWORD, NULL); } /* When GKrellM is started up, load_plugin_config() is called if any | config lines for this plugin are found. The lines must have been | saved by save_plugin_config(). gkrellm_load_chartconfig() must | have the address of a ChartConfig struct pointer. At this point, the | pointer is almost always NULL and the function will allocate a | ChartConfig struct and update the pointer. The struct will be | initialized with values from the config line. */ static void load_plugin_config(gchar *config_line) { gchar config_keyword[32], config_data[CFG_BUFSIZE]; gint n; if ((n = sscanf(config_line, "%31s %[^\n]", config_keyword, config_data)) != 2) return; if (!strcmp(config_keyword, GKRELLM_CHARTCONFIG_KEYWORD)) gkrellm_load_chartconfig(&chart_config, config_data, 1); } static GkrellmMonitor plugin_mon = { CONFIG_NAME, /* Name, for config tab. */ 0, /* Id, 0 if a plugin */ create_plugin, /* The create_plugin() function */ update_plugin, /* The update_plugin() function */ NULL, /* The create_plugin_tab() config function */ NULL, /* The apply_plugin_config() function */ save_plugin_config, /* The save_plugin_config() function */ load_plugin_config, /* The load_plugin_config() function */ MONITOR_CONFIG_KEYWORD, /* config keyword */ NULL, /* Undefined 2 */ NULL, /* Undefined 1 */ NULL, /* private */ PLUGIN_PLACEMENT, NULL, /* Handle if a plugin, filled in by GKrellM */ NULL /* path if a plugin, filled in by GKrellM */ }; #if defined(WIN32) __declspec(dllexport) GkrellmMonitor * gkrellm_init_plugin(win32_plugin_callbacks* calls) #else GkrellmMonitor * gkrellm_init_plugin(void) #endif { /* This is a good place to initialize plugin variables, but you should | not do anything that assumes your plugin will actually be running. | This routine will be called for all plugins GKrellM finds, but if | the user does not enable the plugin, nothing else will be called. */ pGK = gkrellm_ticks(); plugin_style_id = gkrellm_add_chart_style(&plugin_mon, STYLE_NAME); mon = &plugin_mon; return &plugin_mon; }