void grab_key_unsafe (Key *key, gboolean grab, GSList *screens) { int indexes[N_BITS]; /* indexes of bits we need to flip */ int i; int bit; int bits_set_cnt; int uppervalue; guint mask; setup_modifiers (); mask = msd_ignored_mods & ~key->state & GDK_MODIFIER_MASK; bit = 0; /* store the indexes of all set bits in mask in the array */ for (i = 0; mask; ++i, mask >>= 1) { if (mask & 0x1) { indexes[bit++] = i; } } bits_set_cnt = bit; uppervalue = 1 << bits_set_cnt; /* grab all possible modifier combinations for our mask */ for (i = 0; i < uppervalue; ++i) { GSList *l; int j; int result = 0; /* map bits in the counter to those in the mask */ for (j = 0; j < bits_set_cnt; ++j) { if (i & (1 << j)) { result |= (1 << indexes[j]); } } for (l = screens; l; l = l->next) { GdkScreen *screen = l->data; guint *code; for (code = key->keycodes; *code; ++code) { grab_key_real (*code, gdk_screen_get_root_window (screen), grab, result | key->state); } } } }
gboolean match_xi2_key (Key *key, XIDeviceEvent *event) { guint keyval; GdkModifierType consumed; gint group; guint keycode, state; if (key == NULL) return FALSE; setup_modifiers (); state = device_xi2_translate_state (&event->mods, &event->group); if (have_xkb (event->display)) group = XkbGroupForCoreState (state); else group = (state & GDK_KEY_Mode_switch) ? 1 : 0; keycode = event->detail; /* Check if we find a keysym that matches our current state */ if (gdk_keymap_translate_keyboard_state (gdk_keymap_get_default (), keycode, state, group, &keyval, NULL, NULL, &consumed)) { guint lower, upper; guint mask; /* The Key structure contains virtual modifiers, whereas * the XEvent will be using the real modifier, so translate those */ mask = key->state; gdk_keymap_map_virtual_modifiers (gdk_keymap_get_default (), &mask); gdk_keyval_convert_case (keyval, &lower, &upper); /* If we are checking against the lower version of the * keysym, we might need the Shift state for matching, * so remove it from the consumed modifiers */ if (lower == key->keysym) consumed &= ~GDK_SHIFT_MASK; return ((lower == key->keysym || upper == key->keysym) && (state & ~consumed & gsd_used_mods) == mask); } /* The key we passed doesn't have a keysym, so try with just the keycode */ return (key != NULL && key->state == (state & gsd_used_mods) && key_uses_keycode (key, keycode)); }
gboolean match_key (Key *key, XEvent *event) { guint keyval; GdkModifierType consumed; gint group; if (key == NULL) return FALSE; setup_modifiers (); #ifdef HAVE_X11_EXTENSIONS_XKB_H if (have_xkb (event->xkey.display)) group = XkbGroupForCoreState (event->xkey.state); else #endif group = (event->xkey.state & GDK_KEY_Mode_switch) ? 1 : 0; /* Check if we find a keysym that matches our current state */ if (gdk_keymap_translate_keyboard_state (NULL, event->xkey.keycode, event->xkey.state, group, &keyval, NULL, NULL, &consumed)) { guint lower, upper; gdk_keyval_convert_case (keyval, &lower, &upper); /* If we are checking against the lower version of the * keysym, we might need the Shift state for matching, * so remove it from the consumed modifiers */ if (lower == key->keysym) consumed &= ~GDK_SHIFT_MASK; return ((lower == key->keysym || upper == key->keysym) && (event->xkey.state & ~consumed & msd_used_mods) == key->state); } /* The key we passed doesn't have a keysym, so try with just the keycode */ return (key != NULL && key->state == (event->xkey.state & msd_used_mods) && key_uses_keycode (key, event->xkey.keycode)); }
void grab_key_unsafe (Key *key, gboolean grab, GSList *screens) { int indexes[N_BITS]; /* indexes of bits we need to flip */ int i; int bit; int bits_set_cnt; int uppervalue; guint mask, modifiers; GArray *all_mods; GSList *l; setup_modifiers (); mask = gsd_ignored_mods & ~key->state & GDK_MODIFIER_MASK; /* XGrabKey requires real modifiers, not virtual ones */ modifiers = key->state; gdk_keymap_map_virtual_modifiers (gdk_keymap_get_default (), &modifiers); /* If key doesn't have a usable modifier, we don't want * to grab it, since the user might lose a useful key. * * The exception is the XFree86 keys and the Function keys * (which are useful to grab without a modifier). */ if ((modifiers & gsd_used_mods) == 0 && !IN_RANGE(key->keysym, XF86KEYS_RANGE_MIN, XF86KEYS_RANGE_MAX) && !IN_RANGE(key->keysym, FKEYS_RANGE_MIN, FKEYS_RANGE_MAX) && key->keysym != GDK_KEY_Pause && key->keysym != GDK_KEY_Print) { GString *keycodes; keycodes = g_string_new (""); if (key->keycodes != NULL) { guint *c; for (c = key->keycodes; *c; ++c) { g_string_printf (keycodes, " %u", *c); } } g_warning ("Key 0x%x (keycodes: %s) with state 0x%x (resolved to 0x%x) " " has no usable modifiers (usable modifiers are 0x%x)", key->keysym, keycodes->str, key->state, modifiers, gsd_used_mods); g_string_free (keycodes, TRUE); return; } bit = 0; /* store the indexes of all set bits in mask in the array */ for (i = 0; mask; ++i, mask >>= 1) { if (mask & 0x1) { indexes[bit++] = i; } } bits_set_cnt = bit; all_mods = g_array_new (FALSE, TRUE, sizeof(XIGrabModifiers)); uppervalue = 1 << bits_set_cnt; /* store all possible modifier combinations for our mask into all_mods */ for (i = 0; i < uppervalue; ++i) { int j; int result = 0; XIGrabModifiers *mod; /* map bits in the counter to those in the mask */ for (j = 0; j < bits_set_cnt; ++j) { if (i & (1 << j)) { result |= (1 << indexes[j]); } } /* Grow the array by one, to fit our new XIGrabModifiers item */ g_array_set_size (all_mods, all_mods->len + 1); mod = &g_array_index (all_mods, XIGrabModifiers, all_mods->len - 1); mod->modifiers = result | modifiers; } /* Capture the actual keycodes with the modifier array */ for (l = screens; l; l = l->next) { GdkScreen *screen = l->data; guint *code; for (code = key->keycodes; *code; ++code) { grab_key_real (*code, gdk_screen_get_root_window (screen), grab, (XIGrabModifiers *) all_mods->data, all_mods->len); } } g_array_free (all_mods, TRUE); }
void ui_init(int w,int h,int x,int y){ start_time=get_msec(); setup_modifiers(); _w=w; _h=h; winX=x; winY=y; dpy=XOpenDisplay(""); screen=DefaultScreen(dpy); root=DefaultRootWindow(dpy); XMatchVisualInfo(dpy, screen, 32, TrueColor, &visualinfo); uk_log("Iniitializing UI"); uk_log("Win @ (%i,%i,%i,%i)",x,y,w,h); attr.colormap = XCreateColormap( dpy, root, visualinfo.visual, AllocNone) ; attr.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | ButtonReleaseMask; attr.background_pixmap = None ; attr.border_pixel = 0 ; uk_log("root=%x\n",root); uk_log("%i %i %i %i\n",x,y,w,h); win=XCreateWindow(dpy,root, x,y,w,h, 0, visualinfo.depth, InputOutput, visualinfo.visual, CWColormap|CWEventMask|CWBackPixmap|CWBorderPixel, &attr); XWindowAttributes wa; XGetWindowAttributes(dpy,win,&wa); gc = XCreateGC(dpy,win,0,0); double_buffer=XCreatePixmap(dpy,win,wa.width,wa.height,wa.depth); XColor black=_rgb2XColor(255,255,255); XAllocColor(dpy,attr.colormap,&black); XSetForeground(dpy,gc,black.pixel); XFillRectangle(dpy,double_buffer,gc,0,0,wa.width,wa.height); d=double_buffer; uk_log("Created pixmap = %x",double_buffer); XGetWindowAttributes(dpy,win,&wa); load_font(); uk_log("window = %x root = %x",win,root); _set_on_top(dpy,win); _set_sticky(dpy,win); _set_alpha(dpy,win,conf_get_float("alpha",ALPHA)); XMapWindow(dpy,win); XMoveWindow(dpy,win,x,y); // Try to make borderless _make_borderless(dpy,win); // Bind to what stuff? unsigned long eventmask; eventmask=StructureNotifyMask|SubstructureNotifyMask; uk_log("event mask = x%x",eventmask); XSelectInput(dpy,root,eventmask); XFlush(dpy); kbio.start=kbio.stop=kbio.n=0; _setupkeymap(); grabkeys(); // grabkeyboard(); }