首页 > 解决方案 > Ubuntu 18.04 上 X11 呈现文本的问题

问题描述

我有一段代码,在屏幕上显示一个文本。相同的代码在 Xserver 版本 1.18.4 的 Ubuntu 16.04 上运行时显示正确的输出,但是当在 Xserver 版本 1.20.4 的 Ubuntu 18.04 上编译和运行时,它只显示一个白色的空白屏幕。

#include <iostream>
#include<unistd.h>
#include <sstream>
#include <string>              
#include <X11/Xlib.h>            
#include <X11/Xutil.h>          
#include <X11/Xft/Xft.h> 
#include <X11/extensions/XShm.h>      
#include <sys/ipc.h> 
#include <sys/shm.h>
using namespace std;  
int main()
{
    Display *display = XOpenDisplay(NULL);
    Screen *scn = DefaultScreenOfDisplay(display);
    int screen_num = DefaultScreen(display);
    int screen_width = DisplayWidth(display, screen_num);
    int screen_height = DisplayHeight(display, screen_num);
    int defaultScnDepth = DefaultDepthOfScreen(scn);
    Visual *visual = DefaultVisualOfScreen(scn);
    Window window=XCreateSimpleWindow(display,RootWindow(display,screen_num), 50, 50, 400, 400, 2 ,BlackPixel(display,screen_num),WhitePixel(display,screen_num));
    XMapWindow(display, window);
    XShmSegmentInfo shmInfo;
    XImage *xImage;
    Pixmap backPixmap;
    (xImage) = XShmCreateImage(display, visual, defaultScnDepth, ZPixmap, NULL, &shmInfo, screen_width, screen_height);
    shmInfo.shmid = shmget(IPC_PRIVATE, (xImage)->bytes_per_line * (xImage)->height, IPC_CREAT | 0777);
    shmInfo.shmaddr = (char *) shmat(shmInfo.shmid, 0, 0);
    xImage->data = shmInfo.shmaddr;
    shmInfo.readOnly = False;
    XShmAttach(display, &shmInfo);
    (backPixmap) = XShmCreatePixmap(display, window, (char *) (shmInfo.shmaddr), &shmInfo, (xImage)->width, (xImage)->height, (xImage)->depth);
    XGCValues values;
    GC gc = XCreateGC(display, backPixmap, 0, &values);
    XSync(display, false);
    Drawable drawable = backPixmap;
    visual = DefaultVisual(display, DefaultScreen(display));
    Colormap colormap = XCreateColormap(display, window, visual, AllocNone);
    const char *text = "Hello";
    XftDraw *xftDraw = NULL;
    XRenderColor xrFGColor, xrBGColor;
    XftColor     xftFGColor, xftBGColor;
    XftFont *font = NULL;
    font = XftFontOpenName( display, DefaultScreen( display ), "morpheus-18" );  
    xftDraw = XftDrawCreate(display, drawable, visual, colormap);
    std::istringstream strStream(text);
    std::string line;
    while(std::getline(strStream, line))

    {
        xrFGColor.red =  0xffff;
        xrFGColor.green =  0xffff;
        xrFGColor.blue =  0x0;
        xrFGColor.alpha= 0xffff;
        XftColorAllocValue(display, visual, colormap, &xrFGColor, &xftFGColor);
        //  Overlay Text
        XftDrawString8(xftDraw, &xftFGColor, font, 50, 50, (XftChar8 *) text, strlen(text));
        XftColorFree(display, visual, colormap, &xftFGColor);
    }

    XftDrawDestroy(xftDraw);
    XShmPutImage(display, window, gc, xImage, 0, 0, 0, 0, 400, 400, false);
    XSync(display, false);
    getchar();

}

如果有人可以指导我这里出了什么问题,那将非常有帮助。

我还有另一种情况,我在自定义 Linux 操作系统上运行此示例。它运行良好,直到操作系统上的 Xserver 升级到 1.19.6,如果我正在绘制一个矩形,我可以在显示的帖子上看到它,但没有显示文本。如果有人可以帮助我解决这个问题,那将非常有帮助。

标签: c++x11shared-memoryxserverpixman

解决方案


推荐阅读