add keyboard.[ch] to libobt. these are for tracking modifier keys, and translating to/from keycodes/keysyms/modmasks (also include a new obt/internal.h for putting functions that are not public)

This commit is contained in:
Dana Jansens 2007-07-26 21:56:42 -04:00
parent 2f0e73cf9d
commit 1aac72bc4c
7 changed files with 329 additions and 5 deletions

View file

@ -135,6 +135,9 @@ obt_libobt_la_LIBADD = \
obt_libobt_la_SOURCES = \
obt/display.h \
obt/display.c \
obt/internal.h \
obt/keyboard.h \
obt/keyboard.c \
obt/mainloop.h \
obt/mainloop.c \
obt/prop.h \

View file

@ -18,6 +18,8 @@
#include "obt/display.h"
#include "obt/prop.h"
#include "obt/internal.h"
#include "obt/keyboard.h"
#ifdef HAVE_STRING_H
# include <string.h>
@ -108,6 +110,7 @@ gboolean obt_display_open(const char *display_name)
#endif
obt_prop_startup();
obt_keyboard_reload();
}
g_free(n);
@ -116,6 +119,7 @@ gboolean obt_display_open(const char *display_name)
void obt_display_close()
{
obt_keyboard_shutdown();
if (obt_display) XCloseDisplay(obt_display);
}

27
obt/internal.h Normal file
View file

@ -0,0 +1,27 @@
/* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
obt/internal.h for the Openbox window manager
Copyright (c) 2006 Mikael Magnusson
Copyright (c) 2003-2007 Dana Jansens
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
See the COPYING file for a copy of the GNU General Public License.
*/
#ifndef __obt_internal_h
#define __obt_internal_h
void obt_prop_startup();
void obt_keyboard_shutdown();
#endif /* __obt_internal_h */

224
obt/keyboard.c Normal file
View file

