Show emview_main.c syntax highlighted
/*
*
* Copyright (c) 2003 The Regents of the University of California. All
* rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/*
* emview_main.c
*
* Author: girod
*
* $Id: emview_main.c,v 1.24 2005/06/15 00:01:57 girod Exp $
*/
char emview_main_c_id[] = "$Id: emview_main.c,v 1.24 2005/06/15 00:01:57 girod Exp $";
#include "emview_i.h"
#define _GNU_SOURCE
#include <getopt.h>
#undef _GNU_SOURCE
#include <emrun/emrun.h>
/* call emview_init() to initialize the global _core var */
core_t _core = {
gui: {
width: EMVIEW_DEFAULT_WIDTH,
height: EMVIEW_DEFAULT_HEIGHT,
frame_rate: EMVIEW_DEFAULT_REDRAW_INTERVAL
}
};
QUEUE_FUNCTION_INSTANTIATIONS(emview_module, ptrs, modules, emview_module_t, core_t);
/*
* Module Registration functions
*/
emview_module_t *emview_register(emview_module_opts_t *mod_opts)
{
emview_module_t *mod = g_new0(emview_module_t, 1);
/* initialize the module struct */
mod->opts = *mod_opts;
mod->opts.name = strdup(mod->opts.name);
mod->instances = g_ptr_array_new();
/* add it to the core */
emview_module_push(&_core, mod);
return mod;
}
emview_module_t *emvew_module_lookup(char *name)
{
emview_module_t *mod;
for (mod = emview_module_top(&_core); mod; mod = emview_module_next(mod))
if (strcmp(mod->opts.name, name) == 0)
return mod;
return NULL;
}
emview_module_opts_t *emview_get_module_opts(emview_module_t *mod)
{
return &(mod->opts);
}
/*
* Module instance helpers
*/
emview_module_inst_t *emview_mod_inst_lookup_or_create(emview_module_t *mod, char *nick, int *created)
{
int len;
emview_module_inst_t *retval = emview_mod_inst_lookup(mod, nick);
if (retval) {
if (created) *created = 0;
return retval;
}
/* alloc, config */
len = sizeof(emview_module_inst_t) + mod->opts.instance_data_length;
retval = malloc(len);
memset(retval, 0, len);
retval->index = mod->instance_count;
retval->name = strdup(nick);
mod->instance_count++;
if (created) *created = 1;
g_ptr_array_add(mod->instances, retval);
return retval;
}
emview_module_inst_t *emview_mod_inst_lookup(emview_module_t *mod, char *nick)
{
emview_module_inst_t *retval = NULL;
int i;
for (i=0; i<mod->instance_count; i++) {
retval = (emview_module_inst_t *)g_ptr_array_index(mod->instances, i);
if (strcmp(nick, retval->name) == 0)
return retval;
}
return NULL;
}
emview_module_inst_t *emview_mod_inst_index(emview_module_t *mod, int index)
{
if ((index >= 0) && (index < mod->instance_count))
return (emview_module_inst_t *)g_ptr_array_index(mod->instances, index);
return NULL;
}
int emview_mod_inst_count(emview_module_t *mod)
{
return mod->instance_count;
}
char *emview_generic_item_name(emview_module_t *mod, int index, char *source)
{
DECLARE_STATIC_BUF_RING(buf, 10, 256);
emview_module_inst_t *inst = emview_mod_inst_index(mod, index);
if (inst) {
if (source)
sprintf(buf, "%s/%s/%s", mod->opts.name, inst->name, source);
else
sprintf(buf, "%s/%s", mod->opts.name, inst->name);
return buf;
}
elog(LOG_ERR, "No such instance: module %s, instance %d",
mod->opts.name, index);
return "";
}
void emview_dump_docs()
{
buf_t *buf = buf_new();
node_t *n = emview_node_lookup(1);
emview_module_t *mod;
/* instantiate each module */
for (mod = emview_module_top(&_core); mod; mod = emview_module_next(mod))
if (strcmp(mod->opts.name, "__Config"))
bufprintf(buf, "module=%s:dev=<dev>:%s\n", mod->opts.name,
mod->opts.usage ? mod->opts.usage : "");
bufcpy(buf, "", 1);
emview_config_test(n, buf);
/* force config */
emview_config_global_now(NULL, 0, NULL);
buf_free(buf);
buf = buf_new();
bufprintf(buf, "\nEmView Release %s\nListing of Modules, Usage and Options (-D)\n\n", CVSTAG);
/* now, dump it all */
for (mod = emview_module_top(&_core); mod; mod = emview_module_next(mod))
if (strcmp(mod->opts.name, "__Config"))
emview_config_dump(mod, n, buf);
printf("%s", buf->buf);
buf_free(buf);
}
/*
* load locations file
*/
void emview_load_locations()
{
if (_core.location_file_path) {
FILE *f;
char line[1024];
f = fopen(_core.location_file_path, "r");
if (f == NULL) {
elog(LOG_WARNING, "Unable to load locations file '%s': %m",
_core.location_file_path);
return;
}
while (fgets(line, sizeof(line), f)) {
if (line[0] && line[0] != '\n' && line[0] != '#') {
node_t *n = NULL;
int node_id;
float x,y;
int scans = sscanf(line, "%d %f %f", &node_id, &x, &y);
if (scans != 3) {
elog(LOG_WARNING, "Invalid format for location file: '%s'", line);
continue;
}
n = emview_node_lookup(node_id);
n->pos.x = x;
n->pos.y = y;
}
}
fclose(f);
}
}
/*
* emview core initialization. (all but GUI)
*/
void emview_init(emproxy_client_opts_t *opts)
{
emview_sources_init();
emview_devices_init(opts);
emview_node_list_init();
emview_components_init();
}
/*
* emrun callbacks
*/
int emview_exit(void *data)
{
elog(LOG_WARNING, "EmView shutting down..");
emview_analysis_restart(&_core.analysis);
if (_core.trace_path)
close(_core.trace_fd);
gtk_exit(0);
exit(0);
}
static void emview_shutdown(void *data)
{
emview_exit(data);
}
/*
* main program..
*/
void usage(char *name)
{
char *epu;
char *epcl;
char *m_usage;
char *m_expl;
misc_usage(&m_usage, &m_expl);
emproxy_usage(&epu, &epcl);
fprintf(stderr,
"%s: Usage:\n"
" [-h] [-D] %s [-l <location_file>] [--record <tracefile>]\n"
" [--nogui] [--debug_tool] %s %s\n"
"\n"
" --help: this message\n"
" --doc: List the complete Modules and Options\n"
" --record: Store all incoming messages in a trace file\n"
" --replay: Replay from a trace file\n"
" --nogui: Disables GUI window\n"
" --debug_tool: Enables debugging tool, substantial over-head\n"
" --title <str>: Sets the title bar\n"
"%s%s\n"
,
name, name, epu, m_usage, epcl, m_expl);
exit(1);
}
int main(int argc, char **argv)
{
int doc = 0;
emproxy_client_opts_t proxy_opts = {};
/* libmisc options parsing */
misc_init(&argc, argv, CVSTAG);
/* emproxy options parsing */
emproxy_client_config(&proxy_opts, &argc, argv);
_core.gui_title = misc_parse_out_option(&argc, argv, "title", 0);
_core.trace_path = misc_parse_out_option(&argc, argv, "record", 0);
_core.input_trace_path = misc_parse_out_option(&argc, argv, "replay", 0);
_core.nogui = misc_parse_out_switch(&argc, argv, "nogui", 0);
/* parse command line args */
while (1) {
int c;
int option_index = 0;
static struct option long_options[] =
{
{"help", 0, 0, 'h'},
{"doc", 0, 0, 'D'},
{"debug_tool", 0, 0, 't'},
{0, 0, 0, 0}
};
c = getopt_long (argc, argv, "h?l:D",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 'l':
_core.location_file_path = optarg;
break;
case 'D':
doc = 1;
break;
case 't':
break;
default:
usage(argv[0]);
break;
}
}
/* open the trace file */
if (_core.trace_path) {
elog(LOG_NOTICE, "Logging trace to file %s", _core.trace_path);
_core.trace_fd = open(_core.trace_path, O_WRONLY|O_TRUNC|O_CREAT);
if (_core.trace_fd < 0) {
elog(LOG_CRIT, "Unable to open trace file %s for writing: %m",
_core.trace_path);
exit(1);
}
}
/* init the emview core */
emview_init(&proxy_opts);
/* disable path mangling */
sim_path_disable();
/* init config module */
config_main(&argc, argv);
/* call module initializers (mod.c) */
mod_init(&argc, argv);
/* set default title string */
if (_core.gui_title == NULL) {
buf_t *buf = buf_new();
emproxy_describe_opts_to_buf(buf, "EmView", &(proxy_opts));
_core.gui_title = buf->buf;
}
/* start GUI */
if (!_core.nogui)
emview_gui_init(&argc, argv);
/* if doc mode */
if (doc) {
emview_dump_docs();
exit(0);
}
/* load the locations file */
emview_load_locations();
/* trigger node reconfig */
emview_reconfig();
/* emrun init */
{
emrun_opts_t emrun_opts = {
shutdown: emview_shutdown,
silent:1
};
emrun_init(&emrun_opts);
}
if (_core.nogui) {
g_type_init();
g_main();
}
else
gtk_main();
return 1;
}
See more files for this project here