filters/gp_filter_resize_alloc: Check w and h
[gfxprim.git] / demos / c_simple / x11_windows.c
blob7364e1ebd268fafa9a7ba6cb33d621407eb8473e
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2013 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
25 Simple backend example.
29 #include <stdio.h>
30 #include <gfxprim.h>
32 static void redraw(struct gp_pixmap *pixmap)
34 gp_pixel white_pixel, black_pixel;
36 black_pixel = gp_rgb_to_pixmap_pixel(0x00, 0x00, 0x00, pixmap);
37 white_pixel = gp_rgb_to_pixmap_pixel(0xff, 0xff, 0xff, pixmap);
39 gp_fill(pixmap, black_pixel);
40 gp_line(pixmap, 0, 0, pixmap->w - 1, pixmap->h - 1, white_pixel);
41 gp_line(pixmap, 0, pixmap->h - 1, pixmap->w - 1, 0, white_pixel);
44 static int ev_loop(struct gp_backend *backend, const char *name)
46 gp_event ev;
48 if (backend == NULL)
49 return 0;
51 while (gp_backend_get_event(backend, &ev)) {
53 printf("-------------------------- %s\n", name);
55 gp_event_dump(&ev);
57 switch (ev.type) {
58 case GP_EV_KEY:
59 switch (ev.val.val) {
60 case GP_KEY_ESC:
61 case GP_KEY_Q:
62 gp_backend_exit(backend);
63 return 1;
64 break;
66 break;
67 case GP_EV_SYS:
68 switch (ev.code) {
69 case GP_EV_SYS_RESIZE:
70 gp_backend_resize_ack(backend);
71 redraw(backend->pixmap);
72 gp_backend_flip(backend);
73 break;
74 case GP_EV_SYS_QUIT:
75 gp_backend_exit(backend);
76 return 1;
77 break;
79 break;
82 printf("-----------------------------\n");
85 return 0;
88 int main(void)
90 gp_backend *win_1, *win_2;
92 win_1 = gp_x11_init(NULL, 0, 0, 300, 300, "win 1", 0);
93 win_2 = gp_x11_init(NULL, 0, 0, 300, 300, "win 2", 0);
95 if (win_1 == NULL || win_2 == NULL) {
96 gp_backend_exit(win_1);
97 gp_backend_exit(win_2);
98 return 1;
101 /* Update the backend screen */
102 redraw(win_1->pixmap);
103 redraw(win_2->pixmap);
105 gp_backend_flip(win_1);
106 gp_backend_flip(win_2);
108 for (;;) {
110 * Wait for backend event.
112 * Either window is fine as they share connection.
114 gp_backend *b = win_1 ? win_1 : win_2;
116 if (b == NULL)
117 return 0;
119 gp_backend_wait(b);
121 if (ev_loop(win_1, "win 1"))
122 win_1 = NULL;
124 if (ev_loop(win_2, "win 2"))
125 win_2 = NULL;
128 gp_backend_exit(win_1);
129 gp_backend_exit(win_2);
131 return 0;