@ -0,0 +1,224 @@
/* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
obt/keyboard.c for the Openbox window manager
Copyright (c) 2007 Dana Jansens
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
See the COPYING file for a copy of the GNU General Public License.
*/
#include "obt/display.h"
#include "obt/keyboard.h"
#include <X11/Xlib.h>
#include <X11/keysym.h>
/* These masks are constants and the modifier keys are bound to them as
anyone sees fit:
ShiftMask (1<<0), LockMask (1<<1), ControlMask (1<<2), Mod1Mask (1<<3),
Mod2Mask (1<<4), Mod3Mask (1<<5), Mod4Mask (1<<6), Mod5Mask (1<<7)
*/
#define NUM_MASKS 8
#define ALL_MASKS 0xff /* an or'ing of all 8 keyboard masks */
/* Get the bitflag for the n'th modifier mask */
#define nth_mask(n) (1 << n)
static void set_modkey_mask(guchar mask, KeySym sym);
void obt_keyboard_shutdown();
static XModifierKeymap *modmap;
static KeySym *keymap;
static gint min_keycode, max_keycode, keysyms_per_keycode;
/* This is a bitmask of the different masks for each modifier key */
static guchar modkeys_keys[OBT_MODKEY_NUM_KEYS];
static gboolean alt_l = FALSE;
static gboolean meta_l = FALSE;
static gboolean super_l = FALSE;
static gboolean hyper_l = FALSE;
static gboolean started = FALSE;
void obt_keyboard_reload()
{
gint i, j, k;
if (started) obt_keyboard_shutdown(); /* free stuff */
started = TRUE;
/* reset the keys to not be bound to any masks */
for (i = 0; i < OBT_MODKEY_NUM_KEYS; ++i)
modkeys_keys[i] = 0;
modmap = XGetModifierMapping(obt_display);
g_assert(modmap->max_keypermod > 0);
XDisplayKeycodes(obt_display, &min_keycode, &max_keycode);
keymap = XGetKeyboardMapping(obt_display, min_keycode,
max_keycode - min_keycode + 1,
&keysyms_per_keycode);
alt_l = meta_l = super_l = hyper_l = FALSE;
/* go through each of the modifier masks (eg ShiftMask, CapsMask...) */
for (i = 0; i < NUM_MASKS; ++i) {
/* go through each keycode that is bound to the mask */
for (j = 0; j < modmap->max_keypermod; ++j) {
KeySym sym;
/* get a keycode that is bound to the mask (i) */
KeyCode keycode = modmap->modifiermap[i*modmap->max_keypermod + j];
if (keycode) {
/* go through each keysym bound to the given keycode */
for (k = 0; k < keysyms_per_keycode; ++k) {
sym = keymap[(keycode-min_keycode) * keysyms_per_keycode +
k];
if (sym != NoSymbol) {
/* bind the key to the mask (e.g. Alt_L => Mod1Mask) */
set_modkey_mask(nth_mask(i), sym);
}
}
}
}
}
/* CapsLock, Shift, and Control are special and hard-coded */
modkeys_keys[OBT_MODKEY_KEY_CAPSLOCK] = LockMask;
modkeys_keys[OBT_MODKEY_KEY_SHIFT] = ShiftMask;
modkeys_keys[OBT_MODKEY_KEY_CONTROL] = ControlMask;
}
void obt_keyboard_shutdown()
{
XFreeModifiermap(modmap);
modmap = NULL;
XFree(keymap);
keymap = NULL;
started = FALSE;
}
guint obt_keyboard_keycode_to_modmask(guint keycode)
{
gint i, j;
guint mask = 0;
if (keycode == NoSymbol) return 0;
/* go through each of the modifier masks (eg ShiftMask, CapsMask...) */
for (i = 0; i < NUM_MASKS; ++i) {
/* go through each keycode that is bound to the mask */
for (j = 0; j < modmap->max_keypermod; ++j) {
/* compare with a keycode that is bound to the mask (i) */
if (modmap->modifiermap[i*modmap->max_keypermod + j] == keycode)
mask |= nth_mask(i);
}
}
return mask;
}
guint obt_keyboard_only_modmasks(guint mask)
{
mask &= ALL_MASKS;
/* strip off these lock keys. they shouldn't affect key bindings */
mask &= ~LockMask; /* use the LockMask, not what capslock is bound to,
because you could bind it to something else and it
should work as that modifier then. i think capslock
is weird in xkb. */
mask &= ~obt_keyboard_modkey_to_modmask(OBT_MODKEY_KEY_NUMLOCK);
mask &= ~obt_keyboard_modkey_to_modmask(OBT_MODKEY_KEY_SCROLLLOCK);
return mask;
}
guint obt_keyboard_modkey_to_modmask(ObtModkeysKey key)
{
return modkeys_keys[key];
}
static void set_modkey_mask(guchar mask, KeySym sym)
{
/* find what key this is, and bind it to the mask */
if (sym == XK_Num_Lock)
modkeys_keys[OBT_MODKEY_KEY_NUMLOCK] |= mask;
else if (sym == XK_Scroll_Lock)
modkeys_keys[OBT_MODKEY_KEY_SCROLLLOCK] |= mask;
else if (sym == XK_Super_L && super_l)
modkeys_keys[OBT_MODKEY_KEY_SUPER] |= mask;
else if (sym == XK_Super_L && !super_l)
/* left takes precident over right, so erase any masks the right
key may have set */
modkeys_keys[OBT_MODKEY_KEY_SUPER] = mask, super_l = TRUE;
else if (sym == XK_Super_R && !super_l)
modkeys_keys[OBT_MODKEY_KEY_SUPER] |= mask;
else if (sym == XK_Hyper_L && hyper_l)
modkeys_keys[OBT_MODKEY_KEY_HYPER] |= mask;
else if (sym == XK_Hyper_L && !hyper_l)
modkeys_keys[OBT_MODKEY_KEY_HYPER] = mask, hyper_l = TRUE;
else if (sym == XK_Hyper_R && !hyper_l)
modkeys_keys[OBT_MODKEY_KEY_HYPER] |= mask;
else if (sym == XK_Alt_L && alt_l)
modkeys_keys[OBT_MODKEY_KEY_ALT] |= mask;
else if (sym == XK_Alt_L && !alt_l)
modkeys_keys[OBT_MODKEY_KEY_ALT] = mask, alt_l = TRUE;
else if (sym == XK_Alt_R && !alt_l)
modkeys_keys[OBT_MODKEY_KEY_ALT] |= mask;
else if (sym == XK_Meta_L && meta_l)
modkeys_keys[OBT_MODKEY_KEY_META] |= mask;
else if (sym == XK_Meta_L && !meta_l)
modkeys_keys[OBT_MODKEY_KEY_META] = mask, meta_l = TRUE;
else if (sym == XK_Meta_R && !meta_l)
modkeys_keys[OBT_MODKEY_KEY_META] |= mask;
/* CapsLock, Shift, and Control are special and hard-coded */
}
KeyCode obt_keyboard_keysym_to_keycode(KeySym sym)
{
gint i, j;
/* go through each keycode and look for the keysym */
for (i = min_keycode; i <= max_keycode; ++i)
for (j = 0; j < keysyms_per_keycode; ++j)
if (sym == keymap[(i-min_keycode) * keysyms_per_keycode + j])
return i;
return 0;
}
const gchar *obt_keyboard_keycode_to_string(guint keycode)
{
KeySym sym;
const gchar *ret = NULL;
if ((sym = XKeycodeToKeysym(obt_display, keycode, 0)) != NoSymbol)
ret = XKeysymToString(sym);
return g_locale_to_utf8(ret, -1, NULL, NULL, NULL);
}
gunichar obt_keyboard_keycode_to_unichar(guint keycode)
{
gunichar unikey = 0;
const char *key;
if ((key = obt_keyboard_keycode_to_string(keycode)) != NULL &&
/* don't accept keys that aren't a single letter, like "space" */
key[1] == '\0')
{
unikey = g_utf8_get_char_validated(key, -1);
if (unikey == (gunichar)-1 || unikey == (gunichar)-2 || unikey == 0)
unikey = 0;
}
return unikey;
}

