-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwm.c
596 lines (502 loc) · 19.4 KB
/
wm.c
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
#include "wm.h"
#include "buffer.h"
#include "faces.h"
#include "globals.h"
#include <stdlib.h>
#include <string.h>
// TODO Make sure that buffer->displayWindows is ALWAYS updated
// By doing it in core functions that every other functionin this module will use.
// TODO i want it to mange X11 windows aswell add it to lume lazy fuck
// TODO Tags M-0..9
// TODO resurrect_last_killed_window()
void initWindowManager(WindowManager *wm, BufferManager *bm, Font *font, int sw, int sh) {
wm->head = malloc(sizeof(Window));
wm->head->x = 0;
wm->head->y = sh - font->ascent + font->descent; // Adjust for font height
wm->head->width = sw;
wm->head->height = wm->head->y; // Adjust height accordingly
wm->head->buffer = getActiveBuffer(bm);
wm->head->prev = NULL;
wm->head->next = NULL;
wm->head->isActive = true;
wm->head->modeline.height = 25.0;
wm->activeWindow = wm->head;
wm->activeWindow->splitOrientation = VERTICAL;
wm->head->scroll = (Vec2f){0, 0};
wm->count = 1;
wm->head->modeline.window = wm->head;
wm->graveyard = NULL;
wm->head->leftPadding = 0;
wm->head->parameters = (WindowParameters){0};
wm->head->parameters.noOtherWindow = false;
if (global_minimap) {
wm->head->parameters.minimap = true;
wm->head->parameters.minimap_target_width = 110.0f; // Set target width
if (lerp_minimap_on_startup) {
wm->head->parameters.minimap_width = 0.0f;
} else {
wm->head->parameters.minimap_width = 110.0f;
}
wm->head->parameters.minimap_lerp_active = true; // Start lerping
} else {
wm->head->parameters.minimap = false;
}
if (global_visual_line_mode) {
wm->head->parameters.truncateLines = false;
} else {
wm->head->parameters.truncateLines = true;
}
wm->head->parameters.scrollBar = true;
addDisplayWindowToBuffer(wm->head->buffer, wm->head);
}
#include "edit.h"
void split_window_right(WindowManager *wm, WindowParameters *parameters) {
Window *active = wm->activeWindow;
Window *newWindow = malloc(sizeof(Window));
*newWindow = *active; // Copy settings from active window
newWindow->width /= 2;
active->width -= newWindow->width;
newWindow->x += active->width;
active->splitOrientation = VERTICAL;
newWindow->splitOrientation = VERTICAL;
/* newWindow->parameters.noOtherWindow = parameters->noOtherWindow; // FIX */
newWindow->parameters.noOtherWindow = false; // FIX
// Insert new window into the list
newWindow->next = active->next;
if (active->next) {
active->next->prev = newWindow;
}
active->next = newWindow;
newWindow->prev = active;
// NOTE Update window-buffer (displayWindows) and window-modeline relationships
newWindow->modeline.window = newWindow;
addDisplayWindowToBuffer(newWindow->buffer, newWindow);
// Check if the active buffer is a .c or .h file and try to open the
// corresponding file
if (active->buffer && active->buffer->path) {
const char *current_path = active->buffer->path;
size_t len = strlen(current_path);
char *target_path = NULL;
// Check if the current file is a .c file
if (len >= 2 && strcmp(current_path + len - 2, ".c") == 0) {
// Construct .h path
target_path = malloc(len + 1);
if (target_path) {
strcpy(target_path, current_path);
strcpy(target_path + len - 2, ".h"); // Replace .c with .h
}
}
// Check if the current file is a .h file
else if (len >= 2 && strcmp(current_path + len - 2, ".h") == 0) {
// Construct .c path
target_path = malloc(len + 1);
if (target_path) {
strcpy(target_path, current_path);
strcpy(target_path + len - 2, ".c"); // Replace .h with .c
}
}
if (target_path) {
// Temporarily store the original buffer
Buffer *original_buffer = active->buffer;
// Insert the target path into the minibuffer and call find_file
Buffer *minibuffer = getBuffer(&bm, "minibuffer");
if (minibuffer) {
setBufferContent(minibuffer, target_path, true);
find_file(&bm, wm);
// After find_file runs, the new buffer will be created and active
Buffer *target_buffer = getActiveBuffer(&bm);
if (target_buffer) {
// Assign the target buffer to the new window
newWindow->buffer = target_buffer;
recenter(newWindow, true);
// Restore the original buffer to the active window
active->buffer = original_buffer;
// Switch the active window back to the original window
wm->activeWindow = active;
active->isActive = true;
newWindow->isActive = false;
}
}
free(target_path);
}
}
wm->count++;
}
// Function to update buffer associations when a window changes buffers
void updateWindowBufferAssociation(Window *window, Buffer *oldBuffer,
Buffer *newBuffer) {
if (oldBuffer == newBuffer)
return;
if (oldBuffer) {
removeDisplayWindowFromBuffer(oldBuffer, window);
}
if (newBuffer) {
addDisplayWindowToBuffer(newBuffer, window);
}
}
void switch_or_split_window(WindowManager *wm, char *buffer_name, WindowParameters *parameters) {
// Check if a window already displays the buffer
Window *current = wm->head;
while (current != NULL) {
if (current->buffer && strcmp(current->buffer->name, buffer_name) == 0) {
// Switch focus to the window that already displays the buffer
wm->activeWindow->isActive = false;
wm->activeWindow = current;
current->isActive = true;
return;
}
current = current->next;
}
// If no window displays the buffer, split the window
split_window_right(wm, parameters);
other_window(wm, 1);
recenter(wm->activeWindow, true);
// Check if the buffer exists in the BufferManager
Buffer *existing = getBuffer(&bm, buffer_name);
if (existing) {
// Assign existing buffer to the new window and switch
wm->activeWindow->buffer = existing;
switchToBuffer(&bm, buffer_name);
} else {
// Create and switch to the new buffer
char *fontPath = wm->activeWindow->buffer->fontPath;
newBuffer(&bm, wm, buffer_name, "~/", fontPath);
switchToBuffer(&bm, buffer_name);
}
}
void split_window_below(WindowManager *wm, WindowParameters *parameters) {
Window *active = wm->activeWindow;
Window *newWindow = malloc(sizeof(Window));
if (!newWindow) return;
*newWindow = *active; // Copy settings from active window
newWindow->height = active->height / 2;
active->height = newWindow->height; // Make active window also half its original height
// New window should be positioned directly below the active window
newWindow->y = active->y - newWindow->height; // Subtract height because y increases upward
active->splitOrientation = HORIZONTAL; // Set split orientation
newWindow->splitOrientation = HORIZONTAL; // Set split orientation
newWindow->parameters.noOtherWindow = ¶meters->noOtherWindow;
// Insert the new window into the list
newWindow->next = active->next;
if (active->next) {
active->next->prev = newWindow;
}
// NOTE Update window-buffer (displayWindow) and window-modeline relationships
newWindow->modeline.window = newWindow;
addDisplayWindowToBuffer(newWindow->buffer, newWindow);
active->next = newWindow;
newWindow->prev = active;
wm->count++;
}
void redistribute_window_space(WindowManager *wm, Window *win) {
if (!win) return;
// Adjust size of adjacent windows based on the orientation
if (win->splitOrientation == HORIZONTAL) {
// Combine heights if the split was horizontal
if (win->prev) {
win->prev->height += win->height;
} else if (win->next) {
win->next->y = win->y;
win->next->height += win->height;
}
} else if (win->splitOrientation == VERTICAL) {
// Combine widths if the split was vertical
if (win->prev) {
win->prev->width += win->width;
} else if (win->next) {
win->next->x = win->x;
win->next->width += win->width;
}
}
// Refresh the layout of remaining windows
if (wm->head) {
updateWindowDimensions(wm->head, 0, wm->head->y, wm->head->width, wm->head->height);
}
}
void delete_window(WindowManager *wm) {
if (wm->count <= 1) return; // Cannot delete the last window we could go into dashboard
Window *active = wm->activeWindow;
// Remove this window from its buffer's display list
removeDisplayWindowFromBuffer(active->buffer, active);
// Reassign active window
if (active->prev) {
wm->activeWindow = active->prev;
} else if (active->next) {
wm->activeWindow = active->next;
}
// Remove the window from the list
if (active->prev) {
active->prev->next = active->next;
} else {
wm->head = active->next; // Update head if the first window is being deleted
}
if (active->next) {
active->next->prev = active->prev;
}
// Adjust size of adjacent windows based on the orientation
if (active->splitOrientation == HORIZONTAL) {
// Combine heights if the split was horizontal
if (active->prev) {
active->prev->height += active->height;
} else if (active->next) {
active->next->y = active->y;
active->next->height += active->height;
}
} else if (active->splitOrientation == VERTICAL) {
// Combine widths if the split was vertical
if (active->prev) {
active->prev->width += active->width;
} else if (active->next) {
active->next->x = active->x;
active->next->width += active->width;
}
}
free(active);
wm->count--;
}
// TODO kill_other_windows()
void kill_window(WindowManager *wm, Window *win) {
if (wm->count <= 1) return; // Cannot kill the last window
// Remove from active window list
if (win->prev) {
win->prev->next = win->next;
} else {
wm->head = win->next; // Update head if this was the first window
}
if (win->next) {
win->next->prev = win->prev;
}
// Update active window if necessary
if (wm->activeWindow == win) {
wm->activeWindow = (win->next ? win->next : win->prev);
if (wm->activeWindow) {
wm->activeWindow->isActive = true;
}
}
// Clear window's list pointers
win->prev = NULL;
win->next = NULL;
// Add to graveyard (inactive hashmap)
WindowMap *entry = malloc(sizeof(WindowMap));
if (!entry) {
fprintf(stderr, "kill_window: Failed to allocate memory for hashmap entry\n");
return;
}
entry->key = strdup(win->buffer->name);
if (!entry->key) {
free(entry);
fprintf(stderr, "kill_window: Failed to duplicate buffer name\n");
return;
}
entry->window = win;
HASH_ADD_KEYPTR(hh, wm->graveyard, entry->key, strlen(entry->key), entry);
// Remove from buffer's display windows
removeDisplayWindowFromBuffer(win->buffer, win);
// UPDATE wm->lastKilledBufferName
if (wm->lastKilledBufferName) {
free(wm->lastKilledBufferName);
}
wm->lastKilledBufferName = strdup(win->buffer->name);
// Redistribute the space of the killed window
redistribute_window_space(wm, win);
// Update counts
wm->count--;
wm->inactive_count++;
}
Window* resurrect_window(WindowManager *wm, const char *buffer_name) {
// Find the window in the graveyard (inactive hashmap)
WindowMap *entry;
HASH_FIND_STR(wm->graveyard, buffer_name, entry);
if (!entry) {
fprintf(stderr, "resurrect_window: No inactive window found for buffer '%s'\n", buffer_name);
return NULL;
}
Window *win = entry->window;
// Remove from graveyard
HASH_DEL(wm->graveyard, entry);
free(entry->key);
free(entry);
// Add to active list
win->next = wm->head;
if (wm->head) {
wm->head->prev = win;
}
wm->head = win;
// Add to buffer's display windows
addDisplayWindowToBuffer(win->buffer, win);
// Update counts
wm->count++;
wm->inactive_count--;
return win;
}
// TODO Support NoOtherWindow Window parameter.
void other_window(WindowManager *wm, int direction) {
if (direction == 1) {
if (wm->activeWindow->next) {
wm->activeWindow->isActive = false;
wm->activeWindow = wm->activeWindow->next;
wm->activeWindow->isActive = true;
} else if (wm->head) {
wm->activeWindow->isActive = false;
wm->activeWindow = wm->head;
wm->activeWindow->isActive = true;
}
} else if (direction == -1) {
Window *current = wm->head;
if (current == wm->activeWindow) {
while (current->next) {
current = current->next;
}
wm->activeWindow->isActive = false;
wm->activeWindow = current;
wm->activeWindow->isActive = true;
} else {
while (current->next != wm->activeWindow) {
current = current->next;
}
wm->activeWindow->isActive = false;
wm->activeWindow = current;
wm->activeWindow->isActive = true;
}
}
}
void swap_windows(WindowManager *wm, int direction) {
Window *tw = NULL; // Target Window
if (direction == 1) {
tw = wm->activeWindow->next ? wm->activeWindow->next : wm->head;
} else {
Window *current = wm->head;
if (current == wm->activeWindow) {
while (current->next) {
current = current->next; // Go to the last window if current is head
}
tw = current;
} else {
while (current->next != wm->activeWindow) {
current = current->next;
}
tw = current; // Set the target to the previous window
}
}
if (tw) {
// Swap buffers
Buffer *tempBuffer = wm->activeWindow->buffer;
wm->activeWindow->buffer = tw->buffer;
tw->buffer = tempBuffer;
// Swap window parameters if the global flag is set
if (swap_windows_parameters) {
WindowParameters tempParams = wm->activeWindow->parameters;
wm->activeWindow->parameters = tw->parameters;
tw->parameters = tempParams;
}
other_window(wm, direction);
}
}
// TODO void set_window_parameter(Window *window, WindowParameters parameters, bool value) {}
void freeWindowManager(WindowManager *wm) {
WindowMap *curr, *tmp;
// Free graveyard
HASH_ITER(hh, wm->graveyard, curr, tmp) {
HASH_DEL(wm->graveyard, curr);
free(curr->key);
free(curr);
}
Window *current = wm->head;
while (current != NULL) {
Window *next = current->next;
free(current);
current = next;
}
wm->head = NULL;
wm->activeWindow = NULL;
wm->count = 0;
}
void updateWindowDimensions(Window *win, int x, int y, int width, int height) {
if (win == NULL) return;
// Set the current window dimensions
win->x = x;
win->y = y;
win->width = width;
win->height = height;
// If the window has a next window in the same split orientation, recurse into it
if (win->next && win->splitOrientation == win->next->splitOrientation) {
if (win->splitOrientation == HORIZONTAL) {
// Recurse with adjusted y and height
updateWindowDimensions(win->next, x, y - height / 2, width, height / 2);
} else if (win->splitOrientation == VERTICAL) {
// Recurse with adjusted x and half the width
updateWindowDimensions(win->next, x + width / 2, y, width / 2, height);
}
} else if (win->next) {
// If the orientation changes, reset dimensions based on the orientation
if (win->splitOrientation == HORIZONTAL) {
updateWindowDimensions(win->next, x, y - height / 2, width, height / 2);
} else {
updateWindowDimensions(win->next, x + width / 2, y, width / 2, height);
}
}
}
void updateWindows(WindowManager *wm, Font *font, int newWidth, int newHeight) {
// Start the recursive adjustment from the head window
if (wm->head != NULL) {
updateWindowDimensions(wm->head, 0, newHeight - (font->ascent - font->descent), newWidth, newHeight);
}
}
void printActiveWindowDetails(WindowManager *wm) {
Window *win = wm->activeWindow;
if (!win) {
printf("No active window.\n");
return;
}
// Print window details in a structured format
printf("\nActive Window Details:\n");
printf("{\n");
printf(" X: %.2f,\n", win->x);
printf(" Y: %.2f,\n", win->y);
printf(" Width: %.2f,\n", win->width);
printf(" Height: %.2f,\n", win->height);
printf(" Modeline Height: %.2f,\n", win->modeline.height);
printf(" Active: %s,\n", win->isActive ? "True" : "False");
printf(" Split Orientation: %s,\n", (win->splitOrientation == HORIZONTAL) ? "Horizontal" : "Vertical");
printf(" Scroll: { X: %.2f, Y: %.2f },\n", win->scroll.x, win->scroll.y);
printf(" Previous Window: %p,\n", (void *)win->prev);
printf(" Next Window: %p,\n", (void *)win->next);
printf(" Window Count in Manager: %d,\n", wm->count);
printf("\n");
// Print buffer details
Buffer *buf = win->buffer;
if (buf) {
printf(" Buffer: {\n");
printf(" Name: %s,\n", buf->name);
printf(" Scale: %i,\n", buf->scale.index);
printf(" Path: %s,\n", buf->path);
printf(" Size: %zu,\n", buf->size);
printf(" Capacity: %zu,\n", buf->capacity);
printf(" Point: %zu,\n", buf->point);
printf(" Region: { Start: %zu, End: %zu, Active: %s },\n", buf->region.start, buf->region.end, buf->region.active ? "True" : "False");
printf(" ReadOnly: %s,\n", buf->readOnly ? "True" : "False");
// Print syntax array details
/* printf(" SyntaxArray: {\n"); */
/* printf(" Used: %zu,\n", buf->syntaxArray.used); */
/* printf(" Size: %zu,\n", buf->syntaxArray.size); */
/* for (size_t i = 0; i < buf->syntaxArray.used; i++) { */
/* Syntax s = buf->syntaxArray.items[i]; */
/* printf(" Syntax: { Start: %zu, End: %zu, Color: { R: %u, G: %u, B: %u, A: %u } },\n", */
/* s.start, s.end, s.color.r, s.color.g, s.color.b, s.color.a); */
/* } */
/* printf(" },\n"); // Close syntax array */
// Optional: Include more buffer details as needed
printf(" },\n"); // Close buffer
}
printf("}\n"); // Close window
}
bool isBottomWindow(WindowManager *wm, Window *window) {
// Assuming vertical stacking of windows:
for (Window *current = wm->head; current != NULL; current = current->next) {
// Check if there is another window starting below the current one
if (current != window && current->y > window->y) {
return false;
}
}
return true;
}