Commit | Line | Data |
---|---|---|
7d4115a5 KL |
1 | /** |
2 | * Jexer - Java Text User Interface - demonstration program | |
3 | * | |
4 | * Version: $Id$ | |
5 | * | |
6 | * Author: Kevin Lamonte, <a href="mailto:kevin.lamonte@gmail.com">kevin.lamonte@gmail.com</a> | |
7 | * | |
8 | * License: LGPLv3 or later | |
9 | * | |
10 | * Copyright: This module is licensed under the GNU Lesser General | |
11 | * Public License Version 3. Please see the file "COPYING" in this | |
12 | * directory for more information about the GNU Lesser General Public | |
13 | * License Version 3. | |
14 | * | |
15 | * Copyright (C) 2015 Kevin Lamonte | |
16 | * | |
17 | * This program is free software; you can redistribute it and/or | |
18 | * modify it under the terms of the GNU Lesser General Public License | |
19 | * as published by the Free Software Foundation; either version 3 of | |
20 | * the License, or (at your option) any later version. | |
21 | * | |
22 | * This program is distributed in the hope that it will be useful, but | |
23 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
25 | * General Public License for more details. | |
26 | * | |
27 | * You should have received a copy of the GNU Lesser General Public | |
28 | * License along with this program; if not, see | |
29 | * http://www.gnu.org/licenses/, or write to the Free Software | |
30 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
31 | * 02110-1301 USA | |
32 | */ | |
33 | package jexer; | |
34 | ||
4328bb42 KL |
35 | import java.io.InputStream; |
36 | import java.io.OutputStream; | |
37 | import java.io.UnsupportedEncodingException; | |
38 | import java.util.LinkedList; | |
39 | import java.util.List; | |
40 | ||
41 | import jexer.bits.CellAttributes; | |
42 | import jexer.bits.ColorTheme; | |
43 | import jexer.bits.GraphicsChars; | |
44 | import jexer.event.TCommandEvent; | |
45 | import jexer.event.TInputEvent; | |
46 | import jexer.event.TKeypressEvent; | |
47 | import jexer.event.TMouseEvent; | |
48 | import jexer.event.TResizeEvent; | |
49 | import jexer.backend.Backend; | |
50 | import jexer.backend.ECMA48Backend; | |
51 | import static jexer.TCommand.*; | |
52 | import static jexer.TKeypress.*; | |
53 | ||
7d4115a5 KL |
54 | /** |
55 | * TApplication sets up a full Text User Interface application. | |
56 | */ | |
57 | public class TApplication { | |
58 | ||
59 | /** | |
4328bb42 KL |
60 | * Access to the physical screen, keyboard, and mouse. |
61 | */ | |
62 | public Backend backend; | |
63 | ||
64 | /** | |
65 | * Actual mouse coordinate X | |
66 | */ | |
67 | private int mouseX; | |
68 | ||
69 | /** | |
70 | * Actual mouse coordinate Y | |
71 | */ | |
72 | private int mouseY; | |
73 | ||
74 | /** | |
75 | * Event queue that will be drained by either primary or secondary Fiber | |
76 | */ | |
77 | private List<TInputEvent> eventQueue; | |
78 | ||
79 | /** | |
80 | * Windows and widgets pull colors from this ColorTheme. | |
81 | */ | |
82 | public ColorTheme theme; | |
83 | ||
84 | /** | |
85 | * When true, exit the application. | |
86 | */ | |
87 | public boolean quit = false; | |
88 | ||
89 | /** | |
90 | * When true, repaint the entire screen. | |
91 | */ | |
92 | public boolean repaint = true; | |
93 | ||
94 | /** | |
95 | * When true, just flush updates from the screen. | |
7d4115a5 | 96 | */ |
4328bb42 KL |
97 | public boolean flush = false; |
98 | ||
99 | /** | |
100 | * Y coordinate of the top edge of the desktop. | |
101 | */ | |
102 | static public final int desktopTop = 1; | |
103 | ||
104 | /** | |
105 | * Y coordinate of the bottom edge of the desktop. | |
106 | */ | |
107 | public int desktopBottom; | |
108 | ||
109 | /** | |
110 | * Public constructor. | |
111 | * | |
112 | * @param input an InputStream connected to the remote user, or null for | |
113 | * System.in. If System.in is used, then on non-Windows systems it will | |
114 | * be put in raw mode; shutdown() will (blindly!) put System.in in cooked | |
115 | * mode. input is always converted to a Reader with UTF-8 encoding. | |
116 | * @param output an OutputStream connected to the remote user, or null | |
117 | * for System.out. output is always converted to a Writer with UTF-8 | |
118 | * encoding. | |
119 | */ | |
120 | public TApplication(InputStream input, OutputStream output) throws UnsupportedEncodingException { | |
121 | ||
122 | backend = new ECMA48Backend(input, output); | |
123 | theme = new ColorTheme(); | |
124 | desktopBottom = backend.screen.getHeight() - 1; | |
125 | eventQueue = new LinkedList<TInputEvent>(); | |
126 | } | |
127 | ||
128 | /** | |
129 | * Invert the cell at the mouse pointer position. | |
130 | */ | |
131 | private void drawMouse() { | |
132 | CellAttributes attr = backend.screen.getAttrXY(mouseX, mouseY); | |
133 | attr.foreColor = attr.foreColor.invert(); | |
134 | attr.backColor = attr.backColor.invert(); | |
135 | backend.screen.putAttrXY(mouseX, mouseY, attr, false); | |
136 | flush = true; | |
7d4115a5 KL |
137 | |
138 | /* | |
4328bb42 KL |
139 | if (windows.length == 0) { |
140 | repaint = true; | |
141 | } | |
142 | */ | |
143 | // TODO: remove this repaint after the above if (windows.length == 0) | |
144 | // can be used again. | |
145 | repaint = true; | |
146 | } | |
147 | ||
148 | /** | |
149 | * Draw everything. | |
150 | */ | |
151 | final public void drawAll() { | |
152 | if ((flush) && (!repaint)) { | |
153 | backend.flushScreen(); | |
154 | flush = false; | |
155 | return; | |
156 | } | |
157 | ||
158 | if (!repaint) { | |
159 | return; | |
160 | } | |
161 | ||
162 | // If true, the cursor is not visible | |
163 | boolean cursor = false; | |
164 | ||
165 | // Start with a clean screen | |
166 | backend.screen.clear(); | |
167 | ||
168 | // Draw the background | |
169 | CellAttributes background = theme.getColor("tapplication.background"); | |
170 | backend.screen.putAll(GraphicsChars.HATCH, background); | |
171 | ||
172 | /* | |
173 | // Draw each window in reverse Z order | |
174 | TWindow [] sorted = windows.dup; | |
175 | sorted.sort.reverse; | |
176 | foreach (w; sorted) { | |
177 | w.drawChildren(); | |
178 | } | |
179 | ||
180 | // Draw the blank menubar line - reset the screen clipping first so | |
181 | // it won't trim it out. | |
182 | backend.screen.resetClipping(); | |
183 | backend.screen.hLineXY(0, 0, backend.screen.getWidth(), ' ', | |
184 | theme.getColor("tmenu")); | |
185 | // Now draw the menus. | |
186 | int x = 1; | |
187 | foreach (m; menus) { | |
188 | CellAttributes menuColor; | |
189 | CellAttributes menuMnemonicColor; | |
190 | if (m.active) { | |
191 | menuColor = theme.getColor("tmenu.highlighted"); | |
192 | menuMnemonicColor = theme.getColor("tmenu.mnemonic.highlighted"); | |
193 | } else { | |
194 | menuColor = theme.getColor("tmenu"); | |
195 | menuMnemonicColor = theme.getColor("tmenu.mnemonic"); | |
196 | } | |
197 | // Draw the menu title | |
198 | backend.screen.hLineXY(x, 0, cast(int)m.title.length + 2, ' ', | |
199 | menuColor); | |
200 | backend.screen.putStrXY(x + 1, 0, m.title, menuColor); | |
201 | // Draw the highlight character | |
202 | backend.screen.putCharXY(x + 1 + m.mnemonic.shortcutIdx, 0, | |
203 | m.mnemonic.shortcut, menuMnemonicColor); | |
204 | ||
205 | if (m.active) { | |
206 | m.drawChildren(); | |
207 | // Reset the screen clipping so we can draw the next title. | |
208 | backend.screen.resetClipping(); | |
209 | } | |
210 | x += m.title.length + 2; | |
211 | } | |
212 | ||
213 | foreach (m; subMenus) { | |
214 | // Reset the screen clipping so we can draw the next sub-menu. | |
215 | backend.screen.resetClipping(); | |
216 | m.drawChildren(); | |
217 | } | |
218 | */ | |
219 | ||
220 | // Draw the mouse pointer | |
221 | drawMouse(); | |
7d4115a5 | 222 | |
4328bb42 KL |
223 | /* |
224 | // Place the cursor if it is visible | |
225 | TWidget activeWidget = null; | |
226 | if (sorted.length > 0) { | |
227 | activeWidget = sorted[$ - 1].getActiveChild(); | |
228 | if (activeWidget.hasCursor) { | |
229 | backend.screen.putCursor(true, activeWidget.getCursorAbsoluteX(), | |
230 | activeWidget.getCursorAbsoluteY()); | |
231 | cursor = true; | |
232 | } | |
233 | } | |
234 | ||
235 | // Kill the cursor | |
236 | if (cursor == false) { | |
237 | backend.screen.hideCursor(); | |
238 | } | |
239 | */ | |
240 | ||
241 | // Flush the screen contents | |
242 | backend.flushScreen(); | |
243 | ||
244 | repaint = false; | |
245 | flush = false; | |
246 | } | |
247 | ||
248 | /** | |
249 | * Run this application until it exits, using stdin and stdout | |
250 | */ | |
251 | public final void run() { | |
252 | ||
253 | while (quit == false) { | |
254 | // Timeout is in milliseconds, so default timeout after 1 second | |
255 | // of inactivity. | |
256 | int timeout = getSleepTime(1000); | |
7d4115a5 KL |
257 | // std.stdio.stderr.writefln("poll() timeout: %d", timeout); |
258 | ||
4328bb42 | 259 | if (eventQueue.size() > 0) { |
7d4115a5 KL |
260 | // Do not wait if there are definitely events waiting to be |
261 | // processed or a screen redraw to do. | |
262 | timeout = 0; | |
263 | } | |
264 | ||
265 | // Pull any pending input events | |
4328bb42 | 266 | List<TInputEvent> events = backend.getEvents(timeout); |
7d4115a5 KL |
267 | metaHandleEvents(events); |
268 | ||
269 | // Process timers and call doIdle()'s | |
270 | doIdle(); | |
271 | ||
272 | // Update the screen | |
273 | drawAll(); | |
274 | } | |
275 | ||
4328bb42 KL |
276 | /* |
277 | ||
7d4115a5 KL |
278 | // Shutdown the fibers |
279 | eventQueue.length = 0; | |
280 | if (secondaryEventFiber !is null) { | |
281 | assert(secondaryEventReceiver !is null); | |
282 | secondaryEventReceiver = null; | |
283 | if (secondaryEventFiber.state == Fiber.State.HOLD) { | |
284 | // Wake up the secondary handler so that it can exit. | |
285 | secondaryEventFiber.call(); | |
286 | } | |
287 | } | |
288 | ||
289 | if (primaryEventFiber.state == Fiber.State.HOLD) { | |
290 | // Wake up the primary handler so that it can exit. | |
291 | primaryEventFiber.call(); | |
292 | } | |
4328bb42 | 293 | */ |
7d4115a5 KL |
294 | |
295 | backend.shutdown(); | |
4328bb42 KL |
296 | } |
297 | ||
298 | /** | |
299 | * Peek at certain application-level events, add to eventQueue, and wake | |
300 | * up the consuming Fiber. | |
301 | * | |
302 | * @param events the input events to consume | |
303 | */ | |
304 | private void metaHandleEvents(List<TInputEvent> events) { | |
305 | ||
306 | for (TInputEvent event: events) { | |
307 | ||
308 | /* | |
309 | System.err.printf(String.format("metaHandleEvents event: %s\n", | |
310 | event)); System.err.flush(); | |
311 | */ | |
312 | ||
313 | if (quit == true) { | |
314 | // Do no more processing if the application is already trying | |
315 | // to exit. | |
316 | return; | |
317 | } | |
318 | ||
319 | // DEBUG | |
320 | if (event instanceof TKeypressEvent) { | |
321 | TKeypressEvent keypress = (TKeypressEvent)event; | |
322 | if (keypress.key.equals(kbAltX)) { | |
323 | quit = true; | |
324 | return; | |
325 | } | |
326 | } | |
327 | // DEBUG | |
328 | ||
329 | // Special application-wide events ------------------------------- | |
330 | ||
331 | // Abort everything | |
332 | if (event instanceof TCommandEvent) { | |
333 | TCommandEvent command = (TCommandEvent)event; | |
334 | if (command.cmd.equals(cmAbort)) { | |
335 | quit = true; | |
336 | return; | |
337 | } | |
338 | } | |
339 | ||
340 | // Screen resize | |
341 | if (event instanceof TResizeEvent) { | |
342 | TResizeEvent resize = (TResizeEvent)event; | |
343 | backend.screen.setDimensions(resize.width, resize.height); | |
344 | desktopBottom = backend.screen.getHeight() - 1; | |
345 | repaint = true; | |
346 | mouseX = 0; | |
347 | mouseY = 0; | |
348 | continue; | |
349 | } | |
350 | ||
351 | // Peek at the mouse position | |
352 | if (event instanceof TMouseEvent) { | |
353 | TMouseEvent mouse = (TMouseEvent)event; | |
354 | if ((mouseX != mouse.x) || (mouseY != mouse.y)) { | |
355 | mouseX = mouse.x; | |
356 | mouseY = mouse.y; | |
357 | drawMouse(); | |
358 | } | |
359 | } | |
360 | ||
361 | /* | |
362 | ||
363 | // Put into the main queue | |
364 | addEvent(event); | |
365 | ||
366 | // Have one of the two consumer Fibers peel the events off | |
367 | // the queue. | |
368 | if (secondaryEventFiber !is null) { | |
369 | assert(secondaryEventFiber.state == Fiber.State.HOLD); | |
370 | ||
371 | // Wake up the secondary handler for these events | |
372 | secondaryEventFiber.call(); | |
373 | } else { | |
374 | assert(primaryEventFiber.state == Fiber.State.HOLD); | |
375 | ||
376 | // Wake up the primary handler for these events | |
377 | primaryEventFiber.call(); | |
378 | } | |
379 | */ | |
380 | ||
381 | } // for (TInputEvent event: events) | |
382 | ||
383 | } | |
384 | ||
385 | /** | |
386 | * Do stuff when there is no user input. | |
387 | */ | |
388 | private void doIdle() { | |
389 | /* | |
390 | // Now run any timers that have timed out | |
391 | auto now = Clock.currTime; | |
392 | TTimer [] keepTimers; | |
393 | foreach (t; timers) { | |
394 | if (t.nextTick < now) { | |
395 | t.tick(); | |
396 | if (t.recurring == true) { | |
397 | keepTimers ~= t; | |
398 | } | |
399 | } else { | |
400 | keepTimers ~= t; | |
401 | } | |
402 | } | |
403 | timers = keepTimers; | |
404 | ||
405 | // Call onIdle's | |
406 | foreach (w; windows) { | |
407 | w.onIdle(); | |
408 | } | |
7d4115a5 | 409 | */ |
4328bb42 | 410 | } |
7d4115a5 | 411 | |
4328bb42 KL |
412 | /** |
413 | * Get the amount of time I can sleep before missing a Timer tick. | |
414 | * | |
415 | * @param timeout = initial (maximum) timeout | |
416 | * @return number of milliseconds between now and the next timer event | |
417 | */ | |
418 | protected int getSleepTime(int timeout) { | |
419 | /* | |
420 | auto now = Clock.currTime; | |
421 | auto sleepTime = dur!("msecs")(timeout); | |
422 | foreach (t; timers) { | |
423 | if (t.nextTick < now) { | |
424 | return 0; | |
425 | } | |
426 | if ((t.nextTick > now) && | |
427 | ((t.nextTick - now) < sleepTime) | |
428 | ) { | |
429 | sleepTime = t.nextTick - now; | |
430 | } | |
431 | } | |
432 | assert(sleepTime.total!("msecs")() >= 0); | |
433 | return cast(uint)sleepTime.total!("msecs")(); | |
434 | */ | |
435 | return 0; | |
7d4115a5 | 436 | } |
4328bb42 | 437 | |
7d4115a5 | 438 | } |