- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
基于以下 explanation我制作了一个使用 x11 的小 c 程序,它应该有一个自定义颜色图。但是我收到以下错误:
X Error of failed request: BadMatch (invalid parameter attributes)
Major opcode of failed request: 78 (X_CreateColormap)
Serial number of failed request: 15
Current serial number in output stream: 18
这是 xexample.c 的完整代码,使用“gcc xexample.c -lX11 -o xexample”编译
/* include the X library headers */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
/* include some silly stuff */
#include <stdio.h>
#include <stdlib.h>
/* here are our X variables */
Display *dis;
int screen;
Window win;
GC gc;
Colormap cmap;
/* here are our X routines declared! */
void init_x();
void close_x();
void redraw();
void create_colormap();
int main () {
XEvent event; /* the XEvent declaration !!! */
KeySym key; /* a dealie-bob to handle KeyPress Events */
char text[255]; /* a char buffer for KeyPress Events */
init_x();
create_colormap();
/* look for events forever... */
while(1) {
/* get the next event and stuff it into our event variable.
Note: only events we set the mask for are detected!
*/
XNextEvent(dis, &event);
if (event.type==Expose && event.xexpose.count==0) {
/* the window was exposed redraw it! */
redraw();
}
if (event.type==KeyPress&&
XLookupString(&event.xkey,text,255,&key,0)==1) {
/* use the XLookupString routine to convert the invent
KeyPress data into regular text. Weird but necessary...
*/
if (text[0]=='q') {
close_x();
}
printf("You pressed the %c key!\n",text[0]);
}
if (event.type==ButtonPress) {
/* tell where the mouse Button was Pressed */
int x=event.xbutton.x,
y=event.xbutton.y;
strcpy(text,"X is FUN!");
int colortje = rand()%event.xbutton.x%255;
printf("colortje: %d\n", colortje);
XSetForeground(dis,gc,colortje);
XDrawString(dis,win,gc,x,y, text, strlen(text));
}
}
}
void init_x() {
/* get the colors black and white (see section for details) */
unsigned long black,white;
dis=XOpenDisplay((char *)0);
screen=DefaultScreen(dis);
black=BlackPixel(dis,screen),
white=WhitePixel(dis,screen);
win=XCreateSimpleWindow(dis,DefaultRootWindow(dis),0,0,
300, 300, 5,black, white);
XSetStandardProperties(dis,win,"Howdy","Hi",None,NULL,0,NULL);
XSelectInput(dis, win, ExposureMask|ButtonPressMask|KeyPressMask);
gc=XCreateGC(dis, win, 0,0);
XSetBackground(dis,gc,white);
XSetForeground(dis,gc,black);
XClearWindow(dis, win);
XMapRaised(dis, win);
};
void close_x() {
XFreeGC(dis, gc);
XDestroyWindow(dis,win);
XCloseDisplay(dis);
XFreeColormap(dis,cmap);
exit(1);
};
void redraw() {
XClearWindow(dis, win);
};
void create_colormap() {
int i;
XColor tmp[255];
for(i=0;i<255;i++) {
tmp[i].pixel=i;
tmp[i].flags=DoRed|DoGreen|DoBlue;
tmp[i].red=i*256;
tmp[i].blue=i*256;
tmp[i].green=i*256;
}
cmap=XCreateColormap(dis,RootWindow(dis,screen),
DefaultVisual(dis,screen),AllocAll);
XStoreColors(dis,cmap,tmp,255);
XSetWindowColormap(dis,win,cmap);
};
非常感谢您的宝贵时间!
最佳答案
对于最终来到这里的人,这里是现在的工作代码:
/*
To compile:
gcc hi.c -o hi -lX11
*/
/* include the X library headers */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
/* include some silly stuff */
#include <stdio.h>
#include <stdlib.h>
/* here are our X variables */
// Connection to X server and window
static Display *dis;
static int screen,depth,class;
static Window win;
static Visual *visual;
static XEvent event; /* the XEvent declaration !!! */
static XSizeHints hint;
static XVisualInfo visual_info, *visual_list;
static XWindowAttributes attributes;
static XSetWindowAttributes setattributes;
static GC gc;
// Drawing the grid as an X image
static XImage *planeimage;
static char *imagedata;
// Colours
static Colormap cmap;
static XColor *colors;
static unsigned long white, black, foreground, background;
/* here are our X routines declared! */
void init_x();
void close_x();
void redraw();
void create_colormap();
int main () {
KeySym key; /* a dealie-bob to handle KeyPress Events */
char text[255]; /* a char buffer for KeyPress Events */
init_x();
/* look for events forever... */
while(1) {
/* get the next event and stuff it into our event variable.
Note: only events we set the mask for are detected!
*/
printf("Trying to trigger event\n");
XNextEvent(dis, &event);
printf("NextEvent triggered\n");
if (event.type==Expose && event.xexpose.count==0) {
/* the window was exposed redraw it! */
redraw();
}
if (event.type==KeyPress&&
XLookupString(&event.xkey,text,255,&key,0)==1) {
/* use the XLookupString routine to convert the invent
KeyPress data into regular text. Weird but necessary...
*/
printf("You pressed the %c key!\n",text[0]);
if (text[0]=='q') {
close_x();
}
}
if (event.type==ButtonPress) {
/* tell where the mouse Button was Pressed */
int x=event.xbutton.x, y=event.xbutton.y;
int j = 0, nc = 200;
strcpy(text,"X is FUN!");
int colortje = 255*255;
printf("RGB = %d %d %d\n", colors[100].red, colors[100].green , colors[100].blue);
for (j=1; j <= nc; j++) XPutPixel(planeimage,x+j,y,colors[100].pixel);
XPutImage(dis,win,gc,planeimage,0,0,1,1,(unsigned int)(500),(unsigned int)(500));
XSetForeground(dis,gc,colortje);
XDrawString(dis,win,gc,x+100,y, text, strlen(text));
}
}
}
void init_x() {
/* get the colors black and white (see section for details) */
int i;
unsigned long black,white;
dis=XOpenDisplay((char *)0);
screen=DefaultScreen(dis);
visual_info.screen = screen;
visual_list = XGetVisualInfo(dis,DefaultScreen(dis),&visual_info,&i);
visual = XDefaultVisual(dis, screen);
depth = DefaultDepth(dis, screen);
printf("Got default Visual with Depth %d\n",depth);
black=BlackPixel(dis,screen);
white=WhitePixel(dis,screen);
printf("Black: %lu White %lu\n", black, white);
background = black;
foreground = foreground;
hint.x = 10000; hint.y = 0;
hint.width = 500; hint.height = 500;
hint.flags = PPosition | PSize;
printf("Creater colormap\n");
create_colormap();
setattributes.colormap = cmap;
setattributes.background_pixel = BlackPixel(dis,screen);
setattributes.border_pixel = WhitePixel(dis,screen);
setattributes.event_mask = ExposureMask | KeyPressMask | ButtonPressMask | StructureNotifyMask;
printf("Creating window\n");
printf("Got default Visual with Depth %d\n",depth);
win = XCreateWindow(dis, DefaultRootWindow(dis),0,0,
500, 500, 5, depth,
InputOutput, visual,
CWColormap | CWBackPixel | CWBorderPixel,
&setattributes);
XSetStandardProperties(dis,win,"Howdy","Hi",None,NULL,0,&hint);
XGetWindowAttributes(dis,win,&attributes);
XSelectInput(dis,win, ExposureMask|ButtonPressMask|KeyPressMask);
if (DoesBackingStore(DefaultScreenOfDisplay(dis))) {
setattributes.backing_store = WhenMapped;
XChangeWindowAttributes(dis,win,CWBackingStore,&setattributes);
}
gc = XCreateGC(dis, win, 0, 0);
XSetBackground(dis,gc,background);
XSetForeground(dis,gc,foreground);
XSelectInput(dis,win, ExposureMask|ButtonPressMask|KeyPressMask);
// XClearWindow(dis,win);
XMapRaised(dis,win);
XNextEvent(dis, &event);
imagedata = (char *)calloc(500*500,depth);
if (imagedata == NULL) printf("Error in memory allocation\n");
planeimage = XCreateImage(dis, visual, depth, ZPixmap,
0, imagedata, 500, 500 , 8, 0);
};
void close_x() {
XFreeGC(dis, gc);
XDestroyWindow(dis,win);
XCloseDisplay(dis);
XFreeColormap(dis,cmap);
exit(1);
};
void redraw() {
XClearWindow(dis, win);
};
void create_colormap() {
int i, colormap_size;
colormap_size = DisplayCells(dis, screen);
if ((colors = (XColor *)calloc(colormap_size,sizeof(XColor))) == NULL)
{
fprintf(stderr, "No memory for setting up colormap\n");
exit(1);
}
for (i=0; i < colormap_size; i++)
{
colors[i].pixel = i;
colors[i].flags = DoRed | DoGreen | DoBlue;
}
XQueryColors(dis,DefaultColormap(dis,screen),colors,colormap_size);
for (i=0; i <= 255; i++)
{
if (i < colormap_size) {
colors[i].red = 0*0;
colors[i].green = 257*255;
colors[i].blue = 0;
}
else fprintf(stderr,"Cannot set color %d, your screen has colormap size %d\n",i,colormap_size);
}
cmap = XDefaultColormap(dis,screen);
for (i=0; i <= 255 && i < colormap_size; i++)
XAllocColor(dis,cmap,&colors[i]);
printf("Colormap_size: %d\n", colormap_size);
printf("Created cmap\n");
};
关于c - x11 XCreatColormap 导致 BadMatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40686758/
我已经能够破解 Xscreensaver 以获得我想要的外观(非常感谢 luser droog 的帮助)。我添加了一些 Cairo 代码,因为单独使用 Xlib 很难实现我想要的。由于这对我来说只是一
基于以下 explanation我制作了一个使用 x11 的小 c 程序,它应该有一个自定义颜色图。但是我收到以下错误: X Error of failed request: BadMatch (i
我正在使用 tsung ,用 openssl 和 erlang 编译来发送查询。 在 tsung_controller.log 中我收到这个错误: ** Reason for termination
我一直在尝试将 CouchDB 安装在 digital ocean 水滴中的全新 centos7 上。尝试使用以下步骤安装时,我没有收到任何错误: yum -y update yum -y group
我正在使用多个客户端(200、300、500、....3000)对我的服务器进行负载测试。为了进行测试,我使用的是 windows 64 位系统,我在一个系统上运行服务器,在另一个系统上运行客户端。所
我是一名优秀的程序员,十分优秀!