Re: Xinput: units of the coordinates in motion-events?

Christophe Lohr <christophe.lohr-vHHsQtDGzY/[email protected]> Sat, 27 Oct 2018 16:55:47 +0200
Newsgroups gmane.comp.freedesktop.xcb
Message-ID <[email protected]>
Hi Uli,

> So, I guess you have to cast your event to
> xcb_input_button_press_event_t and then use these accessors to get all
> the data you want out of the event.
> Does this somehow help you? Could you test if my guess here is right?

Many thanks for your guidance. I have a clearer understanding of the API
now.

Note: I think that methods of the form 'xcb_generic_iterator_t 
xcb_input_FOO_BAR_end()' are not really dedicated to programmer, and are
here for internal purpose, to compute the address of the end of a chunk
of data and subsequently point to the next chunk.
The others methods were more useful for me.

See attached code. I'm rather happy with this.
;-)

Best regards
Christophe

_______________________________________________
Xcb mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/xcb
pointer.c (text/x-csrc, 3.1 KB)
#include <stdio.h>
#include <xcb/xcb.h>
#include <xcb/xinput.h>
#include <inttypes.h>
#include <stdlib.h>

#define CheckBit(mask, pos)	( (mask) & (1<<(pos)) )


int main () {
  xcb_connection_t *c;
  xcb_screen_t *screen;
  
  c = xcb_connect(NULL, NULL);
  screen = xcb_setup_roots_iterator(xcb_get_setup(c)).data;

  /* Piece of witchery from https://gist.github.com/LemonBoy/dfe1d7ea428794c65b3d */
  {
    struct {
      xcb_input_event_mask_t head;
      xcb_input_xi_event_mask_t mask;
    } xi_mask;
    xi_mask.head.deviceid = XCB_INPUT_DEVICE_ALL;
    xi_mask.head.mask_len = sizeof(xi_mask.mask) / sizeof(uint32_t);
    xi_mask.mask  = XCB_INPUT_XI_EVENT_MASK_KEY_PRESS;
    xi_mask.mask |= XCB_INPUT_XI_EVENT_MASK_MOTION;
    xi_mask.mask |= XCB_INPUT_XI_EVENT_MASK_RAW_MOTION;

    // Grab the input events non-invasively using XInput
    xcb_input_xi_select_events(c, screen->root, 1, &xi_mask.head);
  }
  xcb_flush(c);

  xcb_generic_event_t *event;
  xcb_input_fp3232_iterator_t iter;
  uint32_t *valuator_mask;
  unsigned i;

  while ((event = xcb_wait_for_event(c))) {

    switch (event->response_type & ~0x80) {
        
      case XCB_GE_GENERIC: {
        switch ( ((xcb_ge_event_t *)event)->event_type ) {
          case XCB_INPUT_RAW_MOTION: {
            xcb_input_raw_button_press_event_t *R;
            R = (xcb_input_raw_button_press_event_t *)event;
            printf("RAW device:%-2d  ", R->deviceid);
            valuator_mask = xcb_input_raw_button_press_valuator_mask(R);
            printf("valuator_mask:%x ", *valuator_mask);
            printf(" axis_length:%d  ", xcb_input_raw_button_press_axisvalues_length(R) );
            iter = xcb_input_raw_button_press_axisvalues_iterator(R);
            for (i = 0; iter.rem; i++) {
              if ( CheckBit(*valuator_mask, i) ) {
                printf("axis[%d]:%+-5d ", i, iter.data->integral);
                xcb_input_fp3232_next(&iter);
              } else
                printf("              ");
            }
            printf("\n");
            break; }
          case XCB_INPUT_MOTION: {
            xcb_input_button_press_event_t *R;
            R = (xcb_input_button_press_event_t *)event;
            printf("    device:%-2d  ", R->deviceid);
            valuator_mask = xcb_input_button_press_valuator_mask(R);
            printf("valuator_mask:%x ", *valuator_mask);
            printf(" axis_length:%d  ", xcb_input_button_press_axisvalues_length(R) );
            iter = xcb_input_button_press_axisvalues_iterator(R);
            for (i = 0; iter.rem; i++) {
              if ( CheckBit(*valuator_mask, i) ) {
                printf("axis[%d]:%+-5d ", i, iter.data->integral);
                xcb_input_fp3232_next(&iter);
              } else
                printf("              ");
            }
            printf("\n");
            break; }
          
          case XCB_INPUT_KEY_PRESS:
            goto end;
            break;
 
          default:
            break;
        }
        break;
      }
      
      default:
        break;
    }

    free(event);
  }

end:
  printf("\n");
  xcb_disconnect(c);
  exit(EXIT_SUCCESS);
}