static J4statusPluginContext * _j4status_i3bar_output_init(J4statusCoreInterface *core) { J4statusPluginContext *context; context = g_new0(J4statusPluginContext, 1); context->core = core; context->colours.no_state = NULL; context->colours.unavailable = g_strdup("#0000FF"); context->colours.bad = g_strdup("#FF0000"); context->colours.average = g_strdup("#FFFF00"); context->colours.good = g_strdup("#00FF00"); GKeyFile *key_file; key_file = j4status_config_get_key_file("i3bar"); if ( key_file != NULL ) { _j4status_i3bar_output_update_colour(&context->colours.no_state, key_file, "NoStateColour"); _j4status_i3bar_output_update_colour(&context->colours.unavailable, key_file, "UnavailableColour"); _j4status_i3bar_output_update_colour(&context->colours.bad, key_file, "BadColour"); _j4status_i3bar_output_update_colour(&context->colours.average, key_file, "AverageColour"); _j4status_i3bar_output_update_colour(&context->colours.good, key_file, "GoodColour"); context->align = g_key_file_get_boolean(key_file, "i3bar", "Align", NULL); context->no_click_events = g_key_file_get_boolean(key_file, "i3bar", "NoClickEvents", NULL); g_key_file_free(key_file); } context->json_handle = yajl_alloc(&_j4status_i3bar_output_click_events_callbacks, NULL, context); return context; }
static J4statusPluginContext * _j4status_mem_init(J4statusCoreInterface *core) { const gchar MEM[]= "Memory"; GKeyFile *key_file = j4status_config_get_key_file(MEM); guint period = 0; guint good_threshold = 0; guint bad_threshold = 0; if (key_file) { period = g_key_file_get_integer(key_file, MEM, "Frequency", NULL); good_threshold = g_key_file_get_integer( key_file, MEM, "GoodThreshold", NULL); bad_threshold = g_key_file_get_integer( key_file, MEM, "BadThreshold", NULL); g_key_file_free(key_file); } J4statusSection *section = j4status_section_new(core); j4status_section_set_name(section, "mem"); if (!j4status_section_insert(section)) { j4status_section_free(section); return NULL; } J4statusPluginContext *context = g_new(J4statusPluginContext, 1); context->section = section; context->good_threshold = good_threshold > 0 ? good_threshold : 50; context->bad_threshold = bad_threshold > context->good_threshold ? bad_threshold : 90; context->period = MAX(period, 1); return context; }
static J4statusPluginContext * _j4status_sensors_init(J4statusCoreInterface *core) { gchar **sensors = NULL; gboolean show_details = FALSE; guint64 interval = 0; if ( sensors_init(NULL) != 0 ) return NULL; GKeyFile *key_file; key_file = j4status_config_get_key_file("Sensors"); if ( key_file != NULL ) { sensors = g_key_file_get_string_list(key_file, "Sensors", "Sensors", NULL, NULL); show_details = g_key_file_get_boolean(key_file, "Sensors", "ShowDetails", NULL); interval = g_key_file_get_uint64(key_file, "Sensors", "Interval", NULL); g_key_file_free(key_file); } J4statusPluginContext *context; context = g_new0(J4statusPluginContext, 1); context->core = core; context->config.show_details = show_details; if ( sensors == NULL ) _j4status_sensors_add_sensors(context, NULL); else { sensors_chip_name chip; gchar **sensor; for ( sensor = sensors ; *sensor != NULL ; ++sensor ) { if ( sensors_parse_chip_name(*sensor, &chip) != 0 ) continue; _j4status_sensors_add_sensors(context, &chip); sensors_free_chip_name(&chip); } } g_strfreev(sensors); if ( context->sections == NULL ) { g_message("Missing configuration: No sensor to monitor, aborting"); _j4status_sensors_uninit(context); return NULL; } g_timeout_add_seconds(MAX(2, interval), _j4status_sensors_update, context); return context; }
static J4statusPluginContext * _j4status_alsa_init(J4statusCoreInterface *core) { GKeyFile *key_file; key_file = j4status_config_get_key_file("ALSA"); if ( key_file == NULL ) return NULL; gchar **cards; cards = g_key_file_get_string_list(key_file, "ALSA", "Cards", NULL, NULL); g_key_file_free(key_file); if ( cards == NULL ) return NULL; GList *sections = NULL; gchar **card; for ( card = cards ; *card != NULL ; ++card ) { J4statusAlsaSection *section; section = _j4status_alsa_section_new(core, *card); if ( section != NULL ) sections = g_list_prepend(sections, section); } g_free(cards); if ( sections == NULL ) return NULL; J4statusPluginContext *context; context = g_new0(J4statusPluginContext, 1); context->sections = sections; return context; }
int main(int argc, char *argv[]) { gboolean print_version = FALSE; gboolean one_shot = FALSE; gchar *output_plugin = NULL; gchar **servers_desc = NULL; gchar **streams_desc = NULL; gchar **input_plugins = NULL; gchar **order = NULL; gchar *config = NULL; int retval = 0; GError *error = NULL; GOptionContext *option_context = NULL; GOptionGroup *option_group; #if DEBUG g_setenv("G_MESSAGES_DEBUG", "all", FALSE); #endif /* ! DEBUG */ setlocale(LC_ALL, ""); #ifdef ENABLE_NLS bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR); bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8"); #endif /* ENABLE_NLS */ #if DEBUG const gchar *debug_log_filename = g_getenv("J4STATUS_DEBUG_LOG_FILENAME"); GDataOutputStream *debug_stream = NULL; if ( debug_log_filename != NULL ) { GFile *debug_log; debug_log = g_file_new_for_path(debug_log_filename); GError *error = NULL; GFileOutputStream *debug_log_stream; debug_log_stream = g_file_append_to(debug_log, G_FILE_CREATE_NONE, NULL, &error); if ( debug_log_stream == NULL ) { g_warning("Couldn't open debug log file: %s", error->message); g_clear_error(&error); } else { debug_stream = g_data_output_stream_new(G_OUTPUT_STREAM(debug_log_stream)); g_object_unref(debug_log_stream); g_log_set_default_handler(_j4status_core_debug_log_handler, debug_stream); } g_object_unref(debug_log); } #endif /* DEBUG */ GOptionEntry entries[] = { { "output", 'o', 0, G_OPTION_ARG_STRING, &output_plugin, "Output plugin to use", "<plugin>" }, { "listen", 'l', 0, G_OPTION_ARG_STRING_ARRAY, &servers_desc, "Socket to listen on, will create a stream on connection (may be specified several times)", "<listen description>" }, { "stream", 't', 0, G_OPTION_ARG_STRING_ARRAY, &streams_desc, "Stream to read from/write to (may be specified several times)", "<stream description>" }, { "input", 'i', 0, G_OPTION_ARG_STRING_ARRAY, &input_plugins, "Input plugins to use (may be specified several times)", "<plugin>" }, { "order", 'O', 0, G_OPTION_ARG_STRING_ARRAY, &order, "Order of sections, specified once a section (see man)", "<section id>" }, { "one-shot", '1', 0, G_OPTION_ARG_NONE, &one_shot, "Tells j4status to stop right after starting", NULL }, { "config", 'c', 0, G_OPTION_ARG_STRING, &config, "Config file to use", "<config>" }, { "version", 'V', 0, G_OPTION_ARG_NONE, &print_version, "Print version", NULL }, { NULL } }; option_context = g_option_context_new("- status line generator"); option_group = g_option_group_new(NULL, NULL, NULL, NULL, NULL); g_option_group_set_translation_domain(option_group, GETTEXT_PACKAGE); g_option_group_add_entries(option_group, entries); g_option_context_set_main_group(option_context, option_group); if ( ! g_option_context_parse(option_context, &argc, &argv, &error) ) { g_warning("Option parsing failed: %s\n", error->message); g_clear_error(&error); retval = 1; goto end; } g_option_context_free(option_context); if ( print_version ) { g_fprintf(stdout, PACKAGE_NAME " " PACKAGE_VERSION "\n"); goto end; } if ( config != NULL ) { g_setenv("J4STATUS_CONFIG_FILE", config, TRUE); g_free(config); } GKeyFile *key_file; key_file = j4status_config_get_key_file("Plugins"); if ( key_file != NULL ) { if ( output_plugin == NULL ) output_plugin = g_key_file_get_string(key_file, "Plugins", "Output", NULL); if ( input_plugins == NULL ) input_plugins = g_key_file_get_string_list(key_file, "Plugins", "Input", NULL, NULL); if ( order == NULL ) order = g_key_file_get_string_list(key_file, "Plugins", "Order", NULL, NULL); g_key_file_free(key_file); } J4statusCoreContext *context; context = g_new0(J4statusCoreContext, 1); J4statusCoreInterface interface = { .context = context, .add_section = _j4status_core_add_section, .remove_section = _j4status_core_remove_section, .trigger_generate = _j4status_core_trigger_generate, .trigger_action = _j4status_core_trigger_action, }; #ifdef G_OS_UNIX g_unix_signal_add(SIGTERM, _j4status_core_source_quit, context); g_unix_signal_add(SIGINT, _j4status_core_source_quit, context); g_unix_signal_add(SIGUSR1, _j4status_core_signal_usr1, context); g_unix_signal_add(SIGUSR2, _j4status_core_signal_usr2, context); /* Ignore SIGPIPE as it is useless */ signal(SIGPIPE, SIG_IGN); #endif /* G_OS_UNIX */ context->output_plugin = j4status_plugins_get_output_plugin(&interface, output_plugin); if ( context->output_plugin == NULL ) { g_warning("No usable output plugin, tried '%s'", output_plugin); retval = 10; goto end; } gchar *header = NULL; if ( context->output_plugin->interface.generate_header != NULL ) header = context->output_plugin->interface.generate_header(context->output_plugin->context); /* Creating input/output stream */ context->io = j4status_io_new(context, header, (const gchar * const *) servers_desc, (const gchar * const *) streams_desc); if ( context->io == NULL ) { g_warning("Couldn't create input/output streams"); retval = 2; goto end; } if ( order != NULL ) { context->order_weights = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); gchar **id; for ( id = order ; *id != NULL ; ++id ) g_hash_table_insert(context->order_weights, *id, GINT_TO_POINTER(1 + id - order)); g_free(order); } context->sections_hash = g_hash_table_new(g_str_hash, g_str_equal); context->input_plugins = j4status_plugins_get_input_plugins(&interface, input_plugins); if ( context->input_plugins == NULL ) { g_warning("No input plugins, will stop early"); one_shot = TRUE; retval = 11; } context->sections = g_list_reverse(context->sections); if ( context->order_weights != NULL ) context->sections = g_list_sort(context->sections, _j4status_core_compare_sections); _j4status_core_start(context); if ( one_shot ) g_idle_add(_j4status_core_source_quit, context); context->loop = g_main_loop_new(NULL, FALSE); g_main_loop_run(context->loop); g_main_loop_unref(context->loop); context->loop = NULL; GList *input_plugin_; J4statusInputPlugin *input_plugin; for ( input_plugin_ = context->input_plugins ; input_plugin_ != NULL ; input_plugin_ = g_list_next(input_plugin_) ) { input_plugin = input_plugin_->data; input_plugin->interface.uninit(input_plugin->context); } if ( context->output_plugin->interface.uninit != NULL ) context->output_plugin->interface.uninit(context->output_plugin->context); j4status_io_free(context->io); if ( context->order_weights != NULL ) g_hash_table_unref(context->order_weights); g_hash_table_unref(context->sections_hash); end: #if DEBUG if ( debug_stream != NULL ) g_object_unref(debug_stream); #endif /* DEBUG */ return retval; }
static J4statusPluginContext * _j4status_nl_init(J4statusCoreInterface *core) { gchar **interfaces = NULL; guint64 addresses = ADDRESSES_ALL; GKeyFile *key_file; key_file = j4status_config_get_key_file("Netlink"); if ( key_file != NULL ) { interfaces = g_key_file_get_string_list(key_file, "Netlink", "Interfaces", NULL, NULL); j4status_config_key_file_get_enum(key_file, "Netlink", "Addresses", _j4status_nl_addresses, G_N_ELEMENTS(_j4status_nl_addresses), &addresses); g_key_file_free(key_file); } if ( interfaces == NULL ) { g_message("No interface to monitor, aborting"); return NULL; } gint err; J4statusPluginContext *self; self = g_new0(J4statusPluginContext, 1); self->addresses = addresses; self->sections = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, _j4status_nl_section_free); self->source = g_water_nl_source_new_cache_mngr(NULL, NETLINK_ROUTE, NL_AUTO_PROVIDE, &err); if ( self->source == NULL ) { g_warning("Couldn't subscribe to events: %s", nl_geterror(err)); goto error; } self->sock = g_water_nl_source_get_sock(self->source); self->cache_mngr = g_water_nl_source_get_cache_mngr(self->source); err = rtnl_link_alloc_cache(self->sock, AF_UNSPEC, &self->link_cache); if ( err < 0 ) { g_warning("Couldn't allocate links cache: %s", nl_geterror(err)); goto error; } err = nl_cache_mngr_add_cache(self->cache_mngr, self->link_cache, _j4status_nl_cache_change, self); if ( err < 0 ) { g_warning("Couldn't manage links cache: %s", nl_geterror(err)); goto error; } err = rtnl_addr_alloc_cache(self->sock, &self->addr_cache); if ( err < 0 ) { g_warning("Couldn't allocate addresses cache: %s", nl_geterror(err)); goto error; } err = nl_cache_mngr_add_cache(self->cache_mngr, self->addr_cache, _j4status_nl_cache_change, self); if ( err < 0 ) { g_warning("Couldn't manage addresses cache: %s", nl_geterror(err)); goto error; } gchar **interface; for ( interface = interfaces ; *interface != NULL ; ++interface ) { J4statusNlSection *section; section = _j4status_nl_section_new(self, core, *interface); if ( section != NULL ) g_hash_table_insert(self->sections, GINT_TO_POINTER(section->ifindex), section); else g_free(*interface); } g_free(interfaces); if ( g_hash_table_size(self->sections) < 1 ) goto error; self->formats.up = j4status_format_string_parse(NULL, _j4status_nl_format_up_tokens, G_N_ELEMENTS(_j4status_nl_format_up_tokens), J4STATUS_NL_DEFAULT_UP_FORMAT, NULL); self->formats.down = j4status_format_string_parse(NULL, NULL, 0, J4STATUS_NL_DEFAULT_DOWN_FORMAT, NULL); return self; error: _j4status_nl_uninit(self); return NULL; }