gpui-libghostty 0.2.1

Native libghostty terminal component for GPUI
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
#include <dlfcn.h>
#include <limits.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#include <ghostty.h>

typedef bool (*gpui_ghostty_make_current_cb)(void *userdata);
typedef void (*gpui_ghostty_context_cb)(void *userdata);
typedef void (*gpui_ghostty_wakeup_cb)(void *userdata);

typedef struct gpui_ghostty_surface {
    ghostty_config_t config;
    ghostty_app_t app;
    ghostty_surface_t surface;
    void *platform_userdata;
    gpui_ghostty_make_current_cb make_current;
    gpui_ghostty_context_cb clear_current;
    gpui_ghostty_context_cb swap_buffers;
    void *wakeup_userdata;
    gpui_ghostty_wakeup_cb wakeup;
    void *clipboard_request;
    ghostty_clipboard_e clipboard_location;
    char *clipboard_write;
    ghostty_clipboard_e clipboard_write_location;
    uint32_t width;
    uint32_t height;
    _Atomic bool alive;
} gpui_ghostty_surface;

static pthread_once_t ghostty_once = PTHREAD_ONCE_INIT;
static int ghostty_init_result = -1;
static void *egl_library;
static void *gl_library;
typedef void (*egl_proc)(void);
typedef egl_proc (*egl_get_proc_address_fn)(const char *name);
typedef void (*gl_pixel_store_i_fn)(unsigned int name, int value);
typedef void (*gl_read_buffer_fn)(unsigned int buffer);
typedef void (*gl_read_pixels_fn)(
    int x,
    int y,
    int width,
    int height,
    unsigned int format,
    unsigned int type,
    void *pixels
);
typedef unsigned int (*gl_get_error_fn)(void);

enum {
    GPUI_GL_BACK = 0x0405,
    GPUI_GL_PACK_ALIGNMENT = 0x0D05,
    GPUI_GL_BGRA = 0x80E1,
    GPUI_GL_UNSIGNED_BYTE = 0x1401,
};

static egl_get_proc_address_fn egl_get_proc_address;

static void initialize_ghostty(void) {
    setenv("GHOSTTY_LOG", "stderr", 0);
    ghostty_init_result = ghostty_init(0, NULL);
    egl_library = dlopen("libEGL.so.1", RTLD_LAZY | RTLD_LOCAL);
    gl_library = dlopen("libGL.so.1", RTLD_LAZY | RTLD_LOCAL);
    if (egl_library != NULL) {
        void *symbol = dlsym(egl_library, "eglGetProcAddress");
        memcpy(&egl_get_proc_address, &symbol, sizeof(egl_get_proc_address));
    }
}

static ghostty_gl_proc_t opengl_get_proc_address(const char *name) {
    egl_proc proc = egl_get_proc_address != NULL ? egl_get_proc_address(name) : NULL;
    if (proc == NULL && gl_library != NULL) {
        void *symbol = dlsym(gl_library, name);
        memcpy(&proc, &symbol, sizeof(proc));
    }
    ghostty_gl_proc_t result = NULL;
    memcpy(&result, &proc, sizeof(result));
    return result;
}

static void runtime_wakeup(void *userdata) {
    gpui_ghostty_surface *state = userdata;
    state->wakeup(state->wakeup_userdata);
}

static bool runtime_action(ghostty_app_t app, ghostty_target_s target, ghostty_action_s action) {
    (void)app;
    if (action.tag != GHOSTTY_ACTION_RENDER ||
        target.tag != GHOSTTY_TARGET_SURFACE ||
        target.target.surface == NULL) {
        return false;
    }

    gpui_ghostty_surface *state = ghostty_surface_userdata(target.target.surface);
    if (state == NULL || !state->make_current(state->platform_userdata)) return false;
    ghostty_surface_draw(target.target.surface);
    state->swap_buffers(state->platform_userdata);
    state->clear_current(state->platform_userdata);
    return true;
}

static bool runtime_read_clipboard(void *userdata, ghostty_clipboard_e location, void *request) {
    gpui_ghostty_surface *state = userdata;
    if (state->clipboard_request != NULL) return false;
    state->clipboard_request = request;
    state->clipboard_location = location;
    runtime_wakeup(state);
    return true;
}

