~ted/unity/menu-ref

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// -*- Mode: C++; indent-tabs-mode: nil; tab-width: 2 -*-
/*
 * Copyright (C) 2011 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Jason Smith <jason.smith@canonical.com>
 */

#include <gdk/gdk.h>
#include <string.h>
#include <cmath>

#include "KeyboardUtil.h"

namespace unity {
namespace ui {
namespace {
  const unsigned int FETCH_MASK = XkbGBN_KeyNamesMask | XkbGBN_ClientSymbolsMask | XkbGBN_GeometryMask;
}

KeyboardUtil::KeyboardUtil(Display *display)
  : display_(display)
  , keyboard_(XkbGetKeyboard(display_, FETCH_MASK, XkbUseCoreKbd))
{}

KeyboardUtil::~KeyboardUtil()
{
  XkbFreeKeyboard(keyboard_, 0, True);
}

bool KeyboardUtil::FindKeyInGeometry(XkbGeometryPtr geo, char *key_name, int& res_section, XkbBoundsRec& res_bounds) const
{
  // 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;
}

bool KeyboardUtil::CompareOffsets(int current_x, int current_y, int best_x, int best_y) const
{
  // 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;
}

guint KeyboardUtil::ConvertKeyToKeycode(XkbKeyPtr key) const
{
  if (!keyboard_)
    return 0;

  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;
}

XkbBoundsRec KeyboardUtil::GetAbsoluteKeyBounds(XkbKeyPtr key, XkbRowPtr row, XkbSectionPtr section, XkbGeometryPtr geo) const
{
  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;
}

bool KeyboardUtil::FindKeyInSectionAboveBounds(XkbGeometryPtr geo, int section_index, XkbBoundsRec const& target_bounds, guint &keycode) const
{
  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;
}

guint KeyboardUtil::GetKeycodeAboveKeySymbol(KeySym key_symbol) const
{
  guint result = 0;

  int code = XKeysymToKeycode(display_, key_symbol);

  if (!code || !keyboard_)
    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;
}

bool KeyboardUtil::IsPrintableKeySymbol(KeySym sym)
{
  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;
}

bool KeyboardUtil::IsMoveKeySymbol(KeySym sym)
{
  bool move_key = false;

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

  return move_key;
}

}
}