aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPo Lu2022-05-10 04:11:32 +0000
committerPo Lu2022-05-10 04:11:48 +0000
commit3c5e1f8ec8d5d52e5bbf185d9618852e7d04e3ca (patch)
treed611f01a506f47aa830b365a75447fe5f98d9106 /src
parente568c3845cd98b702ce23bdef1b22e088769c9cd (diff)
downloademacs-3c5e1f8ec8d5d52e5bbf185d9618852e7d04e3ca.tar.gz
emacs-3c5e1f8ec8d5d52e5bbf185d9618852e7d04e3ca.zip
Simplify Haiku selection code
* src/haiku_select.cc (get_clipboard_object): New function. (BClipboard_find_data, BClipboard_get_targets, BClipboard_set_data) (BClipboard_find_system_data) (BClipboard_find_primary_selection_data) (BClipboard_find_secondary_selection_data) (BClipboard_set_system_data, BClipboard_set_primary_selection_data) (BClipboard_set_secondary_selection_data, BClipboard_free_data) (BClipboard_system_targets, BClipboard_primary_targets) (BClipboard_secondary_targets): Delete functions. (be_find_clipboard_data_1, be_set_clipboard_data_1) (be_get_clipboard_targets_1, be_find_clipboard_data) (be_set_clipboard_data, be_get_clipboard_targets): New functions. (be_lock_clipboard_message, be_unlock_clipboard): Use `get_clipboard_object' to get clipboard from ID. * src/haikuselect.c (haiku_get_clipboard_name): New function. (Fhaiku_selection_data, Fhaiku_selection_put) (Fhaiku_selection_owner_p): Adjust to use new simplified functions. * src/haikuselect.h: Update prototypes.
Diffstat (limited to 'src')
-rw-r--r--src/haiku_select.cc168
-rw-r--r--src/haikuselect.c75
-rw-r--r--src/haikuselect.h21
3 files changed, 92 insertions, 172 deletions
diff --git a/src/haiku_select.cc b/src/haiku_select.cc
index a26a0049cbf..43b71138b33 100644
--- a/src/haiku_select.cc
+++ b/src/haiku_select.cc
@@ -35,22 +35,45 @@ static int64 count_clipboard = -1;
35static int64 count_primary = -1; 35static int64 count_primary = -1;
36static int64 count_secondary = -1; 36static int64 count_secondary = -1;
37 37
38static BClipboard *
39get_clipboard_object (enum haiku_clipboard clipboard)
40{
41 switch (clipboard)
42 {
43 case CLIPBOARD_PRIMARY:
44 return primary;
45
46 case CLIPBOARD_SECONDARY:
47 return secondary;
48
49 case CLIPBOARD_CLIPBOARD:
50 return system_clipboard;
51 }
52
53 abort ();
54}
55
38static char * 56static char *
39BClipboard_find_data (BClipboard *cb, const char *type, ssize_t *len) 57be_find_clipboard_data_1 (BClipboard *cb, const char *type, ssize_t *len)
40{ 58{
59 BMessage *data;
60 const char *ptr;
61 ssize_t nbytes;
62 void *value;
63
41 if (!cb->Lock ()) 64 if (!cb->Lock ())
42 return 0; 65 return NULL;
43 66
44 BMessage *dat = cb->Data (); 67 data = cb->Data ();
45 if (!dat) 68
69 if (!data)
46 { 70 {
47 cb->Unlock (); 71 cb->Unlock ();
48 return 0; 72 return NULL;
49 } 73 }
50 74
51 const char *ptr; 75 data->FindData (type, B_MIME_TYPE, (const void **) &ptr,
52 ssize_t bt; 76 &nbytes);
53 dat->FindData (type, B_MIME_TYPE, (const void **) &ptr, &bt);
54 77
55 if (!ptr) 78 if (!ptr)
56 { 79 {
@@ -59,9 +82,9 @@ BClipboard_find_data (BClipboard *cb, const char *type, ssize_t *len)
59 } 82 }
60 83
61 if (len) 84 if (len)
62 *len = bt; 85 *len = nbytes;
63 86
64 void *data = malloc (bt); 87 value = malloc (nbytes);
65 88
66 if (!data) 89 if (!data)
67 { 90 {
@@ -69,13 +92,14 @@ BClipboard_find_data (BClipboard *cb, const char *type, ssize_t *len)
69 return NULL; 92 return NULL;
70 } 93 }
71 94
72 memcpy (data, ptr, bt); 95 memcpy (value, ptr, nbytes);
73 cb->Unlock (); 96 cb->Unlock ();
74 return (char *) data; 97
98 return (char *) value;
75} 99}
76 100
77static void 101static void
78BClipboard_get_targets (BClipboard *cb, char **buf, int buf_size) 102be_get_clipboard_targets_1 (BClipboard *cb, char **buf, int buf_size)
79{ 103{
80 BMessage *data; 104 BMessage *data;
81 char *name; 105 char *name;
@@ -122,116 +146,60 @@ BClipboard_get_targets (BClipboard *cb, char **buf, int buf_size)
122} 146}
123 147
124static void 148static void
125BClipboard_set_data (BClipboard *cb, const char *type, const char *dat, 149be_set_clipboard_data_1 (BClipboard *cb, const char *type, const char *data,
126 ssize_t len, bool clear) 150 ssize_t len, bool clear)
127{ 151{
152 BMessage *message_data;
153
128 if (!cb->Lock ()) 154 if (!cb->Lock ())
129 return; 155 return;
130 156
131 if (clear) 157 if (clear)
132 cb->Clear (); 158 cb->Clear ();
133 159
134 BMessage *mdat = cb->Data (); 160 message_data = cb->Data ();
135 if (!mdat) 161
162 if (!message_data)
136 { 163 {
137 cb->Unlock (); 164 cb->Unlock ();
138 return; 165 return;
139 } 166 }
140 167
141 if (dat) 168 if (data)
142 { 169 {
143 if (mdat->ReplaceData (type, B_MIME_TYPE, dat, len) 170 if (message_data->ReplaceData (type, B_MIME_TYPE, data, len)
144 == B_NAME_NOT_FOUND) 171 == B_NAME_NOT_FOUND)
145 mdat->AddData (type, B_MIME_TYPE, dat, len); 172 message_data->AddData (type, B_MIME_TYPE, data, len);
146 } 173 }
147 else 174 else
148 mdat->RemoveName (type); 175 message_data->RemoveName (type);
176
149 cb->Commit (); 177 cb->Commit ();
150 cb->Unlock (); 178 cb->Unlock ();
151} 179}
152 180
153char * 181char *
154BClipboard_find_system_data (const char *type, ssize_t *len) 182be_find_clipboard_data (enum haiku_clipboard id, const char *type,
155{ 183 ssize_t *len)
156 if (!system_clipboard)
157 return 0;
158
159 return BClipboard_find_data (system_clipboard, type, len);
160}
161
162char *
163BClipboard_find_primary_selection_data (const char *type, ssize_t *len)
164{
165 if (!primary)
166 return 0;
167
168 return BClipboard_find_data (primary, type, len);
169}
170
171char *
172BClipboard_find_secondary_selection_data (const char *type, ssize_t *len)
173{
174 if (!secondary)
175 return 0;
176
177 return BClipboard_find_data (secondary, type, len);
178}
179
180void
181BClipboard_set_system_data (const char *type, const char *data,
182 ssize_t len, bool clear)
183{
184 if (!system_clipboard)
185 return;
186
187 count_clipboard = system_clipboard->SystemCount ();
188 BClipboard_set_data (system_clipboard, type, data, len, clear);
189}
190
191void
192BClipboard_set_primary_selection_data (const char *type, const char *data,
193 ssize_t len, bool clear)
194{
195 if (!primary)
196 return;
197
198 count_primary = primary->SystemCount ();
199 BClipboard_set_data (primary, type, data, len, clear);
200}
201
202void
203BClipboard_set_secondary_selection_data (const char *type, const char *data,
204 ssize_t len, bool clear)
205{ 184{
206 if (!secondary) 185 return be_find_clipboard_data_1 (get_clipboard_object (id),
207 return; 186 type, len);
208
209 count_secondary = secondary->SystemCount ();
210 BClipboard_set_data (secondary, type, data, len, clear);
211} 187}
212 188
213void 189void
214BClipboard_free_data (void *ptr) 190be_set_clipboard_data (enum haiku_clipboard id, const char *type,
191 const char *data, ssize_t len, bool clear)
215{ 192{
216 std::free (ptr); 193 be_set_clipboard_data_1 (get_clipboard_object (id), type,
194 data, len, clear);
217} 195}
218 196
219void 197void
220BClipboard_system_targets (char **buf, int len) 198be_get_clipboard_targets (enum haiku_clipboard id, char **targets,
199 int len)
221{ 200{
222 BClipboard_get_targets (system_clipboard, buf, len); 201 be_get_clipboard_targets_1 (get_clipboard_object (id), targets,
223} 202 len);
224
225void
226BClipboard_primary_targets (char **buf, int len)
227{
228 BClipboard_get_targets (primary, buf, len);
229}
230
231void
232BClipboard_secondary_targets (char **buf, int len)
233{
234 BClipboard_get_targets (secondary, buf, len);
235} 203}
236 204
237bool 205bool
@@ -443,12 +411,7 @@ be_lock_clipboard_message (enum haiku_clipboard clipboard,
443{ 411{
444 BClipboard *board; 412 BClipboard *board;
445 413
446 if (clipboard == CLIPBOARD_PRIMARY) 414 board = get_clipboard_object (clipboard);
447 board = primary;
448 else if (clipboard == CLIPBOARD_SECONDARY)
449 board = secondary;
450 else
451 board = system_clipboard;
452 415
453 if (!board->Lock ()) 416 if (!board->Lock ())
454 return 1; 417 return 1;
@@ -465,12 +428,7 @@ be_unlock_clipboard (enum haiku_clipboard clipboard, bool discard)
465{ 428{
466 BClipboard *board; 429 BClipboard *board;
467 430
468 if (clipboard == CLIPBOARD_PRIMARY) 431 board = get_clipboard_object (clipboard);
469 board = primary;
470 else if (clipboard == CLIPBOARD_SECONDARY)
471 board = secondary;
472 else
473 board = system_clipboard;
474 432
475 if (discard) 433 if (discard)
476 board->Revert (); 434 board->Revert ();
diff --git a/src/haikuselect.c b/src/haikuselect.c
index 8ce71822983..0c808bdb937 100644
--- a/src/haikuselect.c
+++ b/src/haikuselect.c
@@ -35,6 +35,21 @@ struct frame *haiku_dnd_frame;
35 35
36static void haiku_lisp_to_message (Lisp_Object, void *); 36static void haiku_lisp_to_message (Lisp_Object, void *);
37 37
38static enum haiku_clipboard
39haiku_get_clipboard_name (Lisp_Object clipboard)
40{
41 if (EQ (clipboard, QPRIMARY))
42 return CLIPBOARD_PRIMARY;
43
44 if (EQ (clipboard, QSECONDARY))
45 return CLIPBOARD_SECONDARY;
46
47 if (EQ (clipboard, QCLIPBOARD))
48 return CLIPBOARD_CLIPBOARD;
49
50 signal_error ("Invalid clipboard", clipboard);
51}
52
38DEFUN ("haiku-selection-data", Fhaiku_selection_data, Shaiku_selection_data, 53DEFUN ("haiku-selection-data", Fhaiku_selection_data, Shaiku_selection_data,
39 2, 2, 0, 54 2, 2, 0,
40 doc: /* Retrieve content typed as NAME from the clipboard 55 doc: /* Retrieve content typed as NAME from the clipboard
@@ -53,22 +68,15 @@ message in the format accepted by `haiku-drag-message', which see. */)
53 int rc; 68 int rc;
54 69
55 CHECK_SYMBOL (clipboard); 70 CHECK_SYMBOL (clipboard);
56 71 clipboard_name = haiku_get_clipboard_name (clipboard);
57 if (!EQ (clipboard, QPRIMARY) && !EQ (clipboard, QSECONDARY)
58 && !EQ (clipboard, QCLIPBOARD))
59 signal_error ("Invalid clipboard", clipboard);
60 72
61 if (!NILP (name)) 73 if (!NILP (name))
62 { 74 {
63 CHECK_STRING (name); 75 CHECK_STRING (name);
64 76
65 block_input (); 77 block_input ();
66 if (EQ (clipboard, QPRIMARY)) 78 dat = be_find_clipboard_data (clipboard_name,
67 dat = BClipboard_find_primary_selection_data (SSDATA (name), &len); 79 SSDATA (name), &len);
68 else if (EQ (clipboard, QSECONDARY))
69 dat = BClipboard_find_secondary_selection_data (SSDATA (name), &len);
70 else
71 dat = BClipboard_find_system_data (SSDATA (name), &len);
72 unblock_input (); 80 unblock_input ();
73 81
74 if (!dat) 82 if (!dat)
@@ -83,18 +91,11 @@ message in the format accepted by `haiku-drag-message', which see. */)
83 Qforeign_selection, Qt, str); 91 Qforeign_selection, Qt, str);
84 92
85 block_input (); 93 block_input ();
86 BClipboard_free_data (dat); 94 free (dat);
87 unblock_input (); 95 unblock_input ();
88 } 96 }
89 else 97 else
90 { 98 {
91 if (EQ (clipboard, QPRIMARY))
92 clipboard_name = CLIPBOARD_PRIMARY;
93 else if (EQ (clipboard, QSECONDARY))
94 clipboard_name = CLIPBOARD_SECONDARY;
95 else
96 clipboard_name = CLIPBOARD_CLIPBOARD;
97
98 block_input (); 99 block_input ();
99 rc = be_lock_clipboard_message (clipboard_name, &message, false); 100 rc = be_lock_clipboard_message (clipboard_name, &message, false);
100 unblock_input (); 101 unblock_input ();
@@ -139,17 +140,11 @@ In that case, the arguments after NAME are ignored. */)
139 int rc; 140 int rc;
140 void *message; 141 void *message;
141 142
143 CHECK_SYMBOL (clipboard);
144 clipboard_name = haiku_get_clipboard_name (clipboard);
145
142 if (CONSP (name) || NILP (name)) 146 if (CONSP (name) || NILP (name))
143 { 147 {
144 if (EQ (clipboard, QPRIMARY))
145 clipboard_name = CLIPBOARD_PRIMARY;
146 else if (EQ (clipboard, QSECONDARY))
147 clipboard_name = CLIPBOARD_SECONDARY;
148 else if (EQ (clipboard, QCLIPBOARD))
149 clipboard_name = CLIPBOARD_CLIPBOARD;
150 else
151 signal_error ("Invalid clipboard", clipboard);
152
153 rc = be_lock_clipboard_message (clipboard_name, 148 rc = be_lock_clipboard_message (clipboard_name,
154 &message, true); 149 &message, true);
155 150
@@ -164,7 +159,6 @@ In that case, the arguments after NAME are ignored. */)
164 return unbind_to (ref, Qnil); 159 return unbind_to (ref, Qnil);
165 } 160 }
166 161
167 CHECK_SYMBOL (clipboard);
168 CHECK_STRING (name); 162 CHECK_STRING (name);
169 if (!NILP (data)) 163 if (!NILP (data))
170 CHECK_STRING (data); 164 CHECK_STRING (data);
@@ -172,20 +166,8 @@ In that case, the arguments after NAME are ignored. */)
172 dat = !NILP (data) ? SSDATA (data) : NULL; 166 dat = !NILP (data) ? SSDATA (data) : NULL;
173 len = !NILP (data) ? SBYTES (data) : 0; 167 len = !NILP (data) ? SBYTES (data) : 0;
174 168
175 if (EQ (clipboard, QPRIMARY)) 169 be_set_clipboard_data (clipboard_name, SSDATA (name), dat, len,
176 BClipboard_set_primary_selection_data (SSDATA (name), dat, len, 170 !NILP (clear));
177 !NILP (clear));
178 else if (EQ (clipboard, QSECONDARY))
179 BClipboard_set_secondary_selection_data (SSDATA (name), dat, len,
180 !NILP (clear));
181 else if (EQ (clipboard, QCLIPBOARD))
182 BClipboard_set_system_data (SSDATA (name), dat, len, !NILP (clear));
183 else
184 {
185 unblock_input ();
186 signal_error ("Bad clipboard", clipboard);
187 }
188
189 return Qnil; 171 return Qnil;
190} 172}
191 173
@@ -193,18 +175,11 @@ DEFUN ("haiku-selection-owner-p", Fhaiku_selection_owner_p, Shaiku_selection_own
193 0, 1, 0, 175 0, 1, 0,
194 doc: /* Whether the current Emacs process owns the given SELECTION. 176 doc: /* Whether the current Emacs process owns the given SELECTION.
195The arg should be the name of the selection in question, typically one 177The arg should be the name of the selection in question, typically one
196of the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'. For 178of the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'. */)
197convenience, the symbol nil is the same as `PRIMARY', and t is the
198same as `SECONDARY'. */)
199 (Lisp_Object selection) 179 (Lisp_Object selection)
200{ 180{
201 bool value; 181 bool value;
202 182
203 if (NILP (selection))
204 selection = QPRIMARY;
205 else if (EQ (selection, Qt))
206 selection = QSECONDARY;
207
208 block_input (); 183 block_input ();
209 if (EQ (selection, QPRIMARY)) 184 if (EQ (selection, QPRIMARY))
210 value = BClipboard_owns_primary (); 185 value = BClipboard_owns_primary ();
diff --git a/src/haikuselect.h b/src/haikuselect.h
index d4f331a9ccb..b63d3c36536 100644
--- a/src/haikuselect.h
+++ b/src/haikuselect.h
@@ -41,28 +41,15 @@ extern void init_haiku_select (void);
41#endif 41#endif
42/* Whether or not the selection was recently changed. */ 42/* Whether or not the selection was recently changed. */
43 43
44/* Find a string with the MIME type TYPE in the system clipboard. */ 44extern char *be_find_clipboard_data (enum haiku_clipboard, const char *, ssize_t *);
45extern char *BClipboard_find_system_data (const char *, ssize_t *); 45extern void be_set_clipboard_data (enum haiku_clipboard, const char *, const char *,
46extern char *BClipboard_find_primary_selection_data (const char *, ssize_t *); 46 ssize_t, bool);
47extern char *BClipboard_find_secondary_selection_data (const char *, ssize_t *); 47extern void be_get_clipboard_targets (enum haiku_clipboard, char **, int);
48
49extern void BClipboard_set_system_data (const char *, const char *, ssize_t, bool);
50extern void BClipboard_set_primary_selection_data (const char *, const char *,
51 ssize_t, bool);
52extern void BClipboard_set_secondary_selection_data (const char *, const char *,
53 ssize_t, bool);
54
55extern void BClipboard_system_targets (char **, int);
56extern void BClipboard_primary_targets (char **, int);
57extern void BClipboard_secondary_targets (char **, int);
58 48
59extern bool BClipboard_owns_clipboard (void); 49extern bool BClipboard_owns_clipboard (void);
60extern bool BClipboard_owns_primary (void); 50extern bool BClipboard_owns_primary (void);
61extern bool BClipboard_owns_secondary (void); 51extern bool BClipboard_owns_secondary (void);
62 52
63/* Free the returned data. */
64extern void BClipboard_free_data (void *);
65
66extern int be_enum_message (void *, int32 *, int32, int32 *, const char **); 53extern int be_enum_message (void *, int32 *, int32, int32 *, const char **);
67extern int be_get_message_data (void *, const char *, int32, int32, 54extern int be_get_message_data (void *, const char *, int32, int32,
68 const void **, ssize_t *); 55 const void **, ssize_t *);