1/*
2 * Copyright (c) 2011, 2022, Oracle and/or its affiliates.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * 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,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24#include <X11/Intrinsic.h>
25#include <glib.h>
26#include <unistd.h>
27#include <signal.h>
28#include <setjmp.h>
29
30/* Test XtAppMainLoop without a display doesn't wait for an XEvent.
31   Based on https://bugs.freedesktop.org/show_bug.cgi?id=34715 */
32static void _Tick(XtPointer baton, XtIntervalId* id)
33{
34    static int count = 0;
35    XtAppContext app = (XtAppContext)baton;
36
37    count ++;
38#ifdef DEBUG
39    printf("%d beep!\n", count);
40#endif
41
42    if (3 == count)
43        XtAppSetExitFlag(app);
44    else
45        XtAppAddTimeOut(app, 1000, _Tick, app);
46}
47
48static sigjmp_buf jump_env;
49
50static void sigalrm (int sig)
51{
52    siglongjmp(jump_env, 1);
53}
54
55static void test_XtAppMainLoop_34715(void)
56{
57    XtAppContext app;
58    struct sigaction sa;
59
60    g_test_bug_base("https://bugzilla.freedesktop.org/show_bug.cgi?id=");
61    g_test_bug("34715");
62
63    XtToolkitInitialize();
64    app = XtCreateApplicationContext();
65    XtAppAddTimeOut(app, 1000, _Tick, app);
66
67    /* AppTimeouts should finish and exit in 3 seconds.
68       Give them 10 seconds just in case system is busy, then fail. */
69    sa.sa_handler = sigalrm;
70    sa.sa_flags = SA_RESTART;
71    sigemptyset (&sa.sa_mask);
72
73    if (sigsetjmp(jump_env, 1) == 0) {
74	sigaction(SIGALRM, &sa, NULL);
75	alarm(10);
76
77	XtAppMainLoop(app);
78	alarm(0); /* cancel alarm */
79    } else {
80	g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC,
81			    "test timed out");
82    }
83    g_assert_cmpint(XtAppGetExitFlag(app), ==, TRUE);
84    XtDestroyApplicationContext(app);
85}
86
87int main(int argc, char** argv)
88{
89    g_test_init(&argc, &argv, NULL);
90    g_test_bug_base("https://gitlab.freedesktop.org/xorg/lib/libxt/-/issues/");
91
92    g_test_add_func("/Event/XtAppMainLoop/34715", test_XtAppMainLoop_34715);
93
94    return g_test_run();
95}
96