Files
lcdproc/server/menu.c
T

355 lines
7.2 KiB
C
Raw Normal View History

1999-12-09 23:15:14 +00:00
/*
* menu.c
* This file is part of LCDd, the lcdproc server.
*
* This file is released under the GNU General Public License. Refer to the
* COPYING file distributed with this package.
*
* Copyright (c) 1999, William Ferrell, Scott Scriven
*
*
* Handles server-supplied menus defined by a table. Read menu.h for
* more information.
*
* Menus are similar to "pull-down" menus, but have some extra features.
* They can contain "normal" menu items, checkboxes, sliders, "movers",
* etc..
*
* I should probably find a more elegant way of doing this in order
* to handle dynamically-changing menus such as the client list. Tcl/Tk
* has neat ways to do it. Hmm...
*
1999-12-09 23:15:14 +00:00
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "parse.h"
#include "sock.h"
#include "render.h"
#include "main.h"
2001-12-30 00:15:25 +00:00
#include "drivers.h"
1999-12-09 23:15:14 +00:00
#include "menu.h"
2001-12-30 00:15:25 +00:00
#include "input.h"
1999-12-09 23:15:14 +00:00
2001-12-30 00:15:25 +00:00
/* FIXME: Implement this where it is supposed to be...*/
void
framedelay ()
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
sock_poll_clients ();
parse_all_client_messages ();
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
usleep (TIME_UNIT);
1999-12-09 23:15:14 +00:00
}
static void
draw_heartbeat ()
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
static int timer = 0;
2000-04-03 22:13:57 +00:00
if (heartbeat) {
2001-12-30 00:15:25 +00:00
/* Set this to pulsate like a real heart beat... */
/*drivers_icon (!((timer + 4) & 5), 0); */
/*drivers_chr (display_props->width, 1, 0); */
2000-04-03 22:13:57 +00:00
}
2001-12-30 00:15:25 +00:00
drivers_flush ();
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
timer++;
timer &= 0x0f;
1999-12-09 23:15:14 +00:00
}
static int PAD = 255;
typedef struct menu_info {
2000-04-03 22:13:57 +00:00
int selected;
int length;
1999-12-09 23:15:14 +00:00
} menu_info;
static int draw_menu (Menu menu, menu_info * info);
static int fill_menu_info (Menu menu, menu_info * info);
static int menu_handle_action (menu_item * item);
1999-12-09 23:15:14 +00:00
static int slid_func (menu_item * item);
1999-12-09 23:15:14 +00:00
int
do_menu (Menu menu)
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
menu_info info;
int key = 0;
int status = MENU_OK;
int done = 0;
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
int (*func) ();
int (*readfunc) (int);
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
if (!menu)
return MENU_ERROR;
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
fill_menu_info (menu, &info);
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
while (!done) {
2001-12-30 00:15:25 +00:00
/* Keep the cursor off titles... (?) */
2000-04-03 22:13:57 +00:00
while (menu[info.selected].type == TYPE_TITL) {
info.selected++;
2001-12-30 00:15:25 +00:00
/* If the title is the last thing in the menu... */
2000-04-03 22:13:57 +00:00
if (!menu[info.selected].text)
info.selected -= 2;
}
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
draw_menu (menu, &info);
1999-12-09 23:15:14 +00:00
2001-12-30 00:15:25 +00:00
/* FIXME: This should use a better keypress interface, which */
/* FIXME: handles things according to keybindings... */
1999-12-09 23:15:14 +00:00
2001-12-30 00:15:25 +00:00
for (key = drivers_getkey (); key == 0; key = drivers_getkey ()) {
/* sleep for 1/8th second... */
2000-04-03 22:13:57 +00:00
framedelay ();
2001-12-30 00:15:25 +00:00
/* do the heartbeat... */
2000-04-03 22:13:57 +00:00
draw_heartbeat ();
2001-12-30 00:15:25 +00:00
/* Check for client input... */
2000-04-03 22:13:57 +00:00
}
2001-12-30 00:15:25 +00:00
printf( "Received key: %c\n", key );
1999-12-09 23:15:14 +00:00
2001-12-30 00:15:25 +00:00
/* Handle the key according to the keybindings... */
2000-04-03 22:13:57 +00:00
switch (key) {
case 'D':
done = 1;
break;
case 'B':
if (info.selected > 0)
info.selected--;
while (menu[info.selected].type == TYPE_TITL) {
if (info.selected > 0)
info.selected--;
else
break;
}
break;
2001-12-30 00:15:25 +00:00
case INPUT_FORWARD_KEY:
2000-04-03 22:13:57 +00:00
if (menu[info.selected + 1].text)
info.selected++;
break;
2001-12-30 00:15:25 +00:00
case INPUT_PAUSE_KEY:
2000-04-03 22:13:57 +00:00
switch (menu[info.selected].type) {
case TYPE_MENU:
status = do_menu (menu[info.selected].data);
break;
case TYPE_FUNC:
func = menu[info.selected].data;
if (func)
status = func ();
break;
case TYPE_CHEK:
readfunc = menu[info.selected].data;
if (readfunc)
status = readfunc (MENU_CHECK);
status &= 0xffff0000;
break;
case TYPE_SLID:
func = menu[info.selected].data;
if (func)
status = slid_func (&menu[info.selected]);
break;
default:
break;
}
2000-04-03 22:13:57 +00:00
switch (status) {
case MENU_OK:
break;
case MENU_CLOSE:
return MENU_OK;
case MENU_QUIT:
return MENU_QUIT;
2001-12-30 00:15:25 +00:00
/* case MENU_KILL:
* return MENU_KILL;
*/
2000-04-03 22:13:57 +00:00
case MENU_ERROR:
return MENU_ERROR;
}
2001-12-30 00:15:25 +00:00
/* status = menu_handle_action(&menu[info.selected]);*/
/* TODO: It should now do special stuff for "mover" widgets,
* TODO: and handle the return code appropriately.
*/
2000-04-03 22:13:57 +00:00
break;
default:
break;
}
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
}
2000-04-03 22:13:57 +00:00
return status;
1999-12-09 23:15:14 +00:00
}
static int
draw_menu (Menu menu, menu_info * info)
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
int i;
int x = 1, y = 1;
int top = 0, bottom = 0;
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
int (*readfunc) (int);
2001-12-30 00:15:25 +00:00
/* these should maybe be removed: */
int wid = display_props->width, hgt = display_props->height;
2000-04-03 22:13:57 +00:00
if (!menu)
return MENU_ERROR;
2001-12-30 00:15:25 +00:00
drivers_clear ();
2001-12-30 00:15:25 +00:00
/* Scroll down until the selected item is centered, if possible...*/
2000-04-03 22:13:57 +00:00
top = info->selected - (hgt / 2);
if (top < 0)
top = 0;
bottom = top + hgt;
if (bottom > info->length)
bottom = info->length;
top = bottom - hgt;
if (top < 0)
top = 0;
2001-12-30 00:15:25 +00:00
/* Draw all visible items...*/
2000-04-03 22:13:57 +00:00
for (i = top; i < bottom; i++, y++) {
if (i == info->selected)
2001-12-30 00:15:25 +00:00
drivers_chr (2, y, '>');
2000-04-03 22:13:57 +00:00
switch (menu[i].type) {
case TYPE_TITL:
2001-12-30 00:15:25 +00:00
drivers_chr (1, y, PAD);
drivers_chr (2, y, PAD);
drivers_string (4, y, menu[i].text);
2000-04-03 22:13:57 +00:00
for (x = strlen (menu[i].text) + 5; x <= wid; x++)
2001-12-30 00:15:25 +00:00
drivers_chr (x, y, PAD);
2000-04-03 22:13:57 +00:00
break;
case TYPE_MENU:
2001-12-30 00:15:25 +00:00
drivers_string (3, y, menu[i].text);
drivers_chr (wid, y, '>');
2000-04-03 22:13:57 +00:00
break;
case TYPE_FUNC:
2001-12-30 00:15:25 +00:00
drivers_string (3, y, menu[i].text);
2000-04-03 22:13:57 +00:00
break;
case TYPE_CHEK:
if (menu[i].data) {
readfunc = menu[i].data;
if (readfunc (MENU_READ))
2001-12-30 00:15:25 +00:00
drivers_chr (wid, y, 'Y');
2000-04-03 22:13:57 +00:00
else
2001-12-30 00:15:25 +00:00
drivers_chr (wid, y, 'N');
2000-04-03 22:13:57 +00:00
}
2001-12-30 00:15:25 +00:00
drivers_string (3, y, menu[i].text);
2000-04-03 22:13:57 +00:00
break;
case TYPE_SLID:
2001-12-30 00:15:25 +00:00
drivers_string (3, y, menu[i].text);
2000-04-03 22:13:57 +00:00
break;
case TYPE_MOVE:
break;
default:
break;
}
}
2000-04-03 22:13:57 +00:00
if (top != 0)
2001-12-30 00:15:25 +00:00
drivers_chr (1, 1, '^');
2000-04-03 22:13:57 +00:00
if (bottom < info->length)
2001-12-30 00:15:25 +00:00
drivers_chr (1, hgt, 'v');
2000-04-03 22:13:57 +00:00
draw_heartbeat ();
2001-12-30 00:15:25 +00:00
/*drivers_flush(); */
2000-04-03 22:13:57 +00:00
return 0;
1999-12-09 23:15:14 +00:00
}
static int
fill_menu_info (Menu menu, menu_info * info)
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
int i;
2000-04-03 22:13:57 +00:00
info->selected = 0;
2001-12-30 00:15:25 +00:00
/* count the entries in the menu*/
2000-04-03 22:13:57 +00:00
for (i = 0; menu[i].text; i++);
2000-04-03 22:13:57 +00:00
info->length = i;
2000-04-03 22:13:57 +00:00
return 0;
}
static int
menu_handle_action (menu_item * item)
{
2000-04-03 22:13:57 +00:00
return MENU_OK;
1999-12-09 23:15:14 +00:00
}
static int
slid_func (menu_item * item)
1999-12-09 23:15:14 +00:00
{
2000-04-03 22:13:57 +00:00
char str[16];
int key = 0;
int value = 0;
int x, y = 1;
int (*readfunc) (int);
2000-04-03 22:13:57 +00:00
readfunc = item->data;
1999-12-09 23:15:14 +00:00
2001-12-30 00:15:25 +00:00
/*drivers_init_hbar (); OBSOLETE */
2000-04-03 22:13:57 +00:00
while (key != 'A' && key != 'D') {
2001-12-30 00:15:25 +00:00
/* Draw the title... */
drivers_clear ();
drivers_chr (1, y, PAD);
drivers_chr (2, y, PAD);
drivers_string (4, y, item->text);
for (x = strlen (item->text) + 5; x <= display_props->width; x++)
drivers_chr (x, y, PAD);
1999-12-09 23:15:14 +00:00
2001-12-30 00:15:25 +00:00
/* Draw the slider now...*/
2000-04-03 22:13:57 +00:00
value = readfunc (MENU_READ);
if (value < 0 || value >= MENU_CLOSE)
return value;
snprintf (str, sizeof(str), "%i", value);
2001-12-30 00:15:25 +00:00
if (display_props->height >= 4) {
int promille;
drivers_string (8, 4, str);
value = (display_props->width * display_props->cellwidth * value / 256);
promille = (long) 100 * value / 256;
drivers_hbar (1, 3, display_props->width, promille, BAR_PATTERN_FILLED);
2000-04-03 22:13:57 +00:00
} else {
2001-12-30 00:15:25 +00:00
int promille;
drivers_string (17, 2, str);
value = ((display_props->width - 4) * display_props->cellwidth * value / 256);
promille = (long) 100 * value / 256;
drivers_hbar (1, 2, display_props->width, promille, BAR_PATTERN_FILLED);
2000-04-03 22:13:57 +00:00
}
2001-12-30 00:15:25 +00:00
/*drivers_flush(); */
2001-12-30 00:15:25 +00:00
for (key = drivers_getkey (); key == 0; key = drivers_getkey ()) {
/* do the heartbeat... */
2000-04-03 22:13:57 +00:00
draw_heartbeat ();
2001-12-30 00:15:25 +00:00
/* sleep for 1/8th second... */
2000-04-03 22:13:57 +00:00
framedelay ();
2001-12-30 00:15:25 +00:00
/* Check for client input... */
2000-04-03 22:13:57 +00:00
}
2000-04-03 22:13:57 +00:00
switch (key) {
case 'B':
value = readfunc (MENU_MINUS);
break;
case 'C':
value = readfunc (MENU_PLUS);
break;
}
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
if (value >= MENU_CLOSE || value < 0 || key == 'A' || key == 'D')
return value;
}
2000-04-03 22:13:57 +00:00
return MENU_OK;
1999-12-09 23:15:14 +00:00
}