Blame modules/input/gtkimcontextmultipress.c

Packit 98cdb6
/*
Packit 98cdb6
 * Copyright (c) 2006-2009 Openismus GmbH
Packit 98cdb6
 *
Packit 98cdb6
 * This library is free software; you can redistribute it and/or
Packit 98cdb6
 * modify it under the terms of the GNU Lesser General Public
Packit 98cdb6
 * License as published by the Free Software Foundation; either
Packit 98cdb6
 * version 2 of the License, or (at your option) any later version.
Packit 98cdb6
 *
Packit 98cdb6
 * This library is distributed in the hope that it will be useful,
Packit 98cdb6
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 98cdb6
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 98cdb6
 * Lesser General Public License for more details.
Packit 98cdb6
 *
Packit 98cdb6
 * You should have received a copy of the GNU Lesser General Public
Packit 98cdb6
 * License along with this library; if not, write to the
Packit 98cdb6
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit 98cdb6
 * Boston, MA 02111-1307, USA.
Packit 98cdb6
 */
Packit 98cdb6
Packit 98cdb6
#include "gtkimcontextmultipress.h"
Packit 98cdb6
#include <string.h>
Packit 98cdb6
#include <gtk/gtk.h>
Packit 98cdb6
#include <gdk/gdkkeysyms.h>
Packit 98cdb6
#include <gtk/gtkimmodule.h>
Packit 98cdb6
#include <config.h>
Packit 98cdb6
Packit 98cdb6
#define AUTOMATIC_COMPOSE_TIMEOUT 1 /* seconds */
Packit 98cdb6
#define CONFIGURATION_FILENAME MULTIPRESS_CONFDIR G_DIR_SEPARATOR_S "im-multipress.conf"
Packit 98cdb6
Packit 98cdb6
/* This contains rows of characters that can be entered by pressing
Packit 98cdb6
 * a particular key repeatedly.  Each row has one key (such as GDK_a),
Packit 98cdb6
 * and an array of character strings, such as "a".
Packit 98cdb6
 */
Packit 98cdb6
typedef struct
Packit 98cdb6
{
Packit 98cdb6
  gchar **characters; /* array of strings */
Packit 98cdb6
  gsize n_characters; /* number of strings in the array */
Packit 98cdb6
}
Packit 98cdb6
KeySequence;
Packit 98cdb6
Packit 98cdb6
static GObjectClass *im_context_multipress_parent_class = NULL;
Packit 98cdb6
static GType         im_context_multipress_type = 0;
Packit 98cdb6
Packit 98cdb6
static void im_context_multipress_class_init (GtkImContextMultipressClass *klass);
Packit 98cdb6
static void im_context_multipress_init (GtkImContextMultipress *self);
Packit 98cdb6
static void im_context_multipress_finalize (GObject *obj);
Packit 98cdb6
Packit 98cdb6
static void load_config (GtkImContextMultipress *self);
Packit 98cdb6
Packit 98cdb6
static gboolean vfunc_filter_keypress (GtkIMContext *context,
Packit 98cdb6
                                       GdkEventKey  *event);
Packit 98cdb6
static void vfunc_reset (GtkIMContext *context);
Packit 98cdb6
static void vfunc_get_preedit_string (GtkIMContext   *context,
Packit 98cdb6
                                      gchar         **str,
Packit 98cdb6
                                      PangoAttrList **attrs,
Packit 98cdb6
                                      gint           *cursor_pos);
Packit 98cdb6
Packit 98cdb6
/* Notice that we have a *_register_type(GTypeModule*) function instead of a
Packit 98cdb6
 * *_get_type() function, because we must use g_type_module_register_type(),
Packit 98cdb6
 * providing the GTypeModule* that was provided to im_context_init(). That
Packit 98cdb6
 * is also why we are not using G_DEFINE_TYPE().
Packit 98cdb6
 */
