ui/winui/toolkit.cpp

Sun, 10 Nov 2024 15:30:46 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 10 Nov 2024 15:30:46 +0100
changeset 76
641dcc79e0ef
parent 5
83263002816f
child 77
5de33c2d94c6
permissions
-rw-r--r--

update toolkit

0
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2023 Olaf Wintermann. All rights reserved.
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include "pch.h"
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include "toolkit.h"
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include <cx/allocator.h>
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include <cx/mempool.h>
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 #include "../common/context.h"
3
f154867f54dc add nav buttons, update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
37 #include "../common/document.h"
0
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 #include "../common/toolbar.h"
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 #include "icons.h"
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 #include "MainWindow.xaml.h"
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 #include "App.xaml.h"
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 using namespace winrt;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 using namespace Microsoft::UI::Xaml;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 using namespace Microsoft::UI::Xaml::Controls;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 using namespace Microsoft::UI::Xaml::XamlTypeInfo;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 using namespace Microsoft::UI::Xaml::Markup;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 using namespace Windows::UI::Xaml::Interop;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 using namespace winrt::Windows::Foundation;
5
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
53 using namespace Windows::UI::Core;
0
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 static const char* application_name;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 static ui_callback startup_func;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 static void* startup_data;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 static ui_callback open_func;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 void* open_data;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 static ui_callback exit_func;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 void* exit_data;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 static ui_callback appclose_fnc;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 static void* appclose_udata;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 static UiObject* active_window;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72
5
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
73 static winrt::Microsoft::UI::Dispatching::DispatcherQueue uiDispatcherQueue = { nullptr };
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
74
0
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 void ui_app_run_startup() {
5
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
76 uiDispatcherQueue = winrt::Microsoft::UI::Dispatching::DispatcherQueue::GetForCurrentThread();
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
77
0
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 if (startup_func) {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 startup_func(NULL, startup_data);
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 class App : public ApplicationT<App, IXamlMetadataProvider> {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 public:
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 void OnLaunched(LaunchActivatedEventArgs const&) {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 Resources().MergedDictionaries().Append(XamlControlsResources());
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 if (startup_func) {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 startup_func(NULL, startup_data);
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 //auto window = make<winui::implementation::MainWindow>();
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 //window.Activate();
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 IXamlType GetXamlType(TypeName const& type) {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 return provider.GetXamlType(type);
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 IXamlType GetXamlType(hstring const& fullname) {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 return provider.GetXamlType(fullname);
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 com_array<XmlnsDefinition> GetXmlnsDefinitions() {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 return provider.GetXmlnsDefinitions();
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 private:
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 XamlControlsXamlMetaDataProvider provider;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 };
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 UiWidget::UiWidget(winrt::Microsoft::UI::Xaml::UIElement& elm) : uielement(elm) {}
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 extern "C" void destroy_ui_window_wrapper(void* ptr) {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 UiWindow* win = (UiWindow*)ptr;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 delete win;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 extern "C" void destroy_ui_widget_wrapper(void* ptr) {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 UiWidget* widget = (UiWidget*)ptr;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 delete widget;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 extern "C" void destroy_ui_container_wrapper(void* ptr) {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 UiContainer* ctn = (UiContainer*)ptr;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 delete ctn;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 void ui_context_add_window_destructor(UiContext* ctx, UiWindow* win) {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 cxMempoolRegister(ctx->mp, win, destroy_ui_window_wrapper);
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 void ui_context_add_widget_destructor(UiContext* ctx, UiWidget* widget) {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 cxMempoolRegister(ctx->mp, widget, destroy_ui_widget_wrapper);
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 void ui_context_add_container_destructor(UiContext* ctx, UiContainer *container) {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 cxMempoolRegister(ctx->mp, container, destroy_ui_container_wrapper);
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 UiEvent ui_create_int_event(UiObject* obj, int64_t i) {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 UiEvent evt;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 evt.obj = obj;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 evt.window = obj->window;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 evt.document = obj->ctx->document;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 evt.eventdata = nullptr;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 evt.intval = i;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 return evt;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 #include <MddBootstrap.h>
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 void ui_appsdk_bootstrap(void) {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 const UINT32 majorMinorVersion{ 0x00010002 };
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 PCWSTR versionTag{ L"" };
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 const PACKAGE_VERSION minVersion{};
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 const HRESULT hr = MddBootstrapInitialize(majorMinorVersion, versionTag, minVersion);
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 if (FAILED(hr)) {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 exit(102);
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 void ui_init(const char* appname, int argc, char** argv) {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 application_name = appname;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 //ui_appsdk_bootstrap();
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165
3
f154867f54dc add nav buttons, update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
166 uic_init_global_context();
f154867f54dc add nav buttons, update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 0
diff changeset
167 uic_docmgr_init();
0
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 uic_toolbar_init();
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 const char* ui_appname() {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 return application_name;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 void ui_onstartup(ui_callback f, void* userdata) {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 startup_func = f;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 startup_data = userdata;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 void ui_onopen(ui_callback f, void* userdata) {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 open_func = f;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 open_data = userdata;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 void ui_onexit(ui_callback f, void* userdata) {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 exit_func = f;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 exit_data = userdata;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 void ui_main() {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 /*
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 init_apartment();
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 //Application::Start([](auto&&) {make<App>(); });
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 ::winrt::Microsoft::UI::Xaml::Application::Start(
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 [](auto&&)
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 ::winrt::make<::winrt::winui::implementation::App>();
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 });
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 */
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 void (WINAPI * pfnXamlCheckProcessRequirements)();
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 auto module = ::LoadLibrary(L"Microsoft.ui.xaml.dll");
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 if (module)
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 pfnXamlCheckProcessRequirements = reinterpret_cast<decltype(pfnXamlCheckProcessRequirements)>(GetProcAddress(module, "XamlCheckProcessRequirements"));
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 if (pfnXamlCheckProcessRequirements)
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 (*pfnXamlCheckProcessRequirements)();
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 ::FreeLibrary(module);
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 winrt::init_apartment(winrt::apartment_type::single_threaded);
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 ::winrt::Microsoft::UI::Xaml::Application::Start(
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 [](auto&&)
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 ::winrt::make<::winrt::winui::implementation::App>();
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 });
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 class UiWin {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 public:
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 Window window;
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 };
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229 void ui_show(UiObject* obj) {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230 if (obj->wobj) {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 obj->wobj->window.Activate();
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 } else {
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 // TODO
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 }
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236
2483f517c562 add existing toolkit code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 void ui_close(UiObject* obj) {
76
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
238 if (obj->wobj) {
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
239 obj->wobj->window.Close();
641dcc79e0ef update toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 5
diff changeset
240 }
5
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
241 }
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
242
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
243 static void ui_job_thread(UiJob* job) {
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
244 if (!job->job_func(job->job_data) && job->finish_callback) {
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
245 bool isQueued = uiDispatcherQueue.TryEnqueue([job]()
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
246 {
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
247 UiEvent event;
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
248 event.obj = job->obj;
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
249 event.window = job->obj->window;
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
250 event.document = job->obj->ctx->document;
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
251 event.intval = 0;
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
252 event.eventdata = NULL;
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
253 job->finish_callback(&event, job->finish_data);
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
254 delete job;
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
255 });
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
256 if (!isQueued) {
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
257 // TODO: error or try again?
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
258 exit(-1);
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
259 }
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
260 }
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
261 else {
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
262 delete job;
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
263 }
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
264 }
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
265
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
266 UIEXPORT void ui_job(UiObject* obj, ui_threadfunc tf, void* td, ui_callback f, void* fd) {
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
267 UiJob* job = new UiJob;
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
268 job->obj = obj;
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
269 job->job_func = tf;
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
270 job->job_data = td;
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
271 job->finish_callback = f;
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
272 job->finish_data = fd;
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
273
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
274 std::thread jobThread(ui_job_thread, job);
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
275 jobThread.detach();
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
276 }
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
277
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
278 UIEXPORT void ui_call_mainthread(ui_threadfunc tf, void* td) {
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
279 bool isQueued = uiDispatcherQueue.TryEnqueue([tf, td]()
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
280 {
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
281 (void)tf(td);
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
282 });
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
283 if (!isQueued) {
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
284 // TODO: error or try again?
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
285 exit(-1);
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
286 }
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
287 }
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
288
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
289 static UiJob kill_job; // &kill_job indicates to stop the thread
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
290
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
291 static void ui_threadpool_run(UiThreadpool* pool) {
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
292 for (;;) {
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
293 UiJob* job = pool->GetJob();
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
294 if (job == &kill_job) {
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
295 return;
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
296 }
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
297 else if (job) {
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
298 ui_job_thread(job);
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
299 }
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
300 }
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
301 }
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
302
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
303 UiThreadpool::UiThreadpool(int nthreads) {
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
304 for (int i = 0; i < nthreads; i++) {
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
305 std::thread thread(ui_threadpool_run, this);
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
306 thread.detach();
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
307 }
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
308 }
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
309
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
310 void UiThreadpool::EnqueueJob(UiJob* job)
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
311 {
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
312 std::unique_lock<std::mutex> lock(mutex);
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
313 queue.push(job);
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
314 lock.unlock();
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
315 condition.notify_one();
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
316 }
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
317
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
318 UiJob* UiThreadpool::GetJob() {
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
319 std::unique_lock<std::mutex> lock(mutex);
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
320
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
321 UiJob* job = nullptr;
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
322 while (!job) {
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
323 if (queue.empty()) {
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
324 condition.wait(lock);
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
325 continue;
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
326 }
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
327 else
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
328 {
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
329 job = queue.front();
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
330 queue.pop();
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
331 }
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
332 }
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
333
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
334 return job;
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
335 }
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
336
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
337 UIEXPORT UiThreadpool* ui_threadpool_create(int nthreads) {
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
338 return new UiThreadpool(nthreads);
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
339 }
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
340
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
341 UIEXPORT void ui_threadpool_destroy(UiThreadpool* pool) {
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
342 // TODO
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
343 }
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
344
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
345 UIEXPORT void ui_threadpool_job(UiThreadpool* pool, UiObject* obj, ui_threadfunc tf, void* td, ui_callback f, void* fd) {
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
346 UiJob* job = new UiJob;
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
347 job->obj = obj;
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
348 job->job_func = tf;
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
349 job->job_data = td;
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
350 job->finish_callback = f;
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
351 job->finish_data = fd;
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
352 pool->EnqueueJob(job);
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
353 }
83263002816f update toolkit (job/threadpool)
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 3
diff changeset
354

mercurial