]> git.draconx.ca Git - dxcommon.git/blob - m4/curses-compat.m4
DX_C_ALIGNAS: Work around bash-5 parsing bug.
[dxcommon.git] / m4 / curses-compat.m4
1 dnl Copyright © 2022, 2024 Nick Bowler
2 dnl
3 dnl Macros for probing specific curses library behaviour.
4 dnl
5 dnl License WTFPL2: Do What The Fuck You Want To Public License, version 2.
6 dnl This is free software: you are free to do what the fuck you want to.
7 dnl There is NO WARRANTY, to the extent permitted by law.
8
9 dnl DX_CHECK_CURSES_FUNC(function, [args], [prologue])
10 dnl
11 dnl Simple yes/no check for whether the curses library provides the specified
12 dnl function.  If it does, the macro HAVE_CURSES_func macro is defined to 1,
13 dnl where func is the specified function name in capital letters.  The cache
14 dnl variable dx_cv_curses_have_func is set to "yes" if the function is
15 dnl detected, or "no" otherwise.
16 dnl
17 dnl The second argument is the argument list (without its enclosing
18 dnl parentheses) used to construct a syntactically valid function call.
19 dnl Typically most arguments can simply be 0.
20 dnl
21 dnl The third argument is additional C code to be placed before the function
22 dnl call within main.  This can include declarations and statements.
23 AC_DEFUN([DX_CHECK_CURSES_FUNC],
24 [_DX_CURSES_LINKTEST([supports $1], [$1], [$3; $1($2)])])
25
26 dnl DX_CHECK_CURSES_GETMOUSE_NCURSES
27 dnl
28 dnl Check if the curses library provides the getmouse function with an
29 dnl ncurses-compatible interface.  Some other curses libraries also have
30 dnl a function named getmouse but with a totally different interface.
31 dnl
32 dnl If so, the macro HAVE_CURSES_GETMOUSE_NCURSES is defined to 1 and the
33 dnl cache variable dx_cv_curses_have_getmouse_ncurses is set to "yes".
34 dnl Otherwise the cache variable is set to "no".
35 AC_DEFUN([DX_CHECK_CURSES_GETMOUSE_NCURSES],
36 [_DX_CURSES_LINKTEST([has ncurses-like getmouse], [getmouse_ncurses],
37 [MEVENT ev; { extern int getmouse(MEVENT *); getmouse(&ev); } getmouse(&ev)])])
38
39 dnl DX_CHECK_CURSES_MOUSE_SUPPORT
40 dnl
41 dnl Probe a set of mouse-related curses functions to figure out what is
42 dnl needed for basic mouse support.  This is implemented using the preceding
43 dnl macros and sets the same outputs.
44 dnl
45 dnl Additionally, the HAVE_CURSES_MOUSE_SUPPORT macro is defined to 1 if:
46 dnl
47 dnl   - one of mouse_set or mousemask is found, and
48 dnl   - one of ncurses-style getmouse or request_mouse_pos is found.
49 dnl
50 dnl Currently, this probes the following functions, in order:
51 dnl
52 dnl   - mouse_set
53 dnl   - mousemask
54 dnl   - mouseinterval
55 dnl   - getmouse (with ncurses interface)
56 dnl   - request_mouse_pos
57 dnl
58 dnl To save time, not all functions are probed; only the minimum required to
59 dnl determine that curses does (or does not) have usable mouse support.  As a
60 dnl result, libraries that support multiple interfaces such as pdcurses might
61 dnl not have every supported function reflected in the configuration macros.
62 AC_DEFUN([DX_CHECK_CURSES_MOUSE_SUPPORT],
63 [DX_CHECK_CURSES_FUNC([mouse_set], [BUTTON1_PRESSED])
64 AS_IF([test x"$dx_cv_curses_have_mouse_set" != x"yes"],
65   [DX_CHECK_CURSES_FUNC([mousemask], [BUTTON1_PRESSED,0])])
66 AS_CASE([$dx_cv_curses_have_mouse_set$dx_cv_curses_have_mousemask],
67 [*yes*], [DX_CHECK_CURSES_FUNC([mouseinterval], [0])
68 DX_CHECK_CURSES_GETMOUSE_NCURSES
69 AS_IF([test x"$dx_cv_curses_have_getmouse_ncurses" != x"yes"],
70   [DX_CHECK_CURSES_FUNC([request_mouse_pos], [],
71     [switch (BUTTON_STATUS(1)) case BUTTON_PRESSED: return 42;])])
72 AS_CASE(
73 [$dx_cv_curses_have_getmouse_ncurses$dx_cv_curses_have_request_mouse_pos],
74   [*yes*], [AC_DEFINE([HAVE_CURSES_MOUSE_SUPPORT], [1],
75     [Define to 1 if curses has usable mouse support.])])])])
76
77 dnl _DX_CURSES_LINKTEST(message, key, body)
78 dnl
79 dnl Internal macro common to the tests above.  "message" is used to construct
80 dnl the configure output and config.h template description; "key" is used to
81 dnl construct macro and cache variable names; "body" is the C function body
82 dnl to compile.
83 AC_DEFUN([_DX_CURSES_LINKTEST],
84 [AC_CACHE_CHECK([whether curses $1], [AS_TR_SH([dx_cv_curses_have_$2])],
85 [_dx_save_CFLAGS=$CFLAGS _dx_save_LIBS=$LIBS
86 CFLAGS="$CFLAGS $CURSES_CFLAGS" LIBS="$CURSES_LIBS"
87 AC_LINK_IFELSE([AC_LANG_PROGRAM([#include <curses.h>
88 ], [$3;])], [AS_TR_SH([dx_cv_curses_have_$2])=yes],
89 [AS_TR_SH([dx_cv_curses_have_$2])=no])
90 CFLAGS=$_dx_save_CFLAGS LIBS=$_dx_save_LIBS])
91 AS_IF([test x"$AS_TR_SH([dx_cv_curses_have_$2])" = x"yes"],
92   [AC_DEFINE(AS_TR_CPP([HAVE_CURSES_$2]), [1],
93     [Define to 1 if your curses library $1])])])