[PD-cvs] externals/hcs/hid TODO, 1.28, 1.29 hid.c, 1.29, 1.30 hid_linux.c, 1.20, 1.21

Hans-Christoph Steiner eighthave at users.sourceforge.net
Fri Aug 25 03:41:51 CEST 2006


Update of /cvsroot/pure-data/externals/hcs/hid
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11890

Modified Files:
	TODO hid.c hid_linux.c 
Log Message:
code cleanups, replaced sprintf() for snprintf() for security's sake

Index: TODO
===================================================================
RCS file: /cvsroot/pure-data/externals/hcs/hid/TODO,v
retrieving revision 1.28
retrieving revision 1.29
diff -C2 -d -r1.28 -r1.29
*** TODO	15 Aug 2006 01:50:17 -0000	1.28
--- TODO	25 Aug 2006 01:41:49 -0000	1.29
***************
*** 1,2 ****
--- 1,8 ----
+ 
+ ______________________________________________________________________________
+ - switch to snprintf
+ 
+ in hid_darwin.c, replace all sprintf()s with snprintf()s.
+ 
  ______________________________________________________________________________
  - deal with hatswitches!!

Index: hid_linux.c
===================================================================
RCS file: /cvsroot/pure-data/externals/hcs/hid/hid_linux.c,v
retrieving revision 1.20
retrieving revision 1.21
diff -C2 -d -r1.20 -r1.21
*** hid_linux.c	22 Aug 2006 04:13:14 -0000	1.20
--- hid_linux.c	25 Aug 2006 01:41:49 -0000	1.21
***************
*** 16,21 ****
  #include "hid.h"
  
! #define DEBUG(x)
! //#define DEBUG(x) x 
  
  #define LINUX_BLOCK_DEVICE   "/dev/input/event"
--- 16,21 ----
  #include "hid.h"
  
! //#define DEBUG(x)
! #define DEBUG(x) x 
  
  #define LINUX_BLOCK_DEVICE   "/dev/input/event"
