Common lisp interface to bearlibterminal https://bitbucket.org/cfyzium/bearlibterminal. Created with https://github.com/rpav/cl-autowrap.

BearLibTerminal.h 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  2. * BearLibTerminal
  3. * Copyright (C) 2013-2014 Cfyz
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is furnished to do
  10. * so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #ifndef BEARLIBTERMINAL_H
  23. #define BEARLIBTERMINAL_H
  24. #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_WARNINGS)
  25. #define _CRT_SECURE_NO_WARNINGS
  26. #endif
  27. #include <stdio.h>
  28. #include <stddef.h>
  29. #include <stdint.h>
  30. #include <stdarg.h>
  31. #include <wchar.h>
  32. #if defined(__cplusplus)
  33. #include <sstream>
  34. #endif
  35. /*
  36. * Keyboard scancodes for events/states
  37. */
  38. #define TK_A 0x04
  39. #define TK_B 0x05
  40. #define TK_C 0x06
  41. #define TK_D 0x07
  42. #define TK_E 0x08
  43. #define TK_F 0x09
  44. #define TK_G 0x0A
  45. #define TK_H 0x0B
  46. #define TK_I 0x0C
  47. #define TK_J 0x0D
  48. #define TK_K 0x0E
  49. #define TK_L 0x0F
  50. #define TK_M 0x10
  51. #define TK_N 0x11
  52. #define TK_O 0x12
  53. #define TK_P 0x13
  54. #define TK_Q 0x14
  55. #define TK_R 0x15
  56. #define TK_S 0x16
  57. #define TK_T 0x17
  58. #define TK_U 0x18
  59. #define TK_V 0x19
  60. #define TK_W 0x1A
  61. #define TK_X 0x1B
  62. #define TK_Y 0x1C
  63. #define TK_Z 0x1D
  64. #define TK_1 0x1E
  65. #define TK_2 0x1F
  66. #define TK_3 0x20
  67. #define TK_4 0x21
  68. #define TK_5 0x22
  69. #define TK_6 0x23
  70. #define TK_7 0x24
  71. #define TK_8 0x25
  72. #define TK_9 0x26
  73. #define TK_0 0x27
  74. #define TK_RETURN 0x28
  75. #define TK_ENTER 0x28
  76. #define TK_ESCAPE 0x29
  77. #define TK_BACKSPACE 0x2A
  78. #define TK_TAB 0x2B
  79. #define TK_SPACE 0x2C
  80. #define TK_MINUS 0x2D /* - */
  81. #define TK_EQUALS 0x2E /* = */
  82. #define TK_LBRACKET 0x2F /* [ */
  83. #define TK_RBRACKET 0x30 /* ] */
  84. #define TK_BACKSLASH 0x31 /* \ */
  85. #define TK_SEMICOLON 0x33 /* ; */
  86. #define TK_APOSTROPHE 0x34 /* ' */
  87. #define TK_GRAVE 0x35 /* ` */
  88. #define TK_COMMA 0x36 /* , */
  89. #define TK_PERIOD 0x37 /* . */
  90. #define TK_SLASH 0x38 /* / */
  91. #define TK_F1 0x3A
  92. #define TK_F2 0x3B
  93. #define TK_F3 0x3C
  94. #define TK_F4 0x3D
  95. #define TK_F5 0x3E
  96. #define TK_F6 0x3F
  97. #define TK_F7 0x40
  98. #define TK_F8 0x41
  99. #define TK_F9 0x42
  100. #define TK_F10 0x43
  101. #define TK_F11 0x44
  102. #define TK_F12 0x45
  103. #define TK_PAUSE 0x48 /* Pause/Break */
  104. #define TK_INSERT 0x49
  105. #define TK_HOME 0x4A
  106. #define TK_PAGEUP 0x4B
  107. #define TK_DELETE 0x4C
  108. #define TK_END 0x4D
  109. #define TK_PAGEDOWN 0x4E
  110. #define TK_RIGHT 0x4F /* Right arrow */
  111. #define TK_LEFT 0x50 /* Left arrow */
  112. #define TK_DOWN 0x51 /* Down arrow */
  113. #define TK_UP 0x52 /* Up arrow */
  114. #define TK_KP_DIVIDE 0x54 /* '/' on numpad */
  115. #define TK_KP_MULTIPLY 0x55 /* '*' on numpad */
  116. #define TK_KP_MINUS 0x56 /* '-' on numpad */
  117. #define TK_KP_PLUS 0x57 /* '+' on numpad */
  118. #define TK_KP_ENTER 0x58
  119. #define TK_KP_1 0x59
  120. #define TK_KP_2 0x5A
  121. #define TK_KP_3 0x5B
  122. #define TK_KP_4 0x5C
  123. #define TK_KP_5 0x5D
  124. #define TK_KP_6 0x5E
  125. #define TK_KP_7 0x5F
  126. #define TK_KP_8 0x60
  127. #define TK_KP_9 0x61
  128. #define TK_KP_0 0x62
  129. #define TK_KP_PERIOD 0x63 /* '.' on numpad */
  130. #define TK_SHIFT 0x70
  131. #define TK_CONTROL 0x71
  132. /*
  133. * Mouse events/states
  134. */
  135. #define TK_MOUSE_LEFT 0x80 /* Buttons */
  136. #define TK_MOUSE_RIGHT 0x81
  137. #define TK_MOUSE_MIDDLE 0x82
  138. #define TK_MOUSE_X1 0x83
  139. #define TK_MOUSE_X2 0x84
  140. #define TK_MOUSE_MOVE 0x85 /* Movement event */
  141. #define TK_MOUSE_SCROLL 0x86 /* Mouse scroll event */
  142. #define TK_MOUSE_X 0x87 /* Cusor position in cells */
  143. #define TK_MOUSE_Y 0x88
  144. #define TK_MOUSE_PIXEL_X 0x89 /* Cursor position in pixels */
  145. #define TK_MOUSE_PIXEL_Y 0x8A
  146. #define TK_MOUSE_WHEEL 0x8B /* Scroll direction and amount */
  147. #define TK_MOUSE_CLICKS 0x8C /* Number of consecutive clicks */
  148. /*
  149. * If key was released instead of pressed, it's code will be OR'ed with TK_KEY_RELEASED:
  150. * a) pressed 'A': 0x04
  151. * b) released 'A': 0x04|VK_KEY_RELEASED = 0x104
  152. */
  153. #define TK_KEY_RELEASED 0x100
  154. /*
  155. * Virtual key-codes for internal terminal states/variables.
  156. * These can be accessed via terminal_state function.
  157. */
  158. #define TK_WIDTH 0xC0 /* Terminal window size in cells */
  159. #define TK_HEIGHT 0xC1
  160. #define TK_CELL_WIDTH 0xC2 /* Character cell size in pixels */
  161. #define TK_CELL_HEIGHT 0xC3
  162. #define TK_COLOR 0xC4 /* Current foregroung color */
  163. #define TK_BKCOLOR 0xC5 /* Current background color */
  164. #define TK_LAYER 0xC6 /* Current layer */
  165. #define TK_COMPOSITION 0xC7 /* Current composition state */
  166. #define TK_CHAR 0xC8 /* Translated ANSI code of last produced character */
  167. #define TK_WCHAR 0xC9 /* Unicode codepoint of last produced character */
  168. #define TK_EVENT 0xCA /* Last dequeued event */
  169. #define TK_FULLSCREEN 0xCB /* Fullscreen state */
  170. /*
  171. * Other events
  172. */
  173. #define TK_CLOSE 0xE0
  174. #define TK_RESIZED 0xE1
  175. /*
  176. * Generic mode enum.
  177. * Right now it is used for composition option only.
  178. */
  179. #define TK_OFF 0
  180. #define TK_ON 1
  181. /*
  182. * Input result codes for terminal_read function.
  183. */
  184. #define TK_INPUT_NONE 0
  185. #define TK_INPUT_CANCELLED -1
  186. /*
  187. * Terminal uses unsigned 32-bit value for color representation in ARGB order (0xAARRGGBB), e. g.
  188. * a) solid red is 0xFFFF0000
  189. * b) half-transparent green is 0x8000FF00
  190. */
  191. typedef uint32_t color_t;
  192. #if defined(BEARLIBTERMINAL_STATIC_BUILD)
  193. # define TERMINAL_API
  194. #elif defined(_WIN32)
  195. # if defined(BEARLIBTERMINAL_BUILDING_LIBRARY)
  196. # define TERMINAL_API __declspec(dllexport)
  197. # else
  198. # define TERMINAL_API __declspec(dllimport)
  199. # endif
  200. #else
  201. # if defined(BEARLIBTERMINAL_BUILDING_LIBRARY) && __GNUC__ >= 4
  202. # define TERMINAL_API __attribute__ ((visibility ("default")))
  203. # else
  204. # define TERMINAL_API
  205. # endif
  206. #endif
  207. #ifdef __cplusplus
  208. extern "C" {
  209. #endif
  210. TERMINAL_API int terminal_open();
  211. TERMINAL_API void terminal_close();
  212. TERMINAL_API int terminal_set8(const int8_t* value);
  213. TERMINAL_API int terminal_set16(const int16_t* value);
  214. TERMINAL_API int terminal_set32(const int32_t* value);
  215. TERMINAL_API void terminal_refresh();
  216. TERMINAL_API void terminal_clear();
  217. TERMINAL_API void terminal_clear_area(int x, int y, int w, int h);
  218. TERMINAL_API void terminal_crop(int x, int y, int w, int h);
  219. TERMINAL_API void terminal_layer(int index);
  220. TERMINAL_API void terminal_color(color_t color);
  221. TERMINAL_API void terminal_bkcolor(color_t color);
  222. TERMINAL_API void terminal_composition(int mode);
  223. TERMINAL_API void terminal_put(int x, int y, int code);
  224. TERMINAL_API void terminal_put_ext(int x, int y, int dx, int dy, int code, color_t* corners);
  225. TERMINAL_API int terminal_pick(int x, int y, int index);
  226. TERMINAL_API color_t terminal_pick_color(int x, int y, int index);
  227. TERMINAL_API color_t terminal_pick_bkcolor(int x, int y);
  228. TERMINAL_API int terminal_print8(int x, int y, const int8_t* s);
  229. TERMINAL_API int terminal_print16(int x, int y, const int16_t* s);
  230. TERMINAL_API int terminal_print32(int x, int y, const int32_t* s);
  231. TERMINAL_API int terminal_measure8(const int8_t* s);
  232. TERMINAL_API int terminal_measure16(const int16_t* s);
  233. TERMINAL_API int terminal_measure32(const int32_t* s);
  234. TERMINAL_API int terminal_has_input();
  235. TERMINAL_API int terminal_state(int code);
  236. TERMINAL_API int terminal_read();
  237. TERMINAL_API int terminal_read_str8(int x, int y, int8_t* buffer, int max);
  238. TERMINAL_API int terminal_read_str16(int x, int y, int16_t* buffer, int max);
  239. TERMINAL_API int terminal_read_str32(int x, int y, int32_t* buffer, int max);
  240. TERMINAL_API int terminal_peek();
  241. TERMINAL_API void terminal_delay(int period);
  242. TERMINAL_API const int8_t* terminal_get8(const int8_t* key, const int8_t* default_);
  243. TERMINAL_API const int16_t* terminal_get16(const int16_t* key, const int16_t* default_);
  244. TERMINAL_API const int32_t* terminal_get32(const int32_t* key, const int32_t* default_);
  245. TERMINAL_API color_t color_from_name8(const int8_t* name);
  246. TERMINAL_API color_t color_from_name16(const int16_t* name);
  247. TERMINAL_API color_t color_from_name32(const int32_t* name);
  248. #ifdef __cplusplus
  249. } /* End of extern "C" */
  250. #endif
  251. /*
  252. * Utility macro trick which allows macro-in-macro expansion
  253. */
  254. #define TERMINAL_CAT(a, b) TERMINAL_PRIMITIVE_CAT(a, b)
  255. #define TERMINAL_PRIMITIVE_CAT(a, b) a ## b
  256. /*
  257. * wchar_t has different sized depending on platform. Furthermore, it's size
  258. * can be changed for GCC compiler.
  259. */
  260. #if !defined(__SIZEOF_WCHAR_T__)
  261. # if defined(_WIN32)
  262. # define __SIZEOF_WCHAR_T__ 2
  263. # else
  264. # define __SIZEOF_WCHAR_T__ 4
  265. # endif
  266. #endif
  267. #if __SIZEOF_WCHAR_T__ == 2
  268. #define TERMINAL_WCHAR_SUFFIX 16
  269. #define TERMINAL_WCHAR_TYPE int16_t
  270. #else // 4
  271. #define TERMINAL_WCHAR_SUFFIX 32
  272. #define TERMINAL_WCHAR_TYPE int32_t
  273. #endif
  274. /*
  275. * This set of inline functions define basic name substitution + type cast:
  276. * terminal_[w]xxxx -> terminal_xxxx{8|16|32}
  277. */
  278. #if defined(__cplusplus)
  279. #define TERMINAL_INLINE inline
  280. #define TERMINAL_DEFAULT(value) = value
  281. #else
  282. #define TERMINAL_INLINE static inline
  283. #define TERMINAL_DEFAULT(value)
  284. #endif
  285. TERMINAL_INLINE int terminal_set(const char* value)
  286. {
  287. return terminal_set8((const int8_t*)value);
  288. }
  289. TERMINAL_INLINE int terminal_wset(const wchar_t* value)
  290. {
  291. return TERMINAL_CAT(terminal_set, TERMINAL_WCHAR_SUFFIX)((const TERMINAL_WCHAR_TYPE*)value);
  292. }
  293. TERMINAL_INLINE int terminal_print(int x, int y, const char* s)
  294. {
  295. return terminal_print8(x, y, (const int8_t*)s);
  296. }
  297. TERMINAL_INLINE int terminal_wprint(int x, int y, const wchar_t* s)
  298. {
  299. return TERMINAL_CAT(terminal_print, TERMINAL_WCHAR_SUFFIX)(x, y, (const TERMINAL_WCHAR_TYPE*)s);
  300. }
  301. TERMINAL_INLINE int terminal_measure(const char* s)
  302. {
  303. return terminal_measure8((const int8_t*)s);
  304. }
  305. TERMINAL_INLINE int terminal_wmeasure(const wchar_t* s)
  306. {
  307. return TERMINAL_CAT(terminal_measure, TERMINAL_WCHAR_SUFFIX)((const TERMINAL_WCHAR_TYPE*)s);
  308. }
  309. TERMINAL_INLINE int terminal_read_str(int x, int y, char* buffer, int max)
  310. {
  311. return terminal_read_str8(x, y, (int8_t*)buffer, max);
  312. }
  313. TERMINAL_INLINE int terminal_read_wstr(int x, int y, wchar_t* buffer, int max)
  314. {
  315. return TERMINAL_CAT(terminal_read_str, TERMINAL_WCHAR_SUFFIX)(x, y, (TERMINAL_WCHAR_TYPE*)buffer, max);
  316. }
  317. TERMINAL_INLINE const char* terminal_get(const char* key, const char* default_ TERMINAL_DEFAULT((const char*)0))
  318. {
  319. return (const char*)terminal_get8((const int8_t*)key, (const int8_t*)default_);
  320. }
  321. TERMINAL_INLINE const wchar_t* terminal_wget(const wchar_t* key, const wchar_t* default_ TERMINAL_DEFAULT((const wchar_t*)0))
  322. {
  323. return (const wchar_t*)TERMINAL_CAT(terminal_get, TERMINAL_WCHAR_SUFFIX)((const TERMINAL_WCHAR_TYPE*)key, (const TERMINAL_WCHAR_TYPE*)default_);
  324. }
  325. TERMINAL_INLINE color_t color_from_name(const char* name)
  326. {
  327. return color_from_name8((const int8_t*)name);
  328. }
  329. TERMINAL_INLINE color_t color_from_wname(const wchar_t* name)
  330. {
  331. return TERMINAL_CAT(color_from_name, TERMINAL_WCHAR_SUFFIX)((const TERMINAL_WCHAR_TYPE*)name);
  332. }
  333. /*
  334. * These inline functions provide formatted versions for textual API:
  335. * terminal_[w]setf and terminal_[w]printf
  336. *
  337. * Using static termporary buffer is okay because terminal API is not
  338. * required to be multiple-thread safe by design.
  339. */
  340. #if !defined(TERMINAL_FORMAT_BUFFER_SIZE)
  341. #define TERMINAL_FORMAT_BUFFER_SIZE 1024
  342. #endif
  343. #define TERMINAL_FORMATTED_VA(type, name, sign, func, call_sign)\
  344. TERMINAL_INLINE int terminal_v##name##f sign\
  345. {\
  346. static type buffer[TERMINAL_FORMAT_BUFFER_SIZE] = {0};\
  347. int rc = 0;\
  348. if (s == NULL) return 0;\
  349. rc = func(buffer, TERMINAL_FORMAT_BUFFER_SIZE-1, s, args);\
  350. if (rc > 0) rc = terminal_##name call_sign;\
  351. return rc;\
  352. }
  353. #if defined(_WIN32)
  354. #define TERMINAL_VSNWPRINTF _vsnwprintf
  355. #else
  356. #define TERMINAL_VSNWPRINTF vswprintf
  357. #endif
  358. TERMINAL_FORMATTED_VA(char, set, (const char* s, va_list args), vsnprintf, (buffer))
  359. TERMINAL_FORMATTED_VA(wchar_t, wset, (const wchar_t* s, va_list args), TERMINAL_VSNWPRINTF, (buffer))
  360. TERMINAL_FORMATTED_VA(char, print, (int x, int y, const char* s, va_list args), vsnprintf, (x, y, buffer))
  361. TERMINAL_FORMATTED_VA(wchar_t, wprint, (int x, int y, const wchar_t* s, va_list args), TERMINAL_VSNWPRINTF, (x, y, buffer))
  362. TERMINAL_FORMATTED_VA(char, measure, (const char* s, va_list args), vsnprintf, (buffer))
  363. TERMINAL_FORMATTED_VA(wchar_t, wmeasure, (const wchar_t* s, va_list args), TERMINAL_VSNWPRINTF, (buffer))
  364. #define TERMINAL_FORMATTED(outer, inner)\
  365. TERMINAL_INLINE int terminal_##outer\
  366. {\
  367. va_list args;\
  368. int rc = 0;\
  369. va_start(args, s);\
  370. rc = terminal_v##inner;\
  371. va_end(args);\
  372. return rc;\
  373. }
  374. TERMINAL_FORMATTED(setf(const char* s, ...), setf(s, args))
  375. TERMINAL_FORMATTED(wsetf(const wchar_t* s, ...), wsetf(s, args))
  376. TERMINAL_FORMATTED(printf(int x, int y, const char* s, ...), printf(x, y, s, args))
  377. TERMINAL_FORMATTED(wprintf(int x, int y, const wchar_t* s, ...), wprintf(x, y, s, args))
  378. TERMINAL_FORMATTED(measuref(const char* s, ...), measuref(s, args))
  379. TERMINAL_FORMATTED(wmeasuref(const wchar_t* s, ...), wmeasuref(s, args))
  380. #ifdef __cplusplus
  381. /*
  382. * C++ supports function overloading, should take advantage of it.
  383. */
  384. TERMINAL_INLINE int terminal_set(const wchar_t* s)
  385. {
  386. return terminal_wset(s);
  387. }
  388. TERMINAL_FORMATTED(setf(const wchar_t* s, ...), wsetf(s, args))
  389. TERMINAL_INLINE void terminal_color(const char* name)
  390. {
  391. terminal_color(color_from_name(name));
  392. }
  393. TERMINAL_INLINE void terminal_color(const wchar_t* name)
  394. {
  395. terminal_color(color_from_wname(name));
  396. }
  397. TERMINAL_INLINE void terminal_bkcolor(const char* name)
  398. {
  399. terminal_bkcolor(color_from_name(name));
  400. }
  401. TERMINAL_INLINE void terminal_bkcolor(const wchar_t* name)
  402. {
  403. terminal_bkcolor(color_from_wname(name));
  404. }
  405. TERMINAL_INLINE void terminal_put_ext(int x, int y, int dx, int dy, int code)
  406. {
  407. terminal_put_ext(x, y, dx, dy, code, 0);
  408. }
  409. TERMINAL_INLINE int terminal_print(int x, int y, const wchar_t* s)
  410. {
  411. return terminal_wprint(x, y, s);
  412. }
  413. TERMINAL_FORMATTED(printf(int x, int y, const wchar_t* s, ...), wprintf(x, y, s, args))
  414. TERMINAL_INLINE int terminal_measure(const wchar_t* s)
  415. {
  416. return terminal_wmeasure(s);
  417. }
  418. TERMINAL_FORMATTED(measuref(const wchar_t* s, ...), wmeasuref(s, args))
  419. TERMINAL_INLINE int terminal_read_str(int x, int y, wchar_t* buffer, int max)
  420. {
  421. return terminal_read_wstr(x, y, buffer, max);
  422. }
  423. TERMINAL_INLINE color_t color_from_name(const wchar_t* name)
  424. {
  425. return color_from_wname(name);
  426. }
  427. TERMINAL_INLINE int terminal_pick(int x, int y)
  428. {
  429. return terminal_pick(x, y, 0);
  430. }
  431. TERMINAL_INLINE color_t terminal_pick_color(int x, int y)
  432. {
  433. return terminal_pick_color(x, y, 0);
  434. }
  435. TERMINAL_INLINE const wchar_t* terminal_get(const wchar_t* key, const wchar_t* default_ = (const wchar_t*)0)
  436. {
  437. return terminal_wget(key, default_);
  438. }
  439. template<typename T, typename C> T terminal_get(const C* key, const T& default_ = T())
  440. {
  441. const C* result_str = terminal_get(key, (const C*)0);
  442. if (result_str[0] == C(0))
  443. return default_;
  444. T result;
  445. return (bool)(std::basic_istringstream<C>(result_str) >> result)? result: default_;
  446. }
  447. #endif /* __cplusplus */
  448. /*
  449. * Color routines
  450. */
  451. TERMINAL_INLINE color_t color_from_argb(uint8_t a, uint8_t r, uint8_t g, uint8_t b)
  452. {
  453. return ((color_t)a << 24) | (r << 16) | (g << 8) | b;
  454. }
  455. /*
  456. * Other functional sugar
  457. */
  458. TERMINAL_INLINE int terminal_check(int code)
  459. {
  460. return terminal_state(code) > 0;
  461. }
  462. /*
  463. * WinMain entry point handling macro. This allows easier entry point definition.
  464. * The macro will expand to proper WinMain stub regardless of header include order.
  465. */
  466. #if defined(_WIN32)
  467. /*
  468. * WinMain probe macro. It will expand to either X or X_WINDOWS_ depending on
  469. * Windows.h header inclusion.
  470. */
  471. #define TERMINAL_TAKE_CARE_OF_WINMAIN TERMINAL_WINMAIN_PROBE_IMPL(_WINDOWS_)
  472. #define TERMINAL_WINMAIN_PROBE_IMPL(DEF) TERMINAL_PRIMITIVE_CAT(TERMINAL_WINMAIN_IMPL, DEF)
  473. /*
  474. * Trivial no-arguments WinMain implementation. It just calls main.
  475. */
  476. #define TERMINAL_WINMAIN_IMPL_BASE(INSTANCE_T, STRING_T)\
  477. extern "C" int main();\
  478. extern "C" int __stdcall WinMain(INSTANCE_T hInstance, INSTANCE_T hPrevInstance, STRING_T lpCmdLine, int nCmdShow)\
  479. {\
  480. return main();\
  481. }
  482. /*
  483. * Macro expands to empty string. Windows.h is included thus code MUST use
  484. * predefined types or else MSVC will complain.
  485. */
  486. #define TERMINAL_WINMAIN_IMPL TERMINAL_WINMAIN_IMPL_BASE(HINSTANCE, LPSTR)
  487. /*
  488. * Macro expands to macro name. Windows.h wasn't included, so WinMain will be
  489. * defined with fundamental types (enough for linker to find it).
  490. */
  491. #define TERMINAL_WINMAIN_IMPL_WINDOWS_ TERMINAL_WINMAIN_IMPL_BASE(void*, char*)
  492. #else
  493. /*
  494. * Only Windows has WinMain but macro still must be defined for cross-platform
  495. * applications.
  496. */
  497. #define TERMINAL_TAKE_CARE_OF_WINMAIN
  498. #endif
  499. #endif // BEARLIBTERMINAL_H