Beispiel #1
0
gboolean
xfwm_device_grab_keycode (XfwmDevices *devices, Display *display,
                          gint keycode, guint modifiers, Window grab_window,
                          gboolean owner_events, guint event_mask,
                          gint grab_mode, gint paired_device_mode)
{
    gboolean result;
    Status status;
#ifdef HAVE_XI2
    XIGrabModifiers xi2_modifiers;
    XIEventMask xievent_mask;
#endif

#ifdef HAVE_XI2
    if (devices->xi2_available)
    {
        xi2_modifiers.modifiers = xi2_modifier_mask (modifiers);
        xi2_modifiers.status = 0;

        xfwm_device_fill_xi2_event_mask (&xievent_mask, event_mask);
        status = XIGrabKeycode (display, devices->keyboard.xi2_device, keycode, grab_window,
                                grab_mode, paired_device_mode, owner_events,
                                &xievent_mask, 1, &xi2_modifiers);
        g_free (xievent_mask.mask);
        result = (status == XIGrabSuccess);
    }
    else
#endif
    {
        status = XGrabKey (display, keycode, modifiers, grab_window,
                           owner_events, grab_mode, paired_device_mode);
        result = (status == GrabSuccess);
    }
    return result;
}
Beispiel #2
0
void
GrabKeySet(unsigned int keycode, unsigned int modifiers, Win win)
{
   Bool                owner_events = False;
   int                 pointer_mode = GrabModeAsync;
   int                 keyboard_mode = GrabModeSync;
   int                 i;

#if USE_XI2
   EXIEventMask        em;
   XIGrabModifiers     modifiers_inouts[8];
   int                 num_modifiers;

   EXIMaskSetup(&em, DEV_KBD, KeyPressMask | KeyReleaseMask);

   if (modifiers == AnyModifier)
     {
	num_modifiers = 1;
	modifiers_inouts[0].modifiers = XIAnyModifier;
	modifiers_inouts[0].status = 0;
     }
   else
     {
	num_modifiers = 0;
	for (i = 0; i < 8; i++)
	  {
	     if (i && !Mode.masks.mod_combos[i])
		continue;
	     modifiers_inouts[num_modifiers].modifiers =
		modifiers | Mode.masks.mod_combos[i];
	     modifiers_inouts[num_modifiers].status = 0;
	     num_modifiers++;
	  }
     }
   XIGrabKeycode(disp, DEV_KBD, keycode, WinGetXwin(win),
		 keyboard_mode, pointer_mode, owner_events,
		 &em.em, num_modifiers, modifiers_inouts);
#else

   if (modifiers == AnyModifier)
     {
	XGrabKey(disp, keycode, modifiers, WinGetXwin(win), owner_events,
		 pointer_mode, keyboard_mode);
	return;
     }

   for (i = 0; i < 8; i++)
     {
	if (i && !Mode.masks.mod_combos[i])
	   continue;
	XGrabKey(disp, keycode, modifiers | Mode.masks.mod_combos[i],
		 WinGetXwin(win), owner_events, pointer_mode, keyboard_mode);
     }
#endif
}
static void
grab_key_real (guint      keycode,
               GdkWindow *root,
               gboolean   grab,
               XIGrabModifiers *mods,
               int        num_mods)
{
	XIEventMask evmask;
	unsigned char mask[(XI_LASTEVENT + 7)/8];

	memset (mask, 0, sizeof (mask));
	XISetMask (mask, XI_KeyPress);
	XISetMask (mask, XI_KeyRelease);

	evmask.deviceid = XIAllMasterDevices;
	evmask.mask_len = sizeof (mask);
	evmask.mask = mask;

        if (grab) {
                XIGrabKeycode (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
                               XIAllMasterDevices,
                               keycode,
                               GDK_WINDOW_XID (root),
                               GrabModeAsync,
                               GrabModeAsync,
                               False,
                               &evmask,
                               num_mods,
                               mods);
        } else {
                XIUngrabKeycode (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
                                 XIAllMasterDevices,
                                 keycode,
                                 GDK_WINDOW_XID (root),
                                 num_mods,
                                 mods);
        }
}
Beispiel #4
0
Bool
grabKeyCtrl (XWCContext      * ctx,
             Window            w,
             KeyCode           xKCode,
             int               nMods,
             XIGrabModifiers * mods,
             Bool              grab)
{
    XIGrabModifiers * failMods;
    XIEventMask       evmask;
    int               nfailMods, i;
    unsigned char     mask[(XI_LASTEVENT + 7) / 8];
    char              buf[1024];

    snprintf (buf, sizeof (buf), "\t\tGrabbing keycode %d on window 0x%lX:",
              xKCode, w);
    logCtrl (buf, LOG_LVL_1, False);

    if (ctx == NULL)
    {
        logCtrl ("\t\t\tCannot grab key: NULL pointer to XWC context received.",
                 LOG_LVL_NO, False);
        return False;
    }

    if (ctx->kbds == NULL)
    {
        logCtrl ("\t\t\tCannot grab key: NULL pointer to keyboard device list"
                 " received.", LOG_LVL_NO, False);
        return False;
    }

    if (ctx->kbds->nDevs < 0 || ctx->kbds->nDevs > 127)
    {
        logCtrl ("\t\t\tCannot grab key: bad device count.", LOG_LVL_NO, False);
        return False;
    }

    if (ctx->kbds->devs == NULL)
    {
        logCtrl ("\t\t\tCannot grab key: NULL pointer to device array "
                 "received.", LOG_LVL_NO, False);
        return False;
    }

    if (nMods < 0 || nMods > 127)
    {
        logCtrl ("\t\t\tCannot grab key: bad mods count.", LOG_LVL_NO, False);
        return False;
    }

    if (mods == NULL)
    {
        logCtrl ("\t\t\tCannot grab key: NULL pointer to modifiers array "
                 "received.", LOG_LVL_NO, False);
        return False;
    }

    if (w == None)
    {
        logCtrl ("\t\t\tCannot grab key: No window specified.", LOG_LVL_NO,
                 False);
        return False;
    }

    if (grab == False)
    {
        for (i = 0; i < ctx->kbds->nDevs; ++ i)
        {
            XIUngrabKeycode (ctx->xDpy, ctx->kbds->devs[i], xKCode, w, nMods,
                             mods);

            snprintf (buf, sizeof (buf), "\t\t\tkeycode ungrabbed on device "
                      "%d.", ctx->kbds->devs[i]);
            logCtrl (buf, LOG_LVL_2, True);
        }
        return True;
    }

    failMods = (XIGrabModifiers*) malloc (sizeof (XIGrabModifiers) * nMods);

    if (failMods == NULL)
    {
        logCtrl ("\t\t\tCannot grab key: cannot allocate array for failed "
                 "mods.", LOG_LVL_NO, False);
        return False;
    }

    memcpy (failMods, mods, sizeof (XIGrabModifiers) * nMods);

    memset (mask, 0, sizeof (mask));
    XISetMask (mask, XI_KeyRelease);
    XISetMask (mask, XI_KeyPress);

    memset (&evmask, 0, sizeof (evmask));
    evmask.mask_len = sizeof (mask);
    evmask.mask     = mask;

    nfailMods       = 0;

    for (i = 0; i < ctx->kbds->nDevs && nfailMods == 0; ++ i)
    {
        nfailMods = XIGrabKeycode (ctx->xDpy, ctx->kbds->devs[i], xKCode, w,
                                   GrabModeAsync, GrabModeAsync, False, &evmask,
                                   nMods, failMods);
        if (nfailMods == 0)
        {
            snprintf (buf, sizeof (buf), "\t\t\tkeycode grabbed on device %d.",
                      ctx->kbds->devs[i]);
            logCtrl (buf, LOG_LVL_2, True);
        }
    }

    if (nfailMods != 0)
    {
        for (i = 0; i < nfailMods; ++ i)
        {
            snprintf (buf, sizeof (buf), "\t\t\tModifier %x failed with error "
                      "%d\n", failMods[i].modifiers, failMods[i].status);
            logCtrl (buf, LOG_LVL_NO, False);
        }

        free (failMods);

        return False;
    }

    snprintf (buf, sizeof (buf), "\t\t\tSuccess");
    logCtrl (buf, LOG_LVL_2, True);

    free (failMods);

    return True;
}
Beispiel #5
0
void setup_input_devices (GromitData *data)
{
    /* ungrab all */
    release_grab (data, NULL);

    /* and clear our own device data list */
    GHashTableIter it;
    gpointer value;
    g_hash_table_iter_init (&it, data->devdatatable);
    while (g_hash_table_iter_next (&it, NULL, &value))
        g_free(value);
    g_hash_table_remove_all(data->devdatatable);


    /* get devices */
    GdkDeviceManager *device_manager = gdk_display_get_device_manager(data->display);
    GList *devices, *d;
    int i = 0;

    devices = g_list_concat(gdk_device_manager_list_devices
                            (device_manager, GDK_DEVICE_TYPE_MASTER),
                            gdk_device_manager_list_devices
                            (device_manager, GDK_DEVICE_TYPE_SLAVE));
    for(d = devices; d; d = d->next)
    {
        GdkDevice *device = (GdkDevice *) d->data;

        /* only enable devices with 2 ore more axes */
        if (gdk_device_get_source(device) != GDK_SOURCE_KEYBOARD && gdk_device_get_n_axes(device) >= 2)
        {
            gdk_device_set_mode (device, GDK_MODE_SCREEN);

            GromitDeviceData *devdata;

            devdata  = g_malloc0(sizeof (GromitDeviceData));
            devdata->device = device;
            devdata->index = i;

            /* get attached keyboard and grab the hotkey */
            if (!data->hot_keycode)
            {
                g_printerr("ERROR: Grabbing hotkey from attached keyboard "
                           "of '%s' failed, no hotkey defined.\n",
                           gdk_device_get_name(device));
                g_free(devdata);
                continue;
            }

            /* if this is a slave device, we need the master */
            GdkDevice *kdevice=device;
            if(gdk_device_get_device_type (device) == GDK_DEVICE_TYPE_SLAVE)
                kdevice=gdk_device_get_associated_device (device);

            gint dev_id = -1;
            g_object_get(kdevice, "device-id", &dev_id, NULL);

            gint kbd_dev_id = -1;
            XIDeviceInfo* devinfo;
            int devicecount = 0;

            devinfo = XIQueryDevice(GDK_DISPLAY_XDISPLAY(data->display),
                                    dev_id,
                                    &devicecount);
            if(devicecount)
                kbd_dev_id = devinfo->attachment;
            XIFreeDeviceInfo(devinfo);

            if(kbd_dev_id != -1)
            {
                if(data->debug)
                    g_printerr("DEBUG: Grabbing hotkey from keyboard '%d' .\n", kbd_dev_id);

                XIEventMask mask;
                unsigned char bits[4] = {0,0,0,0};
                mask.mask = bits;
                mask.mask_len = sizeof(bits);

                XISetMask(bits, XI_KeyPress);
                XISetMask(bits, XI_KeyRelease);

                XIGrabModifiers modifiers[] = {{XIAnyModifier, 0}};
                int nmods = 1;

                gdk_error_trap_push ();

                XIGrabKeycode( GDK_DISPLAY_XDISPLAY(data->display),
                               kbd_dev_id,
                               data->hot_keycode,
                               GDK_WINDOW_XID(data->root),
                               GrabModeAsync,
                               GrabModeAsync,
                               True,
                               &mask,
                               nmods,
                               modifiers);

                XSync(GDK_DISPLAY_XDISPLAY(data->display), False);
                if(gdk_error_trap_pop())
                {
                    g_printerr("ERROR: Grabbing hotkey from keyboard device %d failed.\n",
                               kbd_dev_id);
                    g_free(devdata);
                    continue;
                }
            }

            g_hash_table_insert(data->devdatatable, device, devdata);
            g_printerr ("Enabled Device %d: \"%s\", (Type: %d)\n",
                        i++, gdk_device_get_name(device), gdk_device_get_source(device));
        }
    }

    g_printerr ("Now %d enabled devices.\n", g_hash_table_size(data->devdatatable));
}