From e2b25ebaca7505ec3e252618a054c93f0d30c456 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Wed, 8 Aug 2018 11:46:10 +0900 Subject: [PATCH] yutani-clipboard --- apps/yutani-clipboard.c | 91 +++++++++++++++++++++++++++++++++++++++++ apps/yutani-query.c | 41 ++++--------------- 2 files changed, 100 insertions(+), 32 deletions(-) create mode 100644 apps/yutani-clipboard.c diff --git a/apps/yutani-clipboard.c b/apps/yutani-clipboard.c new file mode 100644 index 00000000..75605ca1 --- /dev/null +++ b/apps/yutani-clipboard.c @@ -0,0 +1,91 @@ +#include +#include +#include + +#include + +void show_usage(int argc, char * argv[]) { + printf( + "yutani-clipboard - set and obtain clipboard contents\n" + "\n" + "usage: %s -g\n" + " %s -s TEXT...\n" + " %s -f FILE\n" + "\n" + " -s \033[3mset the clipboard text to argument\033[0m\n" + " -f \033[3mset the clibboard text to file\033[0m\n" + " -g \033[3mprint clipboard contents to stdout\033[0m\n" + " -? \033[3mshow this help text\033[0m\n" + "\n", argv[0], argv[0], argv[0]); +} + +yutani_t * yctx; + +int set_clipboard_from_file(char * file) { + FILE * f; + + f = fopen(file, "r"); + if (!f) return 1; + + fseek(f, 0, SEEK_END); + size_t size = ftell(f); + fseek(f, 0, SEEK_SET); + + char * tmp = malloc(size); + fread(tmp, 1, size, f); + + yutani_set_clipboard(yctx, tmp); + + free(tmp); + + return 0; +} + +void get_clipboard(void) { + yutani_special_request(yctx, NULL, YUTANI_SPECIAL_REQUEST_CLIPBOARD); + yutani_msg_t * clipboard = yutani_wait_for(yctx, YUTANI_MSG_CLIPBOARD); + struct yutani_msg_clipboard * cb = (void *)clipboard->data; + + if (*cb->content == '\002') { + int size = atoi(&cb->content[2]); + FILE * clipboard = yutani_open_clipboard(yctx); + char * selection_text = malloc(size + 1); + fread(selection_text, 1, size, clipboard); + selection_text[size] = '\0'; + fclose(clipboard); + fwrite(selection_text, 1, size, stdout); + } else { + char * selection_text = malloc(cb->size+1); + memcpy(selection_text, cb->content, cb->size); + selection_text[cb->size] = '\0'; + fwrite(selection_text, 1, cb->size, stdout); + } + +} + +int main(int argc, char * argv[]) { + yctx = yutani_init(); + if (!yctx) { + fprintf(stderr, "%s: failed to connect to compositor\n", argv[0]); + return 1; + } + int opt; + while ((opt = getopt(argc, argv, "?s:f:g")) != -1) { + switch (opt) { + case 's': + yutani_set_clipboard(yctx, optarg); + return 0; + case 'f': + return set_clipboard_from_file(optarg); + case 'g': + get_clipboard(); + return 0; + case '?': + show_usage(argc,argv); + return 1; + } + } + + show_usage(argc, argv); + return 1; +} diff --git a/apps/yutani-query.c b/apps/yutani-query.c index d3d6c35e..87e39d23 100644 --- a/apps/yutani-query.c +++ b/apps/yutani-query.c @@ -8,11 +8,9 @@ void show_usage(int argc, char * argv[]) { printf( "yutani-query - show misc. information about the display system\n" "\n" - "usage: %s [-rfm?]\n" + "usage: %s [-r?]\n" "\n" " -r \033[3mprint display resoluton\033[0m\n" - " -f \033[3mprint the name of the default font\033[0m\n" - " -m \033[3mprint the name of the monospace font\033[0m\n" " -? \033[3mshow this help text\033[0m\n" "\n", argv[0]); } @@ -22,41 +20,20 @@ int show_resolution(void) { return 0; } -#if 0 -int show_fontname(int font) { - init_shmemfonts(); - printf("%s\n", shmem_font_name(font)); - return 0; -} -#endif - int main(int argc, char * argv[]) { yctx = yutani_init(); if (!yctx) { printf("(not connected)\n"); return 1; } - if (argc > 1) { - for (int i = 1; i < argc; ++i) { - if (argv[i][0] == '-') { - char *c = &argv[i][1]; - while (*c) { - switch (*c) { - case 'r': - return show_resolution(); -#if 0 - case 'f': - return show_fontname(FONT_SANS_SERIF); - case 'm': - return show_fontname(FONT_MONOSPACE); -#endif - case '?': - show_usage(argc, argv); - return 0; - } - c++; - } - } + int opt; + while ((opt = getopt(argc, argv, "?r")) != -1) { + switch (opt) { + case 'r': + return show_resolution(); + case '?': + show_usage(argc,argv); + return 0; } }