Show color_status.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.
*
*/
char color_status_c_id[] = "$Id: color_status.c,v 1.1 2005/06/03 19:32:00 girod Exp $";
#include <emview/emview.h>
#include <libmisc/misc.h>
#include <mod/mod_examples.h>
#include <sys/wait.h>
/* the global module variable */
static emview_module_t *mod = NULL;
/*
* per-instance state..
*/
struct color_instance {
char *device_name;
int refresh;
};
#define DEFAULT_REFRESH 10000
/*
* handle new status
*/
static
int color_status_handle_data(emview_device_t *dev, emproxy_reply_hdr_t *reply,
emview_dev_node_t *node)
{
emview_module_inst_t *inst = (emview_module_inst_t *)
emview_get_device_opts(dev)->private_data;
elog(LOG_DEBUG(10), "Got status update for node %d", emview_dn_get_node_id(node));
/* convert this string to a color */
int color;
if (isdigit(((char*)reply->data)[0])) {
color = atoi((char*)reply->data) % EMVIEW_MAX_COLORS;
}
else {
color = emview_parse_color((char*)reply->data);
}
/* push data to core */
emview_update_source_int(emview_generic_item_name(mod, inst->index, NULL),
emview_dn_get_node_id(node), color);
return 0;
}
static
int color_status_timeout(emview_device_t *dev, emview_dev_node_t *node)
{
elog(LOG_DEBUG(10), "Node %d timed out!", emview_dn_get_node_id(node));
return 0;
}
static
void color_status_update_proxy_string(emview_module_inst_t *inst)
{
struct color_instance *inst_data = (struct color_instance *)inst->private_data;
char buf[128];
/* construct proxy string */
sprintf(buf, "dev=%s:ascii:reread=%d", inst_data->device_name, inst_data->refresh);
emview_update_proxy_string(inst->dev, buf);
}
static
int color_status_config_device(emview_module_t *mod, emview_module_inst_t *instance,
char *device, node_id_t node)
{
emview_device_opts_t opts = {
/* use the device string if supplied, otherwise the module instance name.. */
name: device ? device : instance->name,
private_data: instance,
proxy_string: "",
data_handler: color_status_handle_data,
node_timeout: color_status_timeout,
parent: mod
};
/* alloc private per-instance state and store it in the instance */
struct color_instance *inst_data = g_new0(struct color_instance, 1);
inst_data->device_name = strdup(opts.name);
inst_data->refresh = DEFAULT_REFRESH;
instance->private_data = inst_data;
/* register device */
instance->dev = emview_register_device(&opts);
/* reset proxy string */
color_status_update_proxy_string(instance);
/* register sources */
emview_register_source(instance->dev,
emview_generic_item_name(mod, instance->index, NULL), NULL);
return 0;
}
static
int color_status_config_assign(emview_module_t *mod, emview_module_inst_t *inst,
node_id_t node, parser_state_t *ps)
{
/*
* Register our option class.
*/
char *component = NULL;
/* parse enables from config */
while (misc_parse_next_kvp(ps) >= 0) {
if (strcmp(ps->key, "component") == 0) {
component = strdup(ps->value);
continue;
}
}
if (component == NULL)
component = strdup(emview_generic_item_name(mod, inst->index, NULL));
emview_component_register(g_quark_from_string(component),
node, EMVIEW_COMP_TYPE_BOX);
/* core class */
emview_assign_source(node, component, EMVIEW_BOX_COLOR,
emview_generic_item_name(mod, inst->index, NULL),
emview_generic_item_name(mod, inst->index, NULL),
EMVIEW_ENABLE);
emview_document_option_class
(mod, emview_generic_item_name(mod, inst->index, NULL), "core",
"Color node by parsing text from status device", NULL);
if (component) free(component);
return 0;
}
static
int color_status_config_options(emview_module_t *mod, emview_module_inst_t *inst,
node_id_t node, parser_state_t *ps)
{
struct color_instance *inst_data = (struct color_instance *)inst->private_data;
/* parse enables from config */
while (misc_parse_next_kvp(ps) >= 0) {
if (strcmp(ps->key, "core") == 0) {
emview_option_class_set_enable
(node, emview_generic_item_name(mod, inst->index, NULL), 1);
continue;
}
if (strcmp(ps->key, "refresh") == 0) {
if (ps->value) {
inst_data->refresh = atoi(ps->value);
color_status_update_proxy_string(inst);
}
continue;
}
}
return 0;
}
/*
* Main
*/
int color_status_main(int *argc, char **argv)
{
static emview_module_opts_t mod_opts = {
name: COLOR_STATUS,
description: "Set color from status output",
usage: "core",
config_new_instance: color_status_config_device,
config_assign: color_status_config_assign,
config_opts: color_status_config_options
};
/* register with emview and get context structure */
mod = emview_register(&mod_opts);
return 0;
}
See more files for this project here