1999-12-09 23:15:14 +00:00
/*
2001-12-27 23:30:36 +00:00
* input.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 keypad (and other?) input from the user.
*/
1999-12-09 23:15:14 +00:00
2001-09-21 21:08:10 +00:00
1999-12-09 23:15:14 +00:00
#include <stdlib.h>
#include <stdio.h>
2001-04-27 02:20:38 +00:00
#include <string.h>
1999-12-09 23:15:14 +00:00
2001-05-25 03:14:27 +00:00
#include "shared/sockets.h"
2001-11-29 14:09:13 +00:00
#include "shared/report.h"
1999-12-09 23:15:14 +00:00
2001-12-30 00:15:25 +00:00
#include "drivers.h"
1999-12-09 23:15:14 +00:00
2002-04-26 00:00:31 +00:00
#include "client.h"
1999-12-09 23:15:14 +00:00
#include "screenlist.h"
2002-05-26 19:21:23 +00:00
#include "menuscreens.h"
1999-12-09 23:15:14 +00:00
#include "input.h"
2000-03-30 20:20:41 +00:00
int server_input ( int key );
2002-05-26 19:21:23 +00:00
void input_send_to_client ( Client * c , char * key );
2002-07-14 20:30:33 +00:00
void input_internal_key ( char * key );
1999-12-09 23:15:14 +00:00
2002-05-26 19:21:23 +00:00
LinkedList * keylist ;
1999-12-09 23:15:14 +00:00
2002-05-26 19:21:23 +00:00
int init_input ()
1999-12-09 23:15:14 +00:00
{
2002-07-14 20:30:33 +00:00
debug ( RPT_DEBUG , "%s()" , __FUNCTION__ );
1999-12-09 23:15:14 +00:00
2002-05-26 19:21:23 +00:00
keylist = LL_new ();
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
return 0 ;
1999-12-09 23:15:14 +00:00
}
2002-05-26 19:21:23 +00:00
int
handle_input ()
{
char * key ;
2002-07-14 20:30:33 +00:00
Client * current_client ;
Client * target ;
2002-05-26 19:21:23 +00:00
KeyReservation * kr ;
2002-07-14 20:30:33 +00:00
debug ( RPT_DEBUG , "%s()" , __FUNCTION__ );
2002-05-26 19:21:23 +00:00
2002-07-14 20:30:33 +00:00
current_client = screenlist_current () -> client ;
2002-05-26 19:21:23 +00:00
/* Handle all keypresses */
while (( key = drivers_get_key ()) != NULL ) {
/* Find what client wants the key */
2002-07-14 20:30:33 +00:00
kr = input_find_key ( key , current_client );
2002-05-26 19:21:23 +00:00
if ( kr ) {
/* A hit ! */
2002-07-14 20:30:33 +00:00
report ( RPT_DEBUG , "%s: reserved key: \" %.40s \" " , __FUNCTION__ , key );
target = kr -> client ;
} else {
report ( RPT_DEBUG , "%s: left over key: \" %.40s \" " , __FUNCTION__ , key );
/*target = current_client;*/
target = NULL ; /* left-over keys are always for internal client */
2002-05-26 19:21:23 +00:00
}
2002-07-14 20:30:33 +00:00
if ( target == NULL ) {
report ( RPT_DEBUG , "%s: key is for internal client" , __FUNCTION__ );
input_internal_key ( key );
} else {
/* It's an external client */
report ( RPT_DEBUG , "%s: key is for external client on socket %d" , __FUNCTION__ , target -> sock );
input_send_to_client ( current_client , key );
2002-05-26 19:21:23 +00:00
}
}
return 0 ;
}
void input_send_to_client ( Client * c , char * key )
{
char * s ;
2002-07-14 20:30:33 +00:00
debug ( RPT_DEBUG , "%s( client=[%d], key= \" %.40s \" )" , __FUNCTION__ , c -> sock , key );
2002-05-26 19:21:23 +00:00
/* Allocate just as much as we need */
s = malloc ( strlen ( key ) + strlen ( "key \n " ) + 1 );
sprintf ( s , "key %s \n " , key );
sock_send_string ( c -> sock , s );
free ( s );
}
void
2002-07-14 20:30:33 +00:00
input_internal_key ( char * key )
2002-05-26 19:21:23 +00:00
{
2002-07-14 20:30:33 +00:00
if ( is_menu_key ( key ) || screenlist_current () == menuscreen ) {
menuscreen_key_handler ( key );
2002-05-26 19:21:23 +00:00
}
else {
/* TODO: give keys to server screen */
}
}
/* TODO: REPLACE THE FOLLOWING FUNCTION BY SOMETHING NEW */
2000-03-30 20:20:41 +00:00
int
server_input ( int key )
1999-12-09 23:15:14 +00:00
{
2002-07-14 20:30:33 +00:00
debug ( RPT_DEBUG , "%s( key='%c' )" , __FUNCTION__ , ( char ) key );
2000-03-30 20:20:41 +00:00
2001-09-21 21:08:10 +00:00
switch (( char ) key ) {
2001-12-30 00:15:25 +00:00
case INPUT_PAUSE_KEY :
2001-09-21 21:08:10 +00:00
if ( screenlist_action == SCR_HOLD )
screenlist_action = 0 ;
else
screenlist_action = SCR_HOLD ;
break ;
2001-12-30 00:15:25 +00:00
case INPUT_BACK_KEY :
2001-09-21 21:08:10 +00:00
screenlist_action = SCR_BACK ;
screenlist_prev ();
break ;
2001-12-30 00:15:25 +00:00
case INPUT_FORWARD_KEY :
2001-09-21 21:08:10 +00:00
screenlist_action = SCR_SKIP ;
screenlist_next ();
break ;
2001-12-30 00:15:25 +00:00
case INPUT_MAIN_MENU_KEY :
2002-05-26 19:21:23 +00:00
debug ( RPT_DEBUG , "%s: got the menu key!" , __FUNCTION__ );
2002-04-26 00:00:31 +00:00
//server_menu ();
report ( RPT_ERR , "MENU TEMPORATY DISABLED" );
2001-09-21 21:08:10 +00:00
break ;
default :
2002-05-26 19:21:23 +00:00
debug ( RPT_DEBUG , "%s: Unused key \" %c \" (%i)" , __FUNCTION__ , ( char ) key , key );
2001-09-21 21:08:10 +00:00
break ;
2000-04-03 22:13:57 +00:00
}
1999-12-09 23:15:14 +00:00
2000-04-03 22:13:57 +00:00
return 0 ;
1999-12-09 23:15:14 +00:00
}
2002-05-26 19:21:23 +00:00
int input_reserve_key ( char * key , bool exclusive , Client * client )
{
KeyReservation * kr ;
2002-07-14 20:30:33 +00:00
debug ( RPT_DEBUG , "%s( key= \" %.40s \" , exclusive=%d, client=[%d] )" , __FUNCTION__ , key , exclusive , ( client ? client -> sock : - 1 ));
2002-05-26 19:21:23 +00:00
/* Find out if this key is already reserved in a way that interferes
* with the new reservation.
*/
for ( kr = LL_GetFirst ( keylist ); kr ; kr = LL_GetNext ( keylist )) {
if ( strcmp ( kr -> key , key ) == 0 ) {
if ( kr -> exclusive || exclusive ) {
/* Sorry ! */
return - 1 ;
}
}
}
/* We can now safely add it ! */
kr = malloc ( sizeof ( KeyReservation ));
kr -> key = strdup ( key );
kr -> exclusive = exclusive ;
kr -> client = client ;
LL_Push ( keylist , kr );
2002-07-14 20:30:33 +00:00
report ( RPT_INFO , "Key \" %.40s \" is now reserved in %s mode by client [%d]" , key , ( exclusive ? "exclusive" : "shared" ), ( client ? client -> sock : - 1 ));
2002-05-26 19:21:23 +00:00
return 0 ;
}
void input_release_key ( char * key , Client * client )
{
KeyReservation * kr ;
2002-07-14 20:30:33 +00:00
debug ( RPT_DEBUG , "%s( key= \" %.40s \" , client=[%d] )" , __FUNCTION__ , key , ( client ? client -> sock : - 1 ));
2002-05-26 19:21:23 +00:00
for ( kr = LL_GetFirst ( keylist ); kr ; kr = LL_GetNext ( keylist )) {
if ( kr -> client == client
&& strcmp ( kr -> key , key ) == 0 ) {
free ( kr -> key );
free ( kr );
LL_DeleteNode ( keylist );
2002-07-14 20:30:33 +00:00
report ( RPT_INFO , "Key \" %.40s \" was reserved in %s mode by client [%d] and is now released" , key , ( kr -> exclusive ? "exclusive" : "shared" ), ( client ? client -> sock : - 1 ));
2002-05-26 19:21:23 +00:00
return ;
}
}
}
void input_release_client_keys ( Client * client )
{
KeyReservation * kr ;
2002-07-14 20:30:33 +00:00
debug ( RPT_DEBUG , "%s( client=[%d] )" , __FUNCTION__ , ( client ? client -> sock : - 1 ));
2002-05-26 19:21:23 +00:00
kr = LL_GetFirst ( keylist );
while ( kr ) {
if ( kr -> client == client ) {
2002-07-14 20:30:33 +00:00
report ( RPT_INFO , "Key \" %.40s \" was reserved in %s mode by client [%d] and is now released" , kr -> key , ( kr -> exclusive ? "exclusive" : "shared" ), ( client ? client -> sock : - 1 ));
2002-05-26 19:21:23 +00:00
free ( kr -> key );
free ( kr );
LL_DeleteNode ( keylist );
kr = LL_Get ( keylist );
} else {
kr = LL_GetNext ( keylist );
}
}
}
KeyReservation * input_find_key ( char * key , Client * client )
{
KeyReservation * kr ;
2002-07-14 20:30:33 +00:00
debug ( RPT_DEBUG , "%s( key= \" %.40s \" , client=[%d] )" , __FUNCTION__ , key , ( client ? client -> sock : - 1 ));
2002-05-26 19:21:23 +00:00
for ( kr = LL_GetFirst ( keylist ); kr ; kr = LL_GetNext ( keylist )) {
if ( strcmp ( kr -> key , key ) == 0 ) {
if ( kr -> exclusive || client == kr -> client ) {
return kr ;
}
}
}
return NULL ;
}