3 This file is part of libmspgbase
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
11 #include <X11/Xutil.h>
13 #include <msp/core/except.h>
15 #include "drawcontext.h"
21 DrawContext::DrawContext(Window &w):
22 display(w.get_display()),
26 ::Display *dpy=display.get_display();
28 use_shm=XShmQueryExtension(dpy);
31 XGetWindowAttributes(dpy, window.get_handle(), &wa);
35 image=XShmCreateImage(dpy, wa.visual, wa.depth, ZPixmap, 0, &shminfo, wa.width, wa.height);
37 throw Exception("Could not create shared memory XImage");
39 shminfo.shmid=shmget(IPC_PRIVATE, image->bytes_per_line*image->height, IPC_CREAT|0666);
40 shminfo.shmaddr=image->data=reinterpret_cast<char *>(shmat(shminfo.shmid, 0, 0));
41 shminfo.readOnly=false;
43 XShmAttach(dpy, &shminfo);
46 display.check_error();
50 image=XCreateImage(dpy, wa.visual, wa.depth, ZPixmap, 0, 0, wa.width, wa.height, 8, 0);
52 throw Exception("Could not create XImage");
53 image->data=new char[image->bytes_per_line*image->height];
58 DrawContext::~DrawContext()
63 XShmDetach(display.get_display(), &shminfo);
64 shmdt(shminfo.shmaddr);
65 shmctl(shminfo.shmid, IPC_RMID, 0);
72 unsigned DrawContext::get_depth() const
77 return image->bits_per_pixel;
81 unsigned char *DrawContext::get_data()
86 return reinterpret_cast<unsigned char *>(image->data);
90 void DrawContext::update()
93 ::Display *dpy=display.get_display();
95 GC gc=XCreateGC(dpy, window.get_handle(), 0, 0);
98 XShmPutImage(dpy, window.get_handle(), gc, image, 0, 0, 0, 0, image->width, image->height, false);
100 XPutImage(dpy, window.get_handle(), gc, image, 0, 0, 0, 0, image->width, image->height);
106 } // namespace Graphics