summaryrefslogtreecommitdiff
path: root/X11.c
blob: 887eef9ae496df690bed4e57cf7137c0dc4b068c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <X11/Xlib.h>
#include <X11/Xos.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include <X11/cursorfont.h>

#define ENABLE_X11_SHARED_MEMORY_PIXMAPS

#ifdef ENABLE_X11_SHARED_MEMORY_PIXMAPS
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/time.h>
#include <X11/extensions/XShm.h>

static   int UsingSharedMemoryPixmaps = 0;
#endif

#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

#include "Gfx.h"
#include "Vector.h"
#include "Movie.h"

#define NIL         (0)
#define EVENT_MASK  ( ExposureMask | \
                      KeyPressMask | \
                      ButtonPressMask | \
                      ButtonReleaseMask | \
                      ButtonMotionMask)

/*** global data ***/

static  Display         * dpy;
static  Visual          vis;
static  unsigned int    blackColor;
static  unsigned int    whiteColor;
static  Window          win;
static  XEvent          e;
static  GC              gc;
static  XImage          * image;
static  void            * offscreen = (void*)0;
static  int             mydepth;
static  signed int      startx, starty;
static  int             width = 400, height = 400;

/*** End of global data ***/
void redraw(void) {

#ifdef ENABLE_X11_SHARED_MEMORY_PIXMAPS
    if( UsingSharedMemoryPixmaps ) {
        XShmPutImage( dpy, win, gc, image, 0, 0, 0, 0, width, height, 0 );
    } else {
#endif
        XPutImage   ( dpy, win, gc, image, 0, 0, 0, 0, width, height );
#ifdef ENABLE_X11_SHARED_MEMORY_PIXMAPS
    }
#endif

}

#ifdef ENABLE_X11_SHARED_MEMORY_PIXMAPS
    XShmSegmentInfo * shminfo;
#endif

void handle_signal( int sig ) {
    if( sig == SIGINT ) {
#ifdef ENABLE_X11_SHARED_MEMORY_PIXMAPS
        shmdt( shminfo->shmaddr );
        shmctl( shminfo->shmid, IPC_RMID, 0);
#endif
        fputs( "Exit\n", stderr);
        exit( 0 );
    }
    if( sig == SIGALRM ) {
        if( !Movie_checkframe( ) ) {
            Vector_paint();
            redraw();
        }
    }
}

/*** main() ***/
int main(int argc, char **argv) {

    char        x11_keychar;  /* gedrueckte taste */
    KeySym      key;          /* metadaten beim tastendruecken */
    int         shallredraw = 0;
    Cursor      curs;
    struct itimerval ticks;

    signal( SIGINT, handle_signal );
    signal( SIGALRM, handle_signal );

    Vector_init( 5, 300);

#ifdef ENABLE_X11_SHARED_MEMORY_PIXMAPS
    shminfo = malloc( sizeof( shminfo ));
#endif

    if( (dpy = XOpenDisplay(NIL)) == NULL ) {
        fputs( "Can't XOpenDisplay\n", stderr );
        exit( 0 );
    }

    win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, width, height, 0, blackColor, blackColor);

    /*** We want to get MapNotify events ***/
    XSelectInput(dpy, win, StructureNotifyMask);

    /*** "Map" the window (that is, make it appear on the screen) ***/
    XMapWindow(dpy, win);

    /*** Create a "Graphics Context" ***/
    gc = XCreateGC(dpy, win, 0, NIL);

    /*** Wait for the MapNotify event ***/
    for(;;) {
        XNextEvent(dpy, &e);
        if (e.type == MapNotify)
        break;
    }

    curs = XCreateFontCursor(dpy, 39);
    XDefineCursor( dpy, win, curs );

    switch ( mydepth = DefaultDepth(dpy, DefaultScreen(dpy)) ) {
    case 24:
    case 32:
        break;
    default:
        fputs( "Screen Resolution Aua\n", stderr );
        exit( 0 );
        break;
    }

