Back to index

unity  6.0.0
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes
unity::ui::KeyboardUtil Class Reference

#include <KeyboardUtil.h>

List of all members.

Public Member Functions

 KeyboardUtil (Display *display)
 ~KeyboardUtil ()
guint GetKeycodeAboveKeySymbol (KeySym key_symbol) const

Static Public Member Functions

static bool IsPrintableKeySymbol (KeySym key_symbol)
static bool IsMoveKeySymbol (KeySym sym)

Private Member Functions

bool CompareOffsets (int current_x, int current_y, int best_x, int best_y) const
guint ConvertKeyToKeycode (XkbKeyPtr key) const
bool FindKeyInGeometry (XkbGeometryPtr geo, char *key_name, int &res_section, XkbBoundsRec &res_bounds) const
bool FindKeyInSectionAboveBounds (XkbGeometryPtr geo, int section, XkbBoundsRec const &target_bounds, guint &keycode) const
XkbBoundsRec GetAbsoluteKeyBounds (XkbKeyPtr key, XkbRowPtr row, XkbSectionPtr section, XkbGeometryPtr geo) const

Private Attributes

XkbDescPtr keyboard_
Display * display_

Detailed Description

Definition at line 34 of file KeyboardUtil.h.


Constructor & Destructor Documentation

unity::ui::KeyboardUtil::KeyboardUtil ( Display *  display)

Definition at line 29 of file KeyboardUtil.cpp.

  : display_(display)
{
  unsigned int fetch_mask = XkbGBN_KeyNamesMask | XkbGBN_ClientSymbolsMask | XkbGBN_GeometryMask;
  keyboard_ = XkbGetKeyboard (display, fetch_mask, XkbUseCoreKbd);
}

Definition at line 36 of file KeyboardUtil.cpp.

{
  XkbFreeKeyboard (keyboard_, 0, True);
}

Member Function Documentation

bool unity::ui::KeyboardUtil::CompareOffsets ( int  current_x,
int  current_y,
int  best_x,
int  best_y 
) const [private]

Definition at line 75 of file KeyboardUtil.cpp.

{
  // never EVER prefer something higher on the keyboard than what we have
  if (current_y > best_y)
    return false;

  if (current_x < best_x || current_y < best_y)
    return true;

  return false;
}

Here is the caller graph for this function:

guint unity::ui::KeyboardUtil::ConvertKeyToKeycode ( XkbKeyPtr  key) const [private]

Definition at line 87 of file KeyboardUtil.cpp.

{
  int min_code = keyboard_->min_key_code;
  int max_code = keyboard_->max_key_code;

  for (int i = min_code; i < max_code; i++)
  {
    if (!strncmp(key->name.name, keyboard_->names->keys[i].name, XkbKeyNameLength))
      return i;
  }
  return 0;
}

Here is the caller graph for this function:

bool unity::ui::KeyboardUtil::FindKeyInGeometry ( XkbGeometryPtr  geo,
char *  key_name,
int &  res_section,
XkbBoundsRec &  res_bounds 
) const [private]

Definition at line 41 of file KeyboardUtil.cpp.