Packit 98cdb6
void
Packit 98cdb6
gtk_im_context_multipress_register_type (GTypeModule* type_module)
Packit 98cdb6
{
Packit 98cdb6
  const GTypeInfo im_context_multipress_info =
Packit 98cdb6
    {
Packit 98cdb6
      sizeof (GtkImContextMultipressClass),
Packit 98cdb6
      (GBaseInitFunc) NULL,
Packit 98cdb6
      (GBaseFinalizeFunc) NULL,
Packit 98cdb6
      (GClassInitFunc) &im_context_multipress_class_init,
Packit 98cdb6
      NULL,
Packit 98cdb6
      NULL,
Packit 98cdb6
      sizeof (GtkImContextMultipress),
Packit 98cdb6
      0,
Packit 98cdb6
      (GInstanceInitFunc) &im_context_multipress_init,
Packit 98cdb6
      0,
Packit 98cdb6
    };
Packit 98cdb6
Packit 98cdb6
  im_context_multipress_type =
Packit 98cdb6
    g_type_module_register_type (type_module,
Packit 98cdb6
                                 GTK_TYPE_IM_CONTEXT,
Packit 98cdb6
                                 "GtkImContextMultipress",
Packit 98cdb6
                                 &im_context_multipress_info, 0);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
GType
Packit 98cdb6
gtk_im_context_multipress_get_type (void)
Packit 98cdb6
{
Packit 98cdb6
  g_assert (im_context_multipress_type != 0);
Packit 98cdb6
Packit 98cdb6
  return im_context_multipress_type;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
static void
Packit 98cdb6
key_sequence_free (gpointer value)
Packit 98cdb6
{
Packit 98cdb6
  KeySequence *seq = value;
Packit 98cdb6
Packit 98cdb6
  if (seq != NULL)
Packit 98cdb6
    {
Packit 98cdb6
      g_strfreev (seq->characters);
Packit 98cdb6
      g_slice_free (KeySequence, seq);
Packit 98cdb6
    }
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
static void
Packit 98cdb6
im_context_multipress_class_init (GtkImContextMultipressClass *klass)
Packit 98cdb6
{
Packit 98cdb6
  GtkIMContextClass *im_context_class;
Packit 98cdb6
Packit 98cdb6
  /* Set this so we can use it later: */
Packit 98cdb6
  im_context_multipress_parent_class = g_type_class_peek_parent (klass);
Packit 98cdb6
Packit 98cdb6
  /* Specify our vfunc implementations: */
Packit 98cdb6
  im_context_class = GTK_IM_CONTEXT_CLASS (klass);
Packit 98cdb6
  im_context_class->filter_keypress = &vfunc_filter_keypress;
Packit 98cdb6
  im_context_class->reset = &vfunc_reset;
Packit 98cdb6
  im_context_class->get_preedit_string = &vfunc_get_preedit_string;
Packit 98cdb6
Packit 98cdb6
  G_OBJECT_CLASS (klass)->finalize = &im_context_multipress_finalize;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
static void
Packit 98cdb6
im_context_multipress_init (GtkImContextMultipress *self)
Packit 98cdb6
{
Packit 98cdb6
  self->key_sequences = g_hash_table_new_full (&g_direct_hash, &g_direct_equal,
Packit 98cdb6
                                               NULL, &key_sequence_free);
Packit 98cdb6
  load_config (self);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
static void
Packit 98cdb6
im_context_multipress_finalize (GObject *obj)
Packit 98cdb6
{
Packit 98cdb6
  GtkImContextMultipress *self;
Packit 98cdb6
Packit 98cdb6
  self = GTK_IM_CONTEXT_MULTIPRESS (obj);
Packit 98cdb6
Packit 98cdb6
  /* Release the configuration data: */
Packit 98cdb6
  if (self->key_sequences != NULL)
Packit 98cdb6
    {
Packit 98cdb6
      g_hash_table_destroy (self->key_sequences);
Packit 98cdb6
      self->key_sequences = NULL;
Packit 98cdb6
    }
Packit 98cdb6
Packit 98cdb6
  (*im_context_multipress_parent_class->finalize) (obj);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
Packit 98cdb6
GtkIMContext *
Packit 98cdb6
gtk_im_context_multipress_new (void)
Packit 98cdb6
{
Packit 98cdb6
  return (GtkIMContext *)g_object_new (GTK_TYPE_IM_CONTEXT_MULTIPRESS, NULL);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
static void
Packit 98cdb6
cancel_automatic_timeout_commit (GtkImContextMultipress *multipress_context)
Packit 98cdb6
{
Packit 98cdb6
  if (multipress_context->timeout_id)
Packit 98cdb6
    g_source_remove (multipress_context->timeout_id);
Packit 98cdb6
 
Packit 98cdb6
  multipress_context->timeout_id = 0;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
Packit 98cdb6
/* Clear the compose buffer, so we are ready to compose the next character.
Packit 98cdb6
 */
Packit 98cdb6
static void
Packit 98cdb6
clear_compose_buffer (GtkImContextMultipress *multipress_context)
Packit 98cdb6
{
Packit 98cdb6
  multipress_context->key_last_entered = 0;
Packit 98cdb6
  multipress_context->compose_count = 0;
Packit 98cdb6
Packit 98cdb6
  cancel_automatic_timeout_commit (multipress_context);
Packit 98cdb6
Packit 98cdb6
  if (multipress_context->tentative_match)
Packit 98cdb6
    {
Packit 98cdb6
      multipress_context->tentative_match = NULL;
Packit 98cdb6
      g_signal_emit_by_name (multipress_context, "preedit-changed");
Packit 98cdb6
      g_signal_emit_by_name (multipress_context, "preedit-end");
Packit 98cdb6
    }
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/* Finish composing, provide the character, and clear our compose buffer.
Packit 98cdb6
 */
Packit 98cdb6
static void
Packit 98cdb6
accept_character (GtkImContextMultipress *multipress_context, const gchar *characters)
Packit 98cdb6
{
Packit 98cdb6
  /* Clear the compose buffer, so we are ready to compose the next character.
Packit 98cdb6
   * Note that if we emit "preedit-changed" after "commit", there's a segfault/
Packit 98cdb6
   * invalid-write with GtkTextView in gtk_text_layout_free_line_display(), when
Packit 98cdb6
   * destroying a PangoLayout (this can also be avoided by not using any Pango
Packit 98cdb6
   * attributes in get_preedit_string(). */
Packit 98cdb6
  clear_compose_buffer (multipress_context);
Packit 98cdb6
Packit 98cdb6
  /* Provide the character to GTK+ */
Packit 98cdb6
  g_signal_emit_by_name (multipress_context, "commit", characters);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
static gboolean
Packit 98cdb6
on_timeout (gpointer data)
Packit 98cdb6
{
Packit 98cdb6
  GtkImContextMultipress *multipress_context;
Packit 98cdb6
Packit 98cdb6
  GDK_THREADS_ENTER ();
Packit 98cdb6
Packit 98cdb6
  multipress_context = GTK_IM_CONTEXT_MULTIPRESS (data);
Packit 98cdb6
Packit 98cdb6
  /* A certain amount of time has passed, so we will assume that the user
Packit 98cdb6
   * really wants the currently chosen character */
Packit 98cdb6
  accept_character (multipress_context, multipress_context->tentative_match);
Packit 98cdb6
Packit 98cdb6
  multipress_context->timeout_id = 0;
Packit 98cdb6
Packit 98cdb6
  GDK_THREADS_LEAVE ();
Packit 98cdb6
Packit 98cdb6
  return FALSE; /* don't call me again */
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
static gboolean
Packit 98cdb6
vfunc_filter_keypress (GtkIMContext *context, GdkEventKey *event)
Packit 98cdb6
{
Packit 98cdb6
  GtkIMContextClass      *parent;
Packit 98cdb6
  GtkImContextMultipress *multipress_context;
Packit 98cdb6
Packit 98cdb6
  multipress_context = GTK_IM_CONTEXT_MULTIPRESS (context);
Packit 98cdb6
Packit 98cdb6
  if (event->type == GDK_KEY_PRESS)
Packit 98cdb6
    {
Packit 98cdb6
      KeySequence *possible;
Packit 98cdb6
Packit 98cdb6
      /* Check whether the current key is the same as previously entered, because
Packit 98cdb6
       * if it is not then we should accept the previous one, and start a new
Packit 98cdb6
       * character. */
Packit 98cdb6
      if (multipress_context->compose_count > 0
Packit 98cdb6
          && multipress_context->key_last_entered != event->keyval
Packit 98cdb6
          && multipress_context->tentative_match != NULL)
Packit 98cdb6
        {
Packit 98cdb6
          /* Accept the previously chosen character.  This wipes
Packit 98cdb6
           * the compose_count and key_last_entered. */
Packit 98cdb6
          accept_character (multipress_context,
Packit 98cdb6
                            multipress_context->tentative_match);
Packit 98cdb6
        } 
Packit 98cdb6
Packit 98cdb6
      /* Decide what character this key press would choose: */
Packit 98cdb6
      possible = g_hash_table_lookup (multipress_context->key_sequences,
Packit 98cdb6
                                      GUINT_TO_POINTER (event->keyval));
Packit 98cdb6
      if (possible != NULL)
Packit 98cdb6
        {
Packit 98cdb6
          if (multipress_context->compose_count == 0)
Packit 98cdb6
            g_signal_emit_by_name (multipress_context, "preedit-start");
Packit 98cdb6
Packit 98cdb6
          /* Check whether we are at the end of a compose sequence, with no more
Packit 98cdb6
           * possible characters.  Cycle back to the start if necessary. */
Packit 98cdb6
          if (multipress_context->compose_count >= possible->n_characters)
Packit 98cdb6
            multipress_context->compose_count = 0;
Packit 98cdb6
Packit 98cdb6
          /* Store the last key pressed in the compose sequence. */
Packit 98cdb6
          multipress_context->key_last_entered = event->keyval; 
Packit 98cdb6
Packit 98cdb6
          /* Get the possible match for this number of presses of the key.
Packit 98cdb6
           * compose_count starts at 1, so that 0 can mean not composing. */ 
Packit 98cdb6
          multipress_context->tentative_match =
Packit 98cdb6
            possible->characters[multipress_context->compose_count++];
Packit 98cdb6
Packit 98cdb6
          /* Indicate the current possible character.  This will cause our
Packit 98cdb6
           * vfunc_get_preedit_string() vfunc to be called, which will provide
Packit 98cdb6
           * the current possible character for the user to see. */
Packit 98cdb6
          g_signal_emit_by_name (multipress_context, "preedit-changed");
Packit 98cdb6
Packit 98cdb6
          /* Cancel any outstanding timeout, so we can start the timer again: */
Packit 98cdb6
          cancel_automatic_timeout_commit (multipress_context);
Packit 98cdb6
Packit 98cdb6
          /* Create a timeout that will cause the currently chosen character to
Packit 98cdb6
           * be committed, if nothing happens for a certain amount of time: */
Packit 98cdb6
          multipress_context->timeout_id =
Packit 98cdb6
            g_timeout_add_seconds (AUTOMATIC_COMPOSE_TIMEOUT,
Packit 98cdb6
                                   &on_timeout, multipress_context);
Packit 98cdb6
Packit 98cdb6
          return TRUE; /* key handled */
Packit 98cdb6
        }
Packit 98cdb6
      else
Packit 98cdb6
        {
Packit 98cdb6
          guint32 keyval_uchar;
Packit 98cdb6
Packit 98cdb6
          /* Just accept all other keypresses directly, but commit the
Packit 98cdb6
           * current preedit content first. */
Packit 98cdb6
          if (multipress_context->compose_count > 0
Packit 98cdb6
              && multipress_context->tentative_match != NULL)
Packit 98cdb6
            {
Packit 98cdb6
              accept_character (multipress_context,
Packit 98cdb6
                                multipress_context->tentative_match);
Packit 98cdb6
            }
Packit 98cdb6
          keyval_uchar = gdk_keyval_to_unicode (event->keyval);
Packit 98cdb6
Packit 98cdb6
          /* Convert to a string for accept_character(). */
Packit 98cdb6
          if (keyval_uchar != 0)
Packit 98cdb6
            {
Packit 98cdb6
              /* max length of UTF-8 sequence = 6 + 1 for NUL termination */
Packit 98cdb6
              gchar keyval_utf8[7];
Packit 98cdb6
              gint  length;
Packit 98cdb6
Packit 98cdb6
              length = g_unichar_to_utf8 (keyval_uchar, keyval_utf8);
Packit 98cdb6
              keyval_utf8[length] = '\0';
Packit 98cdb6
Packit 98cdb6
              accept_character (multipress_context, keyval_utf8);
Packit 98cdb6
Packit 98cdb6
              return TRUE; /* key handled */
Packit 98cdb6
            }
Packit 98cdb6
        }
Packit 98cdb6
    }
Packit 98cdb6
Packit 98cdb6
  parent = (GtkIMContextClass *)im_context_multipress_parent_class;
Packit 98cdb6
Packit 98cdb6
  /* The default implementation just returns FALSE, but it is generally
Packit 98cdb6
   * a good idea to call the base class implementation: */
Packit 98cdb6
  if (parent->filter_keypress)
Packit 98cdb6
    return (*parent->filter_keypress) (context, event);
Packit 98cdb6
Packit 98cdb6
  return FALSE;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
static void
Packit 98cdb6
vfunc_reset (GtkIMContext *context)
Packit 98cdb6
{
Packit 98cdb6
  clear_compose_buffer (GTK_IM_CONTEXT_MULTIPRESS (context));
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
static void
Packit 98cdb6
vfunc_get_preedit_string (GtkIMContext   *context,
Packit 98cdb6
                          gchar         **str,
Packit 98cdb6
                          PangoAttrList **attrs,
Packit 98cdb6
                          gint           *cursor_pos)
Packit 98cdb6
{
Packit 98cdb6
  gsize len_bytes = 0;
Packit 98cdb6
  gsize len_utf8_chars = 0;
Packit 98cdb6
Packit 98cdb6
  /* Show the user what character he will get if he accepts: */
Packit 98cdb6
  if (str != NULL)
Packit 98cdb6
    {
Packit 98cdb6
      const gchar *match;
Packit 98cdb6
Packit 98cdb6
      match = GTK_IM_CONTEXT_MULTIPRESS (context)->tentative_match;
Packit 98cdb6
Packit 98cdb6
      if (match == NULL)
Packit 98cdb6
        match = ""; /* *str must not be NUL */
Packit 98cdb6
Packit 98cdb6
      len_bytes = strlen (match); /* byte count */
Packit 98cdb6
      len_utf8_chars = g_utf8_strlen (match, len_bytes); /* character count */
Packit 98cdb6
Packit 98cdb6
      *str = g_strndup (match, len_bytes);
Packit 98cdb6
    }
Packit 98cdb6
Packit 98cdb6
  /* Underline it, to show the user that he is in compose mode: */
Packit 98cdb6
  if (attrs != NULL)
Packit 98cdb6
    {
Packit 98cdb6
      *attrs = pango_attr_list_new ();
Packit 98cdb6
Packit 98cdb6
      if (len_bytes > 0)
Packit 98cdb6
        {
Packit 98cdb6
          PangoAttribute *attr;
Packit 98cdb6
Packit 98cdb6
          attr = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
Packit 98cdb6
          attr->start_index = 0;
Packit 98cdb6
          attr->end_index = len_bytes;
Packit 98cdb6
          pango_attr_list_insert (*attrs, attr);
Packit 98cdb6
        }
Packit 98cdb6
    }
Packit 98cdb6
Packit 98cdb6
  if (cursor_pos)
Packit 98cdb6
    *cursor_pos = len_utf8_chars;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/* Open the configuration file and fill in the key_sequences hash table
Packit 98cdb6
 * with key/character-list pairs taken from the [keys] group of the file.
Packit 98cdb6
 */
Packit 98cdb6
static void
Packit 98cdb6
load_config (GtkImContextMultipress *self)
Packit 98cdb6
{
Packit 98cdb6
  GKeyFile *key_file;
Packit 98cdb6
  GError   *error = NULL;
Packit 98cdb6
  gchar   **keys;
Packit 98cdb6
  gsize     n_keys = 0;
Packit 98cdb6
  gsize     i;
Packit 98cdb6
Packit 98cdb6
  key_file = g_key_file_new ();
Packit 98cdb6
Packit 98cdb6
  if (!g_key_file_load_from_file (key_file, CONFIGURATION_FILENAME,
Packit 98cdb6
                                  G_KEY_FILE_NONE, &error))
Packit 98cdb6
    {
Packit 98cdb6
      g_warning ("Error while trying to open the %s configuration file: %s",
Packit 98cdb6
                 CONFIGURATION_FILENAME, error->message);
Packit 98cdb6
      g_error_free (error);
Packit 98cdb6
      g_key_file_free (key_file);
Packit 98cdb6
      return;
Packit 98cdb6
    }
Packit 98cdb6
Packit 98cdb6
  keys = g_key_file_get_keys (key_file, "keys", &n_keys, &error);
Packit 98cdb6
Packit 98cdb6
  if (error != NULL)
Packit 98cdb6
    {
Packit 98cdb6
      g_warning ("Error while trying to read the %s configuration file: %s",
Packit 98cdb6
                 CONFIGURATION_FILENAME, error->message);
Packit 98cdb6
      g_error_free (error);
Packit 98cdb6
      g_key_file_free (key_file);
Packit 98cdb6
      return;
Packit 98cdb6
    }
Packit 98cdb6
Packit 98cdb6
  for (i = 0; i < n_keys; ++i)
Packit 98cdb6
    {
Packit 98cdb6
      KeySequence *seq;
Packit 98cdb6
      guint        keyval;
Packit 98cdb6
Packit 98cdb6
      keyval = gdk_keyval_from_name (keys[i]);
Packit 98cdb6
Packit 98cdb6
      if (keyval == GDK_VoidSymbol)
Packit 98cdb6
        {
Packit 98cdb6
          g_warning ("Error while trying to read the %s configuration file: "
Packit 98cdb6
                     "invalid key name \"%s\"",
Packit 98cdb6
                     CONFIGURATION_FILENAME, keys[i]);
Packit 98cdb6
          continue;
Packit 98cdb6
        }
Packit 98cdb6
Packit 98cdb6
      seq = g_slice_new (KeySequence);
Packit 98cdb6
      seq->characters = g_key_file_get_string_list (key_file, "keys", keys[i],
Packit 98cdb6
                                                    &seq->n_characters, &error);
Packit 98cdb6
      if (error != NULL)
Packit 98cdb6
        {
Packit 98cdb6
          g_warning ("Error while trying to read the %s configuration file: %s",
Packit 98cdb6
                     CONFIGURATION_FILENAME, error->message);
Packit 98cdb6
          g_error_free (error);
Packit 98cdb6
          error = NULL;
Packit 98cdb6
          g_slice_free (KeySequence, seq);
Packit 98cdb6
          continue;
Packit 98cdb6
        }
Packit 98cdb6
Packit 98cdb6
      /* Ownership of the KeySequence is taken over by the hash table */
Packit 98cdb6
      g_hash_table_insert (self->key_sequences, GUINT_TO_POINTER (keyval), seq);
Packit 98cdb6
    }
Packit 98cdb6
Packit 98cdb6
  g_strfreev (keys);
Packit 98cdb6
  g_key_file_free (key_file);
Packit 98cdb6
}