70
obt/keyboard.h Normal file
View file

@ -0,0 +1,70 @@
/* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
obt/keyboard.h for the Openbox window manager
Copyright (c) 2007 Dana Jansens
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
See the COPYING file for a copy of the GNU General Public License.
*/
#ifndef __obt_keyboard_h
#define __obt_keyboard_h
#include <glib.h>
#include <X11/Xlib.h>
G_BEGIN_DECLS
/*! These keys are bound to the modifier masks in any fashion,
except for CapsLock, Shift, and Control. */
typedef enum {
OBT_MODKEY_KEY_CAPSLOCK,
OBT_MODKEY_KEY_NUMLOCK,
OBT_MODKEY_KEY_SCROLLLOCK,
OBT_MODKEY_KEY_SHIFT,
OBT_MODKEY_KEY_CONTROL,
OBT_MODKEY_KEY_SUPER,
OBT_MODKEY_KEY_HYPER,
OBT_MODKEY_KEY_META,
OBT_MODKEY_KEY_ALT,
OBT_MODKEY_NUM_KEYS
} ObtModkeysKey;
void obt_keyboard_reload();
/*! Get the modifier mask(s) for a KeyCode. (eg. a keycode bound to Alt_L could
return a mask of (Mod1Mask | Mask3Mask)) */
guint obt_keyboard_keycode_to_modmask(guint keycode);
/*! Strip off all modifiers except for the modifier keys. This strips stuff
like Button1Mask, and also LockMask, NumlockMask, and ScrolllockMask */
guint obt_keyboard_only_modmasks(guint mask);
/*! Get the modifier masks for a modifier key. This includes both the left and
right keys when there are both. */
guint obt_keyboard_modkey_to_modmask(ObtModkeysKey key);
/*! Convert a KeySym to a KeyCode, because the X function is terrible - says
valgrind. */
KeyCode obt_keyboard_keysym_to_keycode(KeySym sym);
/*! Give the string form of a KeyCode */
const gchar *obt_keyboard_keycode_to_string(guint keycode);
/*! Translate a KeyCode to the unicode character it represents */
gunichar obt_keyboard_keycode_to_unichar(guint keycode);
G_END_DECLS
#endif /* __obt_keyboard_h */

View file

@ -210,8 +210,6 @@ typedef enum {
OBT_PROP_NUM_ATOMS
} ObtPropAtom;
void obt_prop_startup();
Atom obt_prop_atom(ObtPropAtom a);
gboolean obt_prop_get32(Window win, Atom prop, Atom type, guint32 *ret);

View file

@ -21,7 +21,6 @@
#include "openbox.h"
#include "session.h"
#include "dock.h"
#include "modkeys.h"
#include "event.h"
#include "menu.h"
#include "client.h"
@ -204,7 +203,7 @@ gint main(gint argc, gchar **argv)
if (screen_annex()) { /* it will be ours! */
do {
modkeys_startup(reconfigure);
if (reconfigure) obt_keyboard_reload();
/* get the keycodes for keys we use */
keys[OB_KEY_RETURN] = modkeys_sym_to_code(XK_Return);
@ -359,7 +358,6 @@ gint main(gint argc, gchar **argv)
event_shutdown(reconfigure);
config_shutdown();
actions_shutdown(reconfigure);
modkeys_shutdown(reconfigure);
} while (reconfigure);
}