summaryrefslogtreecommitdiff
path: root/doc/tutorial/src/include/cairo-tutorial-xlib.h
blob: 5e78d03548ac6367e037ee9498de9d7009f301d9 (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
/* cairo-tutorial-xlib.h - a tutorial framework for cairo with xlib
 *
 * Copyright © 2005, Keith Packard
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
 */

#include    <stdio.h>
#include    <stdlib.h>
#include    <ctype.h>
#include    <strings.h>
#include    <X11/Xos.h>
#include    <X11/Xlib.h>
#include    <X11/Xutil.h>
#include    <X11/Xatom.h>
#include    <cairo.h>
#include    <cairo-xlib.h>

#ifndef WIDTH
#define WIDTH 400
#endif

#ifndef HEIGHT
#define HEIGHT 400
#endif

#ifndef DEFAULT_VISUAL
#define DEFAULT_VISUAL 0
#endif

static void
Usage (char *program)
{
    fprintf (stderr, "Usage: %s\n", program);
    fprintf (stderr, "\t-display <display-name>\n");
    fprintf (stderr, "\t-geometry <geometry>\n");
    exit (1);
}

char	    *dpy_name;
VisualID    vid = DEFAULT_VISUAL;
Colormap    colormap;
Visual	    *visual;
int	    depth;
unsigned int width = WIDTH, height = HEIGHT;
Window	    win;
Pixmap	    pix;
GC	    gc;

static void
draw (cairo_t *cr, int width, int height);

static void
draw_to_pixmap (Display *dpy, Pixmap pix)
{
    cairo_surface_t *surface;
    cairo_t *cr;

    surface = cairo_xlib_surface_create (dpy, pix, visual,
					 width, height);
    cr = cairo_create (surface);

    draw (cr, width, height);

    cairo_destroy (cr);
    cairo_surface_destroy (surface);
}

static void
handle_configure (Display *dpy, XConfigureEvent *cev)
{
    width = cev->width;
    height = cev->height;

    XFreePixmap(dpy, pix);
    pix = XCreatePixmap(dpy, win, width, height, depth);
    XFillRectangle(dpy, pix, gc, 0, 0, width, height);
    draw_to_pixmap (dpy, pix);
}

static void
handle_expose (Display *dpy, XExposeEvent *eev)
{
    XCopyArea (dpy, pix, win, gc,
	       eev->x, eev->y,
	       eev->width, eev->height,
	       eev->x, eev->y);
}

int
main (argc, argv)
    int	    argc;
    char    **argv;
{
    Display *dpy;
    Window  root = 0;
    char    **init_argv = argv;
    XSetWindowAttributes    attr;
    int	    scr;
    int	    x = 0, y = 0;
    int	    geometryMask;
    int	    border_width = 1;
    XSizeHints	sizeHints;
    XWMHints	wmHints;
    XClassHint	classHints;
    XEvent	ev;
    XEvent	eev;
    int		HasExpose = 0;
    int		sync = 0;
    XTextProperty   wm_name, icon_name;
    Atom	wm_delete_window;
    unsigned long   gc_mask;
    XGCValues	    gcv;
    char	quit_string[10];
    unsigned long   window_mask;
    int		has_colormap = 0;

    wm_name.value = (unsigned char *) argv[0];
    wm_name.encoding = XA_STRING;
    wm_name.format = 8;
    wm_name.nitems = strlen (argv[0]) + 1;
    icon_name = wm_name;
    gc_mask = 0;
    while (*++argv) {
	if (!strcmp (*argv, "-display"))
	    dpy_name = *++argv;
	else if (!strcmp (*argv, "-visual"))
	    vid = strtol(*++argv, NULL, 0);
	else if (!strcmp (*argv, "-geometry"))
	    geometryMask = XParseGeometry (*++argv, &x, &y, &width, &height);
	else if (!strcmp (*argv, "-sync"))
	    sync = 1;
	else if (!strcmp (*argv, "-bw"))
	    border_width = strtol(*++argv, NULL, 0);
	else if (!strcmp (*argv, "-root"))
	    root = strtol (*++argv, NULL, 0);
	else
	    Usage (*init_argv);
    }
    sizeHints.flags = 0;
    wmHints.flags = InputHint;
    wmHints.input = True;
    classHints.res_name = init_argv[0];
    classHints.res_class = init_argv[0];
    dpy = XOpenDisplay (dpy_name);
    if (!dpy) {
	fprintf (stderr, "Error: failed to open display: %s\n",
		 XDisplayName (dpy_name));
	exit (1);
    }
    if (sync)
	XSynchronize (dpy, sync);
    scr = DefaultScreen (dpy);
    if (!root)
	root = RootWindow (dpy, scr);
    window_mask = CWBackPixel|CWBorderPixel|CWEventMask;
    if (!has_colormap)
	colormap = DefaultColormap (dpy, scr);
    else
    {
	window_mask |= CWColormap;
	attr.colormap = colormap;
    }
    visual = DefaultVisual (dpy, scr);
    depth = DefaultDepth (dpy, scr);
    if (vid)
    {
	XVisualInfo vi, *vi_ret;
	int	    n;

	vi.visualid = vid;
	vi.screen = scr;
	vi_ret = XGetVisualInfo (dpy, VisualIDMask|VisualScreenMask,
				 &vi, &n);
	if (vi_ret)
	{
	    visual = vi_ret->visual;
	    if (!has_colormap)
	    {
		colormap = XCreateColormap (dpy, root, visual, AllocNone);
		window_mask |= CWColormap;
		attr.colormap = colormap;
	    }
	    depth = vi_ret->depth;
	}
    }
    attr.background_pixel = WhitePixel (dpy, scr);
    attr.border_pixel = 0;
    attr.event_mask = ExposureMask|KeyPressMask|KeyReleaseMask|StructureNotifyMask;
    wm_delete_window = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
    win = XCreateWindow (dpy, root, x, y, width, height, border_width,
			 depth, InputOutput,
			 visual,
 			 window_mask,
			 &attr);
    pix = XCreatePixmap (dpy, win, width, height, depth);
    gcv.foreground = WhitePixel (dpy, scr);
    gc = XCreateGC (dpy, pix, GCForeground, &gcv);
    XFillRectangle(dpy, pix, gc, 0, 0, width, height);
    draw_to_pixmap (dpy, pix);
    XSetWMProperties (dpy, win,
		      &wm_name, &icon_name,
		      init_argv, argc,
 		      &sizeHints, &wmHints, 0);
    XSetWMProtocols (dpy, win, &wm_delete_window, 1);
    XMapWindow (dpy, win);
    for (;;) {
	XNextEvent (dpy, &ev);
	if (HasExpose && ev.type != Expose) {
	    HasExpose = 0;
	    handle_expose (dpy, &eev.xexpose);
	}
	switch (ev.type) {
	case ConfigureNotify:
	    handle_configure (dpy, &ev.xconfigure);
	    break;
	case Expose:
	    if (QLength(dpy)) {
		eev = ev;
		HasExpose = 1;
	    } else if (ev.xexpose.count == 0) {
		handle_expose (dpy, &ev.xexpose);
	    }
	    break;
	case KeyPress:
	    if (XLookupString ((XKeyEvent *) &ev, quit_string, sizeof (quit_string), 0, 0) == 1) {
		switch (quit_string[0]) {
		case 'q':
		    exit (0);
		case 'c':
		    XClearArea (dpy, ev.xkey.window, 0, 0, 0, 0, True);
		    break;
		}
	    }
	    break;
	case ClientMessage:
	    exit (0);
	}
    }
}