Event.c revision 9e7bcd65
19e7bcd65Smrg/*
29e7bcd65Smrg * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
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
609e7bcd65Smrg    XtToolkitInitialize();
619e7bcd65Smrg    app = XtCreateApplicationContext();
629e7bcd65Smrg    XtAppAddTimeOut(app, 1000, _Tick, app);
639e7bcd65Smrg
649e7bcd65Smrg    /* AppTimeouts should finish and exit in 3 seconds.
659e7bcd65Smrg       Give them 10 seconds just in case system is busy, then fail. */
669e7bcd65Smrg    sa.sa_handler = sigalrm;
679e7bcd65Smrg    sa.sa_flags = SA_RESTART;
689e7bcd65Smrg    sigemptyset (&sa.sa_mask);
699e7bcd65Smrg
709e7bcd65Smrg    if (sigsetjmp(jump_env, 1) == 0) {
719e7bcd65Smrg	sigaction(SIGALRM, &sa, NULL);
729e7bcd65Smrg	alarm(10);
739e7bcd65Smrg
749e7bcd65Smrg	XtAppMainLoop(app);
759e7bcd65Smrg    } else {
769e7bcd65Smrg	g_assert(XtAppGetExitFlag(app) == TRUE);
779e7bcd65Smrg	g_assert(0 /* timed out */);
789e7bcd65Smrg    }
799e7bcd65Smrg    g_assert(XtAppGetExitFlag(app) == TRUE);
809e7bcd65Smrg    XtDestroyApplicationContext(app);
819e7bcd65Smrg}
829e7bcd65Smrg
839e7bcd65Smrgint main(int argc, char** argv)
849e7bcd65Smrg{
859e7bcd65Smrg    g_test_init(&argc, &argv, NULL);
869e7bcd65Smrg    g_test_bug_base("https://bugzilla.freedesktop.org/show_bug.cgi?id=");
879e7bcd65Smrg
889e7bcd65Smrg    g_test_add_func("/Event/XtAppMainLoop/34715", test_XtAppMainLoop_34715);
899e7bcd65Smrg
909e7bcd65Smrg    return g_test_run();
919e7bcd65Smrg}
92