Tutorial: Using screen_notify()

The following procedure describes how an application can request an asynchronous notification from Screen for keyboard events.

You will learn to:

  1. Define your pulse code:
    #define MY_PULSE_CODE _PULSE_CODE_MINAVAIL
                        
  2. Create variables for your Screen and a keyboard device:
    screen_context_t screen_ctx = NULL;
    screen_device_t  keyboard   = NULL;
    screen_device_t* devices    = NULL;
                        
  3. Create a native context:
    screen_create_context(&screen_ctx, SCREEN_APPLICATION_CONTEXT);
                        
  4. Set up your device object as a keyboard type:
    int devcnt;
    screen_get_context_property_iv(screen_ctx, SCREEN_PROPERTY_DEVICE_COUNT, &devcnt);
    devices = malloc(devcnt*sizeof(void*));
    
    screen_get_context_property_pv(screen_ctx, SCREEN_PROPERTY_DEVICES, (void**)devices);
    
    int i=0;
    int type;
    for(; i<devcnt; i++)
    {
       screen_get_device_property_iv(devices[i], SCREEN_PROPERTY_TYPE, &type);
       if(type == SCREEN_EVENT_KEYBOARD)
       {
          keyboard = devices[i];
       }
    }
    
    if(keyboard == NULL)
    {
       printf("FATAL: Couldn't get keyboard device\n");
       exit(1);
    }
                        

    Events specific to a keyboard can be detected by passing the appropriate screen_device_t handle in the obj argument of screen_notify().

  5. Establish a connection:
       int chid = -1;
       chid = ChannelCreate(0);
    
       int coid = -1;
       coid = ConnectAttach(0, 0, chid, _NTO_SIDE_CHANNEL, 0);
                        
  6. Request notification from Screen:
    struct sigevent event;
    SIGEV_PULSE_INIT(&event, coid, SIGEV_PULSE_PRIO_INHERIT, MY_PULSE_CODE, 0);
    screen_register_event(screen_ctx, &event);
    screen_notify(screen_ctx, SCREEN_NOTIFY_INPUT, keyboard, &event);
                        
  7. Check for receipt of notification:
    while (1) {
       struct _pulse pulse;
       int rcvid = MsgReceivePulse(chid, &pulse, sizeof(pulse), NULL);
       if (rcvid == 0) {
          printf("keyboard input detected...\n");
       }
       ...
    }
                        
  8. Disarm any further notifications of keyboard events from Screen in this context:
    SIGEV_NONE_INIT(&event);
    screen_notify(screen_ctx, SCREEN_NOTIFY_INPUT, keyboard, &event);
    screen_unregister_event(&event);
    free(devices);
    
                        

    Passing NULL or an event with the type of SIGEV_NONE disables notifications that were enabled with the same combination of arguments: ctx, flags, and obj.

  9. Clean up your resources:
    ConnectDetach(coid);
    ChannelDestroy(chid);
    screen_destroy_context(screen_ctx);