91 lines
2.5 KiB
C
91 lines
2.5 KiB
C
#include "cell.h"
|
|
#include <SDL3/SDL.h>
|
|
|
|
// SDL_SetClipboardText(text) -> bool
|
|
JSC_CCALL(clipboard_set_text,
|
|
const char *text = JS_ToCString(js, argv[0]);
|
|
if (!text) return JS_EXCEPTION;
|
|
bool result = SDL_SetClipboardText(text);
|
|
JS_FreeCString(js, text);
|
|
return JS_NewBool(js, result);
|
|
)
|
|
|
|
// SDL_GetClipboardText() -> string
|
|
JSC_CCALL(clipboard_get_text,
|
|
char *text = SDL_GetClipboardText();
|
|
if (!text) return JS_NewString(js, "");
|
|
JSValue result = JS_NewString(js, text);
|
|
SDL_free(text);
|
|
return result;
|
|
)
|
|
|
|
// SDL_HasClipboardText() -> bool
|
|
JSC_CCALL(clipboard_has_text,
|
|
return JS_NewBool(js, SDL_HasClipboardText());
|
|
)
|
|
|
|
// SDL_SetPrimarySelectionText(text) -> bool
|
|
JSC_CCALL(clipboard_set_primary_text,
|
|
const char *text = JS_ToCString(js, argv[0]);
|
|
if (!text) return JS_EXCEPTION;
|
|
bool result = SDL_SetPrimarySelectionText(text);
|
|
JS_FreeCString(js, text);
|
|
return JS_NewBool(js, result);
|
|
)
|
|
|
|
// SDL_GetPrimarySelectionText() -> string
|
|
JSC_CCALL(clipboard_get_primary_text,
|
|
char *text = SDL_GetPrimarySelectionText();
|
|
if (!text) return JS_NewString(js, "");
|
|
JSValue result = JS_NewString(js, text);
|
|
SDL_free(text);
|
|
return result;
|
|
)
|
|
|
|
// SDL_HasPrimarySelectionText() -> bool
|
|
JSC_CCALL(clipboard_has_primary_text,
|
|
return JS_NewBool(js, SDL_HasPrimarySelectionText());
|
|
)
|
|
|
|
// SDL_ClearClipboardData() -> bool
|
|
JSC_CCALL(clipboard_clear,
|
|
return JS_NewBool(js, SDL_ClearClipboardData());
|
|
)
|
|
|
|
// SDL_HasClipboardData(mime_type) -> bool
|
|
JSC_CCALL(clipboard_has_data,
|
|
const char *mime_type = JS_ToCString(js, argv[0]);
|
|
if (!mime_type) return JS_EXCEPTION;
|
|
bool result = SDL_HasClipboardData(mime_type);
|
|
JS_FreeCString(js, mime_type);
|
|
return JS_NewBool(js, result);
|
|
)
|
|
|
|
// SDL_GetClipboardMimeTypes() -> array of strings
|
|
JSC_CCALL(clipboard_get_mime_types,
|
|
size_t count = 0;
|
|
char **types = SDL_GetClipboardMimeTypes(&count);
|
|
if (!types) return JS_NewArray(js);
|
|
|
|
JSValue arr = JS_NewArray(js);
|
|
for (size_t i = 0; i < count; i++) {
|
|
JS_SetPropertyUint32(js, arr, i, JS_NewString(js, types[i]));
|
|
}
|
|
SDL_free(types);
|
|
return arr;
|
|
)
|
|
|
|
static const JSCFunctionListEntry js_clipboard_funcs[] = {
|
|
MIST_FUNC_DEF(clipboard, set_text, 1),
|
|
MIST_FUNC_DEF(clipboard, get_text, 0),
|
|
MIST_FUNC_DEF(clipboard, has_text, 0),
|
|
MIST_FUNC_DEF(clipboard, set_primary_text, 1),
|
|
MIST_FUNC_DEF(clipboard, get_primary_text, 0),
|
|
MIST_FUNC_DEF(clipboard, has_primary_text, 0),
|
|
MIST_FUNC_DEF(clipboard, clear, 0),
|
|
MIST_FUNC_DEF(clipboard, has_data, 1),
|
|
MIST_FUNC_DEF(clipboard, get_mime_types, 0),
|
|
};
|
|
|
|
CELL_USE_FUNCS(js_clipboard_funcs)
|