3 Simple GUI abstraction- Unix implementation
5 Part of the swftools package.
7 Copyright (c) 2005 Matthias Kramm <kramm@quiss.org>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
31 #include <X11/Xutil.h>
32 #include <X11/Xresource.h>
34 #include <X11/Xthreads.h>
35 #include <X11/Xatom.h>
36 #include <X11/extensions/XShm.h>
37 #include "gfxwindow.h"
41 #define USE_PIXMAP 1 // use pixmap instead of ximage for off-screen rendering
43 #define initMutex(mutex) {pthread_mutex_init(mutex, 0);}
44 #define destroyMutex(mutex) {pthread_mutex_destroy((mutex));}
45 #define lockMutex(mutex) pthread_mutex_lock(mutex)
46 #define unlockMutex(mutex) pthread_mutex_unlock(mutex)
56 pthread_mutex_t mutex;
60 queue_t* queue_init(int size, int nmemb)
62 queue_t* queue = malloc(sizeof(queue_t));
63 queue->data = (char*)malloc(size * nmemb);
69 initMutex(&queue->mutex);
73 void queue_destroy(queue_t*queue)
75 free(queue->data);queue->data = 0;
76 destroyMutex(&queue->mutex);
77 memset(queue, 0, sizeof(queue_t));
80 int queue_put(queue_t*queue, void*data)
82 int tmp = queue->writepos+1;
85 lockMutex(&queue->mutex);
86 if(tmp==queue->readpos) {
87 unlockMutex(&queue->mutex);
90 memcpy(&queue->data[queue->writepos*queue->size], data, queue->size);
91 queue->writepos = tmp;
93 unlockMutex(&queue->mutex);
97 int queue_get(queue_t*queue, void*data)
99 lockMutex(&queue->mutex);
100 if(queue->writepos == queue->readpos) {
101 unlockMutex(&queue->mutex);
104 memcpy(data, &queue->data[queue->readpos*queue->size], queue->size);
106 queue->readpos %= queue->nmemb;
108 unlockMutex(&queue->mutex);
112 void queue_flush(queue_t*queue)
114 lockMutex(&queue->mutex);
118 unlockMutex(&queue->mutex);
121 int queue_num(queue_t*queue)
123 return queue->number;
126 static int initializexwindows();
148 pthread_t eventhandler;
149 pthread_mutex_t xmutex;
151 struct _QXWindow*windowring;
160 static int initialized = 0;
165 pthread_cancel(X.eventhandler);
168 pthread_mutex_destroy(&X.xmutex);
171 typedef struct _QXWindow {
172 struct _QXWindow*next;
173 struct _QXWindow*prev;
181 XSetWindowAttributes xswa;
182 XWindowAttributes xwa;
200 pthread_mutex_t wmutex;
205 static int lastbit(U32 bits)
216 static int firstbit(U32 bits)
227 static U32 shiftl(U32 bits,int shift)
229 if(shift>0) return bits<<shift;
230 else return bits>>-shift;
233 static U32 getColorFromRGB(U8 r,U8 g,U8 b)
236 ret|=shiftl(r,(firstbit(X.v->red_mask)-7))&X.v->red_mask;
237 ret|=shiftl(g,(firstbit(X.v->green_mask)-7))&X.v->green_mask;
238 ret|=shiftl(b,(firstbit(X.v->blue_mask)-7))&X.v->blue_mask;
242 static void whatever()
249 static void*eventloop(void*);
251 static int initializexwindows()
258 pthread_mutex_init(&X.xmutex, 0);
261 /* open display, and show some information */
262 X.d=XOpenDisplay(0);//getenv("DISPLAY"));
264 /* printf("Xprotocol V%d.%d Vendor:%s Name:%s Defaults:%s",X.d->proto_major_version,
265 X.d->proto_minor_version,
266 X.d->vendor,X.d->display_name,X.d->defaults);
268 printf("Formats:\n");
269 for(t=0;t<X.d->nformats;t++) {
270 ScreenFormat*sf=&X.d->pixmap_formats[t];
271 printf("%d:depth:%d, bpp:%d, scanline:%d\n",sf->depth,sf->bits_per_pixel,sf->scanline_pad);
273 printf("Screens:\n");
274 for(t=0;t<X.d->nscreens;t++) {
275 Screen*s=&X.d->screens[t];
276 printf("%d*%d, bpp:%d\n",s->width,s->height,s->root_depth);
280 X.s=DefaultScreen(X.d);
281 if(debug) printf("Display:\n");
282 int xbig=DisplayWidth(X.d, X.s);
283 int ybig=DisplayHeight(X.d, X.s);
286 int bppbig=X.bpp=DisplayPlanes(X.d, X.s);
287 if(X.bpp==24) X.bypp=4; //Can this be 3, also?
288 if(X.bpp==16) X.bypp=2;
289 if(X.bpp==8) X.bypp=1;
290 if(debug) printf("%d*%d:%d/%d/%d\n",xbig,ybig,bppbig, X.bpp, X.bypp);
291 if(debug) printf("%d screens, vendor:%s, Ver:%d.%d(%d), at %s\n",
294 ProtocolVersion(X.d),
295 ProtocolRevision(X.d),
300 X.v=XDefaultVisualOfScreen(DefaultScreenOfDisplay(X.d));
302 X.isnative24 = (bppbig==24 && X.v->red_mask==0xff0000 &&
303 X.v->green_mask==0x00ff00 &&
304 X.v->blue_mask==0x0000ff);
305 X.isnative16 = (bppbig==16 && X.v->red_mask==0xf800 &&
306 X.v->green_mask==0x07e0 &&
307 X.v->blue_mask==0x1f);
308 X.isnative8 = (bppbig==8);
309 if(debug) printf("8bpp:%d 16bpp:%d 24(32)bpp:%d\n",X.isnative8,X.isnative16,X.isnative24);
311 X.root=DefaultRootWindow(X.d);
312 if(!X.root) {return 0;}
314 X.atom_wm = XInternAtom( X.d, "WM_PROTOCOLS", False );
315 X.atom_wmdelete = XInternAtom( X.d, "WM_DELETE_WINDOW", False );
317 X.cmap=DefaultColormap(X.d,X.s);
318 X.white=WhitePixel(X.d,X.s);
319 X.black=BlackPixel(X.d,X.s);
321 /* start event handler thread */
323 pthread_create(&X.eventhandler,0,eventloop,0);
331 //needed to set the border attribute
332 #define MWM_HINTS_FUNCTIONS (1L << 0)
333 #define MWM_HINTS_DECORATIONS (1L << 1)
334 #define MWM_HINTS_INPUT_MODE (1L << 2)
335 #define MWM_HINTS_STATUS (1L << 3)
337 #define MWM_FUNC_ALL (1L << 0)
338 #define MWM_FUNC_RESIZE (1L << 1)
339 #define MWM_FUNC_MOVE (1L << 2)
340 #define MWM_FUNC_MINIMIZE (1L << 3)
341 #define MWM_FUNC_MAXIMIZE (1L << 4)
342 #define MWM_FUNC_CLOSE (1L << 5)
344 #define MWM_DECOR_ALL (1L << 0)
345 #define MWM_DECOR_BORDER (1L << 1)
346 #define MWM_DECOR_RESIZEH (1L << 2)
347 #define MWM_DECOR_TITLE (1L << 3)
348 #define MWM_DECOR_MENU (1L << 4)
349 #define MWM_DECOR_MINIMIZE (1L << 5)
350 #define MWM_DECOR_MAXIMIZE (1L << 6)
352 #define MWM_INPUT_MODELESS 0
353 #define MWM_INPUT_PRIMARY_APPLICATION_MODAL 1
354 #define MWM_INPUT_SYSTEM_MODAL 2
355 #define MWM_INPUT_FULL_APPLICATION_MODAL 3
356 #define MWM_INPUT_APPLICATION_MODAL MWM_INPUT_PRIMARY_APPLICATION_MODAL
358 typedef struct _mwmhints {
360 unsigned int functions;
361 unsigned int decorations;
366 static QXWindow* openwindow(int posx,int posy,int w,int h,char*winName)
372 pthread_mutex_lock(&X.xmutex);
375 fprintf(stderr, "Error: XWindows not yet initialized!\n");
376 pthread_mutex_unlock(&X.xmutex);
380 QXWindow*qx= malloc(sizeof(QXWindow));
386 qx->next=X.windowring;
387 qx->prev=X.windowring->prev;
389 X.windowring->prev=qx;
393 char*iconName=winName;
400 qx->mywin=XCreateSimpleWindow(X.d,X.root,posx,posy,qx->lenx,qx->leny,0,X.white,X.black);
403 printf("Error: Couldn't create SimpleWindow\n");
404 pthread_mutex_unlock(&X.xmutex);
407 qx->xwmh=XAllocWMHints();
408 qx->xsh=XAllocSizeHints();
409 qx->xch=XAllocClassHint();
411 qx->xsh->flags=(PPosition|PSize|PMinSize|PMaxSize);
412 qx->xsh->height=qx->xsh->min_height=qx->xsh->max_height=h;
413 qx->xsh->width =qx->xsh->min_width =qx->xsh->max_width =w;
417 if(!XStringListToTextProperty(&winName, 1, &wname)) {
418 printf("Error: XStringToTextProperty failed\n");
419 pthread_mutex_unlock(&X.xmutex);
422 if(!XStringListToTextProperty(&iconName, 1, &iname)) {
423 printf("Error: XStringToTextProperty failed\n");
424 pthread_mutex_unlock(&X.xmutex);
428 XSetWMProperties(X.d,qx->mywin,&wname,&iname,0,0,qx->xsh,qx->xwmh,qx->xch);
430 qx->xgcv.foreground=X.white;
431 qx->xgcv.background=X.black;
432 qx->gc=XCreateGC(X.d,qx->mywin,GCForeground|GCBackground,&qx->xgcv);
434 XGetWindowAttributes(X.d,qx->mywin,&qx->xwa);
436 // enable events for close window
437 XChangeProperty(X.d, qx->mywin, X.atom_wm, XA_ATOM, 32, PropModeReplace, (unsigned char *)(&X.atom_wmdelete), 1 );
440 // (This code comes from the project xine (xine.sourceforge.net)
441 // I wonder if this works with all windowmanagers, though)
444 Atom atomprop = XInternAtom(X.d, "_MOTIF_WM_HINTS", True);
448 mwmhints.flags = MWM_HINTS_DECORATIONS;
449 if(noborder==1) mwmhints.decorations = 2;
450 if(noborder>1) mwmhints.decorations = 0;
451 XChangeProperty(X.d, qx->mywin, atomprop, atomprop, 32,
452 PropModeReplace, (unsigned char *) &mwmhints, 5);
453 XSetTransientForHint (X.d, qx->mywin, X.root);
457 qx->bpp=qx->xwa.depth;
458 if(qx->bpp==24) qx->bypp=4; //Can this be 3, also?
459 if(qx->bpp==16) qx->bypp=2;
460 if(qx->bpp==8) qx->bypp=1;
463 XShmSegmentInfo segInfo;
464 int bpl = qx->lenx*qx->bypp;
465 segInfo.readOnly = 0;
466 segInfo.shmid = shmget(IPC_PRIVATE,qx->leny*bpl,IPC_CREAT|0777);
468 if (segInfo.shmid <0) {
470 fprintf(stderr,"Size = %d x %d\n", qx->lenx, qx->leny);
472 segInfo.shmaddr = (char*)shmat (segInfo.shmid, 0, 0);
473 if ((long)segInfo.shmaddr == -1) {
476 XShmAttach(X.d, &segInfo);
477 if (shmctl(segInfo.shmid, IPC_RMID, &buf) < 0)
480 qx->currentscr = (U8*)segInfo.shmaddr;
483 qx->winbuf = XShmCreatePixmap(X.d,qx->mywin,segInfo.shmaddr,&segInfo,qx->lenx,qx->leny,DefaultDepth(X.d, DefaultScreen(X.d)));
485 qx->winbuf = XShmCreateImage(X.d,X.v,24,ZPixmap,(char*)segInfo.shmaddr, &segInfo, qx->lenx,qx->leny);
486 XInitImage(qx->winbuf);
492 qx->winbuf = XCreatePixmap(X.d,qx->mywin,qx->lenx,qx->leny, DefaultDepth(X.d,X.s));
494 qx->currentscr=malloc(qx->lenx*qx->leny*4);
495 qx->winbuf = XCreateImage(X.d,X.v,24,ZPixmap,0,(char*)qx->currentscr,qx->lenx,qx->leny,8,qx->lenx*4);
496 XInitImage(qx->winbuf);
502 /* get the image data for the area this window will be/is placed on */
507 XTranslateCoordinates(X.d,qx->mywin,X.root,0,0,&x,&y,&tmpwin);
508 qx->background = XGetImage(X.d, DefaultRootWindow(X.d), x, y, qx->lenx, qx->leny, AllPlanes, ZPixmap);
509 printf("%d %d\n",x,y);
512 XMapRaised(X.d,qx->mywin);
515 qx->xswa.event_mask=(qx->xwa.your_event_mask|=
525 XChangeWindowAttributes(X.d,qx->mywin,CWEventMask,&qx->xswa);
527 qx->queue = queue_init(sizeof(gfxevent_t), 256);
529 pthread_mutex_unlock(&X.xmutex);
531 pthread_mutex_init(&qx->wmutex, 0);
535 static void closeWindow(QXWindow*qx)
538 pthread_mutex_lock(&X.xmutex);
539 XDestroyWindow(X.d,qx->mywin);
540 pthread_mutex_unlock(&X.xmutex);
545 static void processEvent(gfxevent_t*event)
554 FD_SET(ConnectionNumber(X.d),&fdset);
556 de.tv_usec = 5000; // 1/200 s
558 pthread_mutex_lock(&X.xmutex);
560 FD_SET(ConnectionNumber(X.d),&fdset);
561 de.tv_sec = de.tv_usec = 0;
562 if(!select(ConnectionNumber(X.d)+1, &fdset, 0, 0, &de)) {
563 pthread_mutex_unlock(&X.xmutex);
568 pthread_mutex_unlock(&X.xmutex);
570 /* 1: find out which windows the message is for */
574 case ClientMessage: w = xe.xclient.window;break;
575 case Expose: w = xe.xexpose.window;break;
576 case NoExpose: w = xe.xexpose.window;break;
577 case ButtonPress: w = xe.xbutton.window;break;
578 case ButtonRelease: w = xe.xbutton.window;break;
579 case KeyPress: w = xe.xkey.window; break;
580 case KeyRelease: w = xe.xkey.window; break;
581 case MotionNotify: w = xe.xmotion.window;break;
582 case EnterNotify: w = xe.xmotion.window;break;
583 case LeaveNotify: w = xe.xmotion.window;break;
585 /* this usually happens for unknown events which
586 we don't care about */
590 QXWindow*qx=X.windowring;
598 while(qx!=X.windowring);
601 /* Not one of our windows. This may be a bug */
604 event->internal = win;
606 /* 2: Go for the event construction */
611 event->type = GFXEVENT_MOUSEPRESS;
612 event->button = xe.xbutton.button;
615 event->type = GFXEVENT_MOUSERELEASE;
616 event->button = xe.xbutton.button;
619 event->type = GFXEVENT_KEYPRESS;
620 event->key = xe.xkey.keycode;
623 event->type = GFXEVENT_KEYRELEASE;
624 event->key = xe.xkey.keycode;
627 event->type = GFXEVENT_MOUSEMOVE;
628 pthread_mutex_lock(&qx->wmutex);
629 event->x = xe.xmotion.x_root - win->x;
630 event->y = xe.xmotion.y_root - win->y;
631 if(!queue_put(win->queue, event)) {
632 printf("Queue overflow for window %08x!\n", win);
634 pthread_mutex_unlock(&qx->wmutex);
637 event->type = GFXEVENT_MOUSEENTER;
638 pthread_mutex_lock(&qx->wmutex);
639 event->x = xe.xmotion.x_root - win->x;
640 event->y = xe.xmotion.y_root - win->y;
641 pthread_mutex_unlock(&qx->wmutex);
644 event->type = GFXEVENT_MOUSELEAVE;
645 pthread_mutex_lock(&qx->wmutex);
646 event->x = xe.xmotion.x - win->x;
647 event->y = xe.xmotion.y - win->y;
648 pthread_mutex_unlock(&qx->wmutex);
651 if ((xe.xclient.message_type == X.atom_wm ) &&
652 ((unsigned)xe.xclient.data.l[0] == X.atom_wmdelete) ) {
653 pthread_mutex_lock(&qx->wmutex);
655 event->type = GFXEVENT_DESTROY;
656 pthread_mutex_unlock(&qx->wmutex);
662 static void*eventloop(void*r)
664 //while(!XEventsQueued(X.d,QueuedAfterReading))
668 event.type = GFXEVENT_NOTHING;
669 event.key = event.x = event.y = event.button = -1;
670 processEvent(&event);
671 if(event.type != GFXEVENT_NOTHING && event.type != GFXEVENT_MOUSEMOVE) {
672 QXWindow*win = (QXWindow*)event.internal;
673 pthread_mutex_lock(&win->wmutex);
674 if(!queue_put(win->queue,&event)) {
675 fprintf(stderr, "Queue overflow for window %08x!\n", win);
677 pthread_mutex_unlock(&win->wmutex);
683 typedef struct _internal {
688 int tmplenx, tmpleny;
692 static void gfxwindow_on(gfxwindow_t*win)
694 internal_t*i = (internal_t*)win->internal;
696 i->qx=openwindow(64, 64, win->width, win->height, i->name);
700 if(i->qx->bypp != 4) {
701 fprintf(stderr, "Warning: Not a native 32 bit screen, using second buffer\n");
702 i->currentscr2 = malloc(win->width*win->height*4);
703 win->currentscr = (unsigned char*)i->currentscr2;
705 win->currentscr = i->qx->currentscr;
710 static void gfxwindow_off(gfxwindow_t*win)
712 internal_t*i = (internal_t*)win->internal;
714 free(i->currentscr2);
718 static void gfxwindow_flippage(gfxwindow_t*win)
720 internal_t*i = (internal_t*)win->internal;
721 pthread_mutex_lock(&X.xmutex);
724 if(i->qx->bypp==2 && X.isnative16) {
726 int l = win->width*win->height;
727 unsigned char*dest = i->qx->currentscr;
728 unsigned char*src = (unsigned char*)i->currentscr2;
730 dest[1] = (src[2]&0xf8)|(src[1]>>5);
731 dest[0] = ((src[1]<<5)&0xe0)|(src[0]>>3);
736 memcpy(i->qx->currentscr, i->currentscr2, i->lenx*i->leny*i->qx->bypp);
741 XSetFunction(X.d,i->qx->gc,GXcopy);
742 XCopyArea(X.d,i->qx->winbuf,i->qx->mywin,i->qx->gc, 0, 0, i->qx->lenx,i->qx->leny, 0, 0);
745 XPutImage(X.d,qx->mywin,qx->gc,qx->winbuf,0,0,0,0,lenx,leny);
747 pthread_mutex_unlock(&X.xmutex);
750 void gfxwindow_setcolor(gfxwindow_t*win, U32 c)
752 internal_t*i = (internal_t*)win->internal;
753 pthread_mutex_lock(&X.xmutex);
754 i->qx->xgcv.foreground=getColorFromRGB((U8)(c>>16),(U8)(c>>8),(U8)c);
755 i->gc=XCreateGC(X.d,i->qx->mywin,GCForeground|GCBackground,&i->qx->xgcv);
756 pthread_mutex_unlock(&X.xmutex);
758 void gfxwindow_putpixel(gfxwindow_t*win, int x, int y, unsigned char c)
760 internal_t*i = (internal_t*)win->internal;
761 if(((U32)x)<(U32)win->width && ((U32)y)<(U32)win->height)
762 *(U32*)&win->currentscr[y*win->width*i->qx->bypp+x*i->qx->bypp]=c;
764 static gfxevent_t gfxwindow_getEvent(gfxwindow_t*win)
766 internal_t*i = (internal_t*)win->internal;
768 pthread_mutex_lock(&i->qx->wmutex);
769 if(!i->qx->queue || !queue_get(i->qx->queue,&event)) {
770 event.type = GFXEVENT_NOTHING;
771 } else if(event.type == GFXEVENT_DESTROY) {
772 queue_destroy(i->qx->queue);i->qx->queue=0;
774 pthread_mutex_unlock(&i->qx->wmutex);
778 static void gfxwindow_move(gfxwindow_t*win, int x,int y)
780 internal_t*i = (internal_t*)win->internal;
781 pthread_mutex_lock(&i->qx->wmutex);
782 pthread_mutex_lock(&X.xmutex);
786 XMoveResizeWindow(X.d, i->qx->mywin, i->qx->x, i->qx->y, i->tmplenx, i->tmpleny);
788 queue_t* queue2 = queue_init(sizeof(gfxevent_t), 256);
790 /* HACK: now that we have moved the window, all mouse move events
791 are outdated- remove them*/
792 while(queue_get(i->qx->queue, &event)) {
793 if(event.type!=GFXEVENT_MOUSEMOVE)
794 queue_put(queue2, &event);
796 queue_destroy(i->qx->queue);
797 i->qx->queue = queue2;
799 pthread_mutex_unlock(&X.xmutex);
800 pthread_mutex_unlock(&i->qx->wmutex);
803 static void gfxwindow_resize(gfxwindow_t*win, int x,int y)
805 internal_t*i = (internal_t*)win->internal;
806 if(x>win->width || y>win->height) {
807 fprintf(stderr, "resize: can only make windows smaller\n");
812 pthread_mutex_lock(&i->qx->wmutex);
815 pthread_mutex_lock(&X.xmutex);
816 XMoveResizeWindow(X.d, i->qx->mywin, i->qx->x, i->qx->y, i->tmplenx, i->tmpleny);
817 pthread_mutex_unlock(&X.xmutex);
818 pthread_mutex_unlock(&i->qx->wmutex);
820 static void gfxwindow_destroy(gfxwindow_t*win)
822 internal_t*i = (internal_t*)win->internal;
824 pthread_mutex_destroy(&i->qx->wmutex);
827 gfxwindow_t* gfxwindow_new(int width, int height)
829 gfxwindow_t*w = (gfxwindow_t*)malloc(sizeof(gfxwindow_t));
830 if(!initializexwindows()) {
831 fprintf(stderr, "Warning: Couldn't initialize X-Windows\n");
833 internal_t*i = malloc(sizeof(internal_t));
835 w->move = gfxwindow_move;
836 w->resize = gfxwindow_resize;
837 w->flippage = gfxwindow_flippage;
838 w->getEvent = gfxwindow_getEvent;
841 w->destroy = gfxwindow_destroy;
843 i->lenx = i->tmplenx = width;
844 i->leny = i->tmpleny = height;