Files
squeekboard/eek/eek-keysym.c
2010-06-04 12:09:08 +09:00

103 lines
3.2 KiB
C

/*
* Copyright (C) 2010 Daiki Ueno <ueno@unixuser.org>
* Copyright (C) 2010 Red Hat, Inc.
*
* 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
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include <glib.h>
#include <stdlib.h>
#include "eek-keysym.h"
struct eek_keysym_label {
guint keysym;
const gchar *label;
};
#include "eek-special-keysym-labels.h"
#include "eek-unicode-keysym-labels.h"
#include "eek-keyname-keysym-labels.h"
static G_CONST_RETURN gchar *
unichar_to_utf8 (gunichar uc)
{
if (g_unichar_isgraph (uc)) {
gchar *buf;
gint len;
len = g_unichar_to_utf8 (uc, NULL);
buf = g_malloc0 (len + 1);
g_unichar_to_utf8 (uc, buf);
return buf;
}
return g_strdup ("");
}
static int
keysym_label_compare (const void *key0, const void *key1)
{
const struct eek_keysym_label *entry0 = key0, *entry1 = key1;
return (gint)entry0->keysym - (gint)entry1->keysym;
}
G_CONST_RETURN gchar *
eek_keysym_to_string (guint keysym)
{
struct eek_keysym_label bsearch_key, *bsearch_val;
/* First, search special keysyms. */
bsearch_key.keysym = keysym;
bsearch_val = bsearch (&bsearch_key,
special_keysym_labels,
G_N_ELEMENTS(special_keysym_labels),
sizeof (struct eek_keysym_label),
keysym_label_compare);
if (bsearch_val)
return g_strdup (bsearch_val->label);
/* Check for Latin-1 characters (1:1 mapping) */
if ((keysym >= 0x0020 && keysym <= 0x007e) ||
(keysym >= 0x00a0 && keysym <= 0x00ff))
return unichar_to_utf8 (keysym);
/* Also check for directly encoded 24-bit UCS characters:
*/
if ((keysym & 0xff000000) == 0x01000000)
return unichar_to_utf8 (keysym & 0x00ffffff);
/* Search known unicode keysyms. */
bsearch_key.keysym = keysym;
bsearch_val = bsearch (&bsearch_key,
unicode_keysym_labels,
G_N_ELEMENTS(unicode_keysym_labels),
sizeof (struct eek_keysym_label),
keysym_label_compare);
if (bsearch_val)
return g_strdup (bsearch_val->label);
/* Finally, search keynames. */
bsearch_key.keysym = keysym;
bsearch_val = bsearch (&bsearch_key,
keyname_keysym_labels,
G_N_ELEMENTS(keyname_keysym_labels),
sizeof (struct eek_keysym_label),
keysym_label_compare);
if (bsearch_val)
return g_strdup (bsearch_val->label);
return g_strdup ("");
}