summaryrefslogtreecommitdiff
path: root/X11.c
diff options
context:
space:
mode:
authorerdgeist <>2003-05-13 12:05:12 +0000
committererdgeist <>2003-05-13 12:05:12 +0000
commit866f0ad8030ea2ceea22a4d61e6ff0006546c3fa (patch)
tree54f3af1248bce246e1dd92b196b818271d21f2f6 /X11.c
Here we go
Diffstat (limited to 'X11.c')
-rwxr-xr-xX11.c264
1 files changed, 264 insertions, 0 deletions
diff --git a/X11.c b/X11.c
new file mode 100755
index 0000000..de552fe
--- /dev/null
+++ b/X11.c
@@ -0,0 +1,264 @@
1#include <X11/Xlib.h>
2#include <X11/Xos.h>
3#include <X11/Xutil.h>
4#include <X11/Xatom.h>
5#include <X11/keysym.h>
6#include <X11/cursorfont.h>
7
8#define ENABLE_X11_SHARED_MEMORY_PIXMAPS
9
10#ifdef ENABLE_X11_SHARED_MEMORY_PIXMAPS
11#include <sys/ipc.h>
12#include <sys/shm.h>
13#include <sys/time.h>
14#include <X11/extensions/XShm.h>
15
16static int UsingSharedMemoryPixmaps = 0;
17#endif
18
19#include <assert.h>
20#include <unistd.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <signal.h>
24
25#include "Gfx.h"
26#include "Vector.h"
27#include "Movie.h"
28
29#define NIL (0)
30#define EVENT_MASK ( ExposureMask | \
31 KeyPressMask | \
32 ButtonPressMask | \
33 ButtonReleaseMask | \
34 ButtonMotionMask)
35
36/*** global data ***/
37
38static Display * dpy;
39static Visual vis;
40static unsigned int blackColor;
41static unsigned int whiteColor;
42static Window win;
43static XEvent e;
44static GC gc;
45static XImage * image;
46static void * offscreen = (void*)0;
47static int mydepth;
48static signed int startx, starty;
49static int width = 400, height = 400;
50
51/*** End of global data ***/
52void redraw(void) {
53
54#ifdef ENABLE_X11_SHARED_MEMORY_PIXMAPS
55 if( UsingSharedMemoryPixmaps ) {
56 XShmPutImage( dpy, win, gc, image, 0, 0, 0, 0, width, height, 0 );
57 } else {
58#endif
59 XPutImage ( dpy, win, gc, image, 0, 0, 0, 0, width, height );
60#ifdef ENABLE_X11_SHARED_MEMORY_PIXMAPS
61 }
62#endif
63
64}
65
66#ifdef ENABLE_X11_SHARED_MEMORY_PIXMAPS
67 XShmSegmentInfo * shminfo;
68#endif
69
70void handle_signal( int sig ) {
71 if( sig == SIGINT ) {
72#ifdef ENABLE_X11_SHARED_MEMORY_PIXMAPS
73 shmdt( shminfo->shmaddr );
74 shmctl( shminfo->shmid, IPC_RMID, 0);
75#endif
76 fputs( "Exit\n", stderr);
77 exit( 0 );
78 }
79 if( sig == SIGALRM ) {
80 if( !Movie_checkframe( ) ) {
81 Vector_paint();
82 redraw();
83 }
84 }
85}
86
87/*** main() ***/
88int main(int argc, char **argv) {
89
90 char x11_keychar; /* gedrueckte taste */
91 KeySym key; /* metadaten beim tastendruecken */
92 int shallredraw = 0;
93 Cursor curs;
94 struct itimerval ticks;
95
96 signal( SIGINT, handle_signal );
97 signal( SIGALRM, handle_signal );
98
99 Vector_init( 8, 300);
100
101#ifdef ENABLE_X11_SHARED_MEMORY_PIXMAPS
102 shminfo = malloc( sizeof( shminfo ));
103#endif
104
105 if( (dpy = XOpenDisplay(NIL)) == NULL ) {
106 fputs( "Can't XOpenDisplay\n", stderr );
107 exit( 0 );
108 }
109
110 win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, width, height, 0, blackColor, blackColor);
111
112 /*** We want to get MapNotify events ***/
113 XSelectInput(dpy, win, StructureNotifyMask);
114
115 /*** "Map" the window (that is, make it appear on the screen) ***/
116 XMapWindow(dpy, win);
117
118 /*** Create a "Graphics Context" ***/
119 gc = XCreateGC(dpy, win, 0, NIL);
120
121 /*** Wait for the MapNotify event ***/
122 for(;;) {
123 XNextEvent(dpy, &e);
124 if (e.type == MapNotify)
125 break;
126 }
127
128 curs = XCreateFontCursor(dpy, 39);
129 XDefineCursor( dpy, win, curs );
130
131 switch ( mydepth = DefaultDepth(dpy, DefaultScreen(dpy)) ) {
132 case 24:
133 case 32:
134 break;
135 default:
136 fputs( "Screen Resolution Aua\n", stderr );
137 exit( 0 );
138 break;
139 }
140
141#ifdef ENABLE_X11_SHARED_MEMORY_PIXMAPS
142 {
143 int major, minor;
144 Bool pixmaps;
145
146 if( XShmQueryVersion( dpy, &major, &minor, &pixmaps) && pixmaps ) {
147 UsingSharedMemoryPixmaps = 1;
148 if((image = XShmCreateImage( dpy, CopyFromParent, DefaultDepth(dpy, DefaultScreen(dpy)), ZPixmap, NULL, shminfo, width, height )) == NULL ) {
149 fputs( "Can't XShmCreateImage\n", stderr );
150 exit( 0 );
151 }
152 shminfo->shmid = shmget( IPC_PRIVATE, image->bytes_per_line * image->height, IPC_CREAT | 0777 );
153 shminfo->shmaddr = offscreen = image->data = shmat( shminfo->shmid, 0, 0);
154 shminfo->readOnly= 0;
155 if( XShmAttach( dpy, shminfo ) == NULL ) {
156 fputs( "Can't XShmAttach\n", stderr );
157 exit( 0 );
158 }
159 } else {
160#endif
161 if( ( image = XCreateImage( dpy, CopyFromParent, DefaultDepth(dpy, DefaultScreen(dpy)), ZPixmap, 0, offscreen, width, height, 32, 0 ) ) == NULL ) {
162 fputs( "Can't XCreateImage\n", stderr );
163 exit( 0 );
164 }
165
166#ifdef ENABLE_X11_SHARED_MEMORY_PIXMAPS
167 }
168 }
169#endif
170
171 Gfx_init(offscreen, width, height);
172 Vector_paint();
173
174 /*** draw application***/
175 redraw();
176 /*** select events ***/
177 XSelectInput(dpy, win, EVENT_MASK);
178
179 ticks.it_value.tv_sec = 0;
180 ticks.it_value.tv_usec = 1000 * 40;
181 ticks.it_interval.tv_sec = 0;
182 ticks.it_interval.tv_usec = 1000 * 40;
183
184 setitimer( ITIMER_REAL, &ticks, (struct itimerval*)0);
185
186 while(1) {
187 XNextEvent(dpy, &e);
188
189 switch(e.type) {
190 case ConfigureNotify:
191 /* fprintf(stderr, "window resized\n"); */
192 break;
193 case Expose:
194 if(e.xexpose.count == 0) {
195 redraw();
196 }
197 break;
198
199 case MotionNotify:
200 Vector_move( startx - ((XButtonEvent*)&e)->x, starty - ((XButtonEvent*)&e)->y );
201 shallredraw = 1;
202 /* Fall through */
203 case ButtonPress:
204 startx = ((XButtonEvent*)&e)->x;
205 starty = ((XButtonEvent*)&e)->y;
206 break;
207
208 case KeyPress:
209 /* fprintf(stderr, "got keypress-event\n"); */
210 /*** klarheit verschaffen ***/
211 XLookupString((XKeyEvent *)&e, &x11_keychar, 1, &key, NULL);
212
213 switch((int)key) {
214 case 'q':
215 Vector_angle( -0.01, 0, 0);
216 shallredraw = 1;
217 break;
218 case 'w':
219 Vector_angle( +0.01, 0, 0);
220 shallredraw = 1;
221 break;
222 case 'a':
223 Vector_angle( 0, -0.01, 0);
224 shallredraw = 1;
225 break;
226 case 's':
227 Vector_angle( 0, +0.01, 0);
228 shallredraw = 1;
229 break;
230 case 'y':
231 Vector_angle( 0, 0, -0.01);
232 shallredraw = 1;
233 break;
234 case 'x':
235 Vector_angle( 0, 0, +0.01);
236 shallredraw = 1;
237 break;
238 case 'c':
239 Vector_reset( );
240 shallredraw = 1;
241 break;
242 default:
243 break;
244 }
245
246 default:
247 printf( "Uncatched event!" );
248 break;
249 }
250 if( shallredraw == 1 ) {
251 Vector_paint();
252 redraw();
253 shallredraw = 0;
254 }
255 }
256#ifdef ENABLE_X11_SHARED_MEMORY_PIXMAPS
257 XShmDetach( dpy, shminfo);
258#endif
259 XDestroyImage( image );
260#ifdef ENABLE_X11_SHARED_MEMORY_PIXMAPS
261 shmdt( shminfo->shmaddr );
262 shmctl( shminfo->shmid, IPC_RMID, 0);
263#endif
264}