5 #include <X11/extensions/XShm.h>
7 #include "display_private.h"
8 #include "drawcontext.h"
9 #include "window_private.h"
16 struct DrawContext::Private
20 XShmSegmentInfo shminfo;
23 DrawContext::DrawContext(Window &w):
24 display(w.get_display()),
28 DisplayHandle dpy = display.get_private().display;
30 priv->use_shm = XShmQueryExtension(dpy);
33 XGetWindowAttributes(dpy, window.get_private().window, &wa);
37 priv->image = XShmCreateImage(dpy, wa.visual, wa.depth, ZPixmap, 0, &priv->shminfo, wa.width, wa.height);
39 throw runtime_error("XShmCreateImage");
41 priv->shminfo.shmid = shmget(IPC_PRIVATE, priv->image->bytes_per_line*priv->image->height, IPC_CREAT|0666);
42 priv->shminfo.shmaddr=priv->image->data = reinterpret_cast<char *>(shmat(priv->shminfo.shmid, 0, 0));
43 priv->shminfo.readOnly = false;
45 XShmAttach(dpy, &priv->shminfo);
48 display.check_error();
52 priv->image = XCreateImage(dpy, wa.visual, wa.depth, ZPixmap, 0, 0, wa.width, wa.height, 8, 0);
54 throw runtime_error("XCreateImage");
55 priv->image->data = new char[priv->image->bytes_per_line*priv->image->height];
59 DrawContext::~DrawContext()
63 XShmDetach(display.get_private().display, &priv->shminfo);
64 shmdt(priv->shminfo.shmaddr);
65 shmctl(priv->shminfo.shmid, IPC_RMID, 0);
68 XDestroyImage(priv->image);
73 unsigned DrawContext::get_depth() const
75 return priv->image->bits_per_pixel;
78 unsigned char *DrawContext::get_data()
80 return reinterpret_cast<unsigned char *>(priv->image->data);
83 void DrawContext::update()
85 DisplayHandle dpy = display.get_private().display;
86 WindowHandle wnd = window.get_private().window;
88 GC gc = XCreateGC(dpy, wnd, 0, 0);
91 XShmPutImage(dpy, wnd, gc, priv->image, 0, 0, 0, 0, priv->image->width, priv->image->height, false);
93 XPutImage(dpy, wnd, gc, priv->image, 0, 0, 0, 0, priv->image->width, priv->image->height);
98 } // namespace Graphics