{
  // seems that Xkb does not give null terminated strings... was painful
  int name_length = XkbKeyNameLength;

  int sections_in_geometry = geo->num_sections;
  for (int i = 0; i < sections_in_geometry; i++)
  {
    XkbSectionPtr section = &geo->sections[i];

    int rows_in_section = section->num_rows;
    for (int j = 0; j < rows_in_section; j++)
    {
      XkbRowPtr row = &section->rows[j];
      int keys_in_row = row->num_keys;
      for (int k = 0; k < keys_in_row; k++)
      {
        XkbKeyPtr key = &row->keys[k];
        if (!strncmp (key->name.name, key_name, name_length))
        {
          res_section = i;
          res_bounds = GetAbsoluteKeyBounds(key, row, section, geo);
          return true;
        }
        // end key
      }
      // end row
    }
    // end section
  }

  return false;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool unity::ui::KeyboardUtil::FindKeyInSectionAboveBounds ( XkbGeometryPtr  geo,
int  section,
XkbBoundsRec const &  target_bounds,
guint &  keycode 
) const [private]

Definition at line 131 of file KeyboardUtil.cpp.

{
  XkbKeyPtr best = NULL;
  int best_x_offset = G_MAXINT;
  int best_y_offset = G_MAXINT;

  int sections_in_geometry = geo->num_sections;
  for (int k = 0; k < sections_in_geometry; k++)
  {

  XkbSectionPtr section = &geo->sections[section_index];
  int rows_in_section = section->num_rows;
  for (int i = 0; i < rows_in_section; i++)
  {
    XkbRowPtr row = &section->rows[i];

    int keys_in_row = row->num_keys;
    for (int j = 0; j < keys_in_row; j++)
    {
      XkbKeyPtr key = &row->keys[j];
      XkbBoundsRec bounds = GetAbsoluteKeyBounds (key, row, section, geo);

      // make sure we are actually over the target bounds
      int center = (bounds.x1 + bounds.x2) / 2;
      if (center < target_bounds.x1 || center > target_bounds.x2)
        continue;

      // make sure the key is actually above our target.
      int current_y_offset = target_bounds.y1 - bounds.y2;
      if (current_y_offset < 0)
        continue;

      int current_x_offset = std::abs(center - (target_bounds.x1 + target_bounds.x2) / 2);

      if (CompareOffsets(current_x_offset, current_y_offset, best_x_offset, best_y_offset))
      {
        best = key;
        best_x_offset = current_x_offset;
        best_y_offset = current_y_offset;
       }
    }
  }
  }

  if (best)
  {
    keycode = ConvertKeyToKeycode(best);
    return true;
  }
  return false;
}

Here is the call graph for this function:

Here is the caller graph for this function:

XkbBoundsRec unity::ui::KeyboardUtil::GetAbsoluteKeyBounds ( XkbKeyPtr  key,
XkbRowPtr  row,
XkbSectionPtr  section,
XkbGeometryPtr  geo 
) const [private]

Definition at line 100 of file KeyboardUtil.cpp.

{
  XkbShapePtr shape = XkbKeyShape(geo, key);

  XkbBoundsRec result;
  result = shape->bounds;

  int x_offset = 0;
  int y_offset = 0;
  int i = 0;
  while (&row->keys[i] != key)
  {
    XkbKeyPtr local_key = &row->keys[i];
    XkbShapePtr local_shape = XkbKeyShape(geo, local_key);

    if (row->vertical)
      y_offset += local_shape->bounds.y2 - local_shape->bounds.y1;
    else
      x_offset += local_shape->bounds.x2 - local_shape->bounds.x1;

    i++;
  }

  result.x1 += row->left + section->left + key->gap + x_offset;
  result.x2 += row->left + section->left + key->gap + x_offset;
  result.y1 += row->top + section->top + key->gap + y_offset;
  result.y2 += row->top + section->top + key->gap + y_offset;

  return result;
}

Here is the caller graph for this function:

guint unity::ui::KeyboardUtil::GetKeycodeAboveKeySymbol ( KeySym  key_symbol) const

Definition at line 183 of file KeyboardUtil.cpp.

{
  guint result = 0;

  int code = XKeysymToKeycode(display_, key_symbol);

  if (!code)
    return result;

  if (keyboard_->min_key_code > code || keyboard_->max_key_code < code)
    return result;

  char* key_str = keyboard_->names->keys[code].name;


  int key_section;
  XkbBoundsRec key_bounds;
  bool found_key = FindKeyInGeometry(keyboard_->geom, key_str, key_section, key_bounds);

  if (found_key)
  {
    guint maybe;
    found_key = FindKeyInSectionAboveBounds(keyboard_->geom, key_section, key_bounds, maybe);

    if (found_key)
      result = maybe;
  }

  return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

bool unity::ui::KeyboardUtil::IsMoveKeySymbol ( KeySym  sym) [static]

Definition at line 231 of file KeyboardUtil.cpp.

{
  bool move_key = false;

  if (sym >= XK_Home && sym <= XK_Begin)
  {
    move_key = true;
  }

  return move_key;
}

Here is the caller graph for this function:

bool unity::ui::KeyboardUtil::IsPrintableKeySymbol ( KeySym  key_symbol) [static]

Definition at line 214 of file KeyboardUtil.cpp.

{
  bool printable_key = false;

  if (sym == XK_Delete || sym == XK_BackSpace || sym == XK_Return)
  {
    printable_key = true;
  }
  else
  {
    unsigned int unicode = gdk_keyval_to_unicode(sym);
    printable_key = g_unichar_isprint(unicode);
  }

  return printable_key;
}

Here is the caller graph for this function:


Member Data Documentation

Definition at line 55 of file KeyboardUtil.h.

XkbDescPtr unity::ui::KeyboardUtil::keyboard_ [private]

Definition at line 54 of file KeyboardUtil.h.


The documentation for this class was generated from the following files: