6 #include <X11/extensions/XShm.h>
10 #include "drawcontext.h"
12 #include "display_priv.h"
19 struct DrawContext::Private
24 XShmSegmentInfo shminfo;
28 DrawContext::DrawContext(Window &w):
29 display(w.get_display()),
33 throw runtime_error("no DrawContext support on windows");
37 ::Display *dpy = display.get_private().display;
39 priv->use_shm = XShmQueryExtension(dpy);
42 XGetWindowAttributes(dpy, window.get_private().window, &wa);
46 priv->image = XShmCreateImage(dpy, wa.visual, wa.depth, ZPixmap, 0, &priv->shminfo, wa.width, wa.height);
48 throw runtime_error("XShmCreateImage");
50 priv->shminfo.shmid = shmget(IPC_PRIVATE, priv->image->bytes_per_line*priv->image->height, IPC_CREAT|0666);
51 priv->shminfo.shmaddr=priv->image->data = reinterpret_cast<char *>(shmat(priv->shminfo.shmid, 0, 0));
52 priv->shminfo.readOnly = false;
54 XShmAttach(dpy, &priv->shminfo);
57 display.check_error();
61 priv->image = XCreateImage(dpy, wa.visual, wa.depth, ZPixmap, 0, 0, wa.width, wa.height, 8, 0);
63 throw runtime_error("XCreateImage");
64 priv->image->data = new char[priv->image->bytes_per_line*priv->image->height];
69 DrawContext::~DrawContext()
74 XShmDetach(display.get_private().display, &priv->shminfo);
75 shmdt(priv->shminfo.shmaddr);
76 shmctl(priv->shminfo.shmid, IPC_RMID, 0);
79 XDestroyImage(priv->image);
85 unsigned DrawContext::get_depth() const
90 return priv->image->bits_per_pixel;
94 unsigned char *DrawContext::get_data()
99 return reinterpret_cast<unsigned char *>(priv->image->data);
103 void DrawContext::update()
106 ::Display *dpy = display.get_private().display;
107 WindowHandle wnd = window.get_private().window;
109 GC gc = XCreateGC(dpy, wnd, 0, 0);
112 XShmPutImage(dpy, wnd, gc, priv->image, 0, 0, 0, 0, priv->image->width, priv->image->height, false);
114 XPutImage(dpy, wnd, gc, priv->image, 0, 0, 0, 0, priv->image->width, priv->image->height);
120 } // namespace Graphics