Show emview_slots.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_slots.c
*
* Author: girod
*
* $Id: emview_slots.c,v 1.6 2004/05/23 00:32:37 girod Exp $
*/
#include "emview_i.h"
#include <time.h>
#include <math.h>
char emview_slots_c_id[] = "$Id: emview_slots.c,v 1.6 2004/05/23 00:32:37 girod Exp $";
QUEUE_INLINE_INSTANTIATIONS(emview_slot_list, ptrs, slots, slot_t, slot_list_t);
/*
* slot functions
*/
QUEUE_FUNCTION_INSTANTIATIONS(emview_slot_list, ptrs, slots, slot_t, slot_list_t);
slot_t *emview_slot_create(GQuark component, int field, GQuark source,
GQuark option_name, int enable_type)
{
source_t *src = emview_source_lookup_q(source);
if (src) {
slot_t *n = g_new0(slot_t, 1);
n->component = component;
n->field = field;
n->source = source;
n->option_name = option_name;
n->type = src->type;
n->enable_type = enable_type;
return n;
}
elog(LOG_WARNING, "Nonexistent source %d!", source);
return NULL;
}
slot_t *emview_slot_copy(slot_t *slot)
{
slot_t *n = g_new0(slot_t, 1);
memmove(n, slot, sizeof(slot_t));
emview_slot_list_el_init(n);
return n;
}
void emview_slot_list_clear(slot_list_t *sl)
{
slot_t *ptr;
while ((ptr = emview_slot_list_pop(sl)))
free(ptr);
}
slot_t *emview_slot_find(slot_list_t *list, GQuark component, int field,
GQuark source, GQuark option_name)
{
slot_t *ptr;
/* check slots */
for (ptr = emview_slot_list_top(list); ptr; ptr = emview_slot_list_next(ptr))
if ((!component || ((ptr->component == component) &&
(ptr->field == field))) &&
(!source || (ptr->source == source)) &&
(!option_name || (ptr->option_name == option_name)))
return ptr;
return NULL;
}
slot_t *emview_slot_find_s(slot_list_t *sl, slot_t *s)
{
return emview_slot_find(sl, s->component, s->field, s->source, s->option_name);
}
int emview_slot_remove(slot_list_t *sl, GQuark component, int field, GQuark source,
GQuark option)
{
slot_t *found = emview_slot_find(sl, component, field, source, option);
if (found) {
emview_slot_list_remove(sl, found);
free(found);
return 0;
}
return -1;
}
int emview_slot_remove_s(slot_list_t *sl, slot_t *s)
{
return emview_slot_remove(sl, s->component, s->field, s->source, s->option_name);
}
int emview_slot_add(slot_list_t *list, GQuark component, int field, GQuark source,
GQuark option_name, int enable_type, int excl)
{
slot_t *new_slot;
if (excl) emview_slot_remove(list, component, field, 0, option_name);
new_slot = emview_slot_create(component, field, source, option_name, enable_type);
if (new_slot) {
emview_slot_list_push(list, new_slot);
return 0;
}
else {
elog(LOG_WARNING, "Unable to create new slot!");
return -1;
}
}
int emview_slot_add_s(slot_list_t *list, slot_t *slot, int excl)
{
return emview_slot_add(list, slot->component, slot->field, slot->source,
slot->option_name, slot->enable_type, excl);
}
void emview_slot_list_assign_low(slot_list_t *slots, component_t *c, node_id_t id)
{
slot_t *s;
for (s = emview_slot_list_top(slots); s; s = emview_slot_list_next(s)) {
if (s->enable_bit && (s->type != EMVIEW_SOURCE_LINK) && (s->component == c->name)) {
source_data_t *d = emview_source_data_lookup_safe_q(s->source, id);
emview_component_assign_low(c, s->field, d);
}
}
}
/*
* Linkage API
*/
#define EMVIEW_ASSIGN_ADD 0
#define EMVIEW_ASSIGN_REMOVE 1
#define EMVIEW_ASSIGN_SET_ENABLE 2
static
int emview_assign_aux(node_t *n, slot_t *slot, int disposition)
{
int retval = -1;
int excl=0;
if (disposition != EMVIEW_ASSIGN_SET_ENABLE) {
/* determine exclusivity */
excl = emview_component_is_exclusive_q(&(n->display.components),
slot->component, slot->field);
if (excl < 0) {
elog(LOG_WARNING, "%s/%d is not a valid field",
g_quark_to_string(slot->component), slot->field);
return retval;
}
}
/* attempt to remove it */
switch (disposition) {
case EMVIEW_ASSIGN_REMOVE:
retval = emview_slot_remove_s(&(n->display.slots), slot);
break;
case EMVIEW_ASSIGN_ADD:
/* skip if this is exactly what is already present? */
if (emview_slot_find_s(&(n->display.slots), slot))
retval = 0;
/* else try to add it now... */
else retval = emview_slot_add_s(&(n->display.slots), slot, excl);
break;
case EMVIEW_ASSIGN_SET_ENABLE:
/* copy enabled bits for all slots on this option */
{
slot_t *ptr;
/* enable all matching option X in current list */
for (ptr = emview_slot_list_top(&(n->display.slots)); ptr; ptr = emview_slot_list_next(ptr))
if (ptr->option_name == slot->option_name)
ptr->enable_bit = slot->enable_bit;
}
break;
default:
elog(LOG_ERR, "Unknown assignment command");
}
return retval;
}
static
int emview_assign_source_aux(node_id_t target, slot_t *slot, int disposition)
{
if (target == 0) {
node_t *n;
int retval = 0;
for (n=emview_node_top(&(_core.node_list)); n; n=emview_node_next(n)) {
if (emview_assign_aux(n, slot, disposition) < 0)
retval = -1;
}
return retval;
}
else {
node_t *n = emview_node_lookup_nocreate(target);
if (n == NULL) {
elog(LOG_WARNING, "Modifying config of nonexistent node id=%d", target);
return -1;
}
return emview_assign_aux(n, slot, disposition);
}
}
int emview_set_assign_source(node_id_t target, const char *component_name,
int field, const char *source_name,
const char *option_name, int enable_type, int remove)
{
int retval = -1;
source_t *src;
slot_t slot = {
field: field,
source: source_name ? g_quark_from_string(source_name) : 0,
component: component_name ? g_quark_from_string(component_name) : 0,
option_name: option_name ? g_quark_from_string(option_name) : 0,
enable_type: enable_type
};
/* lookup the source */
src = emview_source_lookup(source_name);
if (src == NULL) {
elog(LOG_WARNING, "%s is not a valid source", source_name);
goto out;
}
/* make sure it's a data source.. */
if (src->type != EMVIEW_SOURCE_SIMPLE) {
elog(LOG_WARNING, "%s is not a simple data source (%d)", source_name, src->type);
goto out;
}
/* do the assignment */
slot.type = src->type;
retval = emview_assign_source_aux(target, &slot, remove);
out:
return retval;
}
int emview_assign_source(node_id_t target, const char *component_name,
int field, const char *source_name,
const char *option_name, int enable)
{
return emview_set_assign_source(target, component_name, field, source_name,
option_name, enable, EMVIEW_ASSIGN_ADD);
}
int emview_set_assign_link_source(node_id_t target,
const char *default_component_name,
emview_color_t default_link_color,
const char *link_source_name,
const char *option_name, int enable_type,
int remove)
{
int retval = -1;
source_t *src;
slot_t slot = {
component: default_component_name ? g_quark_from_string(default_component_name) : 0,
field: (default_link_color ? default_link_color : EMVIEW_COLOR_BLACK),
source: link_source_name ? g_quark_from_string(link_source_name) : 0,
option_name: option_name ? g_quark_from_string(option_name) : 0,
enable_type: enable_type
};
/* lookup the source */
src = emview_source_lookup(link_source_name);
if (src == NULL) {
elog(LOG_WARNING, "%s is not a valid source", link_source_name);
goto out;
}
/* make sure it's a link source.. */
if (src->type != EMVIEW_SOURCE_LINK) {
elog(LOG_WARNING, "%s is not a link source", link_source_name);
goto out;
}
/* actually do the assignment */
slot.type = src->type;
retval = emview_assign_source_aux(target, &slot, remove);
out:
return retval;
}
int emview_assign_link_source(node_id_t target,
const char *default_component_name,
emview_color_t default_link_color,
const char *link_source_name,
const char *option_name, int enable)
{
return emview_set_assign_link_source(target, default_component_name,
default_link_color, link_source_name,
option_name, enable, EMVIEW_ASSIGN_ADD);
}
void emview_slots_list_status_printable(slot_list_t *slots, buf_t *buf, GQuark option_q)
{
slot_t *ptr;
/* print current list */
for (ptr = emview_slot_list_top(slots); ptr; ptr = emview_slot_list_next(ptr))
if (option_q == 0 || ptr->option_name == option_q)
bufprintf(buf, " %s[%s] -> %s,%s [%s] enable_type=%s %s\n",
g_quark_to_string(ptr->source), emview_unparse_source_type(ptr->type),
g_quark_to_string(ptr->component), emview_unparse_component_field(ptr->field),
option_q ? "" : g_quark_to_string(ptr->option_name),
emview_config_unparse_enable(ptr->enable_type),
ptr->enable_bit ? "Enabled" : "Disabled");
}
int emview_option_class_set_enable(node_id_t target, char *option_name, int enable_bit)
{
int retval = -1;
slot_t slot = {
option_name: option_name ? g_quark_from_string(option_name) : 0,
enable_bit: enable_bit
};
if (slot.option_name == 0) {
elog(LOG_ERR, "Must specify an option name to set");
}
else
retval = emview_assign_source_aux(target, &slot, EMVIEW_ASSIGN_SET_ENABLE);
/* update the tree and menu */
emview_option_tree_update(option_name, enable_bit);
return retval;
}
See more files for this project here