Commit 9a08a7b1 authored by Benjamin Drieu's avatar Benjamin Drieu

Implement repetition

parent 166f7d77
......@@ -14,6 +14,8 @@ struct hiddev_event {
static gboolean verbose = FALSE;
gchar * pedals [ ] = { "Left", "Center", "Right" };
gchar * states [ ] = { "Up", "Down" };
gint grace [ ] = { 0, 0, 0 };
#define MAX_GRACE 5
void main_loop ( GKeyFile * config_file, gchar * device );
......@@ -69,6 +71,8 @@ void main_loop ( GKeyFile * config_file, gchar * device )
int state [ ] = { 0, 0, 0 };
int new_state [ ] = { 0, 0, 0 };
gchar * filename = NULL;
fd_set rfds;
struct timeval tv;
if ( device )
{
......@@ -96,81 +100,118 @@ void main_loop ( GKeyFile * config_file, gchar * device )
while ( 1 )
{
int retval;
FD_ZERO ( &rfds );
FD_SET ( fd, &rfds );
tv.tv_sec = 0;
tv.tv_usec = 50000;
retval = select ( fd + 1, &rfds, NULL, NULL, &tv );
bcopy ( new_state, state, sizeof ( int ) * 3 );
for ( i = 0 ; i < 3 ; i ++ )
if ( retval )
{
while ( 1 )
for ( i = 0 ; i < 3 ; i ++ )
{
int rcount = read ( fd, &event, sizeof ( struct hiddev_event ) );
if ( rcount > 0 && ( event.hid & 0xFF ) == ( i + 1 ) )
{
new_state [ i ] = event.value;
break;
}
else if ( rcount <= 0 )
{
usleep ( 5 * 1000 );
}
}
if ( verbose )
printf ( "New states : %d,%d,%d\n", new_state [ 0 ], new_state [ 1 ], new_state [ 2 ] );
}
for ( i = 0 ; i < 3 ; i ++ )
handle_states ( config_file, state, new_state );
}
}
handle_states ( GKeyFile * config_file, int state [ ], int new_state [ ] )
{
gint i;
for ( i = 0 ; i < 3 ; i ++ )
{
gchar * group_name = g_strconcat ( pedals [ i ], states [ new_state [ i ] ], NULL );
if ( state [ i ] == new_state [ i ] == 1 && g_key_file_get_boolean ( config_file, group_name, "Repeat", 0 ) )
{
if ( new_state [ i ] != state [ i ] )
if ( grace [ i ] < MAX_GRACE )
{
gchar * group_name = g_strconcat ( pedals [ i ], states [ new_state [ i ] ], NULL );
grace [ i ] ++;
continue;
}
}
if ( verbose )
g_printf ( "%s pedal : %s\n", group_name, states [ new_state [ i ] ] );
if ( ! new_state [ i ] )
{
grace [ i ] = 0;
}
if ( new_state [ i ] != state [ i ] ||
( new_state [ i ] == 1 && g_key_file_get_boolean ( config_file, group_name, "Repeat", 0 ) ) )
{
if ( verbose )
g_printf ( "%s pedal : %s vs %s (grace %d)\n", group_name, states [ new_state [ i ] ], states [ state [ i ] ],
grace [ i ] );
if ( g_key_file_has_group ( config_file, group_name ) )
if ( g_key_file_has_group ( config_file, group_name ) )
{
gchar * cmd, * key, * msg, * file;
gboolean create;
cmd = g_key_file_get_string ( config_file, group_name, "Exec", NULL );
key = g_key_file_get_string ( config_file, group_name, "Key", NULL );
file = g_key_file_get_string ( config_file, group_name, "File", NULL );
msg = g_key_file_get_string ( config_file, group_name, "Message", NULL );
create = g_key_file_get_boolean ( config_file, group_name, "Create", 0 );
/* Message appended to file */
if ( file && msg )
{
gchar * cmd, * key, * msg, * file;
gboolean create;
cmd = g_key_file_get_string ( config_file, group_name, "Exec", NULL );
key = g_key_file_get_string ( config_file, group_name, "Key", NULL );
file = g_key_file_get_string ( config_file, group_name, "File", NULL );
msg = g_key_file_get_string ( config_file, group_name, "Message", NULL );
create = g_key_file_get_boolean ( config_file, group_name, "Create", 0 );
/* Message appended to file */
if ( file && msg )
int out = open ( file, O_WRONLY | O_APPEND | ( create ? O_CREAT : 0 ), 0644 );
if ( verbose )
g_printf ( "Appending data to %s : '%s'\n", file, msg );
if ( out >= 0 )
{
int out = open ( file, O_WRONLY | O_APPEND | ( create ? O_CREAT : 0 ), 0644 );
if ( verbose )
g_printf ( "Appending data to %s : '%s'\n", file, msg );
if ( out >= 0 )
if ( write ( out, msg, strlen ( msg ) ) < 0 )
{
if ( write ( out, msg, strlen ( msg ) ) < 0 )
{
g_printerr ( "Cannot write to file %s : %s\n", file, g_strerror ( errno ) );
}
close ( file );
}
else
g_printerr ( "Cannot write to file %s : %s\n", file, g_strerror ( errno ) );
}
close ( file );
}
else
g_printerr ( "Cannot write to file %s : %s\n", file, g_strerror ( errno ) );
/* X keys sent */
if ( key )
{
if ( verbose )
g_printf ( "Sending key '%s'\n", key );
sendkey ( key );
}
}
/* X keys sent */
if ( key )
{
if ( verbose )
g_printf ( "Sending key '%s'\n", key );
sendkey ( key );
}
/* Command executed */
if ( cmd )
{
if ( verbose )
g_printf ( "Executing %s\n", cmd );
g_spawn_command_line_sync ( cmd, NULL, NULL, NULL, NULL );
}
/* Command executed */
if ( cmd )
{
if ( verbose )
g_printf ( "Executing %s\n", cmd );
g_spawn_command_line_sync ( cmd, NULL, NULL, NULL, NULL );
}
g_free ( group_name );
}
}
g_free ( group_name );
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment