gdkkeynames.c 2.66 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GDK - The GIMP Drawing Kit
2 3 4 5 6 7 8 9 10 11 12 13 14
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21 22 23 24
 */

/*
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

25
#include "config.h"
26 27 28 29 30 31

#include "gdkkeysyms.h"
#include "gdkinternals.h"

/* Key handling not part of the keymap */

32
#include "keyname-table.h"
33

34 35 36 37
#include <glib/gprintf.h>
#include <stdlib.h>
#include <string.h>

38
#define GDK_NUM_KEYS G_N_ELEMENTS (gdk_keys_by_keyval)
39 40 41 42

static int
gdk_keys_keyval_compare (const void *pkey, const void *pbase)
{
43
  return (*(int *) pkey) - ((gdk_key *) pbase)->keyval;
44 45
}

46 47
static gchar*
_gdk_keyval_name (guint keyval)
48 49
{
  static gchar buf[100];
50
  gdk_key *found;
51 52 53 54

  /* Check for directly encoded 24-bit UCS characters: */
  if ((keyval & 0xff000000) == 0x01000000)
    {
55
      g_sprintf (buf, "U+%.04X", (keyval & 0x00ffffff));
56 57 58 59
      return buf;
    }

  found = bsearch (&keyval, gdk_keys_by_keyval,
60
		   GDK_NUM_KEYS, sizeof (gdk_key),
61 62 63 64 65 66 67 68
		   gdk_keys_keyval_compare);

  if (found != NULL)
    {
      while ((found > gdk_keys_by_keyval) &&
             ((found - 1)->keyval == keyval))
        found--;
	    
69
      return (gchar *) (keynames + found->offset);
70 71 72
    }
  else if (keyval != 0)
    {
73
      g_sprintf (buf, "%#x", keyval);
74 75 76 77 78 79 80 81 82
      return buf;
    }

  return NULL;
}

static int
gdk_keys_name_compare (const void *pkey, const void *pbase)
{
83
  return strcmp ((const char *) pkey, 
84
		 (const char *) (keynames + ((const gdk_key *) pbase)->offset));
85 86
}

87 88
static guint
_gdk_keyval_from_name (const gchar *keyval_name)
89
{
90
  gdk_key *found;
91 92

  g_return_val_if_fail (keyval_name != NULL, 0);
93 94 95 96

  if (strncmp (keyval_name,"XF86", 4) == 0)
    keyval_name += 4;

97
  found = bsearch (keyval_name, gdk_keys_by_name,
98
		   GDK_NUM_KEYS, sizeof (gdk_key),
99 100 101 102
		   gdk_keys_name_compare);
  if (found != NULL)
    return found->keyval;
  else
103
    return GDK_KEY_VoidSymbol;
104
}