Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2026 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2026 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-ws-lib
6 * Created on: 11 апр. 2026 г.
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/ws/factory.h>
23 #include <lsp-plug.in/ws/IEventHandler.h>
24 #include <lsp-plug.in/test-fw/mtest.h>
25
26 MTEST_BEGIN("ws.display", multimain)
27
28 class Handler: public ws::IEventHandler
29 {
30 private:
31 test_type_t *pTest;
32 ws::IWindow *pWnd;
33 Color sColor;
34
35 public:
36 inline Handler(test_type_t *test, ws::IWindow *wnd)
37 {
38 pTest = test;
39 pWnd = wnd;
40 }
41
42 inline Color &color()
43 {
44 return sColor;
45 }
46
47 virtual status_t handle_event(const ws::event_t *ev)
48 {
49 switch (ev->nType)
50 {
51 case ws::UIE_MOUSE_CLICK:
52 pTest->printf("CLICK\n");
53 break;
54
55 case ws::UIE_MOUSE_DBL_CLICK:
56 pTest->printf("DBL_CLICK\n");
57 break;
58
59 case ws::UIE_MOUSE_TRI_CLICK:
60 pTest->printf("TRI_CLICK\n");
61 break;
62
63 case ws::UIE_REDRAW:
64 {
65 ws::ISurface *s = pWnd->get_surface();
66 if (s == NULL)
67 return STATUS_OK;
68
69 // Perform drawing
70 s->begin();
71 s->clear(sColor);
72 s->end();
73
74 return STATUS_OK;
75 }
76
77 case ws::UIE_MOUSE_MOVE:
78 {
79 size_t screen;
80 ssize_t left, top;
81
82 if (pWnd->display()->get_pointer_location(&screen, &left, &top) == STATUS_OK)
83 pTest->printf("Pointer location: local=(%d, %d), screen=(%d, %d, %d)\n",
84 int(ev->nLeft), int(ev->nTop), int(left), int(top), int(screen));
85 return STATUS_OK;
86 }
87
88 case ws::UIE_SHOW:
89 {
90 pTest->printf("Received UIE_SHOW event\n");
91 break;
92 }
93
94 case ws::UIE_HIDE:
95 {
96 pTest->printf("Received UIE_HIDE event\n");
97 break;
98 }
99
100 case ws::UIE_CLOSE:
101 {
102 pWnd->hide();
103 pWnd->display()->quit_main();
104 break;
105 }
106
107 case ws::UIE_STATE:
108 {
109 pTest->printf("Current window state is: %d\n", int(ev->nCode));
110 break;
111 }
112
113 default:
114 return IEventHandler::handle_event(ev);
115 }
116
117 return STATUS_OK;
118 }
119 };
120
121 MTEST_MAIN
122 {
123 ws::IDisplay *dpy = ws::create_display(0, NULL);
124 MTEST_ASSERT(dpy != NULL);
125 lsp_finally { ws::free_display(dpy); };
126
127 // Create window and do the stuff
128 ws::IWindow *wnd = dpy->create_window();
129 MTEST_ASSERT(wnd != NULL);
130 lsp_finally {
131 wnd->destroy();
132 delete wnd;
133 };
134 MTEST_ASSERT(wnd->init() == STATUS_OK);
135
136 LSPString dst;
137 MTEST_ASSERT(wnd->set_caption("Test multiple main") == STATUS_OK);
138 MTEST_ASSERT(wnd->set_border_style(ws::BS_SIZEABLE) == STATUS_OK);
139 MTEST_ASSERT(wnd->set_window_actions(ws::WA_MOVE | ws::WA_RESIZE | ws::WA_CLOSE | ws::WA_MAXIMIZE | ws::WA_MINIMIZE) == STATUS_OK);
140
141 MTEST_ASSERT(wnd->resize(320, 200) == STATUS_OK);
142 MTEST_ASSERT(wnd->set_size_constraints(160, 100, 640, 400) == STATUS_OK);
143
144 size_t screen = wnd->screen();
145 ssize_t sw, sh;
146 ws::rectangle_t wr;
147 MTEST_ASSERT(dpy->screen_size(screen, &sw, &sh) == STATUS_OK);
148 MTEST_ASSERT(wnd->get_absolute_geometry(&wr) == STATUS_OK);
149 wnd->move((sw - wr.nWidth) / 2, (sh - wr.nHeight) / 2);
150
151 Handler h(this, wnd);
152 wnd->set_handler(&h);
153
154 // Do the first launch
155 printf("Starting first main loop...\n");
156 h.color().set_rgb(0.0f, 0.75f, 0.0f);
157 MTEST_ASSERT(wnd->show() == STATUS_OK);
158 MTEST_ASSERT(dpy->main() == STATUS_OK);
159 MTEST_ASSERT(dpy->process_pending_events() == STATUS_OK);
160
161 // Do the second launch
162 printf("Starting second main loop...\n");
163 h.color().set_rgb(1.0f, 1.0f, 0.0f);
164 MTEST_ASSERT(wnd->show() == STATUS_OK);
165 MTEST_ASSERT(dpy->main() == STATUS_OK);
166 }
167
168 MTEST_END
169
170
171