#ifdef ENABLE_X11_SHARED_MEMORY_PIXMAPS
    {
    int    major, minor;
    Bool   pixmaps;

    if( XShmQueryVersion( dpy, &major, &minor, &pixmaps) && pixmaps ) {
        UsingSharedMemoryPixmaps = 1;
        if((image = XShmCreateImage( dpy, CopyFromParent, DefaultDepth(dpy, DefaultScreen(dpy)), ZPixmap, NULL, shminfo, width, height )) == NULL ) {
            fputs( "Can't XShmCreateImage\n", stderr );
            exit( 0 );
        }
        shminfo->shmid   = shmget( IPC_PRIVATE, image->bytes_per_line * image->height, IPC_CREAT | 0777 );
        shminfo->shmaddr = offscreen = image->data = shmat( shminfo->shmid, 0, 0);
        shminfo->readOnly= 0;
        if( XShmAttach( dpy, shminfo ) == NULL ) {
            fputs( "Can't XShmAttach\n", stderr );
            exit( 0 );
        }
    } else {
#endif
    if( ( image = XCreateImage( dpy, CopyFromParent, DefaultDepth(dpy, DefaultScreen(dpy)), ZPixmap, 0, offscreen, width, height, 32, 0 ) ) == NULL ) {
            fputs( "Can't XCreateImage\n", stderr );
            exit( 0 );
        }

#ifdef ENABLE_X11_SHARED_MEMORY_PIXMAPS
    }
    }
#endif

    Gfx_init(offscreen, width, height);
    Vector_paint();

    /*** draw application***/
    redraw();
    /*** select events ***/
    XSelectInput(dpy, win, EVENT_MASK);

    ticks.it_value.tv_sec     = 0;
    ticks.it_value.tv_usec    = 1000 * 40;
    ticks.it_interval.tv_sec  = 0;
    ticks.it_interval.tv_usec = 1000 * 40;

    setitimer( ITIMER_REAL, &ticks, (struct itimerval*)0);

    while(1) {
        XNextEvent(dpy, &e);

        switch(e.type) {
        case ConfigureNotify:
            /* fprintf(stderr, "window resized\n"); */
             break;
        case Expose:
            if(e.xexpose.count == 0) {
                redraw();
            }
            break;

        case MotionNotify:
            Vector_move( startx - ((XButtonEvent*)&e)->x, starty - ((XButtonEvent*)&e)->y );
            shallredraw = 1;
            /* Fall through */
        case ButtonPress:
            startx = ((XButtonEvent*)&e)->x;
            starty = ((XButtonEvent*)&e)->y;
            break;

        case KeyPress:
            /* fprintf(stderr, "got keypress-event\n"); */
            /*** klarheit verschaffen ***/
            XLookupString((XKeyEvent *)&e, &x11_keychar, 1, &key, NULL);

            switch((int)key) {
                case 'q': 
                    Vector_angle( -0.01, 0, 0);
                    shallredraw = 1;
                    break;
                case 'w': 
                    Vector_angle( +0.01, 0, 0);
                    shallredraw = 1;
                    break;
                case 'a': 
                    Vector_angle( 0, -0.01, 0);
                    shallredraw = 1;
                    break;
                case 's': 
                    Vector_angle( 0, +0.01, 0);
                    shallredraw = 1;
                    break;
                case 'y': 
                    Vector_angle( 0, 0, -0.01);
                    shallredraw = 1;
                    break;
                case 'x': 
                    Vector_angle( 0, 0, +0.01);
                    shallredraw = 1;
                    break;
                case 'c':
                    Vector_reset( );
                    shallredraw = 1;
                    break;
                default:
                    break;	  
            }

            default:
                break;
            }
            if( shallredraw == 1 ) {
                Vector_paint();
                redraw();
                shallredraw = 0;
            }
    }
#ifdef ENABLE_X11_SHARED_MEMORY_PIXMAPS
    XShmDetach( dpy, shminfo);
#endif
    XDestroyImage( image );
#ifdef ENABLE_X11_SHARED_MEMORY_PIXMAPS
    shmdt( shminfo->shmaddr );
    shmctl( shminfo->shmid, IPC_RMID, 0);
#endif
}