yutani-clipboard

This commit is contained in:
K. Lange 2018-08-08 11:46:10 +09:00
parent 9b4a1174b6
commit e2b25ebaca
2 changed files with 100 additions and 32 deletions

91
apps/yutani-clipboard.c Normal file
View File

@ -0,0 +1,91 @@
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <toaru/yutani.h>
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;
}

View File

@ -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;
}
}