Debian Codegraph @

garglk/garglk.h

raw symbols SCIP
1 // Copyright (C) 1998-2016 by Andrew Plotkin.
2 // Copyright (C) 2006-2009 by Tor Andersson, Jesse McGrew.
3 // Copyright (C) 2010 by Ben Cressey, Chris Spiegel.
4 //
5 // This file is part of Gargoyle.
6 //
7 // Gargoyle is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // Gargoyle is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with Gargoyle; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
21 // Private header file for the Gargoyle Implementation of the Glk API.
22 // Glk API which this implements: version 0.7.6.
23 // Glk designed by Andrew Plotkin <[email protected]>
24 // http://www.eblong.com/zarf/glk/index.html
25
26 #ifndef GARGLK_GARGLK_H
27 #define GARGLK_GARGLK_H
28
29 #include <algorithm>
30 #include <array>
31 #include <cmath>
32 #include <cstddef>
33 #include <cstdint>
34 #include <cstdio>
35 #include <cstring>
36 #include <deque>
37 #include <exception>
38 #include <functional>
39 #include <iostream>
40 #include <map>
41 #include <memory>
42 #include <sstream>
43 #include <string>
44 #include <unordered_map>
45 #include <utility>
46 #include <vector>
47
48 #include "optional.hpp"
49 #include "variant.hpp"
50
51 #include "glk.h"
52 #include "gi_dispa.h"
53
54 // This function is called whenever the library code catches an error
55 // or illegal operation from the game program.
56 inline void gli_strict_warning(const std::string &msg)
57 {
58 std::cerr << "Glk library error: " << msg << std::endl;
59 }
60
61 enum class FileFilter { Save, Text, Data };
62 enum class FontType { Monospace, Proportional };
63
64 struct FontFace {
65 FontFace() = delete;
66
67 static FontFace propr() { return {false, false, false}; }
68 static FontFace propb() { return {false, true, false}; }
69 static FontFace propi() { return {false, false, true}; }
70 static FontFace propz() { return {false, true, true}; }
71 static FontFace monor() { return {true, false, false}; }
72 static FontFace monob() { return {true, true, false}; }
73 static FontFace monoi() { return {true, false, true}; }
74 static FontFace monoz() { return {true, true, true}; }
75
76 bool operator==(const FontFace &other) const {
77 return monospace == other.monospace &&
78 bold == other.bold &&
79 italic == other.italic;
80 }
81
82 bool monospace;
83 bool bold;
84 bool italic;
85 };
86
87 // Taken from Boost 1.81.0.
88 // Copyright (c) 2019 Vinnie Falco ([email protected]).
89 inline std::size_t hash_combine(std::size_t seed, std::size_t h) noexcept {
90 return seed ^ (h + 0x9e3779b9 + (seed << 6U) + (seed >> 2U));
91 }
92
93 namespace std {
94 template<>
95 struct hash<FontFace> {
96 std::size_t operator()(const FontFace &fontface) const {
97 return (fontface.monospace ? 1 : 0) |
98 (fontface.bold ? 2 : 0) |
99 (fontface.italic ? 4 : 0);
100 }
101 };
102
103 template<>
104 struct hash<std::pair<FontFace, glui32>> {
105 std::size_t operator()(const std::pair<FontFace, glui32> &pair) const {
106 auto seed = hash_combine(0, std::hash<FontFace>()(pair.first));
107 seed = hash_combine(seed, std::hash<glui32>()(pair.second));
108 return seed;
109 }
110 };
111 }
112
113 namespace garglk {
114
115 // This represents a possible configuration file (garglk.ini).
116 struct ConfigFile {
117 enum class Type {
118 System,
119 User,
120 PerGame,
121 };
122
123 ConfigFile(std::string path_, Type type_) : path(std::move(path_)), type(type_) {
124 }
125
126 std::string format_type() const;
127
128 // The path to the file itself.
129 std::string path;
130
131 // There are three types of config file:
132 //
133 // • System: System-wide configuration (e.g. /etc/garglk.ini)
134 // installed by the administrator, not intended to be edited by
135 // the user.
136 // • User: A “user” config file, one that a user would reasonably
137 // expect to be a config file for general use. This excludes
138 // game-specific config files, for example, while considering
139 // various possibilities for config files, such as $HOME/.garglkrc
140 // or $HOME/.config/garglk.ini.
141 // • PerGame: Per-game configuration. Games are able to ship their
142 // own configuration files to customize how Gargoyle looks when
143 // they are run. These are either named after the file (e.g.
144 // zork1.z3 comes with zork1.ini), or are the file “garglk.ini”
145 // placed in the game directory.
146 Type type;
147 };
148
149 extern std::vector<garglk::ConfigFile> all_configs;
150
151 // C++17: std::clamp
152 template <typename T>
153 constexpr const T &clamp(const T &value, const T &min, const T &max)
154 {
155 return value < min ? min : value > max ? max : value;
156 }
157
158 std::string winopenfile(const char *prompt, FileFilter filter);
159 std::string winsavefile(const char *prompt, FileFilter filter);
160 [[noreturn]]
161 void winabort(const std::string &msg);
162 void winwarning(const std::string &title, const std::string &msg);
163 std::string downcase(const std::string &string);
164 bool fontreplace(const std::string &font, FontType type);
165 std::vector<ConfigFile> configs(const nonstd::optional<std::string> &gamepath);
166 void config_entries(const std::string &fname, bool accept_bare, const std::vector<std::string> &matches, const std::function<void(const std::string &cmd, const std::string &arg, int lineno)> &callback);
167 std::string user_config();
168 bool set_lcdfilter(const std::string &filter);
169 nonstd::optional<std::string> winfontpath(const std::string &filename);
170 std::string windatadir();
171 std::vector<std::string> winthemedirs();
172 nonstd::optional<std::string> winlegacythemedir();
173 nonstd::optional<std::string> winappdir();
174 bool winisfullscreen();
175
176 namespace theme {
177 void init();
178 bool set(std::string name);
179 std::vector<std::string> paths();
180 std::vector<std::string> names();
181 }
182
183 template <typename T, typename Deleter>
184 std::unique_ptr<T, Deleter> unique(T *p, Deleter deleter)
185 {
186 return std::unique_ptr<T, Deleter>(p, deleter);
187 }
188
189 bool read_file(const std::string &filename, std::vector<unsigned char> &buf);
190
191 template <typename Iterable, typename DType>
192 std::string join(const Iterable &values, const DType &delim)
193 {
194 auto it = std::begin(values);
195 std::ostringstream result;
196
197 if (it != std::end(values)) {
198 result << *it++;
199 }
200
201 for (; it != std::end(values); ++it) {
202 result << delim << *it;
203 }
204
205 return result.str();
206 }
207
208 }
209
210 template <std::size_t N>
211 class PixelView;
212
213 // Represents an N-byte pixel, which in reality is just an array of
214 // bytes. It is up to the user of this class to determine the layout,
215 // e.g. BGR, RGBA, etc. In actuality, as far as Gargoyle is concerned,
216 // this will be either RGB or RGBA. An instance of Pixel owns the pixel
217 // data; contrast this with PixelView which holds a pointer to a pixel.
218 template <std::size_t N>
219 class Pixel {
220 static_assert(N > 0, "Pixel size must be non-zero");
221
222 public:
223 template <typename... Args>
224 explicit Pixel(Args... args) : m_pixel{static_cast<unsigned char>(args)...} {
225 static_assert(sizeof...(Args) == N, "Incorrect argument count for Pixel");
226 }
227
228 // This is intentionally _not_ explicit so that PixelViews can be
229 // used wherever a Pixel is expected.
230 Pixel(const PixelView<N> &other) {
231 memcpy(m_pixel.data(), other.data(), N);
232 }
233
234 bool operator==(const Pixel<N> &other) const {
235 return m_pixel == other.m_pixel;
236 }
237
238 bool operator!=(const Pixel<N> &other) const {
239 return !(*this == other);
240 }
241
242 const unsigned char *data() const {
243 return m_pixel.data();
244 }
245
246 unsigned char operator[](std::size_t i) const {
247 return m_pixel[i];
248 }
249
250 private:
251 std::array<unsigned char, N> m_pixel;
252 };
253
254 // Represents a view to existing pixel data (see Pixel). The pixel data
255 // is *not* owned by instances of this class. Instead, users of this
256 // class must pass in a pointer to an appropriately-sized buffer (at
257 // least N bytes) from/to which pixel data will be read/written.
258 // Assigning a Pixel to a PixelView will copy the Pixel's data; it does
259 // _not_ point the PixelView to the Pixel.
260 template <std::size_t N>
261 class PixelView {
262 public:
263 explicit PixelView(unsigned char *data) : m_data(data) {
264 }
265
266 // The meaning of these is ambiguous: copy the data, or copy the
267 // pointer? To prevent their accidental use, delete them.
268 PixelView(const PixelView &) = delete;
269 PixelView &operator=(const PixelView &) = delete;
270
271 PixelView(PixelView &&) = default;
272 PixelView &operator=(PixelView &&) = default;
273
274 PixelView &operator=(const Pixel<N> &other) {
275 std::memcpy(m_data, other.data(), N);
276
277 return *this;
278 }
279
280 const unsigned char *data() const {
281 return m_data;
282 }
283
284 unsigned char operator[](std::size_t i) const {
285 return m_data[i];
286 }
287
288 private:
289 unsigned char *const m_data;
290 };
291
292 template <std::size_t N>
293 class Row {
294 public:
295 explicit Row(unsigned char *row) : m_row(row) {
296 }
297
298 PixelView<N> operator[](std::size_t x) {
299 return PixelView<N>(&m_row[x * N]);
300 }
301
302 void fill(const Pixel<N> &pixel, int start, int end) {
303 auto data = pixel.data();
304 for (int i = start; i < end; i++) {
305 std::memcpy(&m_row[i * N], data, N);
306 }
307 }
308
309 private:
310 unsigned char *const m_row;
311 };
312
313 template <std::size_t N>
314 class ConstRow {
315 public:
316 explicit ConstRow(const unsigned char *row) : m_row(row) {
317 }
318
319 const unsigned char *operator[](std::size_t x) const {
320 return &m_row[x * N];
321 }
322
323 private:
324 const unsigned char *const m_row;
325 };
326
327 template <std::size_t N>
328 class Canvas {
329 public:
330 Canvas() = default;
331
332 Canvas(int width, int height) :
333 m_width(width),
334 m_height(height)
335 {
336 resize(width, height, false);
337 }
338
339 void resize(int width, int height, bool keep) {
340 if (keep) {
341 auto backup = m_pixels;
342 int minwidth = std::min(m_width, width);
343 int minheight = std::min(m_height, height);
344
345 m_pixels.resize(width * height * N);
346
347 for (int y = 0; y < minheight; y++) {
348 std::memcpy(&m_pixels[y * width * N], &backup[y * m_width * N], minwidth * N);
349 }
350 } else {
351 m_pixels.resize(width * height * N);
352 }
353
354 m_pixels.shrink_to_fit();
355
356 m_width = width;
357 m_height = height;
358 m_stride = width * N;
359 }
360
361 int width() const {
362 return m_width;
363 }
364
365 int height() const {
366 return m_height;
367 }
368
369 int stride() const {
370 return m_stride;
371 }
372
373 void fill(const Pixel<N> &pixel) {
374 for (int i = 0; i < m_width * m_height; i++) {
375 std::memcpy(&m_pixels[i * N], pixel.data(), N);
376 }
377 }
378
379 bool empty() const {
380 return m_pixels.empty();
381 }
382
383 Row<N> operator[](std::size_t y) {
384 return Row<N>(&m_pixels[y * stride()]);
385 }
386
387 ConstRow<N> operator[](std::size_t y) const {
388 return ConstRow<N>(&m_pixels[y * stride()]);
389 }
390
391 unsigned char *data() {
392 return m_pixels.data();
393 }
394
395 const unsigned char *data() const {
396 return m_pixels.data();
397 }
398
399 std::size_t size() const {
400 return m_pixels.size();
401 }
402
403 void clear() {
404 m_pixels.clear();
405 m_pixels.shrink_to_fit();
406 m_width = 0;
407 m_height = 0;
408 m_stride = 0;
409 }
410
411 private:
412 std::vector<unsigned char> m_pixels;
413 int m_width = 0;
414 int m_height = 0;
415 int m_stride = 0;
416 };
417
418 using Color = Pixel<3>;
419
420 Color gli_parse_color(const std::string &str);
421
422 class Bleeps {
423 public:
424 class Empty : public std::exception { };
425
426 Bleeps();
427 void update(int number, double duration, int frequency);
428 void update(int number, const std::string &path);
429 std::vector<std::uint8_t> &at(int number);
430
431 private:
432 std::array<nonstd::optional<std::vector<std::uint8_t>>, 2> m_bleeps;
433 };
434
435 extern Bleeps gli_bleeps;
436
437 // Callbacks necessary for the dispatch layer.
438
439 extern gidispatch_rock_t (*gli_register_obj)(void *obj, glui32 objclass);
440 extern void (*gli_unregister_obj)(void *obj, glui32 objclass,
441 gidispatch_rock_t objrock);
442 extern gidispatch_rock_t (*gli_register_arr)(void *array, glui32 len,
443 char *typecode);
444 extern void (*gli_unregister_arr)(void *array, glui32 len, char *typecode,
445 gidispatch_rock_t objrock);
446
447 // Some useful type declarations.
448
449 using window_t = struct glk_window_struct;
450 using stream_t = struct glk_stream_struct;
451 using fileref_t = struct glk_fileref_struct;
452 using channel_t = struct glk_schannel_struct;
453
454 struct window_blank_t;
455 struct window_pair_t;
456 struct window_textgrid_t;
457 struct window_textbuffer_t;
458 struct window_graphics_t;
459
460 // ----------------------------------------------------------------------
461 //
462 // Drawing operations and fonts and stuff
463 //
464
465 // Some globals for gargoyle
466
467 #define TBLINELEN 300
468 #define SCROLLBACK 512
469 #define HISTORYLEN 100
470
471 #define GLI_SUBPIX 8
472
473 extern std::string gli_program_name;
474 extern std::string gli_program_info;
475 extern std::string gli_story_name;
476 extern std::string gli_story_title;
477
478 extern bool gli_terminated;
479 extern bool gli_exiting;
480
481 extern window_t *gli_rootwin;
482 extern window_t *gli_focuswin;
483
484 extern bool gli_force_redraw;
485 extern bool gli_more_focus;
486 extern int gli_cellw;
487 extern int gli_cellh;
488
489 // Unicode ligatures and smart typography glyphs
490 #define UNI_LIG_FF 0xFB00
491 #define UNI_LIG_FI 0xFB01
492 #define UNI_LIG_FL 0xFB02
493 #define UNI_LIG_FFI 0xFB03
494 #define UNI_LIG_FFL 0xFB04
495 #define UNI_LSQUO 0x2018
496 #define UNI_RSQUO 0x2019
497 #define UNI_LDQUO 0x201c
498 #define UNI_RDQUO 0x201d
499 #define UNI_NDASH 0x2013
500 #define UNI_MDASH 0x2014
501
502 struct rect_t {
503 int x0, y0;
504 int x1, y1;
505 };
506
507 struct picture_t {
508 picture_t(unsigned long id_, Canvas<4> rgba_, bool scaled_) :
509 id(id_),
510 rgba(std::move(rgba_)),
511 w(rgba.width()),
512 h(rgba.height()),
513 scaled(scaled_)
514 {
515 }
516
517 unsigned long id;
518 Canvas<4> rgba;
519 int w, h;
520 bool scaled;
521 };
522
523 struct style_t {
524 FontFace font;
525 Color bg;
526 Color fg;
527 bool reverse;
528
529 bool operator==(const style_t &other) const {
530 return font == other.font &&
531 bg == other.bg &&
532 fg == other.fg &&
533 reverse == other.reverse;
534 }
535
536 bool operator!=(const style_t &other) const {
537 return !(*this == other);
538 }
539 };
540
541 using Styles = std::array<style_t, style_NUMSTYLES>;
542
543 extern Canvas<3> gli_image_rgb;
544
545 //
546 // Config globals
547 //
548
549 extern std::string gli_workdir;
550 extern nonstd::optional<std::string> gli_workfile;
551
552 extern Styles gli_tstyles;
553 extern Styles gli_gstyles;
554
555 extern Styles gli_tstyles_def;
556 extern Styles gli_gstyles_def;
557
558 extern Color gli_window_color;
559 extern Color gli_border_color;
560 extern Color gli_caret_color;
561 extern Color gli_more_color;
562 extern Color gli_link_color;
563
564 extern Color gli_window_save;
565 extern Color gli_border_save;
566 extern Color gli_caret_save;
567 extern Color gli_more_save;
568 extern Color gli_link_save;
569
570 extern nonstd::optional<Color> gli_override_fg;
571 extern nonstd::optional<Color> gli_override_bg;
572 extern bool gli_override_reverse;
573
574 extern bool gli_underline_hyperlinks;
575 extern int gli_caret_shape;
576 extern int gli_wborderx;
577 extern int gli_wbordery;
578
579 extern int gli_wmarginx;
580 extern int gli_wmarginy;
581 extern int gli_wmarginx_save;
582 extern int gli_wmarginy_save;
583 extern int gli_wpaddingx;
584 extern int gli_wpaddingy;
585 extern int gli_tmarginx;
586 extern int gli_tmarginy;
587
588 extern double gli_backingscalefactor;
589 extern double gli_zoom;
590
591 extern bool gli_conf_lcd;
592 extern std::array<unsigned char, 5> gli_conf_lcd_weights;
593
594 extern bool gli_conf_graphics;
595 extern bool gli_conf_sound;
596 extern std::deque<std::string> gli_conf_soundfonts;
597
598 extern bool gli_conf_fluidsynth_reverb;
599 extern bool gli_conf_fluidsynth_chorus;
600
601 extern bool gli_conf_fullscreen;
602
603 extern bool gli_wait_on_quit;
604
605 extern bool gli_conf_speak;
606 extern bool gli_conf_speak_input;
607
608 extern std::string gli_conf_speak_language;
609
610 extern bool gli_conf_stylehint;
611 extern bool gli_conf_safeclicks;
612
613 extern bool gli_conf_justify;
614 extern int gli_conf_quotes;
615 extern int gli_conf_dashes;
616 extern int gli_conf_spaces;
617 extern bool gli_conf_caps;
618
619 extern int gli_cols;
620 extern int gli_rows;
621
622 extern bool gli_conf_lockcols;
623 extern bool gli_conf_lockrows;
624
625 extern bool gli_conf_save_window_size;
626 extern bool gli_conf_save_window_location;
627
628 extern Color gli_scroll_bg;
629 extern Color gli_scroll_fg;
630 extern int gli_scroll_width;
631
632 extern int gli_baseline;
633 extern int gli_leading;
634
635 struct FontFiles {
636 struct {
637 nonstd::optional<std::string> base;
638 nonstd::optional<std::string> override;
639
640 const nonstd::optional<std::string> &fontpath() const {
641 return override.has_value() ? override : base;
642 }
643 } r, b, i, z;
644 };
645
646 #define DEFAULT_MONO_FONT "Gargoyle Mono"
647 #define DEFAULT_PROP_FONT "Gargoyle Serif"
648
649 extern std::string gli_conf_propfont;
650 extern FontFiles gli_conf_prop;
651 extern std::string gli_conf_monofont;
652 extern FontFiles gli_conf_mono;
653
654 extern double gli_conf_gamma;
655 extern double gli_conf_propsize;
656 extern double gli_conf_monosize;
657 extern double gli_conf_propaspect;
658 extern double gli_conf_monoaspect;
659
660 extern std::vector<glui32> gli_more_prompt;
661 extern glui32 gli_more_prompt_len;
662 extern int gli_more_align;
663 extern FontFace gli_more_font;
664
665 extern bool gli_forceclick;
666 extern bool gli_copyselect;
667 extern bool gli_drawselect;
668 extern bool gli_claimselect;
669
670 extern bool gli_conf_per_game_config;
671
672 enum class GamedataLocation {
673 Default,
674 Dedicated,
675 Gamedir,
676 };
677
678 extern GamedataLocation gli_conf_gamedata_location;
679
680 enum class Scaler {
681 None,
682 HQX,
683 XBRZ,
684 };
685 extern Scaler gli_conf_scaler;
686
687 extern std::unordered_map<FontFace, std::vector<std::string>> gli_conf_glyph_substitution_files;
688
689 // XXX See issue #730.
690 extern bool gli_conf_redraw_hack;
691
692 template <typename T>
693 T gli_zoom_int(T x)
694 {
695 return std::round(x * gli_zoom);
696 }
697
698 template <typename T>
699 T gli_unzoom_int(T x)
700 {
701 return std::round(x / gli_zoom);
702 }
703
704 //
705 // Standard Glk I/O stuff
706 //
707
708 // A macro that I can't think of anywhere else to put it.
709
710 #define gli_event_clearevent(evp) \
711 ((evp)->type = evtype_None, \
712 (evp)->win = nullptr, \
713 (evp)->val1 = 0, \
714 (evp)->val2 = 0)
715
716 void gli_dispatch_event(event_t *event, bool polled);
717
718 #define MAGIC_WINDOW_NUM (9876)
719 #define MAGIC_STREAM_NUM (8769)
720 #define MAGIC_FILEREF_NUM (7698)
721
722 #define strtype_File (1)
723 #define strtype_Window (2)
724 #define strtype_Memory (3)
725 #define strtype_Resource (4)
726
727 struct glk_stream_struct {
728 glui32 magicnum;
729 glui32 rock;
730
731 int type; // file, window, or memory stream
732 bool unicode; // one-byte or four-byte chars? Not meaningful for windows
733
734 glui32 readcount, writecount;
735 bool readable, writable;
736
737 // for strtype_Window
738 window_t *win;
739
740 // for strtype_File
741 std::FILE *file;
742 glui32 lastop; // 0, filemode_Write, or filemode_Read
743
744 // for strtype_Resource
745 bool isbinary;
746
747 // for strtype_Memory and strtype_Resource. Separate pointers for
748 // one-byte and four-byte streams
749 unsigned char *buf;
750 unsigned char *bufptr;
751 unsigned char *bufend;
752 unsigned char *bufeof;
753 glui32 *ubuf;
754 glui32 *ubufptr;
755 glui32 *ubufend;
756 glui32 *ubufeof;
757 glui32 buflen;
758 gidispatch_rock_t arrayrock;
759
760 gidispatch_rock_t disprock;
761 stream_t *next, *prev; // in the big linked list of streams
762 };
763
764 struct glk_fileref_struct {
765 glui32 magicnum;
766 glui32 rock;
767
768 char *filename;
769 int filetype;
770 bool textmode;
771
772 gidispatch_rock_t disprock;
773 fileref_t *next, *prev; // in the big linked list of filerefs
774 };
775
776 //
777 // Windows and all that
778 //
779
780 // For some reason MinGW does "#define hyper __int64", which conflicts
781 // with attr_s.hyper below. Unconditionally undefine it here so any
782 // files which include windows.h will not cause build failures.
783 #undef hyper
784
785 struct attr_t {
786 bool reverse = false;
787 glui32 style = 0;
788 nonstd::optional<Color> fgcolor;
789 nonstd::optional<Color> bgcolor;
790 glui32 hyper = 0;
791
792 bool operator!=(const attr_t &other) const {
793 return reverse != other.reverse ||
794 style != other.style ||
795 fgcolor != other.fgcolor ||
796 bgcolor != other.bgcolor ||
797 hyper != other.hyper;
798 }
799
800 void set(glui32 style_);
801 void clear();
802 FontFace font(const Styles &styles) const;
803 Color bg(const Styles &styles) const;
804 Color fg(const Styles &styles) const;
805 };
806
807 struct glk_window_struct {
808 glk_window_struct(glui32 type_, glui32 rock_);
809 glk_window_struct(const glk_window_struct &) = delete;
810 glk_window_struct &operator=(const glk_window_struct &) = delete;
811 ~glk_window_struct();
812
813 glui32 magicnum = MAGIC_WINDOW_NUM;
814 glui32 type;
815 glui32 rock;
816
817 window_t *parent = nullptr; // pair window which contains this one
818 rect_t bbox;
819 int yadj = 0;
820 nonstd::variant<
821 std::unique_ptr<window_textgrid_t>,
822 std::unique_ptr<window_textbuffer_t>,
823 std::unique_ptr<window_graphics_t>,
824 std::unique_ptr<window_blank_t>,
825 std::unique_ptr<window_pair_t>
826 > window;
827
828 window_textgrid_t *wingrid() { return nonstd::get<std::unique_ptr<window_textgrid_t>>(window).get(); }
829 window_textbuffer_t *winbuffer() { return nonstd::get<std::unique_ptr<window_textbuffer_t>>(window).get(); }
830 window_graphics_t *wingraphics() { return nonstd::get<std::unique_ptr<window_graphics_t>>(window).get(); }
831 window_blank_t *winblank() { return nonstd::get<std::unique_ptr<window_blank_t>>(window).get(); }
832 window_pair_t *winpair() { return nonstd::get<std::unique_ptr<window_pair_t>>(window).get(); }
833
834 stream_t *str; // the window stream.
835 stream_t *echostr = nullptr; // the window's echo stream, if any.
836
837 bool line_request = false;
838 bool line_request_uni = false;
839 bool char_request = false;
840 bool char_request_uni = false;
841 bool mouse_request = false;
842 bool hyper_request = false;
843 bool more_request = false;
844 bool scroll_request = false;
845 bool image_loaded = false;
846
847 bool echo_line_input = true;
848 std::vector<glui32> line_terminators;
849
850 attr_t attr;
851 Color bgcolor = gli_window_color;
852 Color fgcolor = gli_more_color;
853
854 gidispatch_rock_t disprock;
855 window_t *next, *prev; // in the big linked list of windows
856 };
857
858 struct window_blank_t {
859 explicit window_blank_t(window_t *win) : owner(win) {
860 }
861
862 window_t *owner;
863 };
864
865 struct window_pair_t {
866 window_pair_t(window_t *win, glui32 method, window_t *key_, glui32 size_) :
867 owner(win),
868 dir(method & winmethod_DirMask),
869 vertical(dir == winmethod_Left || dir == winmethod_Right),
870 backward(dir == winmethod_Left || dir == winmethod_Above),
871 division(method & winmethod_DivisionMask),
872 key(key_),
873 size(size_),
874 wborder((method & winmethod_BorderMask) == winmethod_Border)
875 {
876 }
877
878 window_t *owner;
879 window_t *child1 = nullptr, *child2 = nullptr;
880
881 // split info...
882 glui32 dir; // winmethod_Left, Right, Above, or Below
883 bool vertical, backward; // flags
884 glui32 division; // winmethod_Fixed or winmethod_Proportional
885 window_t *key; // NULL or a leaf-descendant (not a Pair)
886 bool keydamage = false; // used as scratch space in window closing
887 glui32 size; // size value
888 bool wborder; // winMethod_Border, NoBorder
889 };
890
891 // One line of the grid window.
892 struct tgline_t {
893 bool dirty = false;
894 std::array<glui32, 256> chars;
895 std::array<attr_t, 256> attrs;
896 };
897
898 struct window_textgrid_t {
899 explicit window_textgrid_t(window_t *owner_) :
900 owner(owner_)
901 {
902 }
903
904 ~window_textgrid_t() {
905 if (inbuf != nullptr && gli_unregister_arr != nullptr) {
906 const char *typedesc = (inunicode ? "&+#!Iu" : "&+#!Cn");
907 gli_unregister_arr(inbuf, inoriglen, const_cast<char *>(typedesc), inarrayrock);
908 }
909 }
910
911 window_t *owner;
912
913 int width = 0, height = 0;
914 std::array<tgline_t, 256> lines;
915
916 int curx = 0, cury = 0; // the window cursor position
917
918 // for line input
919 void *inbuf = nullptr; // unsigned char* for latin1, glui32* for unicode
920 bool inunicode = false;
921 int inorgx = 0, inorgy = 0;
922 int inoriglen, inmax;
923 int incurs, inlen;
924 attr_t origattr;
925 gidispatch_rock_t inarrayrock;
926
927 // style hints and settings
928 Styles styles = gli_gstyles;
929 };
930
931 struct tbline_t {
932 tbline_t() {
933 chars.fill(' ');
934 }
935 int len = 0;
936 int flow_break_pos = -1;
937 bool newline = false, dirty = false, repaint = false;
938 std::shared_ptr<picture_t> lpic, rpic;
939 glui32 lhyper = 0, rhyper = 0;
940 int lm = 0, rm = 0;
941 std::array<glui32, TBLINELEN> chars;
942 std::array<attr_t, TBLINELEN> attrs;
943 };
944
945 struct window_textbuffer_t {
946 explicit window_textbuffer_t(window_t *owner_) :
947 owner(owner_)
948 {
949 lines.resize(scrollback);
950 chars = lines[0].chars.data();
951 attrs = lines[0].attrs.data();
952 }
953
954 ~window_textbuffer_t() {
955 if (inbuf != nullptr && gli_unregister_arr != nullptr) {
956 const char *typedesc = (inunicode ? "&+#!Iu" : "&+#!Cn");
957 gli_unregister_arr(inbuf, inmax, const_cast<char *>(typedesc), inarrayrock);
958 }
959 }
960
961 window_t *owner;
962
963 int width = -1, height = -1;
964 int spaced = 0;
965 int dashed = 0;
966
967 std::vector<tbline_t> lines;
968 int scrollback = SCROLLBACK;
969
970 int numchars = 0; // number of chars in last line: lines[0]
971 glui32 *chars; // alias to lines[0].chars
972 attr_t *attrs; // alias to lines[0].attrs
973
974 // adjust margins temporarily for images
975 int ladjw = 0;
976 int ladjn = 0;
977 int radjw = 0;
978 int radjn = 0;
979
980 // Command history.
981 std::deque<std::vector<glui32>> history;
982 std::deque<std::vector<glui32>>::iterator history_it = history.begin();
983
984 // for paging
985 int lastseen = 0;
986 int scrollpos = 0;
987 int scrollmax = 0;
988
989 // for line input
990 void *inbuf = nullptr; // unsigned char* for latin1, glui32* for unicode
991 bool inunicode = false;
992 int inmax;
993 long infence;
994 long incurs;
995 attr_t origattr;
996 gidispatch_rock_t inarrayrock;
997
998 // style hints and settings
999 Styles styles = gli_tstyles;
1000
1001 // for copy selection
1002 std::vector<glui32> copybuf;
1003 };
1004
1005 struct window_graphics_t {
1006 explicit window_graphics_t(window_t *win) :
1007 owner(win),
1008 bgnd(win->bgcolor)
1009 {
1010 }
1011
1012 window_t *owner;
1013 Color bgnd;
1014 bool dirty = false;
1015 int w = 0, h = 0;
1016 Canvas<3> rgb;
1017 };
1018
1019 // ----------------------------------------------------------------------
1020
1021 extern void gli_initialize_sound();
1022 extern void gli_initialize_tts();
1023 extern void gli_tts_speak(const glui32 *buf, std::size_t len);
1024 extern void gli_tts_flush();
1025 extern void gli_tts_purge();
1026
1027 extern gidispatch_rock_t gli_sound_get_channel_disprock(const channel_t *chan);
1028
1029 // ----------------------------------------------------------------------
1030 //
1031 // All the annoyingly boring and tedious prototypes...
1032 //
1033
1034 [[noreturn]]
1035 extern void gli_exit(int status);
1036
1037 extern void win_blank_rearrange(window_t *win, const rect_t *box);
1038 extern void win_blank_redraw(window_t *win);
1039
1040 extern void win_pair_rearrange(window_t *win, const rect_t *box);
1041 extern void win_pair_redraw(window_t *win);
1042 extern void win_pair_click(window_pair_t *dwin, int x, int y);
1043
1044 extern void win_textgrid_rearrange(window_t *win, rect_t *box);
1045 extern void win_textgrid_redraw(window_t *win);
1046 extern void win_textgrid_putchar_uni(window_t *win, glui32 ch);
1047 extern bool win_textgrid_unputchar_uni(window_t *win, glui32 ch);
1048 extern void win_textgrid_clear(window_t *win);
1049 extern void win_textgrid_move_cursor(window_t *win, int xpos, int ypos);
1050 extern void win_textgrid_init_line(window_t *win, char *buf, int maxlen, int initlen);
1051 extern void win_textgrid_init_line_uni(window_t *win, glui32 *buf, int maxlen, int initlen);
1052 extern void win_textgrid_cancel_line(window_t *win, event_t *ev);
1053 extern void win_textgrid_click(window_textgrid_t *dwin, int x, int y);
1054 extern void gcmd_grid_accept_readchar(window_t *win, glui32 arg);
1055 extern void gcmd_grid_accept_readline(window_t *win, glui32 arg);
1056
1057 extern void win_textbuffer_rearrange(window_t *win, rect_t *box);
1058 extern void win_textbuffer_redraw(window_t *win);
1059 extern void win_textbuffer_putchar_uni(window_t *win, glui32 ch);
1060 extern bool win_textbuffer_unputchar_uni(window_t *win, glui32 ch);
1061 extern void win_textbuffer_clear(window_t *win);
1062 extern void win_textbuffer_init_line(window_t *win, char *buf, int maxlen, int initlen);
1063 extern void win_textbuffer_init_line_uni(window_t *win, glui32 *buf, int maxlen, int initlen);
1064 extern void win_textbuffer_cancel_line(window_t *win, event_t *ev);
1065 extern void win_textbuffer_click(window_textbuffer_t *dwin, int x, int y);
1066 extern void gcmd_buffer_accept_readchar(window_t *win, glui32 arg);
1067 extern void gcmd_buffer_accept_readline(window_t *win, glui32 arg);
1068 extern bool gcmd_accept_scroll(window_t *win, glui32 arg);
1069
1070 // Declarations of library internal functions.
1071
1072 extern void gli_initialize_misc();
1073 extern void gli_initialize_windows();
1074 extern void gli_initialize_babel(const std::string &filename);
1075
1076 extern window_t *gli_window_iterate_treeorder(window_t *win);
1077
1078 extern void gli_window_rearrange(window_t *win, rect_t *box);
1079 extern void gli_window_redraw(window_t *win);
1080 extern void gli_window_put_char_uni(window_t *win, glui32 ch);
1081 extern bool gli_window_unput_char_uni(window_t *win, glui32 ch);
1082 extern bool gli_window_check_terminator(glui32 ch);
1083 extern void gli_window_refocus(window_t *win);
1084
1085 extern void gli_windows_redraw();
1086 extern void gli_windows_size_change(int w, int h, bool post_arrange_event);
1087 extern void gli_windows_unechostream(stream_t *str);
1088
1089 extern void gli_window_click(window_t *win, int x, int y);
1090
1091 void gli_redraw_rect(int x0, int y0, int x1, int y1);
1092
1093 void gli_input_handle_key(glui32 key);
1094 void gli_input_handle_key_paste(glui32 key);
1095 void gli_input_handle_click(int x, int y);
1096 void gli_event_store(glui32 type, window_t *win, glui32 val1, glui32 val2);
1097
1098 extern stream_t *gli_new_stream(int type, int readable, int writable,
1099 glui32 rock);
1100 extern void gli_delete_stream(stream_t *str);
1101 extern stream_t *gli_stream_open_window(window_t *win);
1102 extern strid_t gli_stream_open_pathname(char *pathname, int writemode,
1103 int textmode, glui32 rock);
1104 extern void gli_stream_set_current(stream_t *str);
1105 extern void gli_stream_fill_result(stream_t *str,
1106 stream_result_t *result);
1107 extern void gli_stream_echo_line(stream_t *str, char *buf, glui32 len);
1108 extern void gli_stream_echo_line_uni(stream_t *str, glui32 *buf, glui32 len);
1109 extern void gli_streams_close_all();
1110
1111 void gli_initialize_fonts();
1112 void gli_draw_pixel(int x, int y, const Color &rgb);
1113 void gli_draw_clear(const Color &rgb);
1114 void gli_draw_rect(int x, int y, int w, int h, const Color &rgb);
1115 int gli_draw_string_uni(int x, int y, FontFace face, const Color &rgb, const glui32 *text, int len, int spacewidth);
1116 int gli_string_width_uni(FontFace face, const glui32 *text, int len, int spacewidth);
1117 void gli_draw_caret(int x, int y);
1118 void gli_draw_picture(const picture_t *pic, int x0, int y0, int dx0, int dy0, int dx1, int dy1);
1119
1120 extern void gli_select(event_t *event, bool polled);
1121 #ifdef GARGLK_CONFIG_TICK
1122 extern void gli_tick();
1123 #endif
1124
1125 void wininit();
1126 void winopen();
1127 void wintitle();
1128 void winmore();
1129 void winrepaint(int x0, int y0, int x1, int y1);
1130 bool windark();
1131 void winexit();
1132 void winclipstore(const std::vector<glui32> &text);
1133
1134 void fontload();
1135 void fontunload();
1136
1137 bool giblorb_copy_resource(glui32 usage, glui32 resnum, glui32 &type, std::vector<unsigned char> &buf);
1138
1139 std::shared_ptr<picture_t> gli_picture_load(unsigned long id);
1140 void gli_picture_store(const std::shared_ptr<picture_t> &pic);
1141 std::shared_ptr<picture_t> gli_picture_retrieve(unsigned long id, bool scaled);
1142 std::shared_ptr<picture_t> gli_picture_scale(const picture_t *src, int newcols, int newrows);
1143 void gli_piclist_increment();
1144 void gli_piclist_decrement();
1145
1146 void win_graphics_rearrange(window_t *win, rect_t *box);
1147 void win_graphics_redraw(window_t *win);
1148 void win_graphics_click(window_graphics_t *dwin, int x, int y);
1149
1150 bool win_graphics_draw_picture(std::shared_ptr<picture_t> pic, window_graphics_t *dwin,
1151 glsi32 xpos, glsi32 ypos, glui32 imagewidth, glui32 imageheight);
1152 void win_graphics_erase_rect(window_graphics_t *dwin, bool whole, glsi32 x0, glsi32 y0, glui32 width, glui32 height);
1153 void win_graphics_fill_rect(window_graphics_t *dwin, glui32 color, glsi32 x0, glsi32 y0, glui32 width, glui32 height);
1154 void win_graphics_set_background_color(window_graphics_t *dwin, glui32 color);
1155
1156 bool win_textbuffer_draw_picture(std::shared_ptr<picture_t> pic, window_textbuffer_t *dwin, glui32 window_width, glui32 align, glui32 width, glui32 height, glui32 maxwidth);
1157 void win_textbuffer_flow_break(window_textbuffer_t *win);
1158
1159 void gli_read_config(int argc, char **argv);
1160
1161 // unicode case mapping
1162
1163 using gli_case_block_t = glui32[2];
1164 // If both are 0xFFFFFFFF, you have to look at the special-case table
1165
1166 using gli_case_special_t = glui32[3];
1167 // Each of these points to a subarray of the unigen_special_array
1168 // (in cgunicode.c). In that subarray, element zero is the length,
1169 // and that's followed by length unicode values.
1170
1171 using gli_decomp_block_t = glui32[2];
1172 // The position points to a subarray of the unigen_decomp_array.
1173 // If the count is zero, there is no decomposition.
1174
1175 void gli_putchar_utf8(glui32 val, std::FILE *fl);
1176 extern int gli_encode_utf8(glui32 val, char *buf, int len);
1177 glui32 gli_getchar_utf8(std::FILE *fl);
1178 glui32 gli_parse_utf8(const unsigned char *buf, glui32 buflen, glui32 *out, glui32 outlen);
1179
1180 glui32 gli_strlen_uni(const glui32 *s);
1181
1182 void gli_put_hyperlink(glui32 linkval, unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1);
1183 glui32 gli_get_hyperlink(int x, int y);
1184 void gli_clear_selection();
1185 bool gli_check_selection(int x0, int y0, int x1, int y1);
1186 bool gli_get_selection(int x0, int y0, int x1, int y1, int *rx0, int *rx1);
1187 void gli_clipboard_copy(const std::vector<glui32> &buf);
1188 void gli_start_selection(int x, int y);
1189 void gli_resize_mask(unsigned int x, unsigned int y);
1190 void gli_move_selection(int x, int y);
1191 void gli_notification_waiting();
1192
1193 void gli_edit_config();
1194
1195 nonstd::optional<std::vector<char>> gli_get_scrollback();
1196 std::vector<char> gli_get_text(window_textbuffer_t *dwin);
1197
1198 const std::map<glui32, std::vector<unsigned char>> &gli_get_resource_map(glui32 usage);
1199
1200 // A macro which reads and decodes one character of UTF-8. Needs no
1201 // explanation, I'm sure.
1202 //
1203 // Oh, okay. The character will be written to *chptr (so pass in "&ch",
1204 // where ch is a glui32 variable). eofcond should be a condition to
1205 // evaluate end-of-stream -- true if no more characters are readable.
1206 // nextch is a function which reads the next character; this is invoked
1207 // exactly as many times as necessary.
1208 //
1209 // val0, val1, val2, val3 should be glui32 scratch variables. The macro
1210 // needs these. Just define them, you don't need to pay attention to them
1211 // otherwise.
1212 //
1213 // The macro itself evaluates to true if ch was successfully set, or
1214 // false if something went wrong. (Not enough characters, or an
1215 // invalid byte sequence.)
1216 //
1217 // This is not the worst macro I've ever written, but I forget what the
1218 // other one was.
1219
1220 #define UTF8_DECODE_INLINE(chptr, eofcond, nextch, val0, val1, val2, val3) ( \
1221 (eofcond ? 0 : ( \
1222 (((val0=nextch) < 0x80) ? (*chptr=val0, 1) : ( \
1223 (eofcond ? 0 : ( \
1224 (((val1=nextch) & 0xC0) != 0x80) ? 0 : ( \
1225 (((val0 & 0xE0) == 0xC0) ? (*chptr=((val0 & 0x1F) << 6) | (val1 & 0x3F), 1) : ( \
1226 (eofcond ? 0 : ( \
1227 (((val2=nextch) & 0xC0) != 0x80) ? 0 : ( \
1228 (((val0 & 0xF0) == 0xE0) ? (*chptr=(((val0 & 0xF)<<12) & 0x0000F000) | (((val1 & 0x3F)<<6) & 0x00000FC0) | (((val2 & 0x3F)) & 0x0000003F), 1) : ( \
1229 (((val0 & 0xF0) != 0xF0 || eofcond) ? 0 : (\
1230 (((val3=nextch) & 0xC0) != 0x80) ? 0 : (*chptr=(((val0 & 0x7)<<18) & 0x1C0000) | (((val1 & 0x3F)<<12) & 0x03F000) | (((val2 & 0x3F)<<6) & 0x000FC0) | (((val3 & 0x3F)) & 0x00003F), 1) \
1231 )) \
1232 )) \
1233 )) \
1234 )) \
1235 )) \
1236 )) \
1237 )) \
1238 )) \
1239 )
1240
1241 #endif