Debian Codegraph @

garglk/event.cpp

raw symbols SCIP
1 // Copyright (C) 2006-2009 by Tor Andersson.
2 // Copyright (C) 2010 by Ben Cressey.
3 //
4 // This file is part of Gargoyle.
5 //
6 // Gargoyle is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // Gargoyle is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with Gargoyle; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19
20 #include <algorithm>
21 #include <deque>
22 #include <list>
23
24 #include "glk.h"
25 #include "garglk.h"
26
27 // SDL audio playback happens in a separate thread, including calling
28 // the "sound finished" handler. The handler may need to post an event,
29 // but since it's in a different thread, there's possible simultaneous
30 // access of the gli_events list.
31 //
32 // The Qt sound backend does not have this issue.
33 #ifdef GARGLK_CONFIG_SDL
34 #include <mutex>
35 static std::mutex event_mutex;
36 #endif
37
38 static std::list<event_t> gli_events;
39
40 static void gli_input_guess_focus();
41 static void gli_input_more_focus();
42 static void gli_input_next_focus();
43 static void gli_input_scroll_focus();
44
45 // See issue #730. Some fonts leave trails, where they "leak" into the
46 // margins. A redraw cleans these up. The hidden "redraw_hack" option
47 // does this, allowing affected users to work around it until a proper
48 // fix is implemented.
49 bool gli_conf_redraw_hack = false;
50
51 void gli_dispatch_event(event_t *event, bool polled)
52 {
53 std::list<event_t>::iterator it;
54
55 if (gli_conf_redraw_hack) {
56 gli_force_redraw = true;
57 gli_windows_redraw();
58 }
59
60 #ifdef GARGLK_CONFIG_SDL
61 std::lock_guard<std::mutex> guard(event_mutex);
62 #endif
63
64 if (!polled) {
65 it = gli_events.begin();
66 } else {
67 // §4: glk_select_poll() returns internally-spawned events only,
68 // so find the first such event in the list.
69 it = std::find_if(gli_events.begin(), gli_events.end(), [](const auto &msg) {
70 return msg.type == evtype_Arrange ||
71 msg.type == evtype_Redraw ||
72 msg.type == evtype_SoundNotify ||
73 msg.type == evtype_Timer;
74 });
75 }
76
77 if (it != gli_events.end()) {
78 *event = *it;
79 gli_events.erase(it);
80 }
81 }
82
83 // Various modules can call this to indicate that an event has occurred.
84 void gli_event_store(glui32 type, window_t *win, glui32 val1, glui32 val2)
85 {
86 event_t store{};
87
88 store.type = type;
89 store.win = win;
90 store.val1 = val1;
91 store.val2 = val2;
92
93 #ifdef GARGLK_CONFIG_SDL
94 std::lock_guard<std::mutex> guard(event_mutex);
95 #endif
96 gli_events.push_back(store);
97 }
98
99 void glk_tick()
100 {
101 #ifdef GARGLK_CONFIG_TICK
102 gli_tick();
103 #endif
104 }
105
106 // When pasting, it's possible that one of the pasted characters will
107 // cause the current input event to stop (for char events, any character
108 // will suffice, and for line events, a newline will do it). Once the
109 // current input event is no longer active, any key presses are normally
110 // discarded: if the window's not expecting input, it's got nowhere to
111 // go. This causes problems with text pasting, however: if you paste
112 // multiple lines, the line input event will be disabled after the first
113 // line is pasted, and the rest of the lines will disappear into the
114 // ether. To work around this, if no input events are active when a
115 // paste comes in, the pasted characters will be buffered and replayed
116 // the next time input is requested.
117 static std::deque<glui32> paste_buffer;
118
119 // Handle a keystroke.
120 static bool gli_input_handle_key(glui32 key, bool add_to_buffer)
121 {
122 if (gli_more_focus) {
123 gli_input_more_focus();
124 }
125
126 else {
127 switch (key) {
128 case keycode_Tab:
129 gli_input_next_focus();
130 return true;
131 case keycode_PageUp:
132 case keycode_PageDown:
133 case keycode_MouseWheelUp:
134 case keycode_MouseWheelDown:
135 gli_input_scroll_focus();
136 break;
137 default:
138 gli_input_guess_focus();
139 break;
140 }
141 }
142
143 window_t *win = gli_focuswin;
144
145 if (win == nullptr) {
146 if (gli_terminated) {
147 winexit();
148 }
149
150 return true;
151 }
152
153 bool defer_exit = false;
154 bool handled = true;
155
156 auto buffer = [&add_to_buffer, &key, &handled]() {
157 if (add_to_buffer) {
158 paste_buffer.push_back(key);
159 }
160 handled = false;
161 };
162
163 switch (win->type) {
164 case wintype_TextGrid:
165 if (win->char_request || win->char_request_uni) {
166 gcmd_grid_accept_readchar(win, key);
167 } else if (win->line_request || win->line_request_uni) {
168 gcmd_grid_accept_readline(win, key);
169 } else {
170 buffer();
171 }
172 break;
173 case wintype_TextBuffer:
174 if (win->char_request || win->char_request_uni) {
175 gcmd_buffer_accept_readchar(win, key);
176 } else if (win->line_request || win->line_request_uni) {
177 gcmd_buffer_accept_readline(win, key);
178 } else if (!add_to_buffer && (win->more_request || win->scroll_request)) {
179 defer_exit = gcmd_accept_scroll(win, key);
180 handled = false;
181 } else {
182 buffer();
183 }
184 break;
185 }
186
187 if (gli_terminated && !defer_exit) {
188 winexit();
189 }
190
191 return handled;
192 }
193
194 void gli_input_handle_key(glui32 key)
195 {
196 gli_input_handle_key(key, false);
197 }
198
199 void gli_input_handle_key_paste(glui32 key)
200 {
201 gli_input_handle_key(key, true);
202 }
203
204 void gli_input_handle_click(int x, int y)
205 {
206 if (gli_rootwin != nullptr) {
207 gli_window_click(gli_rootwin, x, y);
208 }
209 }
210
211 static void gli_select_or_poll(event_t *event, bool polled)
212 {
213 static bool first_event = false;
214 if (!first_event) {
215 gli_input_guess_focus();
216 first_event = true;
217 }
218
219 while (!paste_buffer.empty() && gli_input_handle_key(paste_buffer.front(), false)) {
220 paste_buffer.pop_front();
221 }
222
223 gli_select(event, polled);
224 }
225
226 void glk_select(event_t *event)
227 {
228 gli_select_or_poll(event, false);
229 }
230
231 void glk_select_poll(event_t *event)
232 {
233 gli_select_or_poll(event, true);
234 }
235
236 // Pick first window which might want input.
237 // This is called after every keystroke.
238 static void gli_input_guess_focus()
239 {
240 window_t *altwin = gli_focuswin;
241
242 do {
243 if (altwin != nullptr
244 && (altwin->line_request || altwin->char_request ||
245 altwin->line_request_uni || altwin->char_request_uni)) {
246 break;
247 }
248 altwin = gli_window_iterate_treeorder(altwin);
249 } while (altwin != gli_focuswin);
250
251 if (gli_focuswin != altwin) {
252 gli_focuswin = altwin;
253 gli_force_redraw = true;
254 gli_windows_redraw();
255 }
256 }
257
258 // Pick first window with buffered output.
259 // This is called after printing 'more' prompts.
260 static void gli_input_more_focus()
261 {
262 window_t *altwin = gli_focuswin;
263
264 do {
265 if (altwin != nullptr
266 && (altwin->more_request)) {
267 break;
268 }
269 altwin = gli_window_iterate_treeorder(altwin);
270 } while (altwin != gli_focuswin);
271
272 gli_focuswin = altwin;
273 }
274
275 // Pick next window which might want input.
276 // This is called after pressing 'tab'.
277 static void gli_input_next_focus()
278 {
279 window_t *altwin = gli_focuswin;
280
281 do {
282 altwin = gli_window_iterate_treeorder(altwin);
283 if (altwin != nullptr
284 && (altwin->line_request || altwin->char_request ||
285 altwin->line_request_uni || altwin->char_request_uni)) {
286 break;
287 }
288 } while (altwin != gli_focuswin);
289
290 if (gli_focuswin != altwin) {
291 gli_focuswin = altwin;
292 gli_force_redraw = true;
293 gli_windows_redraw();
294 }
295 }
296
297 // Pick first window which might want scrolling.
298 // This is called after pressing page keys.
299 static void gli_input_scroll_focus()
300 {
301 window_t *altwin = gli_focuswin;
302
303 do {
304 if (altwin != nullptr
305 && (altwin->scroll_request)) {
306 break;
307 }
308 altwin = gli_window_iterate_treeorder(altwin);
309 } while (altwin != gli_focuswin);
310
311 gli_focuswin = altwin;
312 }