Move the keysym ↔ Unicode table to .rodata

The table we use for converting between keysyms and Unicode should be
static and constified, so that it can live in the .rodata section of
the ELF shared object, and be shared among processes.

This change moves the table to a source file, instead of an header; the
change also requires the clutter_keysym_to_unicode() function to be
moved from clutter-event.c into this new source file. The declaration is
still in clutter-event.h, so we don't need to do anything special.
This commit is contained in:
Emmanuele Bassi 2011-02-28 14:02:00 +00:00
parent 23c0dac231
commit e2b5762571
3 changed files with 54 additions and 56 deletions

View File

@ -189,6 +189,7 @@ source_c = \
$(srcdir)/clutter-group.c \ $(srcdir)/clutter-group.c \
$(srcdir)/clutter-input-device.c \ $(srcdir)/clutter-input-device.c \
$(srcdir)/clutter-interval.c \ $(srcdir)/clutter-interval.c \
$(srcdir)/clutter-keysyms-table.c \
$(srcdir)/clutter-layout-manager.c \ $(srcdir)/clutter-layout-manager.c \
$(srcdir)/clutter-layout-meta.c \ $(srcdir)/clutter-layout-meta.c \
$(srcdir)/clutter-list-model.c \ $(srcdir)/clutter-list-model.c \
@ -236,7 +237,6 @@ source_h_priv = \
$(srcdir)/clutter-event-translator.h \ $(srcdir)/clutter-event-translator.h \
$(srcdir)/clutter-event-private.h \ $(srcdir)/clutter-event-private.h \
$(srcdir)/clutter-id-pool.h \ $(srcdir)/clutter-id-pool.h \
$(srcdir)/clutter-keysyms-table.h \
$(srcdir)/clutter-master-clock.h \ $(srcdir)/clutter-master-clock.h \
$(srcdir)/clutter-model-private.h \ $(srcdir)/clutter-model-private.h \
$(srcdir)/clutter-offscreen-effect-private.h \ $(srcdir)/clutter-offscreen-effect-private.h \

View File

@ -31,7 +31,6 @@
#include "clutter-debug.h" #include "clutter-debug.h"
#include "clutter-event-private.h" #include "clutter-event-private.h"
#include "clutter-keysyms.h" #include "clutter-keysyms.h"
#include "clutter-keysyms-table.h"
#include "clutter-private.h" #include "clutter-private.h"
/** /**
@ -744,53 +743,6 @@ clutter_event_set_key_unicode (ClutterEvent *event,
event->key.unicode_value = key_unicode; event->key.unicode_value = key_unicode;
} }
/**
* clutter_keysym_to_unicode:
* @keyval: a key symbol
*
* Convert from a Clutter key symbol to the corresponding ISO10646 (Unicode)
* character.
*
* Return value: a Unicode character, or 0 if there is no corresponding
* character.
*/
guint32
clutter_keysym_to_unicode (guint keyval)
{
int min = 0;
int max = G_N_ELEMENTS (clutter_keysym_to_unicode_tab) - 1;
int mid;
/* First check for Latin-1 characters (1:1 mapping) */
if ((keyval >= 0x0020 && keyval <= 0x007e) ||
(keyval >= 0x00a0 && keyval <= 0x00ff))
return keyval;
/* Also check for directly encoded 24-bit UCS characters:
*/
if ((keyval & 0xff000000) == 0x01000000)
return keyval & 0x00ffffff;
/* binary search in table */
while (max >= min)
{
mid = (min + max) / 2;
if (clutter_keysym_to_unicode_tab[mid].keysym < keyval)
min = mid + 1;
else if (clutter_keysym_to_unicode_tab[mid].keysym > keyval)
max = mid - 1;
else
{
/* found it */
return clutter_keysym_to_unicode_tab[mid].ucs;
}
}
/* No matching Unicode value found */
return 0;
}
/** /**
* clutter_event_get_device_id: * clutter_event_get_device_id:
* @event: a clutter event * @event: a clutter event

View File

@ -1,12 +1,11 @@
#ifndef __CLUTTER_KEYSYMS_TABLE_H__ #include <glib.h>
#define __CLUTTER_KEYSYMS_TABLE_H__
/* Code below from GDK, which contains following comment; /* Code below from GDK, which contains following comment:
* *
* Thanks to Markus G. Kuhn <mkuhn@acm.org> for the ksysym<->Unicode * Thanks to Markus G. Kuhn <mkuhn@acm.org> for the ksysym<->Unicode
* mapping functions, from the xterm sources. * mapping functions, from the xterm sources.
*/ */
struct { static const struct {
unsigned short keysym; unsigned short keysym;
unsigned short ucs; unsigned short ucs;
} clutter_keysym_to_unicode_tab[] = { } clutter_keysym_to_unicode_tab[] = {
@ -824,4 +823,51 @@ struct {
/* End numeric keypad */ /* End numeric keypad */
}; };
#endif /* __CLUTTER_KEYSYMS_TABLE_H__ */ static const int clutter_keysym_to_unicode_tab_size =
G_N_ELEMENTS (clutter_keysym_to_unicode_tab);
/**
* clutter_keysym_to_unicode:
* @keyval: a key symbol
*
* Converts @keyval from a Clutter key symbol to the corresponding
* ISO10646 (Unicode) character.
*
* Return value: a Unicode character, or 0 if there is no corresponding
* character.
*/
guint32
clutter_keysym_to_unicode (guint keyval)
{
int min = 0;
int max = clutter_keysym_to_unicode_tab_size - 1;
int mid;
/* First check for Latin-1 characters (1:1 mapping) */
if ((keyval >= 0x0020 && keyval <= 0x007e) ||
(keyval >= 0x00a0 && keyval <= 0x00ff))
return keyval;
/* Also check for directly encoded 24-bit UCS characters: */
if ((keyval & 0xff000000) == 0x01000000)
return keyval & 0x00ffffff;
/* binary search in table */
while (max >= min)
{
mid = (min + max) / 2;
if (clutter_keysym_to_unicode_tab[mid].keysym < keyval)
min = mid + 1;
else if (clutter_keysym_to_unicode_tab[mid].keysym > keyval)
max = mid - 1;
else
{
/* found it */
return clutter_keysym_to_unicode_tab[mid].ucs;
}
}
/* No matching Unicode value found */
return 0;
}