Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
992 views
in Technique[技术] by (71.8m points)

c++ - How a draw a string in a splash screen by Xlib

here is my code and I can not jump out of the while(!done) function

use XFlush(d) can show the display form buffer and do not disappear before XCloseDisplay(d)

and I want to make draw string like that

g++ -o youname youcppname -lX11

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(void)
{
    Display *d;
    Window w;
    XEvent e;
    const char *msg = "Hello, World!";
    int s;
    bool done = false;

    /* open connection with the server */
    d = XOpenDisplay(NULL);
    if (d == NULL)
    {
        fprintf(stderr, "Cannot open display
");
        exit(1);
    }
    s = DefaultScreen(d);

    /* create window */
    w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 480, 320, 0,BlackPixel(d, s), WhitePixel(d, s));
    Atom type = XInternAtom(d,"_NET_WM_WINDOW_TYPE", False);
    Atom value = XInternAtom(d,"_NET_WM_WINDOW_TYPE_SPLASH", False);
    XChangeProperty(d, w, type, XA_ATOM, 32, PropModeReplace, reinterpret_cast<unsigned char*>(&value), 1);
    /* register interest in the delete window message */
    Atom wmDeleteMessage = XInternAtom(d, "WM_DELETE_WINDOW", False);
    XSetWMProtocols(d, w, &wmDeleteMessage, 1);

    /* select kind of events we are interested in */
    XSelectInput(d, w, ExposureMask | KeyPressMask | StructureNotifyMask);

    /* map (show) the window */
    XMapWindow(d, w);
    /* event loop */
    while (!done)
    {
        XNextEvent(d, &e);
        /* draw or redraw the window */
        if (e.type == Expose)
        {
            XDrawString(d, w, DefaultGC(d, s), 50, 50, msg, strlen(msg));
        }

        /* exit on key press */
        switch(e.type)
        {
        case KeyPress:
            XDestroyWindow(d, w);

        break;

        case DestroyNotify:
            done = true;
        break;

        case ClientMessage:
            if (e.xclient.data.l[0] == wmDeleteMessage)
            {
                done = true;
            }
        break;
        }
    }
    /* close connection to server */
    XCloseDisplay(d);

    return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'm not really sure why XFlush didn't work, but this is a hybrid of the event loop and flush. It waits for the expose event, draws the string and exits the loop.

After 5 seconds it unmaps the window then exits 5 seconds later.

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(void)
{
    Display *d;
    Window w;
    XEvent e;
    const char *msg = "Hello, World!";
    int s;
    bool done = false;

    /* open connection with the server */
    d = XOpenDisplay(NULL);
    if (d == NULL)
    {
        fprintf(stderr, "Cannot open display
");
        exit(1);
    }
    s = DefaultScreen(d);

    /* create window */
    w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 480, 320, 0,BlackPixel(d, s), WhitePixel(d, s));
    Atom type = XInternAtom(d,"_NET_WM_WINDOW_TYPE", False);
    Atom value = XInternAtom(d,"_NET_WM_WINDOW_TYPE_SPLASH", False);
    XChangeProperty(d, w, type, XA_ATOM, 32, PropModeReplace, reinterpret_cast<unsigned char*>(&value), 1);
    /* register interest in the delete window message */
    Atom wmDeleteMessage = XInternAtom(d, "WM_DELETE_WINDOW", False);
    XSetWMProtocols(d, w, &wmDeleteMessage, 1);

    /* select kind of events we are interested in */
    XSelectInput(d, w, ExposureMask);

    /* map (show) the window */
    XMapWindow(d, w);

    /* event loop */
    while (!done)
    {
        XNextEvent(d, &e);
        /* draw or redraw the window */
        if (e.type == Expose)
        {
            XDrawString(d, w, DefaultGC(d, s), 50, 50, msg, strlen(msg));
            done = true;
        }
    }
    XFlush(d);
    sleep(5);
    XUnmapWindow(d,w);
    XFlush(d);
    printf("unmapped
");
    sleep(5);
    /* close connection to server */
    XCloseDisplay(d);

    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...