-
Notifications
You must be signed in to change notification settings - Fork 2
/
WaylandServer.cpp
169 lines (137 loc) · 3.93 KB
/
WaylandServer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "WaylandServer.h"
#include "Wayland.h"
#include "HaikuShm.h"
#include "HaikuCompositor.h"
#include "HaikuSubcompositor.h"
#include "HaikuXdgShell.h"
#include "HaikuXdgSurface.h"
#include "HaikuXdgToplevel.h"
#include "HaikuOutput.h"
#include "HaikuDataDeviceManager.h"
#include "HaikuSeat.h"
#include "HaikuTextInput.h"
#include "HaikuServerDecoration.h"
#include "WaylandEnv.h"
#include <wayland-server-core.h>
#include <wayland-server-protocol.h>
#include <xdg-shell-protocol.h>
#include <stdio.h>
#include <new>
#include <Application.h>
#include <OS.h>
#include "AppKitPtrs.h"
static void Assert(bool cond) {if (!cond) abort();}
extern "C" void
wl_client_dispatch(struct wl_client *client, struct wl_closure *closure);
typedef int (*client_enqueue_proc)(void *client_display, struct wl_closure *closure);
static struct wl_display *sDisplay;
ServerHandler gServerHandler;
BMessenger gServerMessenger;
ServerHandler::ServerHandler(): BHandler("server")
{}
void ServerHandler::MessageReceived(BMessage *msg)
{
switch (msg->what) {
case closureSendMsg: {
struct wl_client *client;
msg->FindPointer("client", (void**)&client);
struct wl_closure *closure;
msg->FindPointer("closure", (void**)&closure);
wl_client_dispatch(client, closure);
return;
}
default:
break;
}
BHandler::MessageReceived(msg);
}
class Application: public BApplication {
private:
// TODO: support multiple clients
struct wl_client *fClient{};
public:
Application();
virtual ~Application() = default;
void AddClient(struct wl_client *client);
thread_id Run() override;
void Quit() override;
void MessageReceived(BMessage *msg) override;
};
Application::Application(): BApplication("application/x-vnd.Wayland-App")
{
}
void Application::AddClient(struct wl_client *client)
{
fClient = client;
}
thread_id Application::Run()
{
return BLooper::Run();
}
void Application::Quit()
{
BApplication::Quit();
exit(0);
}
void Application::MessageReceived(BMessage *msg)
{
switch (msg->what) {
case B_KEY_MAP_LOADED:
if (fClient == NULL) return;
WaylandEnv env(this);
HaikuSeatGlobal *seat = HaikuGetSeat(fClient);
if (seat == NULL) return;
seat->UpdateKeymap();
return;
}
return BApplication::MessageReceived(msg);
}
//#pragma mark - entry points
extern "C" _EXPORT int wl_ips_client_connected(void **clientOut, void *clientDisplay, client_enqueue_proc display_enqueue)
{
if (be_app == NULL) {
new Application();
be_app->Run();
}
if (gServerHandler.Looper() == NULL) {
AppKitPtrs::LockedPtr(be_app)->AddHandler(&gServerHandler);
gServerMessenger.SetTo(&gServerHandler);
}
fprintf(stderr, "wl_ips_client_connected\n");
if (sDisplay == NULL) {
sDisplay = wl_display_create();
HaikuSeatGlobal *seat {};
Assert(HaikuShmGlobal::Create(sDisplay) != NULL);
Assert(HaikuCompositorGlobal::Create(sDisplay) != NULL);
Assert(HaikuSubcompositorGlobal::Create(sDisplay) != NULL);
Assert(HaikuOutputGlobal::Create(sDisplay) != NULL);
Assert(HaikuDataDeviceManagerGlobal::Create(sDisplay) != NULL);
Assert((seat = HaikuSeatGlobal::Create(sDisplay)) != NULL);
Assert(HaikuTextInputGlobal::Create(sDisplay, seat) != NULL);
Assert(HaikuXdgShell::Create(sDisplay) != NULL);
Assert(HaikuServerDecorationManagerGlobal::Create(sDisplay) != NULL);
}
fprintf(stderr, "display: %p\n", sDisplay);
struct wl_client *client = wl_client_create_ips(sDisplay, clientDisplay, display_enqueue);
fprintf(stderr, "client: %p\n", client);
static_cast<Application*>(be_app)->AddClient(client);
*clientOut = client;
return 0;
/*
wl_client_destroy(client);
wl_display_destroy(display);
*/
}
extern "C" _EXPORT int wl_ips_closure_send(void *clientIn, struct wl_closure *closure)
{
struct wl_client *client = (struct wl_client*)clientIn;
if (true) {
BMessage msg(ServerHandler::closureSendMsg);
msg.AddPointer("client", client);
msg.AddPointer("closure", closure);
gServerMessenger.SendMessage(&msg);
} else {
wl_client_dispatch(client, closure);
}
return 0;
}