#ifndef HAVE_CONFIG_H
#include "config.h"
#endif

#include <errno.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <lirc/lirc_client.h>

#ifndef IR_DECL
#include "irc.h"
#endif

irc::irc(void)
{
   int    sock;                    // Socket for LIRC
   int    flags;                   // Flags used to set non-blocking mode

   printf("Initialising IR\n");

   sock = lirc_init("mp3machine",1);
   if(sock==-1) {
       printf("Initialisation failed\n");
       exit(0);
   }

   printf("Setting to non blocking mode\n");
   fcntl(sock,F_SETOWN,getpid());
   flags=fcntl(sock,F_GETFL,0);
   if(flags!=-1)
   {
      fcntl(sock,F_SETFL,flags|O_NONBLOCK);
   }
}


char irc::get_key(void)
{
   char *code;
   char str_code[100];
   char ir_code = 0;

   lirc_nextcode(&code);
   if(code!=NULL)
   {
      strcpy(str_code, &code[20]);
      str_code[strlen(str_code) - 17] = '\0';

      // Cursor keys = 27, 91 then LEFT=68, RIGHT=67, UP=65, DOWN=66
      if (strcmp(str_code, "PG_DN_UP")==0)    ir_code = 66;
      if (strcmp(str_code, "PG_UP_UP")==0)    ir_code = 65;
      if (strcmp(str_code, "BACK_UP")==0)     ir_code = 68;
      if (strcmp(str_code, "PLAY_UP")==0)     ir_code = 67;
      if (strcmp(str_code, "FORWARD_UP")==0)  ir_code = 67;
      if (strcmp(str_code, "DISPLAY_UP")==0)  ir_code = 'e';
      if (strcmp(str_code, "PAUSE_UP")==0)    ir_code = 'p';
      if (strcmp(str_code, "STOP_UP")==0)     ir_code = 's';
      if (strcmp(str_code, "FWD_UP")==0)      ir_code = '.';
      if (strcmp(str_code, "REW_UP")==0)      ir_code = ',';
      if (strcmp(str_code, "1_UP")==0)        ir_code = '1';
      if (strcmp(str_code, "2_UP")==0)        ir_code = '2';
      if (strcmp(str_code, "3_UP")==0)        ir_code = '3';
      if (strcmp(str_code, "0_UP")==0)        ir_code = '4';
      if (strcmp(str_code, "CLOSE_UP")==0)    ir_code = 'q';
   }
   free(code);

   return(ir_code);
}

irc::~irc(void)
{
     	lirc_deinit();
}



syntax highlighted by Code2HTML, v. 0.9.1