/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *-------------------------------------------------------------------------
 * by Mathieu Virbel, for sk-8135 keyboard (25/11/05)
 * based on http://www.frogmouth.net/hid-doco/c537.html examples.
 *
 * You need :
 * - kernel headers (tested with 2.6.12-9-686-smp)
 * - libxosd-dev
 * - aumix 
 *
 * Compilation : 
 *  make
 * 
 * Exemple : 
 * 	./sk8135-pcm /dev/input/event2
 *-------------------------------------------------------------------------
 */


#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <asm/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include <xosd.h>


#include <linux/input.h>

int main (int argc, char **argv) {

    int fd = -1;        		/* the file descriptor for the device */
    int yalv;           		/* loop counter */
    size_t read_bytes;  		/* how many bytes were read */
    struct input_event ev[64];	/* the events (up to 64 at once) */
	char buffer[50];			/* buffer for aumix command */
	xosd *osd = NULL;			/* osd structure to display volume */
	FILE* fd_vol = NULL;		/* file descriptor to retreive volume */
	int volume = 0;				/* last volume info */
	char buf_vol[101];			/* nice osd buffer */
	int i = 0;

    /* read() requires a file descriptor, so we check for one, and then open it */
    if (argc != 2) {
		fprintf(stderr, "usage: %s event-device - probably /dev/input/event2\n", argv[0]);
		exit(1);
    }
    if ((fd = open(argv[1], O_RDONLY)) < 0) {
		perror("evdev open");
		exit(1);
    }

	/* create osd object */
	osd = xosd_create(1);
	xosd_set_font(osd, "-*-lucidatypewriter-*-r-*-*-12-*-*-*-*-*-*-*");
	xosd_set_colour(osd, "Green");
	xosd_set_timeout(osd, 2);
	xosd_set_shadow_offset(osd, 2);
  	xosd_set_outline_offset(osd, 1);
	xosd_set_vertical_offset(osd, 10);


	while (1) {

		read_bytes = read(fd, ev, sizeof(struct input_event) * 64);

		if (read_bytes < (int) sizeof(struct input_event)) {
			perror("evtest: short read");
			exit (1);
		}

		for (yalv = 0; yalv < (int) (read_bytes / sizeof(struct input_event)); yalv++) {

			/* check type/code of event */
			if (ev[yalv].type == 3 && ev[yalv].code == 32) {

				/* adapt volume */
				if (ev[yalv].value >= 0)
					sprintf(buffer, "aumix -w+%d", ev[yalv].value);
				else
					sprintf(buffer, "aumix -w%d", ev[yalv].value);

				/* launch aumix */
				system(buffer);

				/* read volume */
				fd_vol = popen("aumix -q | grep pcm | cut -d\\  -f3", "r");
				if (fd_vol) {
					fscanf(fd_vol, "%d", &volume);	
					fclose(fd_vol);
           
					/* nice display */
					buf_vol[100] = '\0';
					for (i = 0; i < 100; i++) {
						if (i < volume) 
							buf_vol[i] = '+';
						else
							buf_vol[i] = '-';
					}
						
					xosd_display(osd, 0, XOSD_string, buf_vol);
				}
			}
		}
	}

	close(fd);
	xosd_destroy(osd);

	exit(0);
}
