/* $Id:$
 *
 * funky.c --- Keycode Daemon for Funny/Function Keys.
 * 
 * Taps /dev/funkey and starts the required action for that keycode.
 *
 * (c) 2000 Rick van Rein.
 * Released under the GNU Public License.
 */


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <stdio.h>


void keypress (unsigned char buf) {
	char *cmd=NULL;
	switch (buf) {
	case 89:	/* Sound mute */
		cmd="/usr/bin/aumix -v 0";
		break;
	case 90:	/* Sound softer */
		cmd="/usr/bin/aumix -v -2";
		break;
	case 91:	/* Sound louder */
		cmd="/usr/bin/aumix -v +2";
		break;
	case 92:	/* Start CD playing */
		cmd="/usr/bin/cdcd play";
		break;
	case 93:	/* Stop CD playing */
		cmd="/usr/bin/cdcd stop";
		break;
	case 94:	/* Skip to previous CD track */
		cmd="/usr/bin/cdcd prev";
		break;
	case 95:	/* Skip to next CD track */
		cmd="/usr/bin/cdcd next";
		break;
	case 118:	/* Jump to init mode 3 (textual) */
		cmd="/sbin/init 3";
		break;
	case 119+128:	/* Put Linux in User Suspend (at key_up) */
		cmd="/usr/bin/apm -S";
		break;
	case 120:	/* Bring Linux to a halt */
		cmd="/sbin/halt";
		break;
	case 121:	/* Fetch email from everywhere around */
		cmd="/usr/bin/fetchmail -s -f /etc/fetchmailrc 2>/dev/console >/dev/console &";
		break;
	case 122:	/* Change to runlevel 3 */
		cmd="/sbin/init 3";
		break;
	default:	/* Ignore the key */
		return;
	}
	system (cmd);
}


int main (int argc, char *argv[]) {
	int funkey;
	unsigned char buf;

	funkey = open ("/dev/funkey", O_RDONLY);
	if (funkey < 0) {
		perror ("Cannot read /dev/funkey");
		exit (1);
	}
	while (read (funkey, &buf, 1)) {
		keypress (buf);
	}
	close (funkey);

	return 0;
}