***************
*** 50,68 ****
  t_symbol* hid_convert_linux_buttons_to_numbers(__u16 linux_code)
  {
!     char hid_code[10];
      if(linux_code >= 0x100) 
      {
!         if(linux_code < BTN_MOUSE)   
!             sprintf(hid_code,"btn_%d",linux_code - BTN_MISC);  /* numbered buttons */
!         else if(linux_code < BTN_JOYSTICK)
!             sprintf(hid_code,"btn_%d",linux_code - BTN_MOUSE);  /* mouse buttons */
!         else if(linux_code < BTN_GAMEPAD)
!             sprintf(hid_code,"btn_%d",linux_code - BTN_JOYSTICK);  /* joystick buttons */
!         else if(linux_code < BTN_DIGI)
!             sprintf(hid_code,"btn_%d",linux_code - BTN_GAMEPAD);  /* gamepad buttons */
!         else if(linux_code < BTN_WHEEL)
!             sprintf(hid_code,"btn_%d",linux_code - BTN_DIGI);  /* tablet buttons */
!         else if(linux_code < KEY_OK)
!             sprintf(hid_code,"btn_%d",linux_code - BTN_WHEEL);  /* wheel buttons */
  	else return 0;
      }
--- 50,68 ----
  t_symbol* hid_convert_linux_buttons_to_numbers(__u16 linux_code)
  {
!     char hid_code[MAXPDSTRING];
      if(linux_code >= 0x100) 
      {
!         if(linux_code < BTN_MOUSE)         /* numbered buttons */
!             snprintf(hid_code, MAXPDSTRING,"btn_%d",linux_code - BTN_MISC);  
!         else if(linux_code < BTN_JOYSTICK) /* mouse buttons */
!             snprintf(hid_code, MAXPDSTRING,"btn_%d",linux_code - BTN_MOUSE);
!         else if(linux_code < BTN_GAMEPAD)  /* joystick buttons */
!             snprintf(hid_code, MAXPDSTRING,"btn_%d",linux_code - BTN_JOYSTICK);
!         else if(linux_code < BTN_DIGI)     /* gamepad buttons */
!             snprintf(hid_code, MAXPDSTRING,"btn_%d",linux_code - BTN_GAMEPAD);
!         else if(linux_code < BTN_WHEEL)    /* tablet buttons */
!             snprintf(hid_code, MAXPDSTRING,"btn_%d",linux_code - BTN_DIGI);
!         else if(linux_code < KEY_OK)       /* wheel buttons */
!             snprintf(hid_code, MAXPDSTRING,"btn_%d",linux_code - BTN_WHEEL);
  	else return 0;
      }
***************
*** 79,86 ****
      if(linux_code > 226)
          return 0;
-     /*         quick hack to get the keys         */
-     /* (in future this should be auto-generated)  */
  
!     static char key_names[227][20] =
          { 
              "key_reserved", "key_esc", "key_1", "key_2", "key_3", "key_4", 
--- 79,84 ----
      if(linux_code > 226)
          return 0;
  
!     static char key_names[227][32] =
          { 
              "key_reserved", "key_esc", "key_1", "key_2", "key_3", "key_4", 
***************
*** 264,274 ****
      debug_print(LOG_DEBUG,"hid_print_device_list");
      int i,fd;
!     char device_output_string[256] = "Unknown";
!     char dev_handle_name[20] = "/dev/input/event0";
  
      post("");
      for(i=0;i<128;++i) 
      {
!         sprintf(dev_handle_name,"/dev/input/event%d",i);
          if(dev_handle_name) 
          {
--- 262,272 ----
      debug_print(LOG_DEBUG,"hid_print_device_list");
      int i,fd;
!     char device_output_string[MAXPDSTRING] = "Unknown";
!     char dev_handle_name[MAXPDSTRING] = "/dev/input/event0";
  
      post("");
      for(i=0;i<128;++i) 
      {
!         snprintf(dev_handle_name, MAXPDSTRING, "/dev/input/event%d", i);
          if(dev_handle_name) 
          {
***************
*** 293,296 ****
--- 291,376 ----
  }
  
+ 
+ 
+ static void hid_build_element_list(t_hid *x) 
+ {
+     post("hid_build_element_list");
+     unsigned long element_bitmask[EV_MAX][NBITS(KEY_MAX)];
+     uint8_t abs_bitmask[ABS_MAX/8 + 1];
+     struct input_absinfo abs_features;
+     t_hid_element *new_element = NULL;
+     t_int i, j;
+   
+     element_count[x->x_device_number] = 0;
+     if( x->x_fd ) 
+     {
+         new_element = getbytes(sizeof(t_hid_element));
+         /* get bitmask representing supported elements (axes, keys, etc.) */
+         memset(element_bitmask, 0, sizeof(element_bitmask));
+         if( ioctl(x->x_fd, EVIOCGBIT(0, EV_MAX), element_bitmask[0]) < 0 )
+             perror("[hid] error: evdev ioctl: element_bitmask");
+         memset(abs_bitmask, 0, sizeof(abs_bitmask));
+         if( ioctl(x->x_fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask) < 0 ) 
+             perror("[hid] error: evdev ioctl: abs_bitmask");
+         for( i = 1; i < EV_MAX; i++ ) 
+         {
+             if(test_bit(i, element_bitmask[0])) 
+             {
+                 new_element->linux_type = i;
+                 new_element->type = gensym(ev[i] ? ev[i] : "?");
+                 /* get bitmask representing supported button types */
+                 ioctl(x->x_fd, EVIOCGBIT(i, KEY_MAX), element_bitmask[i]);
+                 /* cycle through all possible event codes (axes, keys, etc.) 
+                  * testing to see which are supported.
+                  * i = i   j = j
+                  */
+                 for(j = 0; j < KEY_MAX; j++) 
+                 {
+                     if( (i == EV_ABS) && (test_bit(j, abs_bitmask)) )
+                     {
+                         /* this means that the bit is set in the axes list */
+                         if(ioctl(x->x_fd, EVIOCGABS(j), &abs_features)) 
+                             perror("evdev EVIOCGABS ioctl");
+                         new_element->min = abs_features.minimum;
+                         new_element->max = abs_features.maximum;
+                     }
+                     else
+                     {
+                         new_element->min = 0;
+                         new_element->max = 0;
+                     }
+                     if(test_bit(j, element_bitmask[i])) 
+                     {
+                         new_element->linux_code = j;
+                         if((i == EV_KEY) && (j >= BTN_MISC) && (j < KEY_OK) )
+                         {
+                             new_element->name = hid_convert_linux_buttons_to_numbers(j);
+                         }
+                         else
+                         {
+                             new_element->name = gensym(event_names[i][j] ? event_names[i][j] : "?");
+                         }
+                         if( i == EV_REL )
+                             new_element->relative = 1;
+                         else
+                             new_element->relative = 0;
+                         // fill in the t_hid_element struct here
+                         post("x->x_device_number: %d   element_count[]: %d",
+                              x->x_device_number, element_count[x->x_device_number]);
+                         post("linux_type/linux_code: %d/%d  type/name: %s/%s    max: %d   min: %d ", 
+                              new_element->linux_type, new_element->linux_code, 
+                              new_element->type->s_name, new_element->name->s_name,
+                              new_element->max, new_element->min);
+                         post("\tpolled: %d   relative: %d",
+                              new_element->polled, new_element->relative);
+                         element[x->x_device_number][element_count[x->x_device_number]] = new_element;
+                         ++element_count[x->x_device_number];
+                     }
+                 }
+             }        
+         }
+     }
+ }
+ 
  /* ------------------------------------------------------------------------------ */
  /* Pd [hid] FUNCTIONS */
***************
*** 303,308 ****
  /* for debugging, counts how many events are processed each time hid_read() is called */
      DEBUG(t_int event_counter = 0;);
! 
!     t_symbol*hid_code=0;
  
  /* this will go into the generic read function declared in hid.h and
--- 383,388 ----
  /* for debugging, counts how many events are processed each time hid_read() is called */
      DEBUG(t_int event_counter = 0;);
!     unsigned short i;
!     t_hid_element *output_element = NULL;
  
  /* this will go into the generic read function declared in hid.h and
***************
*** 315,349 ****
      while( read (x->x_fd, &(hid_input_event), sizeof(struct input_event)) > -1 )
      {
!         hid_code=0;
!         if( hid_input_event.type == EV_KEY )
!         {
! 	  /* JMZ: originally both functions were called, the latter evtl. overwriting
! 	   * the former; now i only call the latter if the former does not return 
! 	   * a valid result
! 	   */
! 	  if(!(hid_code=hid_convert_linux_buttons_to_numbers(hid_input_event.code)))
!             hid_code=hid_convert_linux_keys(hid_input_event.code);
!         }
!         else if( hid_input_event.type == EV_SYN )
!         {
!             // filter out EV_SYN events, they are currently unused
!         }
!         else if( event_names[hid_input_event.type][hid_input_event.code] != NULL )
!         {
! 	  hid_code = gensym(event_names[hid_input_event.type][hid_input_event.code]);
!         }
!         else 
          {
! 	  hid_code = gensym("unknown");
          }
-         /* TODO: needs porting
-         if( hid_code && hid_input_event.type != EV_SYN )
-             hid_output_event(x, gensym(ev[hid_input_event.type]), hid_code, 
-                              (t_float)hid_input_event.value);*/
          DEBUG(++event_counter;);
      }
      DEBUG(
!         //if(event_counter > 0)
!         //post("output %d events",event_counter);
  	);
  	
--- 395,419 ----
      while( read (x->x_fd, &(hid_input_event), sizeof(struct input_event)) > -1 )
      {
!         if( hid_input_event.type != EV_SYN )
          {
!             for( i=0; i < element_count[x->x_device_number]; ++i )
!             {
!                 output_element = element[x->x_device_number][i];
!                 if( (hid_input_event.type == output_element->linux_type) && \
!                     (hid_input_event.code == output_element->linux_code) )
!                 {
!                     output_element->value = hid_input_event.value;
!                     post("output %d",output_element->value);
!                     break;
!                 }
!             }
!             if( output_element != NULL )
!                 hid_output_event(x, output_element);
          }
          DEBUG(++event_counter;);
      }
      DEBUG(
!         if(event_counter > 0)
!         post("output %d events",event_counter);
  	);
  	
***************
*** 363,368 ****
      debug_print(LOG_DEBUG,"hid_open_device");
  
!     char device_name[256] = "Unknown";
!     char block_device[20] = "/dev/input/event0";
      struct input_event hid_input_event;
  
--- 433,438 ----
      debug_print(LOG_DEBUG,"hid_open_device");
  
!     char device_name[MAXPDSTRING] = "Unknown";
!     char block_device[MAXPDSTRING] = "/dev/input/event0";
      struct input_event hid_input_event;
  
***************
*** 370,374 ****
    
      x->x_device_number = device_number;
!     sprintf(block_device,"/dev/input/event%d",x->x_device_number);
  
      if(block_device) 
--- 440,444 ----
    
      x->x_device_number = device_number;
!     snprintf(block_device,MAXPDSTRING,"/dev/input/event%d",x->x_device_number);
  
      if(block_device) 
***************
*** 395,398 ****
--- 465,469 ----
            x->x_device_number,block_device,device_name);
  
+     post("pre hid_build_element_list");
      hid_build_element_list(x);
  
***************
*** 424,429 ****
      unsigned int i;
      unsigned int last_active_device = 0;
!     char device_name[256] = "Unknown";
!     char block_device[20] = "/dev/input/event0";
      struct input_event  x_input_event; 
      
--- 495,500 ----
      unsigned int i;
      unsigned int last_active_device = 0;
!     char device_name[MAXPDSTRING] = "Unknown";
!     char block_device[MAXPDSTRING] = "/dev/input/event0";
      struct input_event  x_input_event; 
      
***************
*** 434,440 ****
      for(i=0; i<MAX_DEVICES; ++i)
      {
!         sprintf(&block_device,"%s%d",LINUX_BLOCK_DEVICE,i);
          /* open the device read-only, non-exclusive */
!         fd = open (&block_device, O_RDONLY | O_NONBLOCK);
          /* test if device open */
          if(fd < 0 ) { 
--- 505,511 ----
      for(i=0; i<MAX_DEVICES; ++i)
      {
!         snprintf(block_device, MAXPDSTRING, "%s%d", LINUX_BLOCK_DEVICE, i);
          /* open the device read-only, non-exclusive */
!         fd = open (block_device, O_RDONLY | O_NONBLOCK);
          /* test if device open */
          if(fd < 0 ) { 
***************
*** 463,550 ****
  
  
- 
- 
- static void hid_build_element_list(t_hid *x) 
- {
-     unsigned long element_bitmask[EV_MAX][NBITS(KEY_MAX)];
-     uint8_t abs_bitmask[ABS_MAX/8 + 1];
-     struct input_absinfo abs_features;
-     char type_name[256];
-     char code_name[256];
-     t_hid_element *new_element = NULL;
-     t_int i, j;
-   
-     element_count[x->x_device_number] = 0;
-     if( x->x_fd ) 
-     {
-         new_element = getbytes(sizeof(t_hid_element));
-         /* get bitmask representing supported elements (axes, keys, etc.) */
-         memset(element_bitmask, 0, sizeof(element_bitmask));
-         if( ioctl(x->x_fd, EVIOCGBIT(0, EV_MAX), element_bitmask[0]) < 0 )
-             perror("[hid] error: evdev ioctl: element_bitmask");
-         memset(abs_bitmask, 0, sizeof(abs_bitmask));
-         if( ioctl(x->x_fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask) < 0 ) 
-             perror("[hid] error: evdev ioctl: abs_bitmask");
-         for( i = 1; i < EV_MAX; i++ ) 
-         {
-             if(test_bit(i, element_bitmask[0])) 
-             {
-                 new_element->linux_type = i;
-                 new_element->type = gensym(ev[i] ? ev[i] : "?");
-                 /* get bitmask representing supported button types */
-                 ioctl(x->x_fd, EVIOCGBIT(i, KEY_MAX), element_bitmask[i]);
-                 /* cycle through all possible event codes (axes, keys, etc.) 
-                  * testing to see which are supported.
-                  * i = i   j = j
-                  */
-                 for(j = 0; j < KEY_MAX; j++) 
-                 {
-                     if(test_bit(j, abs_bitmask)) 
-                     {
-                         /* this means that the bit is set in the axes list */
-                         if(ioctl(x->x_fd, EVIOCGABS(j), &abs_features)) 
-                             perror("evdev EVIOCGABS ioctl");
-                         new_element->min = abs_features.minimum;
-                         new_element->max = abs_features.maximum;
-                     }
-                     else
-                     {
-                         new_element->min = 0;
-                         new_element->max = 0;
-                     }
-                     if(test_bit(j, element_bitmask[i])) 
-                     {
-                         new_element->linux_code = j;
-                         if((i == EV_KEY) && (j >= BTN_MISC) && (j < KEY_OK) )
-                         {
-                             new_element->name = hid_convert_linux_buttons_to_numbers(j);
-                         }
-                         else
-                         {
-                             new_element->name = gensym(event_names[i][j] ? event_names[i][j] : "?");
-                         }
-                         if( i == EV_REL )
-                             new_element->relative = 1;
-                         else
-                             new_element->relative = 0;
-                         // fill in the t_hid_element struct here
- //                        post("x->x_device_number: %d   element_count[]: %d",
- //                             x->x_device_number, element_count[x->x_device_number]);
-                         post("linux_type/linux_code: %d/%d  type/name: %s %s    max: %d   min: %d ", 
-                              new_element->linux_type, new_element->linux_code, 
-                              new_element->type->s_name, new_element->name->s_name,
-                              new_element->max, new_element->min);
-                         post("\tpolled: %d   relative: %d",
-                              new_element->polled, new_element->relative);
-                         element[x->x_device_number][element_count[x->x_device_number]] = new_element;
-                         ++element_count[x->x_device_number];
-                     }
-                 }
-             }        
-         }
-     }
- }
- 
- 
  void hid_platform_specific_free(t_hid *x)
  {
--- 534,537 ----
***************
*** 552,571 ****
  }
  
  /* device info on the status outlet */
  void hid_platform_specific_info(t_hid* x)
  {
      struct input_id my_id;
!     char device_name[256] = "Unknown";
      char vendor_id_string[7];
      char product_id_string[7];
-     t_symbol *output_symbol;
      t_atom *output_atom = getbytes(sizeof(t_atom));
  
      ioctl(x->x_fd, EVIOCGID);
!     sprintf(vendor_id_string,"0x%04x", my_id.vendor);
      SETSYMBOL(output_atom, gensym(vendor_id_string));
      outlet_anything( x->x_status_outlet, gensym("vendorID"), 
                       1, output_atom);
!     sprintf(product_id_string,"0x%04x", my_id.product);
      SETSYMBOL(output_atom, gensym(product_id_string));
      outlet_anything( x->x_status_outlet, gensym("productID"), 
--- 539,560 ----
  }
  
+ 
+ 
+ 
  /* device info on the status outlet */
  void hid_platform_specific_info(t_hid* x)
  {
      struct input_id my_id;
!     char device_name[MAXPDSTRING] = "Unknown";
      char vendor_id_string[7];
      char product_id_string[7];
      t_atom *output_atom = getbytes(sizeof(t_atom));
  
      ioctl(x->x_fd, EVIOCGID);
!     snprintf(vendor_id_string,7,"0x%04x", my_id.vendor);
      SETSYMBOL(output_atom, gensym(vendor_id_string));
      outlet_anything( x->x_status_outlet, gensym("vendorID"), 
                       1, output_atom);
!     snprintf(product_id_string,7,"0x%04x", my_id.product);
      SETSYMBOL(output_atom, gensym(product_id_string));
      outlet_anything( x->x_status_outlet, gensym("productID"), 

Index: hid.c
===================================================================
RCS file: /cvsroot/pure-data/externals/hcs/hid/hid.c,v
retrieving revision 1.29
retrieving revision 1.30
diff -C2 -d -r1.29 -r1.30
*** hid.c	22 Aug 2006 04:13:14 -0000	1.29
--- hid.c	25 Aug 2006 01:41:49 -0000	1.30
***************
*** 36,41 ****
   */
  
! #define DEBUG(x)
! //#define DEBUG(x) x 
  
  unsigned short global_debug_level = 0;
--- 36,41 ----
   */
  
! //#define DEBUG(x)
! #define DEBUG(x) x 
  
  unsigned short global_debug_level = 0;





More information about the Pd-cvs mailing list