// gcc -lX11 -lXext xtest1.c -o xtest1 #include #include #include #include #include // Globals Display *g_display; int g_screen; XColor createXColorFromRGB(short red, short green, short blue) { XColor color; // m_color.red = red * 65535 / 255; color.red = (red * 0xFFFF) / 0xFF; color.green = (green * 0xFFFF) / 0xFF; color.blue = (blue * 0xFFFF) / 0xFF; color.flags = DoRed | DoGreen | DoBlue; if (!XAllocColor(g_display, DefaultColormap(g_display, g_screen), &color)) { fprintf(stderr, "createXColorFromRGB: Cannot create color\n"); exit(-1); } return color; } void draw(Window w) { GC gc; XGCValues gcv; XColor color; int i; // XColor blue, blue_e; // XAllocNamedColor(dsp, DefaultColormap(dsp, screen), "blue", &blue, &blue_e); // gcv.foreground = BlackPixel(g_display, g_screen); [ GCForeground ] gcv.line_width = 1; gcv.line_style = LineSolid; gc = XCreateGC(g_display, w, GCLineWidth | GCLineStyle, &gcv); for (i = 1; i< 11; i++) { color = createXColorFromRGB(128+i*10, 64+i*10, 24+i*10); XSetForeground(g_display, gc, color.pixel); XFillRectangle(g_display, w, gc, i*10, i*10, i*10 + 150, 1*10+150); // XFillRectangle(g_display, w, gc, 50, 50, 150, 150); } XSync(g_display, False); } int main(int argc, char* argv[]) { Window win; /* pointer to the newly created window. */ unsigned int display_width, display_height; /* height and width of the X display. */ unsigned int width, height; /* height and width for the new window. */ unsigned int win_x, win_y; /* location of the window's top-left corner. */ unsigned int win_border_width; /* width of window's border. */ Atom atoms[10]; char *display_name = getenv("DISPLAY"); /* address of the X display. */ g_display = XOpenDisplay(display_name); if (g_display == NULL) { fprintf(stderr, "%s: cannot connect to X server '%s'\n", argv[0], display_name); exit(1); } /* get the geometry of the default screen for our display. */ g_screen = DefaultScreen(g_display); display_width = DisplayWidth(g_display, g_screen); display_height = DisplayHeight(g_display, g_screen); /* make the new window occupy 1/9 of the screen's size. */ width = (display_width / 3); height = (display_height / 3); /* the window should be placed at the top-left corner of the screen. */ win_x = 0; win_y = 0; /* the window's border shall be 2 pixels wide. */ win_border_width = 2; win = XCreateSimpleWindow(g_display, RootWindow(g_display, g_screen), win_x, win_y, width, height, win_border_width, BlackPixel(g_display, g_screen), WhitePixel(g_display,g_screen)); // Window at the background test atoms[0] = XInternAtom (g_display, "_NET_WM_STATE_BELOW", False); atoms[1] = XInternAtom (g_display, "_NET_WM_STATE_DESKTOP", False); XChangeProperty (g_display, win, XInternAtom (g_display,"_NET_WM_STATE",False),XA_ATOM,32,PropModeReplace, (char *) &atoms, 2); /* // Window transparency/opacity test double transparency = 0.4000; unsigned int opacity = (uint)(0xffffffff * transparency); Atom atom_net_wm_window_opacity = XInternAtom(g_display, "_NET_WM_WINDOW_OPACITY", False); XChangeProperty(g_display, win, atom_net_wm_window_opacity, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &opacity, 1L); */ // Ask these events XSelectInput(g_display, win, ExposureMask | ButtonPressMask); /* make the window actually appear on the screen. */ XMapWindow(g_display, win); /* flush all pending requests to the X server, and wait until */ /* they are processed by the X server. */ XSync(g_display, False); while(1) { XEvent xe; XNextEvent(g_display, &xe); switch (xe.type) { case Expose: draw(win); break; case ButtonPress: /* quit if a button is pressed */ /* note that when using XShapeCombineMask(), i.e. * trying to get a "transparent" background, * no ButtonPress events are ever recognized */ printf("ButtonPress\n"); exit(0); default: printf("Caught event %i\n", xe.type); } } XDestroyWindow(g_display, win); XCloseDisplay(g_display); return 0; }