The ZX Ecosystem v5.1.0;_GUI_v3.1.0
Loading...
Searching...
No Matches
DesktopInterface.h
Go to the documentation of this file.
1
2/* *************************************************************************/
29#ifndef DESKTOPINTERFACE
30#define DESKTOPINTERFACE
31
32#include <cstddef>
33#include <string>
34#include <memory>
35#include <map>
36#include <vector>
37#include <list>
38#include <bitset>
39#include <set>
40#include <algorithm>
41#include <functional>
43
44
45class DesktopInterface; // forward declarations. This class is the main one
47
48
49/* ****************************************************************************
50
51 Abstract class: DesktopInterface
52
53*******************************************************************************/
54
56
62{
63 public:
64
65 /* ------------------------- Types, consts, etc. ----------------- */
66
68
72 enum KeyID { K_ESC =0, K_F1, K_F2, K_F3, K_F4, K_F5, K_F6, K_F7, K_F8, K_F9,
73 K_F10, K_F11, K_F12, K_1, K_2, K_3, K_4, K_5, K_6, K_7, K_8,
74 K_9, K_0, K_BACK, K_DEL, // back-> del prev char; del-> cur.chr
75 K_TAB, K_BEGIN, K_END,K_PGUP,K_PGDOWN,
76 K_Q, K_W, K_E, K_R, K_T, K_Y, K_U,
77 K_I, K_O, K_P, K_ENTER, K_CAPSLOCK, K_A, K_S, K_D, K_F, K_G,
78 K_H, K_J, K_K, K_L, K_CAPSLEFT, K_Z, K_X, K_C, K_V, K_B, K_N,
79 K_M, K_COMMA, K_PERIOD, K_CAPSRIGHT, K_CTRLLEFT, K_ALTLEFT,
80 K_SPACE, K_ALTRIGHT, K_CTRLRIGHT, K_LEFT, K_DOWN, K_UP,
81 K_RIGHT, K_MINUS, K_PLUS, K_DIV, K_MULT, K_BSLASH, K_UNDERS,
82 K_SQUBRO, K_SQUBRC, K_CURBRO, K_CURBRC, K_EXCL, K_DQUOTE,
83 K_AT, K_SHARP, K_DOLLAR, K_PERC, K_AMPER, K_PARO, K_PARC,
84 K_EQUAL, K_QUEST, K_VERT, K_CIRC, K_APOST, K_LESS, K_GREAT,
85 K_SEMIC, K_COLON,
86 NUM_OF_KEY_IDS};
87
89 class KeypressMap: public std::map<KeyID,bool>
90 {
91 public:
92
95
96 virtual ~KeypressMap(void) = default;
98
99 // Leave the compiler to generate default copy and move constructors
100
101
102 bool anyKeyPressed(void) const;
104 };
105
106 static const std::vector<std::string> KeyNames;
108
109 static bool keyIsNumber(KeyID kid) noexcept
111 { return((kid >= KeyID::K_1) && (kid <= KeyID::K_0)); }
112
113 static bool keyIsLetter(KeyID kid) noexcept
115 { return(((kid >= KeyID::K_Q) && (kid <= KeyID::K_P)) ||
116 ((kid >= KeyID::K_A) && (kid <= KeyID::K_L)) ||
117 ((kid >= KeyID::K_Z) && (kid <= KeyID::K_M))); }
118
119 static bool keyIsFunction(KeyID kid) noexcept
121 { return((kid >= KeyID::K_F1) && (kid <= KeyID::K_F12)); }
122
123 static bool keyIsForMotion(KeyID kid) noexcept
125 { return((kid == KeyID::K_TAB) ||
126 (kid == KeyID::K_BEGIN) || (kid == KeyID::K_END) ||
127 (kid == KeyID::K_PGUP) || (kid == KeyID::K_PGDOWN) ||
128 (kid == KeyID::K_LEFT) || (kid == KeyID::K_RIGHT) ||
129 (kid == KeyID::K_DOWN) || (kid == KeyID::K_UP)); }
130
131
132 typedef std::function<double (double t, uint64_t i,
133 unsigned l, unsigned pos)> SoundMonoProducer;
135
151
154 {
156 unsigned sample_size;
161 SoundPlayerParms(void): SoundPlayerParms{1.0/44100.0,1024} {}
162
164 SoundPlayerParms(double sper, unsigned ss): sampling_period{sper},
165 sample_size{ss} { check(); }
166
168 void check(void) const;
169 };
170
171
180 virtual ~DesktopInterface(void) = default;
181
182 DesktopInterface(const DesktopInterface &) = delete;
183 DesktopInterface & operator=(const DesktopInterface &) = delete;
184 DesktopInterface(const DesktopInterface &&) = delete;
185 DesktopInterface & operator=(const DesktopInterface &&) = delete;
186
192 virtual RGBImage readPNGImageFile(const std::string & filepath) = 0;
194
196 virtual void writePNGImageFile(const RGBImage & img,
197 const std::string & filepath) = 0;
199
206 virtual void getDesktopResolution(unsigned &w, unsigned &h) = 0;
208
211 virtual PalettizedWindow * createWindow(const std::string & title,
212 unsigned dimw, unsigned dimh,
213 unsigned scalex, unsigned scaley)=0;
215
221 const SoundPlayerParms & playerParms(void) const noexcept
223 { return(playerparms_); }
224
225 virtual void playSound(double secs, const SoundMonoProducer & waveprod,
226 bool async) = 0;
228
237 virtual void pausePlaying(void) = 0;
239
243 virtual void resumePlaying(void) = 0;
245
247 virtual bool playingPaused(void) = 0;
249
250 virtual bool soundPlaying(void) const noexcept = 0;
252
255 virtual void nextSoundPlayingTime(double &t) noexcept = 0;
257
262 virtual const SoundMonoProducer & lastSoundProducer(void) const = 0;
264
267 virtual double lastSoundDuration(void) const noexcept = 0;
269
278 virtual void enableTextEvents(void) = 0;
280
281 virtual void disableTextEvents(void) = 0;
283
284 virtual bool textEventsEnabled(void) const = 0;
286
293 virtual std::string copyFromClipboard(void) const = 0;
295
297 virtual void copyToClipboard(const std::string & txt) const = 0;
299
304 protected:
305
306 SoundPlayerParms playerparms_;
307
308 DesktopInterface(const SoundPlayerParms & spps): playerparms_{spps} {}
309
310};
311
312
313/* ****************************************************************************
314
315 Abstract class: PalettizedWindow
316
317*******************************************************************************/
318
320
328{
329 public:
330
331 /* -------------------- Types and consts --------------------------- */
332
333 typedef uint8_t PaletteLevel;
334 typedef uint8_t PaletteIndex;
335
338 {
340 unsigned x0,y0;
343 unsigned w,h;
347 Rectangle(void) noexcept:x0{0},y0{0},w{0},h{0} {}
348
350 Rectangle(unsigned x, unsigned y,
351 unsigned wi, unsigned he) noexcept: x0{x},y0{y},w{wi},h{he} {}
352
354 bool empty(void) const noexcept { return((w == 0) || (h == 0)); }
355
357 std::string to_string(void) const
358 { return("winrect{{" + std::to_string(x0) + "," +
359 std::to_string(y0) + "},{" +
360 std::to_string(w) + "," +
361 std::to_string(h) + "}}"); }
362 };
363
364 static constexpr unsigned NUMCOLSPAL = 256;
365
367
370 struct Event
371 {
373 enum class ID {
374 // Events for the window in general (they have no data):
375
376 W_CLOSED = 0,
378 W_RESTORED,
379 W_MOVED,
380 W_EXPOSED,
381
382 // Events for the keyboard:
383
384 K_PRESSED,
386 K_TEXT,
404 // Events for the mouse:
405
406 M_MOVED,
409 M_WHEEL
410 };
411
412 static const uint8_t N_IDS = static_cast<uint8_t>(ID::M_WHEEL) + 1;
414
416 class IDSet: public std::bitset<N_IDS>
417 {
418 public:
419
420 IDSet(bool all = false) noexcept
422 { if (all) set(); }
423
424 IDSet(const std::set<ID> & sids)
426 { for (auto id : sids) set(static_cast<size_t>(id)); }
427
428 bool contains(ID id) const noexcept
430 { return(operator[](static_cast<size_t>(id))); }
431 };
432
434 union {
435 struct {
438 struct {
439 char utf8char[7];
441 struct {
442 int32_t incx;
443 int32_t incy;
444 unsigned x,y;
446 bool buttons[2];
448 struct {
449 char which;
450 unsigned x,y;
453 struct {
454 int32_t incvert;
461
462
463 std::string to_string(void) const;
465 };
466
468
471 class ListOfEvents: public std::list<Event>
472 {
473 public:
474
475 using Base = std::list<Event>;
476 using Base::Base;
477
478 bool contains(Event::ID eid) const noexcept
480 { return(std::find_if(cbegin(),cend(),
481 [eid](const Event & e)->bool
482 { return(e.id == eid); }) != cend()); }
483
484 std::string to_string(void) const;
486 };
487
489
498 class Cursor
499 {
500 public:
501
502 Cursor(const PalettizedWindow & palwin,
503 unsigned x=0, unsigned y=0,
504 size_t bytesperpixel = 1) noexcept;
506
507 virtual ~Cursor(void) = default;
509
510 unsigned x(void) const noexcept { return(x_); }
512
513 unsigned y(void) const noexcept { return(y_); }
515
516 bool outside(void) const noexcept { return(false); }
518
519 void incX(void) noexcept { ++o_; ++x_; }
521
522 void incY(void) noexcept { o_ += pitch_; ++y_; }
524
525 size_t offset(void) const noexcept { return(o_); }
527
528 std::string to_string(void) const noexcept;
530
531 protected:
532
533 unsigned x_,y_;
534 size_t pitch_;
535 size_t o_;
536 };
537
539
543 class CursorCheck: public Cursor
544 {
545 public:
546
548 unsigned x=0, unsigned y=0,
549 size_t bytesperpixel = 1) noexcept;
551
552 bool outside(void) const noexcept;
554
555 void incX(void) noexcept;
557
558 void incY(void) noexcept;
560
561 std::string to_string(void) const noexcept;
563
564 protected:
565
566 size_t n_;
567 };
568
569
573 virtual ~PalettizedWindow(void);
575
577 PalettizedWindow & operator=(const PalettizedWindow &) = delete;
579 PalettizedWindow & operator=(PalettizedWindow &&) = delete;
580
586 unsigned width(void) const noexcept { return(width_); }
588
589 unsigned height(void) const noexcept { return(height_); }
591
592 virtual void refresh(const Rectangle & rect = Rectangle{}) = 0;
594
609 virtual void serveEvents(const PalettizedWindow::Event::IDSet & whichevents
610 = {true},
611 ListOfEvents * evs = nullptr) = 0;
613
622 virtual void setPaletteColors(const PaletteLevel * colors,
623 PaletteIndex first,
624 PaletteIndex count) = 0;
626
634 virtual Cursor getCursor(unsigned x = 0, unsigned y = 0) noexcept = 0;
636
637 virtual CursorCheck getCursorCheck(unsigned x = 0, unsigned y = 0) = 0;
639
640 virtual PaletteIndex getPixel(const Cursor & cursor) = 0;
642
644 virtual void setPixel(const Cursor & cursor, PaletteIndex b) = 0;
646
648 virtual void fillRect(const Rectangle &r, PaletteIndex c) = 0;
650
657 virtual bool getPointingInWindow(unsigned &x, unsigned &y,
658 bool & leftbutton, bool & rightbutton) = 0;
660
677 virtual bool keyPressed(DesktopInterface::KeyID k) = 0;
679
687 virtual void updateKeypressMap(DesktopInterface::KeypressMap & map) = 0;
689
700 protected:
701
702 const std::string title_;
704 const unsigned width_,height_;
706 const size_t size_;
708 const unsigned scx_,scy_;
711 const unsigned swidth_,sheight_;
714 PalettizedWindow(const std::string & tit, unsigned dimw, unsigned dimh,
715 unsigned scalex, unsigned scaley);
717
721};
722
723
724#endif
725 // DesktopInterface
727
A 2D matrix of certain type that has contiguous storage.
Definition: ColorImages.h:434
bool buttons[2]
0-> left, 1-> right; TRUE-> clickd
virtual void playSound(double secs, const SoundMonoProducer &waveprod, bool async)=0
Play a sound.
char utf8char[7]
Recognized UTF-8 char (0-ended).
static bool keyIsLetter(KeyID kid) noexcept
< Return TRUE if the given KEY is a letter.
virtual Cursor getCursor(unsigned x=0, unsigned y=0) noexcept=0
Get a non-checking cursor that points to that pixel.
virtual void nextSoundPlayingTime(double &t) noexcept=0
Fill T with the next time the user's callback will be called.
union PalettizedWindow::Event::@5 data
Data of the event.
KeypressMap(void)
Default constructor: a map with one entry per KeyID, all unpressed.
static bool keyIsFunction(KeyID kid) noexcept
< Return TRUE if the given KEY is a function key.
virtual void copyToClipboard(const std::string &txt) const =0
Must copy TXT, if not empty, to the clipboard.
std::function< double(double t, uint64_t i, unsigned l, unsigned pos)> SoundMonoProducer
A routine to produce a mono-channel sound wave.
uint8_t PaletteLevel
Intensity of a color component (r, g, b).
int32_t incy
Increment in Y.
std::string to_string(void) const
Convert the list to a string.
virtual double lastSoundDuration(void) const noexcept=0
Get the duration of the last sound being played.
struct PalettizedWindow::Event::@5::@10 wheel
Data for a M_WHEEL event.
const unsigned width_
Unscaled dims of the bitmap drawable.
SoundPlayerParms(void)
Default constructor with default parameters.
virtual void updateKeypressMap(DesktopInterface::KeypressMap &map)=0
Must update MAP with the current state of all keys.
virtual bool textEventsEnabled(void) const =0
Must return the current enabled state of K_TEXT events.
std::string to_string(void) const noexcept
Return a description of the cursor.
double sampling_period
In secs, between a sound value and the next.
const unsigned scx_
Scale of the bitmap when rendered.
virtual bool keyPressed(DesktopInterface::KeyID k)=0
Must inform about whether the key K is pressed or not.
virtual void serveEvents(const PalettizedWindow::Event::IDSet &whichevents={true}, ListOfEvents *evs=nullptr)=0
Serve all pending events for the window to the underlying desktop systm.
void incX(void) noexcept
Make the iterator point to the next pixel in x.
static bool keyIsNumber(KeyID kid) noexcept
< Return TRUE if the given KEY is a number.
unsigned y_
Cursor position in coordinates.
int32_t incx
Increment in X.
const SoundPlayerParms & playerParms(void) const noexcept
< Must return the current parameters of the sound player system.
virtual void writePNGImageFile(const RGBImage &img, const std::string &filepath)=0
Must write the data of IMG into the file FILEPATH in PNG format.
struct PalettizedWindow::Event::@5::@6 keyboard
Data for a K_PRESSED / K_UNPRESSED evnt.
unsigned y0
x0,y0 are the pixel at the left-top corner.
unsigned sample_size
Number of samples that are buffered.
virtual CursorCheck getCursorCheck(unsigned x=0, unsigned y=0)=0
Get a checking cursor that points to that pixel.
Rectangle(void) noexcept
Default constructor: empty rectangle.
const unsigned swidth_
Scaled dim of bitmap (dim of window).
unsigned y
Pos of the mouse after event.
virtual PalettizedWindow * createWindow(const std::string &title, unsigned dimw, unsigned dimh, unsigned scalex, unsigned scaley)=0
Factory: create a new window that supports palettized pixels.
void incY(void) noexcept
Make the iterator point to the pixel right below the current one.
virtual PaletteIndex getPixel(const Cursor &cursor)=0
Return the value of the pixel at CURSOR in the window, if all is valid.
char which
'L' (left) or 'R' (right)
bool contains(ID id) const noexcept
< Convenience check of existence of ID in the set.
virtual void enableTextEvents(void)=0
Must enable K_TEXT events from now on to the active window in desktop.
virtual void disableTextEvents(void)=0
Must disable K_TEXT events from now on to the active window in desktop.
size_t o_
Linear size of the window, in bytes.
unsigned w
Dimensions of the rectangle in pixels.
static const SoundMonoProducer SilenceMonoProducer
Pre-defined sound producing routine that generates silence all time.
static constexpr unsigned NUMCOLSPAL
No. of colors in palette.
unsigned width(void) const noexcept
Return the width in (non-scaled) pixels of the window.
IDSet(const std::set< ID > &sids)
struct PalettizedWindow::Event::@5::@8 motion
Data for a M_MOVED event.
const size_t size_
Size in pixels of the window.
virtual void getDesktopResolution(unsigned &w, unsigned &h)=0
Get the resolution of the display in pixels.
bool anyKeyPressed(void) const
Return TRUE if any key appears pressed in the map.
SoundPlayerParms(double sper, unsigned ss)
Specific constructor.
unsigned x(void) const noexcept
X coordinate of the cursor.
virtual const SoundMonoProducer & lastSoundProducer(void) const =0
Get the routine that is currently producing sounds.
unsigned h
Dimensions of the rectangle in pixels.
virtual RGBImage readPNGImageFile(const std::string &filepath)=0
Must read the PNG image file with path FILEPATH and return an RGBImage.
uint8_t PaletteIndex
Index of a color in the palette.
virtual void setPaletteColors(const PaletteLevel *colors, PaletteIndex first, PaletteIndex count)=0
Change the given color(s) in the window palette.
std::string to_string(void) const noexcept
Return a description of the cursor.
static const std::vector< std::string > KeyNames
Key names.
bool contains(Event::ID eid) const noexcept
< To inherit all constructors
size_t offset(void) const noexcept
Return the linear offset within the drawable area of the cursor.
const unsigned sheight_
Scaled dim of bitmap (dim of window).
size_t n_
Linear size of the window, in bytes.
static const uint8_t N_IDS
Number of event IDs
const unsigned height_
Unscaled dims of the bitmap drawable.
virtual bool playingPaused(void)=0
Return whether there is a paused playing or not.
const std::string title_
Title of the window.
bool empty(void) const noexcept
Return TRUE if the rectangle is empty.
Rectangle(unsigned x, unsigned y, unsigned wi, unsigned he) noexcept
Constructor.
IDSet(bool all=false) noexcept
< Constructor of empty (ALL == FALSE) or full (ALL == TRUE) set.
CursorCheck(const PalettizedWindow &palwin, unsigned x=0, unsigned y=0, size_t bytesperpixel=1) noexcept
Constructor for the given window and with the given values.
virtual void setPixel(const Cursor &cursor, PaletteIndex b)=0
Set pixel at CURSOR to B, if all is valid.
virtual bool soundPlaying(void) const noexcept=0
Whether there is a sound being played or not.
std::string to_string(void) const
Return a text describing the rectangle.
Cursor(const PalettizedWindow &palwin, unsigned x=0, unsigned y=0, size_t bytesperpixel=1) noexcept
Default constructor from the coords of a given pixel in PALWIN.
ID
< IDs for the events that the desktop can send to a window.
@ W_MOVED
The drag-and-drop window event.
@ M_BUTTONUNCLICKED
Mouse unclick.
@ W_CLOSED
The close window event.
@ K_UNPRESSED
Key unpressed.
@ W_MINIMIZED
The minimize window event.
@ W_RESTORED
The restore (from min.) window event.
@ W_EXPOSED
Window exposed & needed of a refresh.
@ K_TEXT
Key or combination recognized as text.
@ M_MOVED
Mouse movement.
@ M_WHEEL
Mouse wheel change.
static bool keyIsForMotion(KeyID kid) noexcept
< Return TRUE if the key can be used for moving things on the screen.
DesktopInterface::KeyID key
Key pressed or unpress.
void check(void) const
Throw if the parameters are invalid.
virtual std::string copyFromClipboard(void) const =0
Must return a copy of the text existing in the clipboard, if any.
std::list< Event > Base
A shortcut.
unsigned y(void) const noexcept
Y coordinate of the cursor.
virtual ~KeypressMap(void)=default
Destructor.
virtual void resumePlaying(void)=0
Resume a previously paused playing.
unsigned height(void) const noexcept
Return the height in (non-scaled) pixels of the window.
struct PalettizedWindow::Event::@5::@7 textkey
Data for a K_TEXT event.
size_t pitch_
Length of one row of pixels in memory.
virtual bool getPointingInWindow(unsigned &x, unsigned &y, bool &leftbutton, bool &rightbutton)=0
Must get the mouse status on the window.
struct PalettizedWindow::Event::@5::@9 button
Data for a M_BUTTON* event.
std::string to_string(void) const
Return a text with the event info.
int32_t incvert
Increment in vertical.
virtual void refresh(const Rectangle &rect=Rectangle{})=0
Render current screen content on the desktop with proper scaling.
unsigned x0
x0,y0 are the pixel at the left-top corner.
void incY(void) noexcept
Make the iterator point to the pixel right below the current one.
KeyID
IDs for the most common keys in a keyboard.
void incX(void) noexcept
Make the iterator point to the next pixel in x.
virtual void pausePlaying(void)=0
Pause the current playing, if any, and play just silence.
bool outside(void) const noexcept
Whether the cursor is outside the drawable. FALSE for non-checking.
bool outside(void) const noexcept
Whether the cursor is outside the drawable. FALSE for non-checking.
ID id
ID of the event that occurred.
virtual void fillRect(const Rectangle &r, PaletteIndex c)=0
Fill a rectangle in the window with the given color, efficiently.
const unsigned scy_
Scale of the bitmap when rendered.
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.
To refer to pixels in the drawable area of the window.
Cursor to refer to pixels that includes checking code.
List of events that occurred in the window, in chronological order.
Main parameters of the sound player.
Events that the window can receive from its desktop environment.
A rectangle on a palettized window.