1 /* Haiku window system selection support.
2    Copyright (C) 2021 Free Software Foundation, Inc.
3 
4 This file is part of GNU Emacs.
5 
6 GNU Emacs is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10 
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
18 
19 #include <config.h>
20 
21 #include "lisp.h"
22 #include "blockinput.h"
23 #include "coding.h"
24 #include "haikuselect.h"
25 #include "haikuterm.h"
26 
27 static Lisp_Object
haiku_selection_data_1(Lisp_Object clipboard)28 haiku_selection_data_1 (Lisp_Object clipboard)
29 {
30   Lisp_Object result = Qnil;
31   char *targets[256];
32 
33   block_input ();
34   if (EQ (clipboard, QPRIMARY))
35     BClipboard_primary_targets ((char **) &targets, 256);
36   else if (EQ (clipboard, QSECONDARY))
37     BClipboard_secondary_targets ((char **) &targets, 256);
38   else if (EQ (clipboard, QCLIPBOARD))
39     BClipboard_system_targets ((char **) &targets, 256);
40   else
41     {
42       unblock_input ();
43       signal_error ("Bad clipboard", clipboard);
44     }
45 
46   for (int i = 0; targets[i]; ++i)
47     {
48       result = Fcons (build_unibyte_string (targets[i]),
49 		      result);
50       free (targets[i]);
51     }
52   unblock_input ();
53 
54   return result;
55 }
56 
57 DEFUN ("haiku-selection-targets", Fhaiku_selection_targets,
58        Shaiku_selection_targets, 1, 1, 0,
59        doc: /* Find the types of data available from CLIPBOARD.
60 CLIPBOARD should be the symbol `PRIMARY', `SECONDARY' or `CLIPBOARD'.
61 Return the available types as a list of strings.  */)
62   (Lisp_Object clipboard)
63 {
64   return haiku_selection_data_1 (clipboard);
65 }
66 
67 DEFUN ("haiku-selection-data", Fhaiku_selection_data, Shaiku_selection_data,
68        2, 2, 0,
69        doc: /* Retrieve content typed as NAME from the clipboard
70 CLIPBOARD.  CLIPBOARD is the symbol `PRIMARY', `SECONDARY' or
71 `CLIPBOARD'.  NAME is a MIME type denoting the type of the data to
72 fetch.  */)
73   (Lisp_Object clipboard, Lisp_Object name)
74 {
75   CHECK_SYMBOL (clipboard);
76   CHECK_STRING (name);
77   char *dat;
78   ssize_t len;
79 
80   block_input ();
81   if (EQ (clipboard, QPRIMARY))
82     dat = BClipboard_find_primary_selection_data (SSDATA (name), &len);
83   else if (EQ (clipboard, QSECONDARY))
84     dat = BClipboard_find_secondary_selection_data (SSDATA (name), &len);
85   else if (EQ (clipboard, QCLIPBOARD))
86     dat = BClipboard_find_system_data (SSDATA (name), &len);
87   else
88     {
89       unblock_input ();
90       signal_error ("Bad clipboard", clipboard);
91     }
92   unblock_input ();
93 
94   if (!dat)
95     return Qnil;
96 
97   Lisp_Object str = make_unibyte_string (dat, len);
98   Lisp_Object lispy_type = Qnil;
99 
100   if (!strcmp (SSDATA (name), "text/utf-8") ||
101       !strcmp (SSDATA (name), "text/plain"))
102     {
103       if (string_ascii_p (str))
104 	lispy_type = QSTRING;
105       else
106 	lispy_type = QUTF8_STRING;
107     }
108 
109   if (!NILP (lispy_type))
110     Fput_text_property (make_fixnum (0), make_fixnum (len),
111 			Qforeign_selection, lispy_type, str);
112 
113   block_input ();
114   BClipboard_free_data (dat);
115   unblock_input ();
116 
117   return str;
118 }
119 
120 DEFUN ("haiku-selection-put", Fhaiku_selection_put, Shaiku_selection_put,
121        3, 4, 0,
122        doc: /* Add or remove content from the clipboard CLIPBOARD.
123 CLIPBOARD is the symbol `PRIMARY', `SECONDARY' or `CLIPBOARD'.  NAME
124 is a MIME type denoting the type of the data to add.  DATA is the
125 string that will be placed in the clipboard, or nil if the content is
126 to be removed.  If NAME is the string "text/utf-8" or the string
127 "text/plain", encode it as UTF-8 before storing it into the clipboard.
128 CLEAR, if non-nil, means to erase all the previous contents of the
129 clipboard.  */)
130   (Lisp_Object clipboard, Lisp_Object name, Lisp_Object data,
131    Lisp_Object clear)
132 {
133   CHECK_SYMBOL (clipboard);
134   CHECK_STRING (name);
135   if (!NILP (data))
136     CHECK_STRING (data);
137 
138   block_input ();
139   /* It seems that Haiku applications counter-intuitively expect
140      UTF-8 data in both text/utf-8 and text/plain.  */
141   if (!NILP (data) && STRING_MULTIBYTE (data) &&
142       (!strcmp (SSDATA (name), "text/utf-8") ||
143        !strcmp (SSDATA (name), "text/plain")))
144     data = ENCODE_UTF_8 (data);
145 
146   char *dat = !NILP (data) ? SSDATA (data) : NULL;
147   ptrdiff_t len = !NILP (data) ? SBYTES (data) : 0;
148 
149   if (EQ (clipboard, QPRIMARY))
150     BClipboard_set_primary_selection_data (SSDATA (name), dat, len,
151 					   !NILP (clear));
152   else if (EQ (clipboard, QSECONDARY))
153     BClipboard_set_secondary_selection_data (SSDATA (name), dat, len,
154 					     !NILP (clear));
155   else if (EQ (clipboard, QCLIPBOARD))
156     BClipboard_set_system_data (SSDATA (name), dat, len, !NILP (clear));
157   else
158     {
159       unblock_input ();
160       signal_error ("Bad clipboard", clipboard);
161     }
162   unblock_input ();
163 
164   return Qnil;
165 }
166 
167 void
syms_of_haikuselect(void)168 syms_of_haikuselect (void)
169 {
170   DEFSYM (QSECONDARY, "SECONDARY");
171   DEFSYM (QCLIPBOARD, "CLIPBOARD");
172   DEFSYM (QSTRING, "STRING");
173   DEFSYM (QUTF8_STRING, "UTF8_STRING");
174   DEFSYM (Qforeign_selection, "foreign-selection");
175   DEFSYM (QTARGETS, "TARGETS");
176 
177   defsubr (&Shaiku_selection_data);
178   defsubr (&Shaiku_selection_put);
179   defsubr (&Shaiku_selection_targets);
180 }
181