Debian Codegraph @

modules/lsp-ws-lib/src/test/mtest/display/events.cpp

raw
1 /*
2 * Copyright (C) 2022 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2022 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-ws-lib
6 * Created on: 17 июл. 2022 г.
7 *
8 * lsp-ws-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-ws-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-ws-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/stdlib/stdio.h>
23 #include <lsp-plug.in/test-fw/mtest.h>
24 #include <lsp-plug.in/ws/factory.h>
25 #include <lsp-plug.in/ws/IEventHandler.h>
26
27 MTEST_BEGIN("ws.display", events)
28
29 class Handler: public ws::IEventHandler
30 {
31 private:
32 test_type_t *pTest;
33 ws::IWindow *pWnd;
34
35 public:
36 inline Handler(test_type_t *test, ws::IWindow *wnd)
37 {
38 pTest = test;
39 pWnd = wnd;
40 }
41
42 status_t log_mouse_event(const char *event, const ws::event_t *ev, const char *extra = NULL)
43 {
44 size_t screen;
45 ssize_t left, top;
46
47 if (pWnd->display()->get_pointer_location(&screen, &left, &top) == STATUS_OK)
48 pTest->printf("%s: local=(%d, %d), screen=(%d, %d, %d)%s\n",
49 event, int(ev->nLeft), int(ev->nTop), int(left), int(top), int(screen),
50 (extra) ? extra : "");
51
52 return STATUS_OK;
53 }
54
55 virtual status_t handle_event(const ws::event_t *ev)
56 {
57 char buf[80];
58
59 switch (ev->nType)
60 {
61 // Mouse events
62 case ws::UIE_MOUSE_DOWN:
63 snprintf(buf, sizeof(buf), " button=%d (0x%d), state=0x%x", int(ev->nCode), int(ev->nCode), int(ev->nState));
64 return log_mouse_event("MOUSE_DOWN", ev, buf);
65 case ws::UIE_MOUSE_UP:
66 snprintf(buf, sizeof(buf), " button=%d (0x%d), state=0x%x", int(ev->nCode), int(ev->nCode), int(ev->nState));
67 return log_mouse_event("MOUSE_UP", ev, buf);
68 case ws::UIE_MOUSE_SCROLL:
69 snprintf(buf, sizeof(buf), " direction=%d, state=0x%x", int(ev->nCode), int(ev->nState));
70 return log_mouse_event("MOUSE_SCROLL", ev, buf);
71 case ws::UIE_MOUSE_CLICK:
72 snprintf(buf, sizeof(buf), " button=%d", int(ev->nCode));
73 return log_mouse_event("MOUSE_CLICK", ev, buf);
74 case ws::UIE_MOUSE_DBL_CLICK:
75 snprintf(buf, sizeof(buf), " button=%d", int(ev->nCode));
76 return log_mouse_event("MOUSE_DBL_CLICK", ev, buf);
77 case ws::UIE_MOUSE_TRI_CLICK:
78 snprintf(buf, sizeof(buf), " button=%d", int(ev->nCode));
79 return log_mouse_event("MOUSE_TRI_CLICK", ev, buf);
80 case ws::UIE_MOUSE_MOVE:
81 return log_mouse_event("MOUSE_MOVE", ev);
82 case ws::UIE_MOUSE_IN:
83 return log_mouse_event("MOUSE_IN", ev);
84 case ws::UIE_MOUSE_OUT:
85 return log_mouse_event("MOUSE_OUT", ev);
86
87 // Keyboard events
88 case ws::UIE_KEY_DOWN:
89 pTest->printf("KEY_DOWN: code=%d (0x%x), raw=%d (0x%x), state=0x%x\n",
90 int(ev->nCode), int(ev->nCode), int(ev->nRawCode), int(ev->nRawCode), int(ev->nState));
91 break;
92 case ws::UIE_KEY_UP:
93 pTest->printf("KEY_UP: code=%d (0x%x), raw=%d (0x%x), state=0x%x\n",
94 int(ev->nCode), int(ev->nCode), int(ev->nRawCode), int(ev->nRawCode), int(ev->nState));
95 break;
96
97 // Redraw event
98 case ws::UIE_REDRAW:
99 {
100 pTest->printf("REDRAW\n");
101
102 Color c(0.0f, 0.5f, 0.75f);
103 ws::ISurface *s = pWnd->get_surface();
104 if (s == NULL)
105 return STATUS_OK;
106
107 // Perform drawing
108 s->begin();
109 s->clear(c);
110 s->end();
111
112 return STATUS_OK;
113 }
114
115 // Window events
116 case ws::UIE_SIZE_REQUEST:
117 pTest->printf("SIZE_REQUEST: size=(%d, %d)\n",
118 int(ev->nWidth), int(ev->nHeight));
119 return STATUS_OK;
120 case ws::UIE_RESIZE:
121 pTest->printf("RESIZE: coord=(%d, %d), size=(%d, %d)\n",
122 int(ev->nLeft), int(ev->nTop), int(ev->nWidth), int(ev->nHeight));
123 return STATUS_OK;
124 case ws::UIE_FOCUS_IN:
125 pTest->printf("FOCUS_IN\n");
126 return STATUS_OK;
127 case ws::UIE_FOCUS_OUT:
128 pTest->printf("FOCUS_OUT\n");
129 return STATUS_OK;
130 case ws::UIE_SHOW:
131 pTest->printf("SHOW\n");
132 return STATUS_OK;
133 case ws::UIE_HIDE:
134 pTest->printf("HIDE\n");
135 return STATUS_OK;
136
137 case ws::UIE_CLOSE:
138 {
139 pTest->printf("CLOSE\n");
140
141 pWnd->hide();
142 pWnd->display()->quit_main();
143 break;
144 }
145
146 default:
147 return IEventHandler::handle_event(ev);
148 }
149
150 return STATUS_OK;
151 }
152 };
153
154 MTEST_MAIN
155 {
156 ws::IDisplay *dpy = ws::create_display(0, NULL);
157 MTEST_ASSERT(dpy != NULL);
158 lsp_finally { ws::free_display(dpy); };
159
160 io::Path font;
161 MTEST_ASSERT(font.fmt("%s/font/example.ttf", resources()));
162 MTEST_ASSERT(dpy->add_font("example", &font) == STATUS_OK);
163 MTEST_ASSERT(dpy->add_font_alias("alias", "example") == STATUS_OK);
164
165 ws::IWindow *wnd = dpy->create_window();
166 MTEST_ASSERT(wnd != NULL);
167 lsp_finally {
168 wnd->destroy();
169 delete wnd;
170 };
171 MTEST_ASSERT(wnd->init() == STATUS_OK);
172 MTEST_ASSERT(wnd->set_caption("Test events") == STATUS_OK);
173 MTEST_ASSERT(wnd->resize(320, 200) == STATUS_OK);
174 MTEST_ASSERT(wnd->set_window_actions(ws::WA_ALL) == STATUS_OK);
175 MTEST_ASSERT(wnd->set_size_constraints(160, 100, 640, 400) == STATUS_OK);
176
177 Handler h(this, wnd);
178 wnd->set_handler(&h);
179
180 MTEST_ASSERT(wnd->show() == STATUS_OK);
181 MTEST_ASSERT(dpy->main() == STATUS_OK);
182 }
183
184 MTEST_END
185
186
187
188
189