1 /*
2 * Copyright (C) 2014 BlueKitchen GmbH
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the copyright holders nor the names of
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * 4. Any redistribution, use, or modification is done solely for
17 * personal benefit and not for any commercial purpose or for
18 * monetary gain.
19 *
20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24 * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * Please inquire about commercial licensing options at
34 * [email protected]
35 *
36 */
37
38 #define BTSTACK_FILE__ "btstack_stdin_windows.c"
39
40 #include <Windows.h>
41 #include <errno.h>
42 #include <stdio.h>
43
44 #include "btstack_run_loop.h"
45 #include "btstack_defines.h"
46 #include <stdlib.h>
47
48 #include "btstack_stdin.h"
49 #include "btstack_stdin_windows.h"
50
51 // From MSDN:
52 // __WIN32 Defined as 1 when the compilation target is 32-bit ARM, 64-bit ARM, x86, or x64.
53 // Otherwise, undefined.
54
55 #include <conio.h> //provides non standard getch() function
56 #include <signal.h>
57
58 static btstack_data_source_t stdin_source;
59 static int activated = 0;
60
61 static HANDLE stdin_reader_thread_handle;
62 static char key_read_buffer;
63 static HANDLE key_processed_handle;
64 static void (*stdin_handler)(char c);
65 static void (*ctrl_c_handler)(void);
66
stdin_reader_thread_process(void * p)67 static DWORD WINAPI stdin_reader_thread_process(void * p){
68 while (true){
69 key_read_buffer = _getch();
70 SignalObjectAndWait(stdin_source.source.handle, key_processed_handle, INFINITE, FALSE);
71 }
72 return 0;
73 }
74
stdin_process(btstack_data_source_t * ds,btstack_data_source_callback_type_t callback_type)75 static void stdin_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type){
76
77 // handle CTRL-C
78 if (key_read_buffer == 0x03){
79 if (ctrl_c_handler != NULL){
80 (*ctrl_c_handler)();
81 } else {
82 printf("Ignore CTRL-c\n");
83 }
84 } else {
85 if (stdin_handler){
86 (*stdin_handler)(key_read_buffer);
87 }
88 }
89
90 SetEvent(key_processed_handle);
91 }
92
btstack_stdin_windows_init(void)93 void btstack_stdin_windows_init(void){
94 if (activated) return;
95
96 // asynchronous io on stdin via OVERLAPPED seems to be problematic.
97
98 // Use separate thread and event objects instead
99 stdin_source.source.handle = CreateEvent(NULL, FALSE, FALSE, NULL);
100 key_processed_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
101 // default attributes, default stack size, proc, args, start immediately, don't care for thread id
102 stdin_reader_thread_handle = CreateThread(NULL, 0, &stdin_reader_thread_process, NULL, 0, NULL);
103
104 btstack_run_loop_enable_data_source_callbacks(&stdin_source, DATA_SOURCE_CALLBACK_READ);
105 btstack_run_loop_set_data_source_handler(&stdin_source, stdin_process);
106 btstack_run_loop_add_data_source(&stdin_source);
107
108 activated = 1;
109 }
110
btstack_stdin_window_register_ctrl_c_callback(void (* callback)(void))111 void btstack_stdin_window_register_ctrl_c_callback(void (*callback)(void)){
112 ctrl_c_handler = callback;
113 }
114
btstack_stdin_setup(void (* handler)(char c))115 void btstack_stdin_setup(void (*handler)(char c)){
116 btstack_stdin_windows_init();
117 stdin_handler = handler;
118 }
119
btstack_stdin_reset(void)120 void btstack_stdin_reset(void){
121 if (!activated) return;
122 activated = 0;
123 stdin_handler = NULL;
124
125 btstack_run_loop_remove_data_source(&stdin_source);
126
127 // shutdown thread
128 TerminateThread(stdin_reader_thread_handle, 0);
129 WaitForSingleObject(stdin_reader_thread_handle, INFINITE);
130 CloseHandle(stdin_reader_thread_handle);
131
132 // free events
133 CloseHandle(stdin_source.source.handle);
134 CloseHandle(key_processed_handle);
135 }
136
137