#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static void paint(GdkWindow *); static PangoContext * pango_context; static Display * display; static int errorHandler(Display *, XErrorEvent *); int main ( int argc, char ** argv) { XSetWindowAttributes attr; Window w; GdkWindow * gw; if (!(display = XOpenDisplay(XDisplayName(NULL)))) { fprintf(stderr, "can't open display"); _exit(1); } g_type_init(); g_thread_init(NULL); gdk_init(&argc, &argv); XSynchronize(display, True); XSetErrorHandler(errorHandler); pango_context = pango_xft_get_context(display, DefaultScreen(display)); attr.override_redirect = 1; attr.background_pixel = WhitePixel(display, DefaultScreen(display)); attr.colormap = DefaultColormap(display, DefaultScreen(display)); w = XCreateWindow(display, DefaultRootWindow(display), 0, 0, 64, 64, 0, CopyFromParent, CopyFromParent, CopyFromParent, CWOverrideRedirect|CWColormap, &attr); fprintf(stderr, "Created window %x (root=%x)\n", (int)w, (int)DefaultRootWindow(display)); XSelectInput(display, w, ButtonPressMask|ExposureMask); gw = gdk_window_foreign_new((GdkNativeWindow)w); if (!gw) { fprintf(stderr, "Can't create by foreign ?\n"); } XMapWindow(display, w); while (1) { XEvent e; XNextEvent(display, &e); fprintf(stderr, "an event receivied (%d)\n", e.type); if (e.type == Expose) { paint(gw); } else if (e.type == ButtonPress) { gdk_window_destroy(gw); XCloseDisplay(display); _exit(0); } } } void paint(GdkWindow * gw) { PangoLayout * pgl = pango_layout_new(pango_context); PangoFontDescription * pgf = pango_font_description_from_string("Courier 10"); GdkGC * gc0; GdkGC * gc1; GdkVisual * best = gdk_visual_get_best(); GdkColormap * cmap = gdk_colormap_new(best, FALSE); GdkColor fg; GdkColor bg; GdkPixmap * buf; bg.red = bg.green = bg.blue = 0; fg.red = fg.green = fg.blue = 65535; buf = gdk_pixmap_new(gw, 64, 64, -1); gc0 = gdk_gc_new(gw); gc1 = gdk_gc_new(buf); gdk_colormap_alloc_color(cmap, &bg, TRUE, TRUE); gdk_colormap_alloc_color(cmap, &fg, TRUE, TRUE); gdk_drawable_set_colormap(buf, cmap); pango_layout_set_font_description(pgl, pgf); gdk_gc_set_foreground(gc1, &bg); gdk_gc_set_background(gc1, &fg); gdk_draw_rectangle(buf, gc1, TRUE, 0, 0, 64, 64); gdk_gc_set_foreground(gc1, &fg); gdk_gc_set_background(gc1, &bg); gdk_gc_set_foreground(gc0, &fg); gdk_gc_set_background(gc0, &bg); pango_layout_set_text(pgl, "Test", -1); gdk_draw_layout(buf, gc1, 5, 20, pgl); gdk_draw_drawable(gw, gc0, buf, 0, 0, 0, 0, -1, -1); pango_font_description_free(pgf); g_object_unref(pgl); gdk_window_show(gw); fprintf(stderr, "paint done\n"); } int errorHandler(Display * d, XErrorEvent * e) { char * buf; fprintf(stderr, "error rcvd, serial=%lx, error code=%d\n" "major=%d, minor=%d\n", e->serial, e->error_code, e->request_code, e->minor_code); buf = (char *)g_malloc0(1024); XGetErrorText(d, e->error_code, buf, 1023); fprintf(stderr, "Error info : %s\n", buf); g_free(buf); return 0; }