File: key-handler.c

package info (click to toggle)
gst-plugins-bad1.0 1.26.10-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 69,996 kB
  • sloc: ansic: 722,568; cpp: 278,154; objc: 3,556; xml: 3,351; sh: 1,095; python: 508; makefile: 175; java: 75
file content (268 lines) | stat: -rw-r--r-- 6,665 bytes parent folder | download | duplicates (6)
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* GStreamer
 * Copyright (C) 2022 Seungha Yang <seungha@centricular.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "key-handler.h"

#ifdef G_OS_WIN32
#include <windows.h>

typedef struct _Win32KeyHandler
{
  GThread *thread;
  HANDLE cancellable;
  HANDLE console_handle;
  GMutex lock;
  gboolean closing;

  KeyInputCallback callback;
  gpointer user_data;
} Win32KeyHandler;

typedef struct
{
  KeyInputCallback callback;
  gpointer user_data;
  gchar value;
  gboolean is_ascii;
} KeyInputCallbackData;

static Win32KeyHandler *_handler = NULL;

static gboolean
handler_source_func (KeyInputCallbackData * data)
{
  data->callback (data->value, data->is_ascii, data->user_data);

  return G_SOURCE_REMOVE;
}

static gpointer
handler_thread_func (Win32KeyHandler * handler)
{
  HANDLE handles[2];

  handles[0] = handler->cancellable;
  handles[1] = handler->console_handle;

  while (TRUE) {
    DWORD ret = WaitForMultipleObjects (2, handles, FALSE, INFINITE);
    INPUT_RECORD buffer;
    DWORD num_read = 0;
    KeyInputCallbackData *data;

    if (ret == WAIT_FAILED) {
      gst_printerrln ("Wait failed");
      return NULL;
    }

    g_mutex_lock (&handler->lock);
    if (handler->closing) {
      g_mutex_unlock (&handler->lock);

      return NULL;
    }
    g_mutex_unlock (&handler->lock);

    if (!PeekConsoleInput (handler->console_handle, &buffer, 1, &num_read) ||
        num_read != 1)
      continue;

    ReadConsoleInput (handler->console_handle, &buffer, 1, &num_read);
    if (buffer.EventType != KEY_EVENT || !buffer.Event.KeyEvent.bKeyDown)
      continue;

    data = g_new0 (KeyInputCallbackData, 1);
    data->callback = handler->callback;
    data->user_data = handler->user_data;

    switch (buffer.Event.KeyEvent.wVirtualKeyCode) {
      case VK_UP:
        data->value = (gchar) KB_ARROW_UP;
        break;
      case VK_DOWN:
        data->value = (gchar) KB_ARROW_DOWN;
        break;
      case VK_LEFT:
        data->value = (gchar) KB_ARROW_LEFT;
        break;
      case VK_RIGHT:
        data->value = (gchar) KB_ARROW_RIGHT;
        break;
      default:
        data->value = buffer.Event.KeyEvent.uChar.AsciiChar;
        data->is_ascii = TRUE;
        break;
    }

    g_main_context_invoke_full (NULL,
        G_PRIORITY_DEFAULT,
        (GSourceFunc) handler_source_func, data, (GDestroyNotify) g_free);
  }
}

void
set_key_handler (KeyInputCallback callback, gpointer user_data)
{
  if (_handler || !callback)
    return;

  _handler = g_new0 (Win32KeyHandler, 1);

  SECURITY_ATTRIBUTES attr;
  attr.nLength = sizeof (SECURITY_ATTRIBUTES);
  attr.lpSecurityDescriptor = NULL;
  attr.bInheritHandle = FALSE;

  _handler->cancellable = CreateEvent (&attr, TRUE, FALSE, NULL);
  _handler->console_handle = GetStdHandle (STD_INPUT_HANDLE);
  _handler->callback = callback;
  _handler->user_data = user_data;
  g_mutex_init (&_handler->lock);
  _handler->thread =
      g_thread_new ("key-handler", (GThreadFunc) handler_thread_func, _handler);
}

void
unset_key_handler (void)
{
  if (!_handler)
    return;

  g_mutex_lock (&_handler->lock);
  _handler->closing = TRUE;
  g_mutex_unlock (&_handler->lock);

  SetEvent (_handler->cancellable);
  g_thread_join (_handler->thread);
  CloseHandle (_handler->cancellable);
  g_mutex_clear (&_handler->lock);

  g_clear_pointer (&_handler, g_free);
}
#else /* G_OS_WIN32 */

#include <termios.h>
#include <unistd.h>
#include <stdio.h>

typedef struct _LinuxKeyHandler
{
  gulong watch_id;
  struct termios term_settings;
  KeyInputCallback callback;
  GSource *source;
  gpointer user_data;
} LinuxKeyHandler;

static LinuxKeyHandler *_handler = NULL;

static gboolean
_handlerio_func (GIOChannel * channel,
    GIOCondition condition, LinuxKeyHandler * handler)
{
  if (condition & G_IO_IN) {
    GIOStatus status;
    gchar buf[16] = { 0, };
    gsize read;

    status =
        g_io_channel_read_chars (channel, buf, sizeof (buf) - 1, &read, NULL);
    if (status == G_IO_STATUS_ERROR) {
      return G_SOURCE_REMOVE;
    }

    if (status == G_IO_STATUS_NORMAL) {
      gchar value;
      gboolean is_ascii = FALSE;

      if (g_strcmp0 (buf, "\033[A") == 0) {
        value = (gchar) KB_ARROW_UP;
      } else if (g_strcmp0 (buf, "\033[B") == 0) {
        value = (gchar) KB_ARROW_DOWN;
      } else if (g_strcmp0 (buf, "\033[D") == 0) {
        value = (gchar) KB_ARROW_LEFT;
      } else if (g_strcmp0 (buf, "\033[C") == 0) {
        value = (gchar) KB_ARROW_RIGHT;
      } else {
        value = buf[0];
        is_ascii = TRUE;
      }

      handler->callback (value, is_ascii, handler->user_data);
    }
  }

  return G_SOURCE_CONTINUE;
}

void
set_key_handler (KeyInputCallback callback, gpointer user_data)
{
  struct termios new_settings;
  struct termios old_settings;
  GIOChannel *io_channel;

  if (_handler || !callback)
    return;

  if (tcgetattr (STDIN_FILENO, &old_settings) != 0)
    return;

  new_settings = old_settings;
  new_settings.c_lflag &= ~(ECHO | ICANON | IEXTEN);
  new_settings.c_cc[VMIN] = 0;
  new_settings.c_cc[VTIME] = 0;

  if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &new_settings) != 0)
    return;

  setvbuf (stdin, NULL, _IONBF, 0);

  _handler = g_new0 (LinuxKeyHandler, 1);
  _handler->term_settings = old_settings;
  _handler->callback = callback;
  _handler->user_data = user_data;

  io_channel = g_io_channel_unix_new (STDIN_FILENO);

  _handler->source = g_io_create_watch (io_channel, G_IO_IN);
  g_io_channel_unref (io_channel);

  g_source_set_callback (_handler->source, (GSourceFunc) _handlerio_func,
      _handler, NULL);
  g_source_attach (_handler->source, NULL);
}

void
unset_key_handler (void)
{
  if (!_handler)
    return;

  if (_handler->source) {
    g_source_destroy (_handler->source);
    g_source_unref (_handler->source);
  }

  tcsetattr (STDIN_FILENO, TCSAFLUSH, &_handler->term_settings);
  setvbuf (stdin, NULL, _IOLBF, 0);

  g_clear_pointer (&_handler, g_free);
}
#endif