3 This file is part of libmspgbase
4 Copyright © 2008 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
12 #include <X11/extensions/XShm.h>
13 #include <X11/Xutil.h>
15 #include <msp/core/except.h>
17 #include "drawcontext.h"
19 #include "display_priv.h"
24 struct DrawContext::Private
29 XShmSegmentInfo shminfo;
33 DrawContext::DrawContext(Window &w):
34 display(w.get_display()),
38 throw Exception("DrawContext not supported on win32 yet");
42 ::Display *dpy=display.get_private().display;
44 priv->use_shm=XShmQueryExtension(dpy);
47 XGetWindowAttributes(dpy, window.get_private().window, &wa);
51 priv->image=XShmCreateImage(dpy, wa.visual, wa.depth, ZPixmap, 0, &priv->shminfo, wa.width, wa.height);
53 throw Exception("Could not create shared memory XImage");
55 priv->shminfo.shmid=shmget(IPC_PRIVATE, priv->image->bytes_per_line*priv->image->height, IPC_CREAT|0666);
56 priv->shminfo.shmaddr=priv->image->data=reinterpret_cast<char *>(shmat(priv->shminfo.shmid, 0, 0));
57 priv->shminfo.readOnly=false;
59 XShmAttach(dpy, &priv->shminfo);
62 display.check_error();
66 priv->image=XCreateImage(dpy, wa.visual, wa.depth, ZPixmap, 0, 0, wa.width, wa.height, 8, 0);
68 throw Exception("Could not create XImage");
69 priv->image->data=new char[priv->image->bytes_per_line*priv->image->height];
74 DrawContext::~DrawContext()
79 XShmDetach(display.get_private().display, &priv->shminfo);
80 shmdt(priv->shminfo.shmaddr);
81 shmctl(priv->shminfo.shmid, IPC_RMID, 0);
84 XDestroyImage(priv->image);
90 unsigned DrawContext::get_depth() const
95 return priv->image->bits_per_pixel;
99 unsigned char *DrawContext::get_data()
104 return reinterpret_cast<unsigned char *>(priv->image->data);
108 void DrawContext::update()
111 ::Display *dpy=display.get_private().display;
112 WindowHandle wnd=window.get_private().window;
114 GC gc=XCreateGC(dpy, wnd, 0, 0);
117 XShmPutImage(dpy, wnd, gc, priv->image, 0, 0, 0, 0, priv->image->width, priv->image->height, false);
119 XPutImage(dpy, wnd, gc, priv->image, 0, 0, 0, 0, priv->image->width, priv->image->height);
125 } // namespace Graphics