static void runtime_confirm_read_clipboard(
    void *userdata,
    const char *text,
    void *request,
    ghostty_clipboard_request_e kind
) {
    (void)kind;
    gpui_ghostty_surface *state = userdata;
    if (state->surface != NULL) {
        ghostty_surface_complete_clipboard_request(state->surface, text, request, true);
    }
}

static void runtime_write_clipboard(
    void *userdata,
    ghostty_clipboard_e location,
    const ghostty_clipboard_content_s *content,
    size_t count,
    bool confirm
) {
    (void)confirm;
    gpui_ghostty_surface *state = userdata;
    for (size_t index = 0; index < count; index++) {
        if (strcmp(content[index].mime, "text/plain") != 0) continue;
        char *copy = strdup(content[index].data);
        if (copy == NULL) return;
        free(state->clipboard_write);
        state->clipboard_write = copy;
        state->clipboard_write_location = location;
        runtime_wakeup(state);
        return;
    }
}

static void runtime_close_surface(void *userdata, bool process_alive) {
    (void)process_alive;
    gpui_ghostty_surface *state = userdata;
    atomic_store_explicit(&state->alive, false, memory_order_release);
    runtime_wakeup(state);
}

gpui_ghostty_surface *gpui_ghostty_surface_linux_new(
    void *platform_userdata,
    gpui_ghostty_make_current_cb make_current,
    gpui_ghostty_context_cb clear_current,
    gpui_ghostty_context_cb swap_buffers,
    const char *working_directory,
    const char *command,
    bool load_user_config,
    const char *theme_config_path,
    double scale_factor,
    void *wakeup_userdata,
    gpui_ghostty_wakeup_cb wakeup
) {
    pthread_once(&ghostty_once, initialize_ghostty);
    if (ghostty_init_result != GHOSTTY_SUCCESS || platform_userdata == NULL ||
        make_current == NULL || clear_current == NULL || swap_buffers == NULL ||
        wakeup_userdata == NULL || wakeup == NULL || !make_current(platform_userdata)) {
        return NULL;
    }

    gpui_ghostty_surface *state = calloc(1, sizeof(gpui_ghostty_surface));
    if (state == NULL) {
        clear_current(platform_userdata);
        return NULL;
    }
    atomic_init(&state->alive, true);
    state->platform_userdata = platform_userdata;
    state->make_current = make_current;
    state->clear_current = clear_current;
    state->swap_buffers = swap_buffers;
    state->wakeup_userdata = wakeup_userdata;
    state->wakeup = wakeup;

    state->config = ghostty_config_new();
    if (state->config == NULL) goto fail;
    if (load_user_config) {
        ghostty_config_load_default_files(state->config);
        ghostty_config_load_recursive_files(state->config);
    }
    if (theme_config_path != NULL) {
        ghostty_config_load_file(state->config, theme_config_path);
    }
    ghostty_config_finalize(state->config);

    ghostty_runtime_config_s runtime = {
        .userdata = state,
        .supports_selection_clipboard = true,
        .wakeup_cb = runtime_wakeup,
        .action_cb = runtime_action,
        .read_clipboard_cb = runtime_read_clipboard,
        .confirm_read_clipboard_cb = runtime_confirm_read_clipboard,
        .write_clipboard_cb = runtime_write_clipboard,
        .close_surface_cb = runtime_close_surface,
    };
    state->app = ghostty_app_new(&runtime, state->config);
    if (state->app == NULL) goto fail;

    ghostty_surface_config_s surface_config = ghostty_surface_config_new();
    surface_config.platform_tag = GHOSTTY_PLATFORM_OPENGL;
    surface_config.platform.opengl.get_proc_address = opengl_get_proc_address;
    surface_config.userdata = state;
    surface_config.scale_factor = scale_factor;
    ghostty_env_var_s environment[] = {
        { .key = "TERM", .value = "xterm-256color" },
        { .key = "COLORTERM", .value = "truecolor" },
        { .key = "TERM_PROGRAM", .value = "gpui-ghostty" },
    };
    surface_config.working_directory = working_directory;
    surface_config.command = command;
    surface_config.env_vars = environment;
    surface_config.env_var_count = sizeof(environment) / sizeof(environment[0]);
    surface_config.wait_after_command = false;
    surface_config.context = GHOSTTY_SURFACE_CONTEXT_WINDOW;
    state->surface = ghostty_surface_new(state->app, &surface_config);
    if (state->surface == NULL) goto fail;

    ghostty_app_set_focus(state->app, true);
    ghostty_surface_set_focus(state->surface, true);
    clear_current(platform_userdata);
    return state;

fail:
    if (state->surface != NULL) ghostty_surface_free(state->surface);
    if (state->app != NULL) ghostty_app_free(state->app);
    if (state->config != NULL) ghostty_config_free(state->config);
    clear_current(platform_userdata);
    free(state);
    return NULL;
}

void gpui_ghostty_surface_linux_free(gpui_ghostty_surface *state) {
    if (state == NULL) return;
    bool current = state->make_current(state->platform_userdata);
    if (state->surface != NULL) ghostty_surface_free(state->surface);
    if (state->app != NULL) ghostty_app_free(state->app);
    if (state->config != NULL) ghostty_config_free(state->config);
    free(state->clipboard_write);
    if (current) state->clear_current(state->platform_userdata);
    free(state);
}

void gpui_ghostty_surface_linux_tick(gpui_ghostty_surface *state) {
    if (state == NULL || state->app == NULL) return;
    ghostty_app_tick(state->app);
}

bool gpui_ghostty_surface_linux_is_alive(const gpui_ghostty_surface *state) {
    return state != NULL && atomic_load_explicit(&state->alive, memory_order_acquire) &&
        !ghostty_surface_process_exited(state->surface);
}

bool gpui_ghostty_surface_linux_snapshot(
    gpui_ghostty_surface *state,
    uint8_t **pixels,
    uint32_t *width,
    uint32_t *height,
    size_t *length
) {
    if (pixels == NULL || width == NULL || height == NULL || length == NULL) return false;
    *pixels = NULL;
    *width = 0;
    *height = 0;
    *length = 0;
    if (state == NULL || state->surface == NULL || state->width == 0 || state->height == 0 ||
        state->width > INT_MAX || state->height > INT_MAX) {
        return false;
    }

    gl_pixel_store_i_fn pixel_store = NULL;
    gl_read_buffer_fn read_buffer = NULL;
    gl_read_pixels_fn read_pixels = NULL;
    gl_get_error_fn get_error = NULL;
    egl_proc symbol = opengl_get_proc_address("glPixelStorei");
    memcpy(&pixel_store, &symbol, sizeof(pixel_store));
    symbol = opengl_get_proc_address("glReadBuffer");
    memcpy(&read_buffer, &symbol, sizeof(read_buffer));
    symbol = opengl_get_proc_address("glReadPixels");
    memcpy(&read_pixels, &symbol, sizeof(read_pixels));
    symbol = opengl_get_proc_address("glGetError");
    memcpy(&get_error, &symbol, sizeof(get_error));
    if (pixel_store == NULL || read_buffer == NULL || read_pixels == NULL || get_error == NULL) {
        return false;
    }

    size_t byte_length = (size_t)state->width * state->height * 4;
    uint8_t *copy = malloc(byte_length);
    if (copy == NULL || !state->make_current(state->platform_userdata)) {
        free(copy);
        return false;
    }
    ghostty_surface_draw(state->surface);
    while (get_error() != 0) {}
    pixel_store(GPUI_GL_PACK_ALIGNMENT, 1);
    read_buffer(GPUI_GL_BACK);
    read_pixels(
        0,
        0,
        (int)state->width,
        (int)state->height,
        GPUI_GL_BGRA,
        GPUI_GL_UNSIGNED_BYTE,
        copy
    );
    unsigned int error = get_error();
    state->clear_current(state->platform_userdata);
    if (error != 0) {
        free(copy);
        return false;
    }

    *pixels = copy;
    *width = state->width;
    *height = state->height;
    *length = byte_length;
    return true;
}

void gpui_ghostty_surface_linux_snapshot_free(uint8_t *pixels) {
    free(pixels);
}

void *gpui_ghostty_surface_linux_take_clipboard_read(
    gpui_ghostty_surface *state,
    bool *selection
) {
    if (state == NULL || state->clipboard_request == NULL) return NULL;
    void *request = state->clipboard_request;
    state->clipboard_request = NULL;
    *selection = state->clipboard_location == GHOSTTY_CLIPBOARD_SELECTION;
    return request;
}

void gpui_ghostty_surface_linux_complete_clipboard_read(
    gpui_ghostty_surface *state,
    void *request,
    const char *text
) {
    if (state != NULL && state->surface != NULL && request != NULL && text != NULL) {
        ghostty_surface_complete_clipboard_request(state->surface, text, request, false);
    }
}

char *gpui_ghostty_surface_linux_take_clipboard_write(
    gpui_ghostty_surface *state,
    bool *selection
) {
    if (state == NULL || state->clipboard_write == NULL) return NULL;
    char *text = state->clipboard_write;
    state->clipboard_write = NULL;
    *selection = state->clipboard_write_location == GHOSTTY_CLIPBOARD_SELECTION;
    return text;
}

void gpui_ghostty_surface_linux_free_clipboard_write(char *text) {
    free(text);
}

void gpui_ghostty_surface_linux_set_size(
    gpui_ghostty_surface *state,
    uint32_t width,
    uint32_t height,
    double scale_factor
) {
    if (state == NULL || state->surface == NULL || width == 0 || height == 0) return;
    state->width = width;
    state->height = height;
    ghostty_surface_set_content_scale(state->surface, scale_factor, scale_factor);
    ghostty_surface_set_size(state->surface, width, height);
    ghostty_surface_set_occlusion(state->surface, true);
    ghostty_surface_refresh(state->surface);
}

void gpui_ghostty_surface_linux_set_visible(gpui_ghostty_surface *state, bool visible) {
    if (state == NULL || state->surface == NULL) return;
    ghostty_surface_set_occlusion(state->surface, visible);
    if (visible) ghostty_surface_refresh(state->surface);
}

void gpui_ghostty_surface_linux_set_focus(gpui_ghostty_surface *state, bool focused) {
    if (state == NULL || state->surface == NULL) return;
    ghostty_app_set_focus(state->app, focused);
    ghostty_surface_set_focus(state->surface, focused);
}

bool gpui_ghostty_surface_linux_key(
    gpui_ghostty_surface *state,
    int action,
    int modifiers,
    int consumed_modifiers,
    uint32_t keycode,
    const char *text,
    uint32_t unshifted_codepoint
) {
    if (state == NULL || state->surface == NULL) return false;
    ghostty_input_key_s event = {
        .action = (ghostty_input_action_e)action,
        .mods = (ghostty_input_mods_e)modifiers,
        .consumed_mods = (ghostty_input_mods_e)consumed_modifiers,
        .keycode = keycode,
        .text = text,
        .unshifted_codepoint = unshifted_codepoint,
        .composing = false,
    };
    return ghostty_surface_key(state->surface, event);
}

void gpui_ghostty_surface_linux_text(
    gpui_ghostty_surface *state,
    const char *text,
    size_t length
) {
    if (state != NULL && state->surface != NULL) ghostty_surface_text(state->surface, text, length);
}

void gpui_ghostty_surface_linux_mouse_position(
    gpui_ghostty_surface *state,
    double x,
    double y,
    int modifiers
) {
    if (state != NULL && state->surface != NULL) {
        ghostty_surface_mouse_pos(state->surface, x, y, (ghostty_input_mods_e)modifiers);
    }
}

void gpui_ghostty_surface_linux_mouse_button(
    gpui_ghostty_surface *state,
    int mouse_state,
    int button,
    int modifiers
) {
    if (state != NULL && state->surface != NULL) {
        ghostty_surface_mouse_button(
            state->surface,
            (ghostty_input_mouse_state_e)mouse_state,
            (ghostty_input_mouse_button_e)button,
            (ghostty_input_mods_e)modifiers
        );
    }
}

void gpui_ghostty_surface_linux_mouse_scroll(
    gpui_ghostty_surface *state,
    double x,
    double y,
    int modifiers
) {
    if (state != NULL && state->surface != NULL) {
        ghostty_surface_mouse_scroll(state->surface, x, y, modifiers);
    }
}