X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2Fbackend%2FGenericBackend.java;h=ede3c0bff4365743f1d8c38d3c4b130f1cdc01f0;hb=505be508ae7d3fb48122be548b310a238cfb91eb;hp=d96f7a93f468afc404a61888faf62bfcef337d7a;hpb=42873e30bf487bc0b695d60652dba44f82185dbb;p=nikiroo-utils.git diff --git a/src/jexer/backend/GenericBackend.java b/src/jexer/backend/GenericBackend.java index d96f7a9..ede3c0b 100644 --- a/src/jexer/backend/GenericBackend.java +++ b/src/jexer/backend/GenericBackend.java @@ -3,7 +3,7 @@ * * The MIT License (MIT) * - * Copyright (C) 2017 Kevin Lamonte + * Copyright (C) 2019 Kevin Lamonte * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -31,6 +31,8 @@ package jexer.backend; import java.util.List; import jexer.event.TInputEvent; +import jexer.event.TCommandEvent; +import static jexer.TCommand.*; /** * This abstract class provides a screen, keyboard, and mouse to @@ -39,11 +41,43 @@ import jexer.event.TInputEvent; */ public abstract class GenericBackend implements Backend { + // ------------------------------------------------------------------------ + // Variables -------------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * The session information. */ protected SessionInfo sessionInfo; + /** + * The screen to draw on. + */ + protected Screen screen; + + /** + * Input events are processed by this Terminal. + */ + protected TerminalReader terminal; + + /** + * By default, GenericBackend adds a cmAbort after it sees + * cmBackendDisconnect, so that TApplication will exit when the user + * closes the Swing window or disconnects the ECMA48 streams. But + * MultiBackend wraps multiple Backends, and needs to decide when to send + * cmAbort differently. Setting this to false is how it manages that. + * Note package private access. + */ + boolean abortOnDisconnect = true; + + // ------------------------------------------------------------------------ + // Constructors ----------------------------------------------------------- + // ------------------------------------------------------------------------ + + // ------------------------------------------------------------------------ + // Backend ---------------------------------------------------------------- + // ------------------------------------------------------------------------ + /** * Getter for sessionInfo. * @@ -53,11 +87,6 @@ public abstract class GenericBackend implements Backend { return sessionInfo; } - /** - * The screen to draw on. - */ - protected Screen screen; - /** * Getter for screen. * @@ -68,30 +97,75 @@ public abstract class GenericBackend implements Backend { } /** - * Subclasses must provide an implementation that syncs the logical - * screen to the physical device. + * Sync the logical screen to the physical device. */ - public abstract void flushScreen(); + public void flushScreen() { + screen.flushPhysical(); + } + + /** + * Check if there are events in the queue. + * + * @return if true, getEvents() has something to return to the application + */ + public boolean hasEvents() { + return terminal.hasEvents(); + } /** - * Subclasses must provide an implementation to get keyboard, mouse, and - * screen resize events. + * Get keyboard, mouse, and screen resize events. * * @param queue list to append new events to */ - public abstract void getEvents(List queue); + public void getEvents(final List queue) { + if (terminal.hasEvents()) { + terminal.getEvents(queue); + + // This default backend assumes a single user, and if that user + // becomes disconnected we should terminate the application. + if ((queue.size() > 0) && (abortOnDisconnect == true)) { + TInputEvent event = queue.get(queue.size() - 1); + if (event instanceof TCommandEvent) { + TCommandEvent command = (TCommandEvent) event; + if (command.equals(cmBackendDisconnect)) { + queue.add(new TCommandEvent(cmAbort)); + } + } + } + } + } /** - * Subclasses must provide an implementation that closes sockets, - * restores console, etc. + * Close the I/O, restore the console, etc. */ - public abstract void shutdown(); + public void shutdown() { + terminal.closeTerminal(); + } /** - * Subclasses must provide an implementation that sets the window title. + * Set the window title. * * @param title the new title */ - public abstract void setTitle(final String title); + public void setTitle(final String title) { + screen.setTitle(title); + } + + /** + * Set listener to a different Object. + * + * @param listener the new listening object that run() wakes up on new + * input + */ + public void setListener(final Object listener) { + terminal.setListener(listener); + } + + /** + * Reload backend options from System properties. + */ + public void reloadOptions() { + terminal.reloadOptions(); + } }