The ZX Ecosystem v5.1.0;_GUI_v3.1.0
Loading...
Searching...
No Matches
ZXEcosystem.h
Go to the documentation of this file.
1
2/***************************************************************************/
24#ifndef ZXECOSYSTEM
25#define ZXECOSYSTEM
26
27#include <cstdint>
28#include <string>
29#include <mutex>
30#include <atomic>
31#include <chrono>
32#include <functional>
40
41
43namespace zxeco
44{
45
46/*****************************************************************************
47*
48* Singleton class: ZXEco
49*
50*******************************************************************************/
51
54
58class ZXEco
59{
60 public:
61
66 enum class RefreshType {
67 SCREEN = 1,
68 EVENTS = 2,
69 BOTH = 3
70 // (must be the OR of the others).
71 };
72
74 enum class RefreshMode {
75 NEVER,
77 };
78
81
83 typedef std::function<void (ZXEco &)> UserCode;
84
85 static const UserCode kNullUserCode;
87
90 {
91 public:
92
93 RawGeometry(void) = default;
95
96 RawGeometry(const DoubleArea & sizes,
97 unsigned bw, unsigned bh,
98 unsigned sx, unsigned sy);
100
101
102 const DoubleArea & basics(void) const noexcept { return(dispdef_); }
104
105 const BorderRect & topBorder(void) const noexcept { return(btop_); }
107
109 const BorderRect & leftBorder(void) const noexcept { return(bleft_); }
111
114 const BorderRect & rightBorder(void) const noexcept { return(bright_); }
119 const BorderRect & bottomBorder(void) const noexcept {return(bbottom_);}
121
123 unsigned borderH(void) const noexcept { return(borderh_); }
126 unsigned borderW(void) const noexcept { return(borderw_); }
129 unsigned firstH(void) const noexcept { return(borderh_); }
132 unsigned firstW(void) const noexcept { return(borderw_); }
135 unsigned lastH(void) const noexcept { return(lasth_); }
138 unsigned lastW(void) const noexcept { return(lastw_); }
141 unsigned totH(void) const noexcept { return(toth_); }
144 unsigned totW(void) const noexcept { return(totw_); }
147 unsigned winH(void) const noexcept { return(windowh_); }
150 unsigned winW(void) const noexcept { return(windoww_); }
153 unsigned scaleX(void) const noexcept { return(scalex_); }
156 unsigned scaleY(void) const noexcept { return(scaley_); }
158
159 std::string to_string(void) const;
161
162 private:
163
164 DoubleArea dispdef_;
165 BorderRect btop_,bleft_,bright_,bbottom_;
166 unsigned scalex_,scaley_;
167 unsigned borderw_,borderh_;
168 unsigned lastw_,lasth_;
169 unsigned totw_,toth_;
170 unsigned windoww_,windowh_;
171 };
172
179 static ZXEco & theZXEco(DesktopInterface * desktop = nullptr);
181
195 ZXEco(const ZXEco &) = delete;
196 ZXEco & operator=(const ZXEco &) = delete;
197 ZXEco(ZXEco && disptomove) = delete;
198 ZXEco & operator=(ZXEco && disptomove) = delete;
199
200 virtual ~ZXEco(void);
201
208 DesktopInterface & myDesktopInterface(void) noexcept { return(*mydesktop_);}
210
213 void activate(const std::string & title,
214 PixelDist borderwidth, PixelDist borderheight,
215 unsigned scalex = 1,
216 unsigned scaley = 1,
217 const PixArea & size = {SCREENW,SCREENH},
219 uint8_t * bitmap = nullptr, uint8_t * attrmap = nullptr);
221
234 void close(void);
236
239 bool valid(void);
241
246 const std::string & windowTitle(void) const;
248 std::chrono::time_point<std::chrono::steady_clock> startTime(void) noexcept
249 { return(t0_); }
251
254 const RawGeometry & rawGeometry(void) const;
256
270 Screen & screen(void) const;
272
279 void refreshCode(const UserCode & codetorun,
282 unsigned period = 20,
283 const PalettizedWindow::Event::IDSet & whichevents =
285 const UserCode & periodiccode = kNullUserCode);
287
342 bool isInRefreshingCode(void) const noexcept { return(isincode_); }
344
346 const PalettizedWindow::Event::IDSet & whichevents = {true});
348
371 const PalettizedWindow::Event::IDSet & which
372 = {true},
373 const PalettizedWindow::Event::IDSet & equalstoskip
374 = {false}) noexcept;
376
385 void clearEvents(void) noexcept;
387
390 RefreshType refreshType(void) const noexcept { return(refreshtype_); }
392
393 RefreshMode refreshMode(void) const noexcept { return(refreshmode_); }
395
397 noexcept
399
400 { return(refreshwhichevents_); }
401
402 const LongTimeStat & refreshStats(void);
404
408 void resetRefreshStats(void);
410
413 bool refreshingScreen(void) const noexcept { return(isrefreshing_); }
415
425 bool inkAndPaperSwappedByFlash(void) const noexcept;
427
432 void activateDecolourMode(const CharRect & rect = {});
434
444 void deactivateDecolourMode(void);
446
450 bool decolourModeActive(void) const noexcept { return(decolour_mode_); }
452
453 void windowUpdate(const PixRect & rect = PixRect{});
455
485 void windowRefresh(const PixRect & rect = PixRect{});
487
496 void windowBorderUpdate(const BorderRect & rect = BorderRect{});
498
507 void windowBorderRefresh(const BorderRect & rect = BorderRect{});
509
535
549 bool pointingStatus(PointingStatus & ps) noexcept;
551
569
575 Membrane membrane(void);
577
587 Keyboard keyboard(std::string * lastprstr = nullptr);
589
609 bool waitForKey(Keyboard & kb, unsigned millis = 20, bool zxandpc = false);
611
630 void waitForNoKey(unsigned millis = 20, bool zxandpc = false);
632
639 Keyboard waitOneKey(unsigned ms = 20)
640 { waitForNoKey(ms); Keyboard kb; waitForKey(kb,ms); waitForNoKey(ms);
641 return(kb); }
643
646 Keyboard waitForKeyChange(const Keyboard & oldkb,
647 std::string * lastprstr = nullptr,
648 unsigned millis = 20);
650
658 void forceCapsLock(bool state = true);
660
668 void setVolume(double v);
670
675 double volume(void) const noexcept { return(soundvol_); }
677
678 void play(const Beep &b, bool async = false);
680
693 Screen::InputCallbacks flatInputCallbacks(void);
695
704 private:
705
706 static const unsigned COLBORDERINK_;
707
708 static DesktopInterface * mydesktop_;
709
710 static uint8_t zxColToPal(BrightColor bc) noexcept
712 { return(bc.linear()); }
713
714 static uint8_t pixelToPalCol(uint8_t pixelval,
715 const AttrColors & cs) noexcept
716 { return(zxColToPal({pixelval ? cs.ink().basic() :
717 cs.paper().basic(),
718 static_cast<bool>(cs.bright())})); }
722 static void bothPixelPalCols(const AttrColors & cs,
723 uint8_t & palcolpap,
724 uint8_t & palcolink) noexcept
725 {
726 palcolink = zxColToPal({cs.ink().basic(),
727 static_cast<bool>(cs.bright())});
728 palcolpap = zxColToPal({cs.paper().basic(),
729 static_cast<bool>(cs.bright())});
730 }
735 RawGeometry dispinfo_;
736 std::string mywindowtitle_;
737 PalettizedWindow * mywindow_;
738 Screen * myscreen_;
740 RefreshType refreshtype_;
741 PalettizedWindow::Event::IDSet refreshwhichevents_;
742 RefreshMode refreshmode_;
743 LongTimeStat refreshexectimes_;
744 std::atomic<bool> isrefreshing_;
745 std::atomic<bool> decolour_mode_;
746 CharRect decolour_rect_;
747 std::atomic<bool> isincode_;
749 Membrane membr_;
750 Keyboard keybd_;
751 double soundvol_;
752 std::chrono::time_point<std::chrono::steady_clock> t0_;
753 std::mutex winmux_,runcodemux_,keymux_,pointmux_,soundmux_;
754
755 ZXEco(void) noexcept;
756
757 bool activated(void) const noexcept { return(mywindow_ != nullptr); }
758 void deactivate(void) noexcept;
759 void checkActivated(void) const
760 { if (!activated())
761 RUNTIMEEXCEP("No screen since the ecosystem is inactive"); }
762
763 template <class RECT>
764 PalettizedWindow::Rectangle winRectFrom(const RECT & r) noexcept
765 { return(PalettizedWindow::Rectangle{static_cast<unsigned>(r.corner.x +
766 dispinfo_.borderW()),
767 static_cast<unsigned>(r.corner.y +
768 dispinfo_.borderH()),
769 static_cast<unsigned>(r.area.width),
770 static_cast<unsigned>(r.area.height)});}
771
772 void updateWindowContent(const PixRect & rect = PixRect{});
773 void refreshWindowAndEvents(const PixRect & rect = PixRect{});
774 bool decolourGridForCursor(const CharCursor & c) const;
775 void printAtWindow(uint8_t c, unsigned x0, unsigned y0,
776 unsigned inkpalcol = COLBORDERINK_);
777 void printDecolourGuides(void);
778 void updateBorder(BasicColor col, const BorderRect & rect = BorderRect{});
779 bool swapPixels(bool pass);
780
781 void zxmapFromKMap(Membrane & m) const noexcept;
782};
783
785 { return( (static_cast<int>(a) & static_cast<int>(b)) != 0 ); }
787
791} // end namespace zxeco
792
793#endif
794 // ZXEcosystem
796
#define RUNTIMEEXCEP(txt)
Raise a runtime exception with the given std::string TXT + additional info.
Definition: CppAddons.h:93
@ W_CLOSED
The close window event.
KeyID
IDs for the most common keys in a keyboard.
Interface with the functionality that a desktop must provide.
A map of the keyboard, as seen by the desktop.
A palettized window on the desktop.
List of events that occurred in the window, in chronological order.
Events that the window can receive from its desktop environment.
A rectangle on a palettized window.
Class that computes some statistics of a series of values of a given type.
constexpr PixelDist SCREENH
Height in pixels of the original zx screen.
Definition: ZXGraphics.h:188
constexpr PixelDist SCREENW
Width in pixels of the original zx screen.
Definition: ZXGraphics.h:186
Rect< BorderDistT, BorderDistT > BorderRect
Shortcut for this kind of rectangle.
Definition: ZXGraphics.h:744
uint16_t PixelDist
A distance measured in screen pixels.
Definition: ZXGraphics.h:96
Rect< CharDistT, CharDistT > CharRect
Shortcut for this kind of rectangle.
Definition: ZXGraphics.h:735
Rect< PixelDistT, PixelDistT > PixRect
Shortcut for this kind of rectangle.
Definition: ZXGraphics.h:738
Cursor< CharCoordT, CharCoordT > CharCursor
Shortcut for this kind of cursor.
Definition: ZXGraphics.h:468
void resetRefreshStats(void)
Reset the statistics of refresh.
RefreshMode
Modes of refreshing while executing user code.
Definition: ZXEcosystem.h:74
@ NEVER
Don't refresh anything automatically.
@ PERIODIC
Refresh periodical & automatically.
void play(const Beep &b, bool async=false)
Play a beep.
std::string to_string(void) const
Return a textual representation of the object.
const PalettizedWindow::Event::IDSet & refreshWhichEvents(void) const noexcept
< Return a reference to the current set of events that are being recorded
Definition: ZXEcosystem.h:394
unsigned lastH(void) const noexcept
Return the unscaled y-coord of the bottom-right pixel of drawable.
Definition: ZXEcosystem.h:131
unsigned firstH(void) const noexcept
Return the unscaled y-coord of the top-left pixel of the drawable.
Definition: ZXEcosystem.h:125
void forceCapsLock(bool state=true)
Forces a new state for the caps lock by simulating pressing CAPS+2.
bool desktopKeyPressed(DesktopInterface::KeyID k)
Update the internal image of PC keyboard and return TRUE if K pressed.
void windowRefresh(const PixRect &rect=PixRect{})
Command the desktop window to show on the desktop its current content.
double volume(void) const noexcept
Get the current volume of sound.
Definition: ZXEcosystem.h:671
RefreshMode refreshMode(void) const noexcept
Return the current refresh mode (not thread-safe).
Definition: ZXEcosystem.h:391
unsigned borderW(void) const noexcept
Return the unscaled width of the left border zone in pixels.
Definition: ZXEcosystem.h:122
std::function< void(ZXEco &)> UserCode
Prototype for user routines that can be executed while refreshing.
Definition: ZXEcosystem.h:83
Membrane membrane(void)
Get a copy of the internal membrane after updating PC keyboard.
void close(void)
Deactivate the zx ecosystem.
RefreshType refreshType(void) const noexcept
Return the current refresh type (not thread-safe).
Definition: ZXEcosystem.h:388
Screen & screen(void) const
If the ecosystem has been activated, return a reference to the screen.
void deactivateDecolourMode(void)
Deactivate the decolour mode and all attrs return to their colours.
unsigned winH(void) const noexcept
Scaled height in pixels of the desktop window of border + drawable.
Definition: ZXEcosystem.h:143
unsigned lastW(void) const noexcept
Return the unscaled w-coord of the bottom-right pixel of drawable.
Definition: ZXEcosystem.h:134
Screen::InputCallbacks flatInputCallbacks(void)
Construct and return the callbacks needed for input in the flat model.
RawGeometry(void)=default
Default constructor: invalid data.
unsigned totH(void) const noexcept
Return the unscaled height in pixels of border + drawable.
Definition: ZXEcosystem.h:137
void windowUpdate(const PixRect &rect=PixRect{})
Commit the given region of the ZX Screen to the desktop window.
unsigned firstW(void) const noexcept
Return the unscaled x-coord of the top-left pixel of the drawable.
Definition: ZXEcosystem.h:128
void waitForNoKey(unsigned millis=20, bool zxandpc=false)
Wait for no key pressed, sampling the keyboard every MILLIS.
bool decolourModeActive(void) const noexcept
Return TRUE if the decolour mode is currently active.
Definition: ZXEcosystem.h:447
RawGeometry(const DoubleArea &sizes, unsigned bw, unsigned bh, unsigned sx, unsigned sy)
Constructor from the information provided at ZXEco activation.
const BorderRect & topBorder(void) const noexcept
Return a reference to the unscaled top border rectangle.
Definition: ZXEcosystem.h:105
void activate(const std::string &title, PixelDist borderwidth, PixelDist borderheight, unsigned scalex=1, unsigned scaley=1, const PixArea &size={SCREENW, SCREENH}, const Screen::ExpandToken &exptok=Screen::kInvExpandToken, uint8_t *bitmap=nullptr, uint8_t *attrmap=nullptr)
Activate the zx ecosystem, opening its window.
bool refreshingScreen(void) const noexcept
Return whether there is a refresh in course or not, right now.
Definition: ZXEcosystem.h:411
bool inkAndPaperSwappedByFlash(void) const noexcept
Return whether a swapping of paper and ink colors is happening.
unsigned winW(void) const noexcept
Scaled width in pixels of the desktop window of border + drawable.
Definition: ZXEcosystem.h:146
unsigned totW(void) const noexcept
Return the unscaled width in pixels of border + drawable.
Definition: ZXEcosystem.h:140
PalettizedWindow & desktopWindow(void)
Return the desktop window used by the managed display methods.
void refreshCode(const UserCode &codetorun, RefreshMode rm=RefreshMode::PERIODIC, RefreshType rt=RefreshType::BOTH, unsigned period=20, const PalettizedWindow::Event::IDSet &whichevents={{PalettizedWindow::Event::ID::W_CLOSED}}, const UserCode &periodiccode=kNullUserCode)
Execute user code once with some refresh mode, type and parameters.
void refresh(RefreshType rt=RefreshType::BOTH, const PalettizedWindow::Event::IDSet &whichevents={true})
Do a refresh of the screen, the window events, or both, manually.
unsigned borderH(void) const noexcept
Return the unscaled height of the top border zone in pixels.
Definition: ZXEcosystem.h:119
void clearEvents(void) noexcept
Dismiss all still recorded events in the ZXEco window from the desktop.
DesktopInterface & myDesktopInterface(void) noexcept
Return the associated DesktopInterface.
Definition: ZXEcosystem.h:208
RefreshType
Types of refreshes that can be done while executing user code.
Definition: ZXEcosystem.h:66
@ BOTH
Refreshes both things.
@ EVENTS
Only refreshes events received.
@ SCREEN
Only refreshes screen on display.
static const UserCode kNullUserCode
A user code that does nothing.
Definition: ZXEcosystem.h:85
const LongTimeStat & refreshStats(void)
Consult the statistics of refresh.
static ZXEco & theZXEco(DesktopInterface *desktop=nullptr)
Return the singleton ZXEco object for the application.
const BorderRect & rightBorder(void) const noexcept
Return a reference to the unscaled right border rectangle.
Definition: ZXEcosystem.h:112
void windowBorderRefresh(const BorderRect &rect=BorderRect{})
Command the desktop window to show on the desktop the border content.
bool pointingStatus(PointingStatus &ps) noexcept
Get the status of the mouse within the drawable zone of the ZX window.
const BorderRect & bottomBorder(void) const noexcept
Return a reference to the unscaled bottom border rectangle.
Definition: ZXEcosystem.h:116
bool isInRefreshingCode(void) const noexcept
Return TRUE if the caller is in the context of a previous refresh().
Definition: ZXEcosystem.h:340
const std::string & windowTitle(void) const
Return a reference to the title of the last activation.
Keyboard waitForKeyChange(const Keyboard &oldkb, std::string *lastprstr=nullptr, unsigned millis=20)
Wait for a change in the ZX keyboard that makes it different from OLDKB.
SimpleStats< double, SSOvPol_IgnoreMoreData< double > > LongTimeStat
Simple statistics about durations, gathered for a long time.
Definition: ZXEcosystem.h:80
const DoubleArea & basics(void) const noexcept
Return a reference to the drawable area within the window.
Definition: ZXEcosystem.h:102
unsigned scaleX(void) const noexcept
Scale factor in X.
Definition: ZXEcosystem.h:149
unsigned scaleY(void) const noexcept
Scale factor in Y.
Definition: ZXEcosystem.h:152
bool valid(void)
Whether the zx ecosystem is valid or not.
void activateDecolourMode(const CharRect &rect={})
Activate the decolour mode, where all attrs are turned into a gray grid.
Keyboard waitOneKey(unsigned ms=20)
Wait for only one ZX key or combination of keys to be pressed.
Definition: ZXEcosystem.h:636
Keyboard keyboard(std::string *lastprstr=nullptr)
Update internal membrane and PC/ZX keyboards, returning the latter.
void windowBorderUpdate(const BorderRect &rect=BorderRect{})
Commit the given region of the border to the desktop window.
bool extractEvent(PalettizedWindow::Event &ev, const PalettizedWindow::Event::IDSet &which={true}, const PalettizedWindow::Event::IDSet &equalstoskip={false}) noexcept
Extract into EV the oldest event in the system, or return FALSE if none.
const RawGeometry & rawGeometry(void) const
Return a const reference to the raw geometry of the window.
const BorderRect & leftBorder(void) const noexcept
Return a reference to the unscaled left border rectangle.
Definition: ZXEcosystem.h:108
std::chrono::time_point< std::chrono::steady_clock > startTime(void) noexcept
Return the start-up time of the zx ecosystem.
Definition: ZXEcosystem.h:247
void setVolume(double v)
Change sound volume (from 0.0 to 1.0) for future sounds.
bool waitForKey(Keyboard &kb, unsigned millis=20, bool zxandpc=false)
Wait for any key to be pressed, sampling the keyboard every MILLIS.
Class that provides access to the entire ecosystem: graphics, keyboard, etc.
Definition: ZXEcosystem.h:59
Raw geometrical information about the window geometry of ZXEco objects.
Definition: ZXEcosystem.h:90
An area with both char and pixel resolutions, possibly synchronized.
Definition: ZXGraphics.h:769
std::function< std::string(uint8_t c)> ExpandToken
User's routine that must expand a given token code into the token text.
Definition: ZXScreen.h:96
static const ExpandToken kInvExpandToken
Expand token routine that always generates CHRINVALID as expansion.
Definition: ZXScreen.h:98
The class that provides the main support for managing the ZX screen.
Definition: ZXScreen.h:89
The main namespace of the library, that spans across all the zx modules.
Definition: ZXChars.h:31
constexpr bool operator&(ZXEco::RefreshType a, ZXEco::RefreshType b)
Definition needed for completing the RefreshType functionality.
Definition: ZXEcosystem.h:784