~malept/gtkhotkey/win32-support

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
250
/*
 * This file is part of GtkHotkey.
 * Copyright (C) 2008, 2009 Mark Lee <gtkhotkey@lazymalevolence.com>
 *
 *   GtkHotkey 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 3 of the License, or
 *   (at your option) any later version.
 *
 *   GtkHotkey 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 GtkHotkey.  If not, see <http://www.gnu.org/licenses/>.
 */
using Gdk;

namespace GtkHotkey
{
  [Compact]
  private class AtomValue
  {
    public Win32.Atom atom;
    public AtomValue (Win32.Atom atom)
    {
      this.atom = atom;
    }
    public static uint hash (void* key)
    {
      if (key == null)
      {
        return 0;
      }
      else
      {
        return (uint)(((AtomValue)key).atom);
      }
    }
    public static bool equals (void* a, void* b)
    {
      return ((AtomValue)a).atom == ((AtomValue)b).atom;
    }
  }
  [Compact]
  private class Win32Hotkey
  {
    public Win32.Atom atom;
    public Info info;
    public Win32Hotkey (Win32.Atom atom, Info info)
    {
      this.atom = atom;
      this.info = info;
    }
    ~Win32Hotkey ()
    {
      this.atom.delete_global ();
    }
  }

  public class Win32Listener : Listener
  {

    private Gdk.Window hidden = null;
    private Win32.WindowHandle hwnd;
    private HashTable<AtomValue,Win32Hotkey> hotkey_registry;

    construct
    {
      this.hidden = this.create_hidden_msg_window ();
      this.hwnd = Win32.WindowHandle.from_gdk_window (this.hidden);
      this.hotkey_registry = new HashTable<AtomValue,Win32Hotkey> ((HashFunc)AtomValue.hash,
                                                                        (EqualFunc)AtomValue.equals);
      this.hidden.add_filter (this.hotkey_filter);
    }

    ~Win32Listener ()
    {
      List<weak AtomValue>? keys = this.hotkey_registry.get_keys ();
      foreach (weak AtomValue av in keys)
      {
        this.do_remove_hotkey (av.atom);
      }
      this.hidden.remove_filter (this.hotkey_filter); // XXX Fix VAPI
      this.hidden.destroy ();
      this.hidden = null;
    }

    private bool
    do_remove_hotkey (Win32.Atom atom)
    {
      Win32.HotKey.unregister (this.hwnd, atom);
      AtomValue av = new AtomValue (atom);
      return this.hotkey_registry.remove (av);
    }

    /**
     * "Ported" from GTK+ SVN revision 20900, file
     * </modules/engines/ms-windows/msw_theme_main.c>.
     * Licensed under the LGPL-2 or later.
     */
    private Window
    create_hidden_msg_window ()
    {
      WindowAttr attributes = WindowAttr ();
      int attributes_mask;

      attributes.x = -100;
      attributes.y = -100;
      attributes.width = 10;
      attributes.height = 10;
      attributes.window_type = WindowType.TEMP;
      attributes.wclass = WindowClass.INPUT_ONLY;
      attributes.override_redirect = true;
      attributes.event_mask = 0;

      attributes_mask = WindowAttributesType.X |
                        WindowAttributesType.Y |
                        WindowAttributesType.NOREDIR;
      return new Window (get_default_root_window (), attributes,
                         attributes_mask);
    }

    private Win32.Atom atomize (Info hotkey)
    {
      string key;
      Win32.Atom atom;
      key = hotkey.application_id + ":" + hotkey.key_id;
      atom = Win32.Atom.find_global (key);
      if (atom == 0)
      {
        return Win32.Atom.add_global (key);
      }
      else
      {
        return atom;
      }
    }

    private FilterReturn
    hotkey_filter (XEvent xevt, Event evt)
    {
      weak Win32.Message* p_msg = (Win32.Message*)xevt;
      weak Win32.Message msg = *p_msg;
      if (msg.message == Win32.Message.HOTKEY)
      {
        AtomValue av = new AtomValue ((Win32.Atom)msg.wParam);
        weak Win32Hotkey? wh = this.hotkey_registry.lookup (av);
        if (wh != null)
        {
          time_t current_time = time_t ();
          uint event_time = (uint)current_time;
          this.activated (wh.info, event_time);
          if (wh.info != null)
          {
            wh.info.activated (event_time);
          }
          return Gdk.FilterReturn.REMOVE;
        }
      }
      return Gdk.FilterReturn.CONTINUE;
    }

    private uint
    convert_modifier_mask (Gdk.ModifierType modifier)
    {
      uint win32_modifier = 0;
      if ((modifier & ModifierType.SHIFT_MASK) != 0)
      {
        win32_modifier |= Win32.HotKey.MOD_SHIFT;
      }
      if ((modifier & ModifierType.CONTROL_MASK) != 0)
      {
        win32_modifier |= Win32.HotKey.MOD_CONTROL;
      }
      if ((modifier & ModifierType.MOD1_MASK) != 0)
      {
        win32_modifier |= Win32.HotKey.MOD_ALT;
      }
      if ((modifier & ModifierType.SUPER_MASK) != 0 ||
          (modifier & ModifierType.HYPER_MASK) != 0)
      {
        win32_modifier |= Win32.HotKey.MOD_WIN;
      }
      return win32_modifier;
    }

    private uint
    convert_keyval (uint keyval)
    {
      weak Gdk.Keymap km;
      weak Gdk.KeymapKey[]? keys;
      km = Gdk.Keymap.get_default ();
      if (km.get_entries_for_keyval (keyval, out keys))
      {
        return keys[0].keycode;
      }
      else
      {
        return 0;
      }
    }

    public override bool bind_hotkey (Info hotkey) throws Error
    {
      bool result = false;
      weak Win32Hotkey? wh = null;
      Win32.Atom atom = this.atomize (hotkey);
      AtomValue av = new AtomValue (atom);
      wh = this.hotkey_registry.lookup (av);
      if (wh == null)
      {
        uint keyval, modifier, keycode;
        Gdk.ModifierType gdk_modifier;
        Gtk.accelerator_parse (hotkey.signature, out keyval,
                               out gdk_modifier);
        modifier = this.convert_modifier_mask (gdk_modifier);
        keycode = this.convert_keyval (keyval);
        result = Win32.HotKey.register (this.hwnd, atom, modifier, keycode);
        if (result)
        {
          Win32Hotkey new_wh = new Win32Hotkey (atom, hotkey);
          this.hotkey_registry.insert (#av, #new_wh);
        }
        else
        {
          warning ("Could not register hotkey.");
        }
      }
      return result;
    }
    public override bool unbind_hotkey (Info hotkey) throws Error
    {
      weak Win32Hotkey? wh = null;
      Win32.Atom atom = this.atomize (hotkey);
      AtomValue av = new AtomValue (atom);
      wh = this.hotkey_registry.lookup (av);
      if (wh != null)
      {
        return this.do_remove_hotkey (atom);
      }
      else
      {
        return false;
      }
    }
  }
}
/* vim: set ts=2 sts=2 sw=2 et ai cindent : */