davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

formerly fine working lib don't work with Jessie 2016-05-10

Sat May 21, 2016 6:46 pm

a formerly fine working lib don't work with Jessie 2016-05-10 any more.

My programs could be terminated by pressing "ESC" - don't work any longer.

why ??

Code: Select all

#ifndef RPICONIO_H
#define   RPICONIO_H

// courtesy of AndyD, raspberrypi.org forums, and peterfido, roboternetz.de forum
// version 003

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <termio.h>
#include <unistd.h>

#include <linux/input.h>
#include <termios.h>
#include <signal.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/select.h>


// Terminal: move cursor
#define cursup    "\033[A"
#define curshome  "\033[0;0H"

// keyboard dev
int    fkbd;
char * kbdin = "/dev/input/event0";
struct input_event ev[64];

int shiftl=0;
int shiftr=0;
int _keyshift_=0; // mod keypress state
int ctrll=0;
int ctrlr=0;
int _keyctrl_=0; // mod keypress state
int capsl=0;
int altl=0;
int altgr=0;
int _keyalt_=0;  // mod keypress state
int windows=0;
int kontext=0;
int _keypress_=0;  // keypress state
int modscode=0;
volatile int _kbscode_ ;

#define _ESC_   1
#define _F1_   59
#define _F2_   60
#define _F3_   61
#define _F4_   62
#define _F5_   63
#define _F6_   64
#define _F7_   65
#define _F8_   66
#define _F9_   67
#define _F10_  68
#define _F11_  69
#define _F12_  70



//*************************************************************
// conio.h - mimics
//*************************************************************

bool kbhit(void)
{
    struct termios original;
    tcgetattr(STDIN_FILENO, &original);

    struct termios term;
    memcpy(&term, &original, sizeof(term));

    term.c_lflag &= ~ICANON;
    tcsetattr(STDIN_FILENO, TCSANOW, &term);

    int characters_buffered = 0;
    ioctl(STDIN_FILENO, FIONREAD, &characters_buffered);

    tcsetattr(STDIN_FILENO, TCSANOW, &original);

    bool pressed = (characters_buffered != 0);

    return pressed;
}

void echoOff(void)
{
    struct termios term;
    tcgetattr(STDIN_FILENO, &term);

    term.c_lflag &= ~ECHO;
    tcsetattr(STDIN_FILENO, TCSANOW, &term);
}

void echoOn(void)
{
    struct termios term;
    tcgetattr(STDIN_FILENO, &term);

    term.c_lflag |= ECHO;
    tcsetattr(STDIN_FILENO, TCSANOW, &term);
}




//*************************************************************
// keyboard scancode
//*************************************************************

int getkbscancode() {
   int  rd,  size = sizeof (struct input_event);
   int keybscan=0;  // scan code "normal key"
   
   
   if ((rd = read (fkbd, ev, size * 64)) < size)
          printf ("Fehler mit Tastatur");
       
        if (ev[1].type != EV_KEY) return 0;
       
        if (ev[1].value==0){         //Taste losgelassen
                switch (ev[1].code) {
                    case 42: shiftl=0;   break;
                    case 54: shiftr=0;   break;
                    case 29: ctrll=0;    break;
                    case 97: ctrlr=0;    break;
                    case 56: altl=0;     break;
                    case 125: windows=0; break;
                    case 100: altgr=0;   break;
                    case 127: kontext=0; break;
                }
        }
        else
        {
            if (ev[1].value==1){           
             //==1 fuer nur einen Druck ohne Wiederholung. >=1 fuer Erkennung von gedrueckt gehaltener Taste
                 modscode = 0;
                 switch (ev[1].code) {
                    case 42: shiftl=1;   break;
                    case 54: shiftr=1;   break;
                    case 29: ctrll=1;    break;
                    case 97: ctrlr=1;    break;
                    case 56: altl=1;     break;
                    case 125: windows=1; break;
                    case 100: altgr=1;   break;
                    case 127: kontext=1; break;                   
                   
                    // Ab hier 'normale Tasten'                   
                    default: keybscan=ev[1].code;// Scancode ablegen
                   
                    _keypress_ = keybscan;  // standard keypress state
                    _keyshift_ = 0;         // reset modifier key pressed
                    _keyalt_   = 0;
                    _keyctrl_  = 0;
                                       
                    if(shiftl || shiftr ) { modscode+=1024; _keyshift_=1; } 
                    if(ctrll  || ctrlr  ) { modscode+=2048; _keyctrl_=1; }
                    if(altl)              { modscode+=4096; _keyalt_=1; }
                    if(altgr)             { modscode+=(2048+4096);  _keyalt_=1; _keyctrl_=1; }
                    if(windows)           modscode+=8192;   
                    if(kontext)           modscode+=16384; 
                                   
                    if(keybscan>0) {
                       _kbscode_= keybscan + modscode;
                       return keybscan;
                    }
                    else  return 0 ;
                //break;
                }
            }
        }
        return 0 ;
}
 

int setupKbdin() {
  if ((fkbd = open (kbdin, O_RDONLY)) == -1){
        printf ("Fehler mit Tastatur");
        return -1;
  }
  else return 0;
}


#endif

Code: Select all

// SNIP***

//*************************************************************


void* thread1Go(void *)   // medium priority:  keyboard + heartbeat monitoring
{
   int   c;  // keyboard key
   
   while(_TASKS_ACTIVE_) {         
      c=0;
      if (kbhit())    {
          c = getchar();          // ESC to quit program
          if( c==27 ) {
               printf("\n\n ESC pressed - program terminated by user \n");
               _TASKS_ACTIVE_=0;  // semaphore to stop all tasks
               printf("\n wait for tasks to join main()... \n\n");
               return NULL;
          }
      }    
   
   }         
   return NULL;
}


//SNIP ***
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

jahboater
Posts: 4778
Joined: Wed Feb 04, 2015 6:38 pm

Re: formerly fine working lib don't work with Jessie 2016-05

Sun May 22, 2016 8:37 pm

Because you have written it all by hand rather than using the standard, time proven, portable, library :-)

getch() in ncurses returns ESC correctly (of course) with Jessie 2016-05-10.

Return to “C/C++”