filters/gp_filter_resize_alloc: Check w and h
[gfxprim.git] / demos / c_simple / input_example.c
blob70b6c0ff2ef73c36e6a8616ce6bc2bf25dfa2095
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-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
21 * *
22 * Copyright (C) 2009-2013 Cyril Hrubis <metan@ucw.cz> *
23 * *
24 *****************************************************************************/
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
30 #include "gfxprim.h"
32 static gp_pixmap *win;
33 static gp_backend *backend;
35 static gp_pixel red, green, white, black;
37 static void draw_event(gp_event *ev)
39 static gp_size size = 0;
41 if (ev->type != GP_EV_KEY)
42 return;
44 int align = GP_ALIGN_RIGHT|GP_VALIGN_BOTTOM;
46 gp_text_clear(win, NULL, 20, 20, align, black, size);
47 size = gp_print(win, NULL, 20, 20, align,
48 white, black, "Key=%s",
49 gp_event_key_name(ev->val.key.key));
51 gp_backend_flip(backend);
54 static void event_loop(void)
56 for (;;) {
57 gp_backend_wait(backend);
59 while (gp_backend_events_queued(backend)) {
60 gp_event ev;
62 gp_backend_get_event(backend, &ev);
63 gp_event_dump(&ev);
65 switch (ev.type) {
66 case GP_EV_KEY:
67 draw_event(&ev);
69 switch (ev.val.key.key) {
70 case GP_KEY_ESC:
71 gp_backend_exit(backend);
72 exit(0);
73 break;
74 case GP_BTN_LEFT:
75 gp_hline_xxy(win, ev.cursor_x - 3,
76 ev.cursor_x + 3,
77 ev.cursor_y, red);
78 gp_vline_xyy(win, ev.cursor_x,
79 ev.cursor_y - 3,
80 ev.cursor_y + 3, red);
81 gp_backend_flip(backend);
82 break;
83 default:
84 break;
86 break;
87 case GP_EV_REL:
88 switch (ev.code) {
89 static int size = 0;
90 case GP_EV_REL_POS:
91 if (gp_event_get_key(&ev, GP_BTN_LEFT)) {
92 gp_putpixel(win, ev.cursor_x,
93 ev.cursor_y,
94 green);
96 int align = GP_ALIGN_RIGHT|GP_VALIGN_BOTTOM;
98 gp_text_clear(win, NULL, 20, 40, align,
99 black, size);
100 size = gp_print(win, NULL, 20, 40, align,
101 white, black, "X=%3u Y=%3u dX=%3i dY=%3i",
102 ev.cursor_x, ev.cursor_y,
103 ev.val.rel.rx, ev.val.rel.ry);
104 gp_backend_flip(backend);
105 break;
107 break;
108 case GP_EV_SYS:
109 switch (ev.code) {
110 case GP_EV_SYS_RESIZE:
111 gp_backend_resize_ack(backend);
112 break;
113 case GP_EV_SYS_QUIT:
114 gp_backend_exit(backend);
115 exit(0);
116 break;
118 break;
124 int main(int argc, char *argv[])
126 const char *backend_opts = "X11";
127 int opt;
129 while ((opt = getopt(argc, argv, "b:h")) != -1) {
130 switch (opt) {
131 case 'b':
132 backend_opts = optarg;
133 break;
134 case 'h':
135 gp_backend_init("help", NULL);
136 return 0;
137 break;
138 default:
139 fprintf(stderr, "Invalid paramter '%c'\n", opt);
140 return 1;
144 backend = gp_backend_init(backend_opts, "Input Test");
146 if (backend == NULL) {
147 fprintf(stderr, "Failed to initalize backend '%s'\n",
148 backend_opts);
149 return 1;
152 win = backend->pixmap;
154 red = gp_rgb_to_pixmap_pixel(0xff, 0x00, 0x00, win);
155 green = gp_rgb_to_pixmap_pixel(0x00, 0xff, 0x00, win);
156 white = gp_rgb_to_pixmap_pixel(0xff, 0xff, 0xff, win);
157 black = gp_rgb_to_pixmap_pixel(0x00, 0x00, 0x00, win);
159 gp_fill(win, black);
160 gp_backend_flip(backend);
162 for (;;) {
163 gp_backend_wait(backend);
164 event_loop();