-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_wm.cpp
More file actions
276 lines (228 loc) · 9.22 KB
/
Copy pathexample_wm.cpp
File metadata and controls
276 lines (228 loc) · 9.22 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// example_wm.cpp
// Example: Embedding Havel in a window manager
#include "../include/Havel.hpp"
#include <iostream>
#include <string>
#include <map>
// ============================================================================
// Window Manager state
// ============================================================================
struct WMWindow {
int id;
std::string title;
std::string appClass;
int x = 0, y = 0;
int width = 800, height = 600;
bool focused = false;
bool floating = false;
};
struct WMState {
std::map<int, WMWindow> windows;
int focusedWindowId = -1;
int currentWorkspace = 1;
std::string currentMode = "default";
havel::VM vm;
WMState() {
initVM();
}
void initVM() {
vm.setHostContext(this);
registerFunctions();
loadScripts();
}
void registerFunctions() {
// Window management
vm.registerFn("getWindows", [this](havel::VM& vm, auto& args) {
// Return list of window IDs
vm.load("_wins = []");
auto wins = vm.getGlobal("_wins");
havel::Array arr(wins);
for (const auto& [id, win] : windows) {
arr.push(havel::Value(static_cast<double>(id)));
}
return wins;
});
vm.registerFn("getFocusedWindow", [this](havel::VM& vm, auto& args) {
if (focusedWindowId < 0) return havel::Value();
auto it = windows.find(focusedWindowId);
if (it == windows.end()) return havel::Value();
// Return window info as object
std::ostringstream oss;
oss << "_win = {id = " << it->second.id
<< ", title = \"" << it->second.title << "\""
<< ", class = \"" << it->second.appClass << "\""
<< ", x = " << it->second.x
<< ", y = " << it->second.y
<< ", width = " << it->second.width
<< ", height = " << it->second.height
<< ", floating = " << (it->second.floating ? "true" : "false")
<< "}";
vm.load(oss.str());
return vm.getGlobal("_win");
});
vm.registerFn("focusWindow", [this](havel::VM& vm, auto& args) {
if (args.empty()) return havel::Value();
int id = static_cast<int>(args[0].asNumber());
if (windows.find(id) != windows.end()) {
focusedWindowId = id;
for (auto& [wid, win] : windows) {
win.focused = (wid == id);
}
std::cout << "Focused window " << id << std::endl;
}
return havel::Value();
});
vm.registerFn("closeWindow", [this](havel::VM& vm, auto& args) {
if (args.empty()) return havel::Value();
int id = static_cast<int>(args[0].asNumber());
windows.erase(id);
std::cout << "Closed window " << id << std::endl;
return havel::Value();
});
vm.registerFn("moveWindow", [this](havel::VM& vm, auto& args) {
if (args.size() < 3) return havel::Value();
int id = static_cast<int>(args[0].asNumber());
if (windows.find(id) != windows.end()) {
windows[id].x = static_cast<int>(args[1].asNumber());
windows[id].y = static_cast<int>(args[2].asNumber());
}
return havel::Value();
});
vm.registerFn("resizeWindow", [this](havel::VM& vm, auto& args) {
if (args.size() < 3) return havel::Value();
int id = static_cast<int>(args[0].asNumber());
if (windows.find(id) != windows.end()) {
windows[id].width = static_cast<int>(args[1].asNumber());
windows[id].height = static_cast<int>(args[2].asNumber());
}
return havel::Value();
});
vm.registerFn("toggleFloating", [this](havel::VM& vm, auto& args) {
if (focusedWindowId < 0) return havel::Value();
auto it = windows.find(focusedWindowId);
if (it != windows.end()) {
it->second.floating = !it->second.floating;
std::cout << "Window " << focusedWindowId
<< " floating: " << it->second.floating << std::endl;
}
return havel::Value();
});
// Workspace management
vm.registerFn("getWorkspace", [this](havel::VM& vm, auto& args) {
return havel::Value(static_cast<double>(currentWorkspace));
});
vm.registerFn("setWorkspace", [this](havel::VM& vm, auto& args) {
if (!args.empty()) {
currentWorkspace = static_cast<int>(args[0].asNumber());
std::cout << "Switched to workspace " << currentWorkspace << std::endl;
}
return havel::Value();
});
// Mode management
vm.registerFn("getMode", [this](havel::VM& vm, auto& args) {
return havel::Value(currentMode);
});
vm.registerFn("setMode", [this](havel::VM& vm, auto& args) {
if (!args.empty()) {
currentMode = args[0].asString();
std::cout << "Mode: " << currentMode << std::endl;
}
return havel::Value();
});
// Utility
vm.registerFn("print", [this](havel::VM& vm, auto& args) {
for (const auto& arg : args) {
std::cout << arg.toString() << " ";
}
std::cout << std::endl;
return havel::Value();
});
vm.registerFn("spawn", [this](havel::VM& vm, auto& args) {
if (args.empty()) return havel::Value();
std::string cmd = args[0].asString();
std::cout << "Spawning: " << cmd << std::endl;
// In real WM: system(cmd.c_str()) or fork/exec
return havel::Value();
});
}
void loadScripts() {
// Load WM configuration scripts
vm.load(R"(
// Keybindings
fn onModEnter() {
print("Mod+Enter pressed")
spawn("alacritty")
}
fn onModQ() {
print("Mod+Q pressed - closing window")
let win = getFocusedWindow()
if (win) {
closeWindow(win.id)
}
}
fn onModF() {
print("Mod+F pressed - toggling floating")
toggleFloating()
}
fn onModJ() {
print("Mod+J - focus next")
// Focus next window logic
}
fn onModK() {
print("Mod+K - focus previous")
// Focus previous window logic
}
// Window rules
fn onWindowCreated(win) {
print("Window created: " + win.title)
// Example: float certain windows
if (win.class == "floatme") {
toggleFloating()
}
}
)");
}
void handleKeyPress(const std::string& keyCombo) {
// Call appropriate handler based on key combo
std::string handler;
if (keyCombo == "Mod+Enter") handler = "onModEnter";
else if (keyCombo == "Mod+q") handler = "onModQ";
else if (keyCombo == "Mod+f") handler = "onModF";
else if (keyCombo == "Mod+j") handler = "onModJ";
else if (keyCombo == "Mod+k") handler = "onModK";
if (!handler.empty()) {
auto result = vm.call(handler);
if (!result) {
std::cerr << "Script error: " << result.error << std::endl;
}
}
}
void onWindowCreated(int id, const std::string& title, const std::string& appClass) {
windows[id] = WMWindow{id, title, appClass};
// Notify script
vm.load("_newWin = {id = " + std::to_string(id) +
", title = \"" + title + "\"" +
", class = \"" + appClass + "\"}");
vm.call("onWindowCreated", {vm.getGlobal("_newWin")});
}
};
// ============================================================================
// Main
// ============================================================================
int main() {
std::cout << "=== Havel Window Manager Integration Example ===" << std::endl;
WMState wm;
// Simulate window creation
wm.onWindowCreated(1, "Alacritty", "Terminal");
wm.onWindowCreated(2, "Firefox", "Browser");
wm.onWindowCreated(3, "VSCode", "Editor");
// Focus first window
wm.handleKeyPress("Mod+j");
// Simulate key presses
std::cout << "\n--- Simulating key presses ---" << std::endl;
wm.handleKeyPress("Mod+Enter"); // Spawn terminal
wm.handleKeyPress("Mod+q"); // Close window
wm.handleKeyPress("Mod+f"); // Toggle floating
std::cout << "\n=== WM Example Complete ===" << std::endl;
return 0;
}