Debian Codegraph @

modules/lsp-ws-lib/src/test/mtest/display/grab.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: 18 июл. 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", grab)
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: ts=%lld local=(%d, %d), screen=(%d, %d, %d)%s\n",
49 event, (long long)(ev->nTime), 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 {
67 snprintf(buf, sizeof(buf), " button=%d (0x%d), state=0x%x", int(ev->nCode), int(ev->nCode), int(ev->nState));
68 log_mouse_event("MOUSE_UP", ev, buf);
69
70 if (pWnd->is_grabbing_events())
71 {
72 pWnd->ungrab_events();
73 pTest->printf("Finished event grabbing\n");
74 }
75 else
76 {
77 pWnd->grab_events(ws::GRAB_MENU);
78 pTest->printf("Starting event grabbing\n");
79 }
80 pWnd->invalidate();
81
82 return STATUS_OK;
83 }
84 case ws::UIE_MOUSE_SCROLL:
85 snprintf(buf, sizeof(buf), " direction=%d, state=0x%x", int(ev->nCode), int(ev->nState));
86 return log_mouse_event("MOUSE_SCROLL", ev, buf);
87 case ws::UIE_MOUSE_CLICK:
88 snprintf(buf, sizeof(buf), " button=%d", int(ev->nCode));
89 return log_mouse_event("MOUSE_CLICK", ev, buf);
90 case ws::UIE_MOUSE_DBL_CLICK:
91 snprintf(buf, sizeof(buf), " button=%d", int(ev->nCode));
92 return log_mouse_event("MOUSE_DBL_CLICK", ev, buf);
93 case ws::UIE_MOUSE_TRI_CLICK:
94 snprintf(buf, sizeof(buf), " button=%d", int(ev->nCode));
95 return log_mouse_event("MOUSE_TRI_CLICK", ev, buf);
96 case ws::UIE_MOUSE_MOVE:
97 return log_mouse_event("MOUSE_MOVE", ev);
98 case ws::UIE_MOUSE_IN:
99 return log_mouse_event("MOUSE_IN", ev);
100 case ws::UIE_MOUSE_OUT:
101 return log_mouse_event("MOUSE_OUT", ev);
102
103 // Keyboard events
104 case ws::UIE_KEY_DOWN:
105 pTest->printf("KEY_DOWN: ts=%lld code=%d (0x%x), raw=%d (0x%x), state=0x%x\n",
106 (long long)(ev->nTime), int(ev->nCode), int(ev->nCode), int(ev->nRawCode), int(ev->nRawCode), int(ev->nState));
107 break;
108 case ws::UIE_KEY_UP:
109 pTest->printf("KEY_UP: ts=%lld code=%d (0x%x), raw=%d (0x%x), state=0x%x\n",
110 (long long)(ev->nTime), int(ev->nCode), int(ev->nCode), int(ev->nRawCode), int(ev->nRawCode), int(ev->nState));
111 break;
112
113 // Redraw event
114 case ws::UIE_REDRAW:
115 {
116 bool grabbing = pWnd->is_grabbing_events();
117 pTest->printf("REDRAW grabbing=%s\n", (grabbing) ? "true" : "false");
118
119 Color c;
120 c.set_rgb24((grabbing) ? 0xff2222 : 0x0088cc);
121
122 ws::ISurface *s = pWnd->get_surface();
123 if (s == NULL)
124 return STATUS_OK;
125
126 // Perform drawing
127 s->begin();
128 s->clear(c);
129 s->end();
130
131 return STATUS_OK;
132 }
133
134 // Window events
135 case ws::UIE_SIZE_REQUEST:
136 pTest->printf("SIZE_REQUEST: size=(%d, %d)\n",
137 int(ev->nWidth), int(ev->nHeight));
138 return STATUS_OK;
139 case ws::UIE_RESIZE:
140 pTest->printf("RESIZE: coord=(%d, %d), size=(%d, %d)\n",
141 int(ev->nLeft), int(ev->nTop), int(ev->nWidth), int(ev->nHeight));
142 return STATUS_OK;
143 case ws::UIE_FOCUS_IN:
144 pTest->printf("FOCUS_IN\n");
145 return STATUS_OK;
146 case ws::UIE_FOCUS_OUT:
147 pTest->printf("FOCUS_OUT\n");
148 return STATUS_OK;
149 case ws::UIE_SHOW:
150 pTest->printf("SHOW\n");
151 return STATUS_OK;
152 case ws::UIE_HIDE:
153 pTest->printf("HIDE\n");
154 return STATUS_OK;
155
156 case ws::UIE_CLOSE:
157 {
158 pTest->printf("CLOSE\n");
159
160 pWnd->hide();
161 pWnd->display()->quit_main();
162 break;
163 }
164
165 default:
166 return IEventHandler::handle_event(ev);
167 }
168
169 return STATUS_OK;
170 }
171 };
172
173 MTEST_MAIN
174 {
175 ws::IDisplay *dpy = ws::create_display(0, NULL);
176 MTEST_ASSERT(dpy != NULL);
177 lsp_finally { ws::free_display(dpy); };
178
179 io::Path font;
180 MTEST_ASSERT(font.fmt("%s/font/example.ttf", resources()));
181 MTEST_ASSERT(dpy->add_font("example", &font) == STATUS_OK);
182 MTEST_ASSERT(dpy->add_font_alias("alias", "example") == STATUS_OK);
183
184 ws::IWindow *wnd = dpy->create_window();
185 MTEST_ASSERT(wnd != NULL);
186 lsp_finally {
187 wnd->destroy();
188 delete wnd;
189 };
190 MTEST_ASSERT(wnd->init() == STATUS_OK);
191 MTEST_ASSERT(wnd->set_caption("Test grab") == STATUS_OK);
192 MTEST_ASSERT(wnd->resize(320, 200) == STATUS_OK);
193 MTEST_ASSERT(wnd->set_window_actions(ws::WA_ALL) == STATUS_OK);
194 MTEST_ASSERT(wnd->set_size_constraints(160, 100, 640, 400) == STATUS_OK);
195
196 Handler h(this, wnd);
197 wnd->set_handler(&h);
198
199 MTEST_ASSERT(wnd->show() == STATUS_OK);
200 MTEST_ASSERT(dpy->main() == STATUS_OK);
201 }
202
203 MTEST_END
204
205
206
207
208
209
210
211