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()),
27 ::Display *dpy=display.get_display();
29 use_shm=false; XShmQueryExtension(dpy);
32 XGetWindowAttributes(dpy, window.get_handle(), &wa);
36 image=XShmCreateImage(dpy, wa.visual, wa.depth, ZPixmap, 0, &shminfo, wa.width, wa.height);
38 throw Exception("Could not create shared memory XImage");
40 shminfo.shmid=shmget(IPC_PRIVATE, image->bytes_per_line*image->height, IPC_CREAT|0666);
41 shminfo.shmaddr=image->data=reinterpret_cast<char *>(shmat(shminfo.shmid, 0, 0));
42 shminfo.readOnly=false;
44 XShmAttach(dpy, &shminfo);
47 display.check_error();
51 image=XCreateImage(dpy, wa.visual, wa.depth, ZPixmap, 0, 0, wa.width, wa.height, 8, 0);
53 throw Exception("Could not create XImage");
54 image->data=new char[image->bytes_per_line*image->height];
59 DrawContext::~DrawContext()
64 XShmDetach(display.get_display(), &shminfo);
65 shmdt(shminfo.shmaddr);
66 shmctl(shminfo.shmid, IPC_RMID, 0);
73 void DrawContext::update()
76 ::Display *dpy=display.get_display();
78 GC gc=XCreateGC(dpy, window.get_handle(), 0, 0);
81 XShmPutImage(dpy, window.get_handle(), gc, image, 0, 0, 0, 0, image->width, image->height, false);
83 XPutImage(dpy, window.get_handle(), gc, image, 0, 0, 0, 0, image->width, image->height);
89 } // namespace Graphics