5 #include <X11/extensions/XShm.h>
8 #include <msp/core/except.h>
10 #include "drawcontext.h"
12 #include "display_priv.h"
17 struct DrawContext::Private
22 XShmSegmentInfo shminfo;
26 DrawContext::DrawContext(Window &w):
27 display(w.get_display()),
31 throw Exception("DrawContext not supported on win32 yet");
35 ::Display *dpy=display.get_private().display;
37 priv->use_shm=XShmQueryExtension(dpy);
40 XGetWindowAttributes(dpy, window.get_private().window, &wa);
44 priv->image=XShmCreateImage(dpy, wa.visual, wa.depth, ZPixmap, 0, &priv->shminfo, wa.width, wa.height);
46 throw Exception("Could not create shared memory XImage");
48 priv->shminfo.shmid=shmget(IPC_PRIVATE, priv->image->bytes_per_line*priv->image->height, IPC_CREAT|0666);
49 priv->shminfo.shmaddr=priv->image->data=reinterpret_cast<char *>(shmat(priv->shminfo.shmid, 0, 0));
50 priv->shminfo.readOnly=false;
52 XShmAttach(dpy, &priv->shminfo);
55 display.check_error();
59 priv->image=XCreateImage(dpy, wa.visual, wa.depth, ZPixmap, 0, 0, wa.width, wa.height, 8, 0);
61 throw Exception("Could not create XImage");
62 priv->image->data=new char[priv->image->bytes_per_line*priv->image->height];
67 DrawContext::~DrawContext()
72 XShmDetach(display.get_private().display, &priv->shminfo);
73 shmdt(priv->shminfo.shmaddr);
74 shmctl(priv->shminfo.shmid, IPC_RMID, 0);
77 XDestroyImage(priv->image);
83 unsigned DrawContext::get_depth() const
88 return priv->image->bits_per_pixel;
92 unsigned char *DrawContext::get_data()
97 return reinterpret_cast<unsigned char *>(priv->image->data);
101 void DrawContext::update()
104 ::Display *dpy=display.get_private().display;
105 WindowHandle wnd=window.get_private().window;
107 GC gc=XCreateGC(dpy, wnd, 0, 0);
110 XShmPutImage(dpy, wnd, gc, priv->image, 0, 0, 0, 0, priv->image->width, priv->image->height, false);
112 XPutImage(dpy, wnd, gc, priv->image, 0, 0, 0, 0, priv->image->width, priv->image->height);
118 } // namespace Graphics