Injecting a Screen mtouch event

The following walkthrough takes you through the process of injecting an Screen mtouch event into a specified display.

  1. Create some basic variables you'll need for your application:
    screen_context_t screen_ctx;    /* a connection to the screen windowing system */
    screen_display_t screen_disp;   /* a screen display */
    screen_display_t *screen_dlist; /* a list of all available displays */
    screen_event_t screen_ev;       /* a screen event to handle */
    int ndisplays;                  /* number of available displays */
    int val;                        /* a variable used to set/get window properties */
    const char *display = "1";      /* the display type */
    int rval = EXIT_FAILURE;        /* the application return code*/
    int rc;                         /* a return code */
                        
  2. Create your native context with a privileged context that allows for the injection of Screen events. In this example, you will use the SCREEN_INPUT_PROVIDER_CONTEXT context.
    rc = screen_create_context(&screen_ctx, SCREEN_INPUT_PROVIDER_CONTEXT);
                        
  3. Identify the display into which your Screen event is to be injected.
    In this example, the display used is the internal display, unless it is otherwise specified as a command-line argument.
    rc = screen_get_context_property_iv(screen_ctx,
                                        SCREEN_PROPERTY_DISPLAY_COUNT,
                                        &ndisplays);
    if (rc) {
        perror("screen_get_context_property_iv(SCREEN_PROPERTY_DISPLAY_COUNT)");
        goto fail2;
    }
    
    screen_dlist = calloc(ndisplays, sizeof(*screen_dlist));
    if (screen_dlist == NULL) {
        fprintf(stderr, "could not allocate memory for display list\n");
        goto fail2;
    }
    
    rc = screen_get_context_property_pv(screen_ctx,
                                        SCREEN_PROPERTY_DISPLAYS,
                                        (void **)screen_dlist);
    if (rc) {
        perror("screen_get_context_property_pv(SCREEN_PROPERTY_DISPLAYS)");
        free(screen_dlist);
        goto fail2;
    }
    
    if (isdigit(*display)) {
        j = atoi(display) - 1;
    } else {
        int type = -1;
        if (strcmp(display, "internal") == 0) {
            type = SCREEN_DISPLAY_TYPE_INTERNAL;
        } else if (strcmp(display, "composite") == 0) {
            type = SCREEN_DISPLAY_TYPE_COMPOSITE;
        } else if (strcmp(display, "svideo") == 0) {
            type = SCREEN_DISPLAY_TYPE_SVIDEO;
        } else if (strcmp(display, "YPbPr") == 0) {
            type = SCREEN_DISPLAY_TYPE_COMPONENT_YPbPr;
        } else if (strcmp(display, "rgb") == 0) {
            type = SCREEN_DISPLAY_TYPE_COMPONENT_RGB;
        } else if (strcmp(display, "rgbhv") == 0) {
            type = SCREEN_DISPLAY_TYPE_COMPONENT_RGBHV;
        } else if (strcmp(display, "dvi") == 0) {
            type = SCREEN_DISPLAY_TYPE_DVI;
        } else if (strcmp(display, "hdmi") == 0) {
            type = SCREEN_DISPLAY_TYPE_HDMI;
        } else if (strcmp(display, "other") == 0) {
            type = SCREEN_DISPLAY_TYPE_OTHER;
        } else {
            fprintf(stderr, "unknown display type %s\n", display);
            free(screen_dlist);
            goto fail2;
        }
        for (j = 0; j < ndisplays; j++) {
            screen_get_display_property_iv(screen_dlist[j],
                                           SCREEN_PROPERTY_TYPE,
                                           &val);
            if (val == type) {
                break;
            }
        }
    }
    
    if (j >= ndisplays) {
        fprintf(stderr, "couldn't find display %s\n", display);
        free(screen_dlist);
        goto fail2;
    }
    
    screen_disp = screen_dlist[j];
    free(screen_dlist);
                        
  4. Create a Screen event.

    This is the Screen mtouch event that you will be injecting into the display.

    rc = screen_create_event(&screen_ev);
                        
  5. Set the type of the event that you will be injecting.

    In this example, you will be injecting an mtouch event.

    val = SCREEN_EVENT_MTOUCH_TOUCH;
    rc = screen_set_event_property_iv(screen_ev,
                                      SCREEN_PROPERTY_TYPE,
                                      &val);
                        
  6. Set the value of each property that is associated with your mtouch event. The properties you set be used are used in the mtouch_event structure.
    1. Set the value of the device ID that is associated with this mtouch event.
      int device = 0;
      rc = screen_set
      _event_property_iv(screen_ev,
                         SCREEN_PROPERTY_DEVICE_INDEX,
                         &device);
                          
    2. Set the value of the position of the touch point that is associated with this mtouch event.
      int pos[2] = {5, 5};
      rc = screen_set_event_property_iv(screen_ev,
                                        SCREEN_PROPERTY_POSITION,
                                        &pos);
                          
    3. Set the value of the sequence ID that is associated with this mtouch event.

      The sequence ID is the sequence number of the mtouch event. You need to increment this number for each new mtouch event you send.

      int seq_id = 1;
      rc = screen_set_event_property_iv(screen_ev,
                                        SCREEN_PROPERTY_SEQUENCE_ID,
                                        &pos);
                          
    4. Set the value of the size of the touch area that is associated with this mtouch event.

      The size, width and height in pixels, of the touch area.

      int size[2] = { 5, 5};
      rc = screen_set_event_property_iv(screen_ev,
                                        SCREEN_PROPERTY_SIZE,
                                        &size);
                          
    5. Set the value of the source position and source size.

      In this example, we use the same position and size because the offset of the window and display are one and the same.

      rc = screen_set_event_property_iv(screen_ev,
                                        SCREEN_PROPERTY_SOURCE_POSITION,
                                        &pos);
      rc = screen_set_event_property_iv(screen_ev,
                                        SCREEN_PROPERTY_SOURCE_SIZE,
                                        &size);
                          
    6. Set the value of the touch orientation.
      int touch_orientation = 1;
      rc = screen_set_event_property_iv(screen_ev,
                                        SCREEN_PROPERTY_TOUCH_ORIENTATION,
                                        &touch_orientation);
                          
    7. Set the value of the touch point ID.

      The touch point ID is the order of occurence of the mtouch event for multi-touch events; multiple mtouch events are used to describe one multi-touch event (e.g., a two-finger swipe).

      int touchId = 1;
      rc = screen_set_event_property_iv(screen_ev,
                                        SCREEN_PROPERTY_TOUCH_ID,
                                        &touchId);
                          
    8. Set the value of the touch pressure.

      The amount of pressure on the touch point is a relative value between 0 and (232-1) where 0 represents the lightest pressure.

      int touch_pressure = 1;
      rc = screen_set_event_property_iv(screen_ev,
                                        SCREEN_PROPERTY_TOUCH_PRESSURE,
                                        &touchId);
                          
  7. Inject your Screen event.
    rc = screen_inject_event(screen_disp, screen_ev);
                        
  8. Release the resources.
    screen_destroy_event(screen_ev);
    screen_destroy_context(screen_ctx);