19e7bcd65Smrg/*
2fdf6a26fSmrg * Copyright (c) 2011, 2022, Oracle and/or its affiliates.
39e7bcd65Smrg *
49e7bcd65Smrg * Permission is hereby granted, free of charge, to any person obtaining a
59e7bcd65Smrg * copy of this software and associated documentation files (the "Software"),
69e7bcd65Smrg * to deal in the Software without restriction, including without limitation
79e7bcd65Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
89e7bcd65Smrg * and/or sell copies of the Software, and to permit persons to whom the
99e7bcd65Smrg * Software is furnished to do so, subject to the following conditions:
109e7bcd65Smrg *
119e7bcd65Smrg * The above copyright notice and this permission notice (including the next
129e7bcd65Smrg * paragraph) shall be included in all copies or substantial portions of the
139e7bcd65Smrg * Software.
149e7bcd65Smrg *
159e7bcd65Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
169e7bcd65Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
179e7bcd65Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
189e7bcd65Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
199e7bcd65Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
209e7bcd65Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
219e7bcd65Smrg * DEALINGS IN THE SOFTWARE.
229e7bcd65Smrg */
239e7bcd65Smrg
249e7bcd65Smrg#include <X11/Intrinsic.h>
259e7bcd65Smrg#include <glib.h>
269e7bcd65Smrg#include <unistd.h>
279e7bcd65Smrg#include <signal.h>
289e7bcd65Smrg#include <setjmp.h>
299e7bcd65Smrg
309e7bcd65Smrg/* Test XtAppMainLoop without a display doesn't wait for an XEvent.
319e7bcd65Smrg   Based on https://bugs.freedesktop.org/show_bug.cgi?id=34715 */
329e7bcd65Smrgstatic void _Tick(XtPointer baton, XtIntervalId* id)
339e7bcd65Smrg{
349e7bcd65Smrg    static int count = 0;
359e7bcd65Smrg    XtAppContext app = (XtAppContext)baton;
369e7bcd65Smrg
379e7bcd65Smrg    count ++;
389e7bcd65Smrg#ifdef DEBUG
399e7bcd65Smrg    printf("%d beep!\n", count);
409e7bcd65Smrg#endif
419e7bcd65Smrg
429e7bcd65Smrg    if (3 == count)
439e7bcd65Smrg        XtAppSetExitFlag(app);
449e7bcd65Smrg    else
459e7bcd65Smrg        XtAppAddTimeOut(app, 1000, _Tick, app);
469e7bcd65Smrg}
479e7bcd65Smrg
489e7bcd65Smrgstatic sigjmp_buf jump_env;
499e7bcd65Smrg
509e7bcd65Smrgstatic void sigalrm (int sig)
519e7bcd65Smrg{
529e7bcd65Smrg    siglongjmp(jump_env, 1);
539e7bcd65Smrg}
549e7bcd65Smrg
559e7bcd65Smrgstatic void test_XtAppMainLoop_34715(void)
569e7bcd65Smrg{
579e7bcd65Smrg    XtAppContext app;
589e7bcd65Smrg    struct sigaction sa;
599e7bcd65Smrg
60fdf6a26fSmrg    g_test_bug_base("https://bugzilla.freedesktop.org/show_bug.cgi?id=");
61fdf6a26fSmrg    g_test_bug("34715");
62fdf6a26fSmrg
639e7bcd65Smrg    XtToolkitInitialize();
649e7bcd65Smrg    app = XtCreateApplicationContext();
659e7bcd65Smrg    XtAppAddTimeOut(app, 1000, _Tick, app);
669e7bcd65Smrg
679e7bcd65Smrg    /* AppTimeouts should finish and exit in 3 seconds.
689e7bcd65Smrg       Give them 10 seconds just in case system is busy, then fail. */
699e7bcd65Smrg    sa.sa_handler = sigalrm;
709e7bcd65Smrg    sa.sa_flags = SA_RESTART;
719e7bcd65Smrg    sigemptyset (&sa.sa_mask);
729e7bcd65Smrg
739e7bcd65Smrg    if (sigsetjmp(jump_env, 1) == 0) {
749e7bcd65Smrg	sigaction(SIGALRM, &sa, NULL);
759e7bcd65Smrg	alarm(10);
769e7bcd65Smrg
779e7bcd65Smrg	XtAppMainLoop(app);
78fdf6a26fSmrg	alarm(0); /* cancel alarm */
799e7bcd65Smrg    } else {
80fdf6a26fSmrg	g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC,
81fdf6a26fSmrg			    "test timed out");
829e7bcd65Smrg    }
83fdf6a26fSmrg    g_assert_cmpint(XtAppGetExitFlag(app), ==, TRUE);
849e7bcd65Smrg    XtDestroyApplicationContext(app);
859e7bcd65Smrg}
869e7bcd65Smrg
879e7bcd65Smrgint main(int argc, char** argv)
889e7bcd65Smrg{
899e7bcd65Smrg    g_test_init(&argc, &argv, NULL);
90fdf6a26fSmrg    g_test_bug_base("https://gitlab.freedesktop.org/xorg/lib/libxt/-/issues/");
919e7bcd65Smrg
929e7bcd65Smrg    g_test_add_func("/Event/XtAppMainLoop/34715", test_XtAppMainLoop_34715);
939e7bcd65Smrg
949e7bcd65Smrg    return g_test_run();
959e7bcd65Smrg}
96