Commit 2ac9c967 authored by Benjamin Drieu's avatar Benjamin Drieu

Initial import

parents
This diff is collapsed.
This diff is collapsed.
SUBDIRS = src
#!/bin/sh
# Touch the timestamps on all the files since CVS messes them up
directory=`dirname $0`
touch $directory/configure.in
aclocal \
&& autoheader \
&& automake -c -a \
&& autoconf2.50 \
&& ./configure $*
[BookyPedal]
Device=/dev/infinity-in-usb-2
[LeftDown]
Exec = killall ogg123
[CenterDown]
File = /tmp/foo
Message = Down\n
Create = 1
[CenterUp]
File = /tmp/foo
Message = Up\n
Create = 1
/* config.h.in. Generated from configure.ac by autoheader. */
/* Define to 1 if you have the <fcntl.h> header file. */
#undef HAVE_FCNTL_H
/* Define to 1 if you have the <inttypes.h> header file. */
#undef HAVE_INTTYPES_H
/* Define to 1 if you have the <memory.h> header file. */
#undef HAVE_MEMORY_H
/* Define to 1 if you have the <stdint.h> header file. */
#undef HAVE_STDINT_H
/* Define to 1 if you have the <stdlib.h> header file. */
#undef HAVE_STDLIB_H
/* Define to 1 if you have the <strings.h> header file. */
#undef HAVE_STRINGS_H
/* Define to 1 if you have the <string.h> header file. */
#undef HAVE_STRING_H
/* Define to 1 if you have the <sys/stat.h> header file. */
#undef HAVE_SYS_STAT_H
/* Define to 1 if you have the <sys/types.h> header file. */
#undef HAVE_SYS_TYPES_H
/* Define to 1 if you have the <unistd.h> header file. */
#undef HAVE_UNISTD_H
/* Define to the address where bug reports for this package should be sent. */
#undef PACKAGE_BUGREPORT
/* Define to the full name of this package. */
#undef PACKAGE_NAME
/* Define to the full name and version of this package. */
#undef PACKAGE_STRING
/* Define to the one symbol short name of this package. */
#undef PACKAGE_TARNAME
/* Define to the home page for this package. */
#undef PACKAGE_URL
/* Define to the version of this package. */
#undef PACKAGE_VERSION
/* Define to 1 if you have the ANSI C header files. */
#undef STDC_HEADERS
AC_INIT(pedalbear, 0.1)
AC_CONFIG_SRCDIR(src/pedalbear.c)
AC_CONFIG_HEADER(config.h)
AM_INIT_AUTOMAKE([-Wall foreign])
AM_MAINTAINER_MODE
AC_GNU_SOURCE
# Checks for programs.
AC_PROG_CC
# Checks for libraries.
# Checks for header files.
AC_CHECK_HEADERS([fcntl.h stdlib.h string.h])
# Checks for typedefs, structures, and compiler characteristics.
# Checks for library functions.
PKG_CHECK_MODULES(GLIB, glib-2.0)
AC_CONFIG_FILES([Makefile src/Makefile])
AC_OUTPUT
bin_PROGRAMS = pedalbear
pedalbear_SOURCES = pedalbear.c
pedalbear_LDADD = $(GLIB_LIBS)
pedalbear_CFLAGS = $(GLIB_CFLAGS)
#include <stdio.h>
#include <fcntl.h>
#include <linux/input.h>
#include <glib.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
struct hiddev_event {
unsigned hid;
signed int value;
};
static gboolean verbose = FALSE;
gchar * pedals [ ] = { "Left", "Center", "Right" };
gchar * states [ ] = { "Up", "Down" };
void main_loop ( GKeyFile * config_file, gchar * device );
int main ( int argc, gchar * argv [ ] )
{
static gchar * config_filename = NULL;
static gchar * device = NULL;
GKeyFile * config_file;
static GOptionEntry entries [ ] =
{
{ "config", 'c', 0, G_OPTION_ARG_STRING, &config_filename, "Set config file", NULL },
{ "device", 'd', 0, G_OPTION_ARG_STRING, &device, "Set to alternative device", NULL },
{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL },
{ NULL }
};
GError * error = NULL;
GOptionContext * context;
context = g_option_context_new ("- Booky transcription pedal daemon");
g_option_context_add_main_entries (context, entries, "Bookypedal");
if ( ! g_option_context_parse ( context, &argc, &argv, &error ) )
{
g_print ("option parsing failed: %s\n", error->message);
exit ( 1 );
}
g_option_context_set_help_enabled ( context, TRUE );
config_file = g_key_file_new ( ) ;
g_key_file_load_from_file ( config_file,
( config_filename ? config_filename : "/etc/pedalbear.conf" ),
G_KEY_FILE_NONE, &error );
if ( error )
{
g_printerr ( "Cannot open config file %s: %s\n", ( config_filename ?
config_filename : "/etc/pedalbear.conf" ),
error -> message );
g_error_free ( error );
exit ( 1 );
}
main_loop ( config_file, device );
}
/** Main loop. */
void main_loop ( GKeyFile * config_file, gchar * device )
{
struct hiddev_event event;
int fd, i;
int state [ ] = { 0, 0, 0 };
int new_state [ ] = { 0, 0, 0 };
gchar * filename = NULL;
if ( device )
{
filename = g_strdup ( device );
}
else
{
filename = g_key_file_get_string ( config_file, "BookyPedal", "Device", NULL );
if ( ! filename )
{
g_printerr ( "No device found in config file and no device specified at command line.\nAborting.\n" );
exit ( 1 );
}
}
fd = open ( filename, O_NONBLOCK );
if ( verbose )
g_printf ( "Opening device '%s'\n", filename );;
if ( fd < 0 )
{
g_printerr ( "Cannot open device %s: %s.\n", filename, g_strerror ( errno ) );
exit ( 1 );
}
g_free ( filename );
while ( 1 )
{
bcopy ( new_state, state, sizeof ( int ) * 3 );
for ( i = 0 ; i < 3 ; i ++ )
{
while ( 1 )
{
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 );
}
}
}
for ( i = 0 ; i < 3 ; i ++ )
{
if ( new_state [ i ] != state [ i ] )
{
gchar * group_name = g_strconcat ( pedals [ i ], states [ new_state [ i ] ], NULL );
if ( verbose )
g_printf ( "%s pedal : %s\n", group_name, states [ new_state [ i ] ] );
if ( g_key_file_has_group ( config_file, group_name ) )
{
gchar * cmd, * file, * msg;
gboolean create;
cmd = g_key_file_get_string ( config_file, group_name, "Exec", 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 );
if ( file && msg )
{
int out = open ( file, O_WRONLY | O_APPEND | ( create ? O_CREAT : 0 ) );
if ( verbose )
g_printf ( "Appending data to %s : '%s'\n", file, msg );
if ( out >= 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 ) );
}
if ( cmd )
{
g_spawn_command_line_sync ( cmd, NULL, NULL, NULL, NULL );
}
}
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