yutani-clipboard.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2018 K. Lange
  5. *
  6. * yutani-clipboard - Manipulate the Yutani clipboard
  7. *
  8. * Gets and sets clipboard values.
  9. */
  10. #include <stdio.h>
  11. #include <unistd.h>
  12. #include <getopt.h>
  13. #include <toaru/yutani.h>
  14. void show_usage(int argc, char * argv[]) {
  15. printf(
  16. "yutani-clipboard - set and obtain clipboard contents\n"
  17. "\n"
  18. "usage: %s -g\n"
  19. " %s -s TEXT...\n"
  20. " %s -f FILE\n"
  21. "\n"
  22. " -s \033[3mset the clipboard text to argument\033[0m\n"
  23. " -f \033[3mset the clibboard text to file\033[0m\n"
  24. " -g \033[3mprint clipboard contents to stdout\033[0m\n"
  25. " -n \033[3mensure a linefeed is printed\033[0m\n"
  26. " -? \033[3mshow this help text\033[0m\n"
  27. "\n", argv[0], argv[0], argv[0]);
  28. }
  29. yutani_t * yctx;
  30. int force_linefeed = 0;
  31. int set_clipboard_from_file(char * file) {
  32. FILE * f;
  33. f = fopen(file, "r");
  34. if (!f) return 1;
  35. fseek(f, 0, SEEK_END);
  36. size_t size = ftell(f);
  37. fseek(f, 0, SEEK_SET);
  38. char * tmp = malloc(size);
  39. fread(tmp, 1, size, f);
  40. yutani_set_clipboard(yctx, tmp);
  41. free(tmp);
  42. return 0;
  43. }
  44. void get_clipboard(void) {
  45. yutani_special_request(yctx, NULL, YUTANI_SPECIAL_REQUEST_CLIPBOARD);
  46. yutani_msg_t * clipboard = yutani_wait_for(yctx, YUTANI_MSG_CLIPBOARD);
  47. struct yutani_msg_clipboard * cb = (void *)clipboard->data;
  48. if (*cb->content == '\002') {
  49. int size = atoi(&cb->content[2]);
  50. FILE * clipboard = yutani_open_clipboard(yctx);
  51. char * selection_text = malloc(size + 1);
  52. fread(selection_text, 1, size, clipboard);
  53. selection_text[size] = '\0';
  54. fclose(clipboard);
  55. fwrite(selection_text, 1, size, stdout);
  56. if (force_linefeed && size && selection_text[size-1] != '\n') {
  57. printf("\n");
  58. }
  59. } else {
  60. char * selection_text = malloc(cb->size+1);
  61. memcpy(selection_text, cb->content, cb->size);
  62. selection_text[cb->size] = '\0';
  63. fwrite(selection_text, 1, cb->size, stdout);
  64. if (force_linefeed && cb->size && selection_text[cb->size-1] != '\n') {
  65. printf("\n");
  66. }
  67. }
  68. }
  69. int main(int argc, char * argv[]) {
  70. yctx = yutani_init();
  71. if (!yctx) {
  72. fprintf(stderr, "%s: failed to connect to compositor\n", argv[0]);
  73. return 1;
  74. }
  75. int opt;
  76. while ((opt = getopt(argc, argv, "?s:f:gn")) != -1) {
  77. switch (opt) {
  78. case 's':
  79. yutani_set_clipboard(yctx, optarg);
  80. return 0;
  81. case 'f':
  82. return set_clipboard_from_file(optarg);
  83. case 'n':
  84. force_linefeed = 1;
  85. break;
  86. case 'g':
  87. get_clipboard();
  88. return 0;
  89. case '?':
  90. show_usage(argc,argv);
  91. return 1;
  92. }
  93. }
  94. show_usage(argc, argv);
  95. return 1;
  96. }