Files
lcdproc/server/screenlist.c
T

289 lines
5.9 KiB
C
Raw Normal View History

2008-11-30 22:31:20 +00:00
/** \file server/screenlist.c
* All actions that can be performed on the list of screens.
* This file also manages the rotation of screens.
*/
/* This file is part of LCDd, the lcdproc server.
*
2008-11-30 22:31:20 +00:00
* This file is released under the GNU General Public License.
* Refer to the COPYING file distributed with this package.
*
2011-01-05 23:34:37 +00:00
* Copyright (c) 1999, William Ferrell, Selene Scriven
* 2003, Joris Robijn
*/
1999-12-09 23:15:14 +00:00
#include <stdlib.h>
#include <stdio.h>
#include "shared/LL.h"
#include "shared/sockets.h"
2001-11-29 14:09:13 +00:00
#include "shared/report.h"
2012-02-22 22:25:32 +00:00
#include "client.h"
1999-12-09 23:15:14 +00:00
#include "screen.h"
2012-02-22 22:25:32 +00:00
#include "screenlist.h"
1999-12-09 23:15:14 +00:00
#include "main.h" /* for timer */
/* Local functions */
2007-04-02 21:29:53 +00:00
int compare_priority(void *one, void *two);
1999-12-09 23:15:14 +00:00
bool autorotate = UNSET_INT; /* If on, INFO and FOREGROUND screens will rotate */
LinkedList *screenlist = NULL;
2007-04-02 21:29:53 +00:00
Screen *current_screen = NULL;
long int current_screen_start_time = 0;
1999-12-09 23:15:14 +00:00
int
2007-04-02 21:29:53 +00:00
screenlist_init(void)
1999-12-09 23:15:14 +00:00
{
2007-04-02 21:29:53 +00:00
report(RPT_DEBUG, "%s()", __FUNCTION__);
2007-04-02 21:29:53 +00:00
screenlist = LL_new();
2000-04-03 22:13:57 +00:00
if (!screenlist) {
report(RPT_ERR, "%s: Error allocating", __FUNCTION__);
2000-04-03 22:13:57 +00:00
return -1;
}
return 0;
1999-12-09 23:15:14 +00:00
}
int
2007-04-02 21:29:53 +00:00
screenlist_shutdown(void)
1999-12-09 23:15:14 +00:00
{
2007-04-02 21:29:53 +00:00
report(RPT_DEBUG, "%s()", __FUNCTION__);
2007-04-02 21:29:53 +00:00
if (!screenlist) {
/* Program shutdown before completed startup */
return -1;
}
2007-04-02 21:29:53 +00:00
LL_Destroy(screenlist);
2000-04-03 22:13:57 +00:00
return 0;
1999-12-09 23:15:14 +00:00
}
int
2007-04-02 21:29:53 +00:00
screenlist_add(Screen *s)
{
if (!screenlist)
return -1;
2007-04-02 21:29:53 +00:00
return LL_Push(screenlist, s);
}
int
2007-04-02 21:29:53 +00:00
screenlist_remove(Screen *s)
1999-12-09 23:15:14 +00:00
{
2007-04-02 21:29:53 +00:00
debug(RPT_DEBUG, "%s(s=[%.40s])", __FUNCTION__, s->id);
if (!screenlist)
2000-04-03 22:13:57 +00:00
return -1;
/* Are we trying to remove the current screen ? */
if (s == current_screen) {
2007-04-02 21:29:53 +00:00
screenlist_goto_next();
if (s == current_screen) {
/* Hmm, no other screen had same priority */
void *res = LL_Remove(screenlist, s, NEXT);
/* And now once more */
2007-04-02 21:29:53 +00:00
screenlist_goto_next();
return (res == NULL) ? -1 : 0;
}
}
return (LL_Remove(screenlist, s, NEXT) == NULL) ? -1 : 0;
1999-12-09 23:15:14 +00:00
}
void
2007-04-02 21:29:53 +00:00
screenlist_process(void)
1999-12-09 23:15:14 +00:00
{
2007-04-02 21:29:53 +00:00
Screen *s;
Screen *f;
2007-04-02 21:29:53 +00:00
report(RPT_DEBUG, "%s()", __FUNCTION__);
if (!screenlist)
return;
/* Sort the list according to priority class */
2007-04-02 21:29:53 +00:00
LL_Sort(screenlist, compare_priority);
f = LL_GetFirst(screenlist);
/**** First we need to check out the current situation. ****/
/* Check whether there is an active screen */
2007-04-02 21:29:53 +00:00
s = screenlist_current();
if (!s) {
/* We have no active screen yet.
* Try to switch to the first screen in the list... */
s = f;
if (!s) {
/* There was no screen in the list */
return;
}
2007-04-02 21:29:53 +00:00
screenlist_switch(s);
return;
}
else {
/* There already was an active screen.
* Check to see if it has an expiry time. If so, decrease it
* and then check to see if it has expired. Remove the screen
* if expired. */
if (s->timeout != -1) {
--(s->timeout);
report(RPT_DEBUG, "Active screen [%.40s] has timeout->%d", s->id, s->timeout);
if (s->timeout <= 0) {
/* Expired, we can destroy it */
report(RPT_DEBUG, "Removing expired screen [%.40s]", s->id);
2007-04-02 21:29:53 +00:00
client_remove_screen(s->client, s);
screen_destroy(s);
}
}
}
/**** OK, current situation examined. We can now see if we need to switch. */
/* Is there a screen of a higher priority class than the
* current one ? */
if (f->priority > s->priority) {
/* Yes, switch to that screen, job done */
report(RPT_DEBUG, "%s: High priority screen [%.40s] selected", __FUNCTION__, f->id);
screenlist_switch(f);
return;
}
/* Current screen has been visible long enough and is it of 'normal'
* priority ?
*/
if (autorotate && (timer - current_screen_start_time >= s->duration)
&& s->priority > PRI_BACKGROUND && s->priority <= PRI_FOREGROUND) {
/* Ah, rotate! */
screenlist_goto_next();
}
}
void
2007-04-02 21:29:53 +00:00
screenlist_switch(Screen *s)
{
2007-04-02 21:29:53 +00:00
Client *c;
char str[256];
if (!s) return;
2007-04-02 21:29:53 +00:00
report(RPT_DEBUG, "%s(s=[%.40s])", __FUNCTION__, s->id);
if (s == current_screen) {
/* Nothing to be done */
return;
}
if (current_screen) {
c = current_screen->client;
if (c) {
/* Tell the client we're not listening any more...*/
2007-04-02 21:29:53 +00:00
snprintf(str, sizeof(str), "ignore %s\n", current_screen->id);
sock_send_string(c->sock, str);
} else {
/* It's a server screen, no need to inform it. */
}
}
c = s->client;
if (c) {
/* Tell the client we're paying attention...*/
2007-04-02 21:29:53 +00:00
snprintf(str, sizeof(str), "listen %s\n", s->id);
sock_send_string(c->sock, str);
} else {
/* It's a server screen, no need to inform it. */
}
2007-04-02 21:29:53 +00:00
report(RPT_INFO, "%s: switched to screen [%.40s]", __FUNCTION__, s->id);
current_screen = s;
current_screen_start_time = timer;
1999-12-09 23:15:14 +00:00
}
Screen *
2007-04-02 21:29:53 +00:00
screenlist_current(void)
1999-12-09 23:15:14 +00:00
{
return current_screen;
1999-12-09 23:15:14 +00:00
}
int
2007-04-02 21:29:53 +00:00
screenlist_goto_next(void)
1999-12-09 23:15:14 +00:00
{
Screen *s;
2007-04-02 21:29:53 +00:00
debug(RPT_DEBUG, "%s()", __FUNCTION__);
if (!current_screen)
return -1;
1999-12-09 23:15:14 +00:00
/* Find current screen in screenlist */
for (s = LL_GetFirst(screenlist); s && s != current_screen; s = LL_GetNext(screenlist))
;
1999-12-09 23:15:14 +00:00
/* One step forward */
s = LL_GetNext(screenlist);
2007-04-02 21:29:53 +00:00
if (!s || s->priority < current_screen->priority) {
/* To far, go back to start of screenlist */
s = LL_GetFirst(screenlist);
}
2007-04-02 21:29:53 +00:00
screenlist_switch(s);
return 0;
1999-12-09 23:15:14 +00:00
}
int
2007-04-02 21:29:53 +00:00
screenlist_goto_prev(void)
1999-12-09 23:15:14 +00:00
{
Screen *s;
1999-12-09 23:15:14 +00:00
2007-04-02 21:29:53 +00:00
debug(RPT_DEBUG, "%s()", __FUNCTION__);
if (!current_screen)
return -1;
1999-12-09 23:15:14 +00:00
/* Find current screen in screenlist */
2007-04-02 21:29:53 +00:00
for (s = LL_GetFirst(screenlist); s && s != current_screen; s = LL_GetNext(screenlist));
/* One step back */
s = LL_GetPrev(screenlist);
2007-04-02 21:29:53 +00:00
if (!s) {
/* We're at the start of the screenlist. We should find the
* last screen with the same priority as the first screen.
*/
2007-04-02 21:29:53 +00:00
Screen *f = LL_GetFirst(screenlist);
Screen *n;
s = f;
2007-04-02 21:29:53 +00:00
while ((n = LL_GetNext(screenlist)) && n->priority == f->priority) {
s = n;
}
}
2007-04-02 21:29:53 +00:00
screenlist_switch(s);
return 0;
1999-12-09 23:15:14 +00:00
}
/* Internal function for sorting. */
int
2007-04-02 21:29:53 +00:00
compare_priority(void *one, void *two)
1999-12-09 23:15:14 +00:00
{
Screen *a, *b;
1999-12-09 23:15:14 +00:00
2001-12-30 00:15:25 +00:00
/*debug(RPT_DEBUG, "compare_priority: %8x %8x", one, two);*/
2000-04-03 22:13:57 +00:00
if (!one)
return 0;
if (!two)
return 0;
a = (Screen *) one;
b = (Screen *) two;
1999-12-09 23:15:14 +00:00
2001-12-30 00:15:25 +00:00
/*debug(RPT_DEBUG, "compare_priority: done?");*/
1999-12-09 23:15:14 +00:00
return (b->priority - a->priority);
1999-12-09 23:15:14 +00:00
}