📄 net.c
字号:
/* If sysdep code is not reporting route up/down events, then | gkrellm_net_assign_data() sets a net as up if data is assigned for it. | So, once a second, compare a net up state before a data read to the | state after a read and use that to internally generate up/down events. | If sysdep code assigns data even if a net is not routed, then there | will be no automatic chart toggling and charts will always be visible | if enabled. */ if (GK.second_tick) { if (!net_use_routed) { for (list = net_mon_list; list; list = list->next) { net = (NetMon *) list->data; net->up_prev = net->up; net->up = FALSE; } } else (*check_net_routes)(); } (*read_net_data)(); if (GK.second_tick && !net_use_routed) { for (list = net_mon_list; list; list = list->next) { net = (NetMon *) list->data; if (net->up && !net->up_prev) net->up_event = TRUE; else if (!net->up && net->up_prev) net->down_event = TRUE; } } for (list = net_mon_list; list; list = list->next) { net = (NetMon *) list->data; if (!net->chart) continue; p = net->chart->panel; if (net->up || net->force_up) { if (net->rx > net->rx_old) draw_led(net, RX_LED, RX_ON); else draw_led(net, RX_LED, RX_OFF); if (net->tx > net->tx_old) draw_led(net, TX_LED, TX_ON); else draw_led(net, TX_LED, TX_OFF); } net->rx_old = net->rx; net->tx_old = net->tx; if (GK.second_tick) { gkrellm_store_chartdata(net->chart, 0, net->tx, net->rx); net->rx_current = gkrellm_get_current_chartdata(net->rx_cd); net->tx_current = gkrellm_get_current_chartdata(net->tx_cd); rxd = (gdouble) net->rx_current; txd = (gdouble) net->tx_current; net->rx_totalA += rxd; net->tx_totalA += txd; net->rx_totalB += rxd; net->tx_totalB += txd; if (GK.day_tick) { tm = gkrellm_get_current_time(); net_stats_shift_down(&net->day_stats[0], N_DAY_STATS, DAY_STAT); if (tm->tm_wday == 0) net_stats_shift_down(&net->week_stats[0], N_WEEK_STATS, WEEK_STAT); if (net_accounting_month_new(tm)) net_stats_shift_down(&net->month_stats[0], N_MONTH_STATS, MONTH_STAT); } net->day_stats[0].rx += rxd; net->week_stats[0].rx += rxd; net->month_stats[0].rx += rxd; net->day_stats[0].tx += txd; net->week_stats[0].tx += txd; net->month_stats[0].tx += txd; if (net->alert) { bytes = 0; if (net->alert_uses_rx) bytes += net->rx_current; if (net->alert_uses_tx) bytes += net->tx_current; gkrellm_check_alert(net->alert, bytes); } gkrellm_panel_label_on_top_of_decals(p, gkrellm_alert_decal_visible(net->alert)); refresh_net_chart(net); } gkrellm_update_krell(p, KRELL(p), net->tx + net->rx); gkrellm_draw_panel_layers(p); } if (GK.second_tick) { update_timer_button_monitor(); timed_net_visibility(); if (net_timed && !net_timed->up && !net_timed->force_up) { draw_led(net_timed, RX_LED, RX_OFF); draw_led(net_timed, TX_LED, TX_OFF); } for (list = net_mon_list; list; list = list->next) { net = (NetMon *) list->data; if (!net->locked) { if (net->up_event && !net->chart && net->enabled) { create_net_monitor(dynamic_net_vbox, net, TRUE); gkrellm_pack_side_frames(); } else if (net->down_event && net->chart) { destroy_chart(net); gkrellm_pack_side_frames(); } } net->up_event = net->down_event = FALSE; } } if (GK.hour_tick) { tm = gkrellm_get_current_time(); if ((tm->tm_hour % 6) == 0) gkrellm_net_save_data(); } } /* A timed interface has its chart locked and is forced enabled. */static voidcreate_timed_monitor(void) { GList *list; NetMon *net; TimerType *tt; if (_GK.client_mode) return; net_timed = NULL; timer_button_type = TIMER_TYPE_NONE; time(&net_timer0); if (!*timer_button_iface || !strcmp(timer_button_iface, "none")) return; /* Making a timed mon out of one that is already up? It needs to be | moved to a different vbox, so destroy and create dance. */ for (list = net_mon_list; list; list = list->next) { net = (NetMon *) list->data; if (!strcmp(net->name, timer_button_iface)) { destroy_chart(net); break; } } if ((net = lookup_net(timer_button_iface)) == NULL) net = new_net(timer_button_iface); net->enabled = TRUE; net->locked = TRUE; create_net_monitor(net_vbox, net, TRUE); if (!net->up) gkrellm_chart_hide(net->chart, FALSE); /* Don't hide its panel */ net_timed = net; for (list = timer_defaults_list; list; list = list->next) { tt = (TimerType *) list->data; if (!strncmp(timer_button_iface, tt->name, strlen(tt->name) - 1)) { timer_button_type = tt->type; break; } } get_connect_time(); }static voidload_net_extra_piximages(void) { gchar **xpm; gint w, h; /* Check for theme_dir/net/decal_net_leds.png. */ gkrellm_load_piximage("decal_net_leds", decal_net_leds_xpm, &decal_net_led_piximage, NET_STYLE_NAME); w = gdk_pixbuf_get_width(decal_net_led_piximage->pixbuf); w *= gkrellm_get_theme_scale(); h = gdk_pixbuf_get_height(decal_net_led_piximage->pixbuf) / N_LEDS; h *= gkrellm_get_theme_scale(); gkrellm_scale_piximage_to_pixmap(decal_net_led_piximage, &decal_net_led_pixmap, &decal_net_led_mask, w, h * N_LEDS); /* Check for theme_dir/net/decal_timer_button.png */ gkrellm_load_piximage("decal_timer_button", decal_timer_button_xpm, &decal_timer_button_piximage, TIMER_STYLE_NAME); h = gdk_pixbuf_get_height(decal_timer_button_piximage->pixbuf) / N_TB_DECALS; h *= gkrellm_get_theme_scale(); gkrellm_scale_piximage_to_pixmap(decal_timer_button_piximage, &decal_timer_button_pixmap, &decal_timer_button_mask, -1, h * N_TB_DECALS); /* Here is where I define the net timer panel theme extensions. I ask | for a theme extension image: | THEME_DIR/timer/bg_timer.png | and for a border for it from the gkrellmrc in the format: | set_piximage_border timer_bg_timer l,r,t,b | There is no default for bg_timer, ie it may end up being NULL. */ if (!bg_timer_style) /* Used just for the bg_timer border */ bg_timer_style = gkrellm_style_new0(); if (bg_timer_piximage) gkrellm_destroy_piximage(bg_timer_piximage); bg_timer_piximage = NULL; xpm = (gkrellm_using_default_theme()) ? bg_timer_xpm : NULL; gkrellm_load_piximage("bg_timer", xpm, &bg_timer_piximage, TIMER_STYLE_NAME); gkrellm_set_gkrellmrc_piximage_border("timer_bg_timer", bg_timer_piximage, bg_timer_style); }static voidnet_spacer_visibility(void) { GList *list; gboolean enabled = FALSE; for (list = net_mon_list; list; list = list->next) if (((NetMon *) list->data)->enabled) enabled = TRUE; if (timer_button_enabled || enabled) gkrellm_spacers_show(mon_net); else gkrellm_spacers_hide(mon_net); }static voidcreate_net(GtkWidget *vbox, gint first_create) { GList *list; NetMon *net; load_net_extra_piximages(); /* Make a couple of vboxes here so I can control the net layout. | I want interface linked to the timer button to go last. */ if (first_create) { dynamic_net_vbox = gtk_vbox_new(FALSE, 0); gtk_box_pack_start(GTK_BOX(vbox), dynamic_net_vbox, FALSE, FALSE, 0); gtk_widget_show(dynamic_net_vbox); net_vbox = gtk_vbox_new(FALSE, 0); gtk_box_pack_start(GTK_BOX(vbox), net_vbox, FALSE, FALSE, 0); gtk_widget_show(net_vbox); (*(read_net_data))(); /* need net up states */ create_timed_monitor(); for (list = net_mon_list; list; list = list->next) { net = (NetMon *) list->data; if (net == net_timed) continue; if ((net->up || net->force_up) && net->enabled) create_net_monitor(dynamic_net_vbox, net, first_create); } load_net_data(); } else { /* Some decals don't live in a panel, they will be drawn onto | charts when needed. So, must destroy them at create events (not | done automatically in panel lists) and then recreate them with NULL | panel and style pointers. */ gkrellm_destroy_decal(decal_totalA); gkrellm_destroy_decal(decal_totalB); gkrellm_destroy_decal(decal_reset); gkrellm_destroy_decal(decal_stats); decal_totalA = decal_totalB = decal_reset = decal_stats = NULL; for (list = net_mon_list; list; list = list->next) { net = (NetMon *) list->data; if (net->chart) create_net_monitor(NULL, net, 0); } } decal_totalA = gkrellm_create_decal_pixmap(NULL, gkrellm_decal_misc_pixmap(), gkrellm_decal_misc_mask(), N_MISC_DECALS, NULL, 0, 0); decal_totalB = gkrellm_create_decal_pixmap(NULL, gkrellm_decal_misc_pixmap(), gkrellm_decal_misc_mask(), N_MISC_DECALS, NULL, 0, 0); decal_reset = gkrellm_create_decal_pixmap(NULL, gkrellm_decal_misc_pixmap(), gkrellm_decal_misc_mask(), N_MISC_DECALS, NULL, 0, 0); decal_stats = gkrellm_create_decal_pixmap(NULL, gkrellm_decal_misc_pixmap(), gkrellm_decal_misc_mask(), N_MISC_DECALS, NULL, 0, 0); net_spacer_visibility(); }static voidcreate_timer(GtkWidget *vbox, gint first_create) { timer_vbox = vbox; create_net_timer(timer_vbox, first_create); ascent = 0; ascent_alt = 0; if (first_create) draw_timer(timer_panel, (int) (time(0) - net_timer0), 1); else draw_timer(timer_panel, last_time , 1); gkrellm_draw_panel_layers(timer_panel); net_timer_visibility(); }#define NET_CONFIG_KEYWORD "net"static voidcb_alert_trigger(GkrellmAlert *alert, NetMon *net) { /* Full panel alert, default decal. */ alert->panel = net->chart->panel; }static voidcreate_alert(NetMon *net) { net->alert = gkrellm_alert_create(NULL, net->name, _("Bytes per second"), TRUE, FALSE, TRUE, 1e10, 1000, 1000, 10000, 0); gkrellm_alert_delay_config(net->alert, 1, 60 * 60, 0); gkrellm_alert_trigger_connect(net->alert, cb_alert_trigger, net); gkrellm_alert_config_connect(net->alert, cb_alert_config, net); gkrellm_alert_config_create_connect(net->alert, cb_alert_config_create, net); gkrellm_alert_command_process_connect(net->alert, cb_command_process, net); }static voidsave_net_config(FILE *f) { GList *list; NetMon *net; for (list = net_mon_list; list; list = list->next) { net = (NetMon *) list->data; fprintf(f, "%s enables %s %d %d %d\n", NET_CONFIG_KEYWORD, net->name, net->enabled, net->chart_labels, net->force_up); gkrellm_save_chartconfig(f, net->chart_config, NET_CONFIG_KEYWORD, net->name); if (*net->label) fprintf(f, "%s label %s %s\n", NET_CONFIG_KEYWORD, net->name, net->label); if (*(net->launch.command)) fprintf(f, "%s launch %s %s\n", NET_CONFIG_KEYWORD, net->name, net->launch.command); if (*(net->launch.tooltip_comment)) fprintf(f, "%s tooltip %s %s\n", NET_CONFIG_KEYWORD, net->name, net->launch.tooltip_comment); if (net->alert) { gkrellm_save_alertconfig(f, net->alert, NET_CONFIG_KEYWORD, net->name); fprintf(f, "%s extra_alert_config %s %d %d\n", NET_CONFIG_KEYWORD, net->name, net->alert_uses_rx, net->alert_uses_tx); } } if (!_GK.client_mode || timer_button_type != TIMER_TYPE_SERVER) fprintf(f, "%s timer_enabled %d\n", NET_CONFIG_KEYWORD, timer_button_enabled); fprintf(f, "%s timer_seconds %d\n", NET_CONFIG_KEYWORD, timer_seconds); if (!_GK.client_mode) fprintf(f, "%s timer_iface %s\n", NET_CONFIG_KEYWORD, timer_button_iface); fprintf(f, "%s timer_on %s\n", NET_CONFIG_KEYWORD, timer_on_command); fprintf(f, "%s timer_off %s\n", NET_CONFIG_KEYWORD, timer_off_command); fprintf(f, "%s text_format %s\n", NET_CONFIG_KEYWORD, text_format); fprintf(f, "%s reset_mday %d\n", NET_CONFIG_KEYWORD, reset_mday); fprintf(f, "%s net_stats_window_height %d\n", NET_CONFIG_KEYWORD, net_stats_window_height); }static voidload_net_config(gchar *arg) { NetMon *net; gchar config[32], name[32]; gchar item[CFG_BUFSIZE], item1[CFG_BUFSIZE]; gboolean enable = TRUE, ch_labels = TRUE, force = FALSE; gint n; n = sscanf(arg, "%31s %[^\n]", config, item); if (n != 2) return; if (!_GK.client_mode || timer_button_type != TIMER_TYPE_SERVER) if (!strcmp(config, "timer_enabled") && ! _GK.demo) sscanf(item, "%d", &timer_button_enabled); if (!strcmp(config, "timer_seconds")) sscanf(item, "%d", &timer_seconds); else if (!strcmp(config, "reset_mday")) sscanf(item, "%d", &reset_mday); else if (!strcmp(config, "net_stats_window_height")) sscanf(item, "%d", &net_stats_window_height); else if (!strcmp(config, "timer_iface") && !_GK.client_mode) { if (_GK.demo && !strcmp(item, "none")) gkrellm_dup_string(&timer_button_iface, "ppp0"); else gkrellm_dup_string(&timer_button_iface, item); } else if (!strcmp(config, "timer_on")) gkrellm_dup_string(&timer_on_command, item); else if (!strcmp(config, "timer_off")) gkrellm_dup_string(&timer_off_command, item); else if (!strcmp(config, "text_format")) gkrellm_locale_dup_string(&text_format, item, &text_format_locale); else if (sscanf(item, "%31s %[^\n]", name, item1) == 2) { if (!strcmp(config, "iface")) /* Hack to get some of 1.0 config */ { /* Can't get resolution, label, launch or tooltip */ sscanf(item1, "%*d %d %*s %d %d", &enable, &ch_labels, &force); strcpy(config, "enables"); sprintf(item1, "%d %d %d", enable, ch_labels, force); } /* Remaining configs will have a net name */ if (!strcmp(config, "enables")) { if ((net = lookup_net(name)) == NULL) net = new_net(name); sscanf(item1, "%d %d %d", &net->enabled, &net->chart_labels, &net->force_up); if (!net_use_routed) net->force_up = FALSE; return; } if ((net = lookup_net(name)) == NULL) return; if (!strcmp(config, GKRELLM_CHARTCONFIG_KEYWORD)) gkrellm_load_chartconfig(&net->chart_config, item1, 2); else if (!strcmp(config, GKRELLM_ALERTCONFIG_KEYWORD)) { if (!net->alert) create_alert(net); gkrellm_load_alertconfig(&net->alert, item1); } else if (!strcmp(config, "extra_alert_config")) sscanf(item1, "%d %d", &net->alert_uses_rx, &net->alert_uses_tx); else if (!strcmp(config, "label")) gkrellm_dup_string(&net->label, item1); else if (!strcmp(config, "launch")) gkrellm_dup_string(&net->launch.command, item1); else if (!strcmp(config, "tooltip")) gkrellm_dup_string(&net->launch.tooltip_comment, item1); } }/* -------------- User config interface --------------------- */#define STEP 1static GtkWidget *pon_entry, *poff_entry, *timer_iface_combo, *text_format_combo;static GtkWid
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -