mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-16 16:56:09 +03:00

This is my attempt at fixing #63. It works! But: 1. The `NotificationCenter` subscription is triggered once for every open tab. That's obviously wrong. But I'm not sure and could use some pointers where else to put the subscription. That leads me to... 2. I'm _not_ knowledgable in Swift/AppKit/SwiftUI, so I might have put the wrong/right things in the wrong/right places. For example: wasn't sure what's to be handled in Swift and what's to be handled by the core in Zig. Would love some pointers :)
296 lines
8.4 KiB
C
296 lines
8.4 KiB
C
// Ghostty embedding API. The documentation for the embedding API is
|
|
// only within the Zig source files that define the implementations. This
|
|
// isn't meant to be a general purpose embedding API (yet) so there hasn't
|
|
// been documentation or example work beyond that.
|
|
//
|
|
// The only consumer of this API is the macOS app, but the API is built to
|
|
// be more general purpose.
|
|
#ifndef GHOSTTY_H
|
|
#define GHOSTTY_H
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
|
|
//-------------------------------------------------------------------
|
|
// Macros
|
|
|
|
#define GHOSTTY_SUCCESS 0
|
|
|
|
//-------------------------------------------------------------------
|
|
// Types
|
|
|
|
// Opaque types
|
|
typedef void *ghostty_app_t;
|
|
typedef void *ghostty_config_t;
|
|
typedef void *ghostty_surface_t;
|
|
|
|
// Enums are up top so we can reference them later.
|
|
typedef enum {
|
|
GHOSTTY_SPLIT_RIGHT,
|
|
GHOSTTY_SPLIT_DOWN
|
|
} ghostty_split_direction_e;
|
|
|
|
typedef enum {
|
|
GHOSTTY_SPLIT_FOCUS_PREVIOUS,
|
|
GHOSTTY_SPLIT_FOCUS_NEXT,
|
|
GHOSTTY_SPLIT_FOCUS_TOP,
|
|
GHOSTTY_SPLIT_FOCUS_LEFT,
|
|
GHOSTTY_SPLIT_FOCUS_BOTTOM,
|
|
GHOSTTY_SPLIT_FOCUS_RIGHT,
|
|
} ghostty_split_focus_direction_e;
|
|
|
|
typedef enum {
|
|
GHOSTTY_MOUSE_RELEASE,
|
|
GHOSTTY_MOUSE_PRESS,
|
|
} ghostty_input_mouse_state_e;
|
|
|
|
typedef enum {
|
|
GHOSTTY_MOUSE_UNKNOWN,
|
|
GHOSTTY_MOUSE_LEFT,
|
|
GHOSTTY_MOUSE_RIGHT,
|
|
GHOSTTY_MOUSE_MIDDLE,
|
|
} ghostty_input_mouse_button_e;
|
|
|
|
typedef enum {
|
|
GHOSTTY_MOUSE_MOMENTUM_NONE,
|
|
GHOSTTY_MOUSE_MOMENTUM_BEGAN,
|
|
GHOSTTY_MOUSE_MOMENTUM_STATIONARY,
|
|
GHOSTTY_MOUSE_MOMENTUM_CHANGED,
|
|
GHOSTTY_MOUSE_MOMENTUM_ENDED,
|
|
GHOSTTY_MOUSE_MOMENTUM_CANCELLED,
|
|
GHOSTTY_MOUSE_MOMENTUM_MAY_BEGIN,
|
|
} ghostty_input_mouse_momentum_e;
|
|
|
|
// This is a packed struct (see src/input/mouse.zig) but the C standard
|
|
// afaik doesn't let us reliably define packed structs so we build it up
|
|
// from scratch.
|
|
typedef int ghostty_input_scroll_mods_t;
|
|
|
|
typedef enum {
|
|
GHOSTTY_MODS_NONE = 0,
|
|
GHOSTTY_MODS_SHIFT = 1 << 0,
|
|
GHOSTTY_MODS_CTRL = 1 << 1,
|
|
GHOSTTY_MODS_ALT = 1 << 2,
|
|
GHOSTTY_MODS_SUPER = 1 << 3,
|
|
GHOSTTY_MODS_CAPS = 1 << 4,
|
|
GHOSTTY_MODS_NUM = 1 << 5,
|
|
} ghostty_input_mods_e;
|
|
|
|
typedef enum {
|
|
GHOSTTY_ACTION_RELEASE,
|
|
GHOSTTY_ACTION_PRESS,
|
|
GHOSTTY_ACTION_REPEAT,
|
|
} ghostty_input_action_e;
|
|
|
|
typedef enum {
|
|
GHOSTTY_KEY_INVALID,
|
|
|
|
// a-z
|
|
GHOSTTY_KEY_A,
|
|
GHOSTTY_KEY_B,
|
|
GHOSTTY_KEY_C,
|
|
GHOSTTY_KEY_D,
|
|
GHOS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Y_KP_4,
|
|
GHOSTTY_KEY_KP_5,
|
|
GHOSTTY_KEY_KP_6,
|
|
GHOSTTY_KEY_KP_7,
|
|
GHOSTTY_KEY_KP_8,
|
|
GHOSTTY_KEY_KP_9,
|
|
GHOSTTY_KEY_KP_DECIMAL,
|
|
GHOSTTY_KEY_KP_DIVIDE,
|
|
GHOSTTY_KEY_KP_MULTIPLY,
|
|
GHOSTTY_KEY_KP_SUBTRACT,
|
|
GHOSTTY_KEY_KP_ADD,
|
|
GHOSTTY_KEY_KP_ENTER,
|
|
GHOSTTY_KEY_KP_EQUAL,
|
|
|
|
// modifiers
|
|
GHOSTTY_KEY_LEFT_SHIFT,
|
|
GHOSTTY_KEY_LEFT_CONTROL,
|
|
GHOSTTY_KEY_LEFT_ALT,
|
|
GHOSTTY_KEY_LEFT_SUPER,
|
|
GHOSTTY_KEY_RIGHT_SHIFT,
|
|
GHOSTTY_KEY_RIGHT_CONTROL,
|
|
GHOSTTY_KEY_RIGHT_ALT,
|
|
GHOSTTY_KEY_RIGHT_SUPER,
|
|
} ghostty_input_key_e;
|
|
|
|
// Fully defined types. This MUST be kept in sync with equivalent Zig
|
|
// structs. To find the Zig struct, grep for this type name. The documentation
|
|
// for all of these types is available in the Zig source.
|
|
typedef struct {
|
|
void *userdata;
|
|
void *nsview;
|
|
double scale_factor;
|
|
} ghostty_surface_config_s;
|
|
|
|
typedef void (*ghostty_runtime_wakeup_cb)(void *);
|
|
typedef const ghostty_config_t (*ghostty_runtime_reload_config_cb)(void *);
|
|
typedef void (*ghostty_runtime_set_title_cb)(void *, const char *);
|
|
typedef const char* (*ghostty_runtime_read_clipboard_cb)(void *);
|
|
typedef void (*ghostty_runtime_write_clipboard_cb)(void *, const char *);
|
|
typedef void (*ghostty_runtime_new_split_cb)(void *, ghostty_split_direction_e);
|
|
typedef void (*ghostty_runtime_close_surface_cb)(void *, bool);
|
|
typedef void (*ghostty_runtime_focus_split_cb)(void *, ghostty_split_focus_direction_e);
|
|
typedef void (*ghostty_runtime_goto_tab_cb)(void *, int32_t);
|
|
|
|
typedef struct {
|
|
void *userdata;
|
|
ghostty_runtime_wakeup_cb wakeup_cb;
|
|
ghostty_runtime_reload_config_cb reload_config_cb;
|
|
ghostty_runtime_set_title_cb set_title_cb;
|
|
ghostty_runtime_read_clipboard_cb read_clipboard_cb;
|
|
ghostty_runtime_write_clipboard_cb write_clipboard_cb;
|
|
ghostty_runtime_new_split_cb new_split_cb;
|
|
ghostty_runtime_close_surface_cb close_surface_cb;
|
|
ghostty_runtime_focus_split_cb focus_split_cb;
|
|
ghostty_runtime_goto_tab_cb goto_tab_cb;
|
|
} ghostty_runtime_config_s;
|
|
|
|
//-------------------------------------------------------------------
|
|
// Published API
|
|
|
|
int ghostty_init(void);
|
|
|
|
ghostty_config_t ghostty_config_new();
|
|
void ghostty_config_free(ghostty_config_t);
|
|
void ghostty_config_load_cli_args(ghostty_config_t);
|
|
void ghostty_config_load_string(ghostty_config_t, const char *, uintptr_t);
|
|
void ghostty_config_load_default_files(ghostty_config_t);
|
|
void ghostty_config_load_recursive_files(ghostty_config_t);
|
|
void ghostty_config_finalize(ghostty_config_t);
|
|
|
|
ghostty_app_t ghostty_app_new(const ghostty_runtime_config_s *, ghostty_config_t);
|
|
void ghostty_app_free(ghostty_app_t);
|
|
bool ghostty_app_tick(ghostty_app_t);
|
|
void *ghostty_app_userdata(ghostty_app_t);
|
|
|
|
ghostty_surface_t ghostty_surface_new(ghostty_app_t, ghostty_surface_config_s*);
|
|
void ghostty_surface_free(ghostty_surface_t);
|
|
ghostty_app_t ghostty_surface_app(ghostty_surface_t);
|
|
void ghostty_surface_refresh(ghostty_surface_t);
|
|
void ghostty_surface_set_content_scale(ghostty_surface_t, double, double);
|
|
void ghostty_surface_set_focus(ghostty_surface_t, bool);
|
|
void ghostty_surface_set_size(ghostty_surface_t, uint32_t, uint32_t);
|
|
void ghostty_surface_key(ghostty_surface_t, ghostty_input_action_e, ghostty_input_key_e, ghostty_input_key_e, ghostty_input_mods_e);
|
|
void ghostty_surface_char(ghostty_surface_t, uint32_t);
|
|
void ghostty_surface_mouse_button(ghostty_surface_t, ghostty_input_mouse_state_e, ghostty_input_mouse_button_e, ghostty_input_mods_e);
|
|
void ghostty_surface_mouse_pos(ghostty_surface_t, double, double);
|
|
void ghostty_surface_mouse_scroll(ghostty_surface_t, double, double, ghostty_input_scroll_mods_t);
|
|
void ghostty_surface_ime_point(ghostty_surface_t, double *, double *);
|
|
void ghostty_surface_request_close(ghostty_surface_t);
|
|
void ghostty_surface_split(ghostty_surface_t, ghostty_split_direction_e);
|
|
void ghostty_surface_split_focus(ghostty_surface_t, ghostty_split_focus_direction_e);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif /* GHOSTTY_H */
|