Skip to content

Commit 9ebe692

Browse files
committed
Allow drop clients to use machine id as well
1 parent 88a4d90 commit 9ebe692

4 files changed

Lines changed: 97 additions & 76 deletions

File tree

docs/dnd-protocol.rst

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ the end of the last chunk.
3838
Accepting drops
3939
-----------------
4040

41-
In order to inform the terminal emulator that the program accepts drops, it
41+
In order to inform the terminal emulator that the client accepts drops, it
4242
must send the following escape code::
4343

4444
OSC _dnd_code ; t=a ; payload ST
@@ -48,7 +48,17 @@ The list of MIME types is optional, it is needed if the program wants to accept
4848
exotic or private use MIME types on platforms such as macOS, where the system
4949
does not deliver drop events unless the MIME type is registered.
5050

51-
When the program is done accepting drops, or at exit, it should send the escape
51+
The terminal emulator may respond to this escape code with an escape code of
52+
the form::
53+
54+
OSC _dnd_code ; t=a ; machine id ST
55+
56+
Here, the :ref:`machine id <machine_id>` is an id that identifies the machine
57+
the terminal is running on and can be used by the client to determine whether
58+
to request remote files from the terminal when a drop occurs.
59+
See :ref:`below <machine_id>` for the semantics of the machine id.
60+
61+
When the client is done accepting drops, or at exit, it should send the escape
5262
code::
5363

5464
OSC _dnd_code ; t=A ST
@@ -132,8 +142,12 @@ terminal emulator must then inform the OS that the drop is completed.
132142
Dropping from remote machines
133143
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
134144

135-
In order to support dropping of files from remote machines,
136-
clients can first request the :rfc:`text/uri-list <2483>` MIME
145+
In order to support dropping of files from remote machines, the client
146+
can use the :ref:`machine id <machine_id>` previously sent by the terminal.
147+
If it is different from the id of the machine the client is running on, it
148+
can choose to request remote files, as follows.
149+
150+
Clients can first request the :rfc:`text/uri-list <2483>` MIME
137151
type to get a list of dropped URIs. For every URI in the list, they can
138152
send the terminal emulator a data request of the form::
139153

kitty/dnd.c

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -144,40 +144,6 @@ reset_drop(Window *w) {
144144
}
145145
}
146146

147-
void
148-
drop_register_window(Window *w, const uint8_t *payload, size_t payload_sz, bool on, uint32_t client_id, bool more) {
149-
w->drop.wanted = on;
150-
w->drop.client_id = client_id;
151-
if (!on) { drop_free_data(w); zero_at_ptr(&w->drop); return; }
152-
if (!payload || !payload_sz) return;
153-
size_t sz = w->drop.registered_mimes ? strlen(w->drop.registered_mimes) : 0;
154-
if (sz + payload_sz > MIME_LIST_SIZE_CAP) return;
155-
w->drop.registered_mimes = realloc(w->drop.registered_mimes, sz + payload_sz + 1);
156-
if (w->drop.registered_mimes) {
157-
memcpy(w->drop.registered_mimes + sz, payload, payload_sz);
158-
sz += payload_sz;
159-
w->drop.registered_mimes[sz] = 0;
160-
}
161-
if (more) return;
162-
if (w->drop.registered_mimes) {
163-
OSWindow *osw = os_window_for_kitty_window(w->id);
164-
if (osw) {
165-
size_t num = 0;
166-
RAII_ALLOC(const char*, mimes, malloc(sizeof(char*) * strlen(w->drop.registered_mimes)));
167-
if (mimes) {
168-
char* token = strtok(w->drop.registered_mimes, " ");
169-
while (token != NULL) {
170-
mimes[num++] = token;
171-
token = strtok(NULL, " ");
172-
}
173-
register_mimes_for_drop(osw, mimes, num);
174-
}
175-
}
176-
}
177-
free(w->drop.registered_mimes); w->drop.registered_mimes = NULL;
178-
}
179-
180-
181147
static int
182148
string_arrays_cmp(const char **a, size_t an, const char **b, size_t bn) {
183149
if (an != bn) return (int)an - (int)bn;
@@ -289,6 +255,46 @@ queue_payload_to_child(id_type id, uint32_t client_id, PendingData *pending, con
289255
if (pending->count) check_for_pending_writes();
290256
}
291257

258+
void
259+
drop_register_window(Window *w, const uint8_t *payload, size_t payload_sz, bool on, uint32_t client_id, bool more) {
260+
w->drop.wanted = on;
261+
w->drop.client_id = client_id;
262+
if (!on) { drop_free_data(w); zero_at_ptr(&w->drop); return; }
263+
if (!payload || !payload_sz) return;
264+
size_t sz = w->drop.registered_mimes ? strlen(w->drop.registered_mimes) : 0;
265+
if (sz + payload_sz > MIME_LIST_SIZE_CAP) return;
266+
w->drop.registered_mimes = realloc(w->drop.registered_mimes, sz + payload_sz + 1);
267+
if (w->drop.registered_mimes) {
268+
memcpy(w->drop.registered_mimes + sz, payload, payload_sz);
269+
sz += payload_sz;
270+
w->drop.registered_mimes[sz] = 0;
271+
}
272+
if (more) return;
273+
if (w->drop.registered_mimes) {
274+
OSWindow *osw = os_window_for_kitty_window(w->id);
275+
if (osw) {
276+
size_t num = 0;
277+
RAII_ALLOC(const char*, mimes, malloc(sizeof(char*) * strlen(w->drop.registered_mimes)));
278+
if (mimes) {
279+
char* token = strtok(w->drop.registered_mimes, " ");
280+
while (token != NULL) {
281+
mimes[num++] = token;
282+
token = strtok(NULL, " ");
283+
}
284+
register_mimes_for_drop(osw, mimes, num);
285+
}
286+
}
287+
}
288+
free(w->drop.registered_mimes); w->drop.registered_mimes = NULL;
289+
const char* host_machine_id = machine_id();
290+
if (host_machine_id) {
291+
char header[32] = {0};
292+
int n = snprintf(header, sizeof(header), "\x1b]%d;t=a", DND_CODE);
293+
queue_payload_to_child(
294+
w->id, w->drop.client_id, &w->drop.pending, header, n, host_machine_id, strlen(host_machine_id), false);
295+
}
296+
}
297+
292298
void
293299
drop_move_on_child(Window *w, const char** mimes, size_t num_mimes, bool is_drop) {
294300
if (!w->drop.hovered) {
@@ -866,13 +872,6 @@ do_drop_request_uri_data(Window *w, int32_t mime_idx, int32_t file_idx) {
866872
return sync;
867873
}
868874

869-
void
870-
drop_request_uri_data(Window *w, const char *payload, size_t payload_sz) {
871-
(void)w; (void)payload; (void)payload_sz;
872-
/* This function is no longer used in the new protocol; URI data requests
873-
* are now handled through the unified t=r queue via do_drop_request_uri_data. */
874-
}
875-
876875
/* Handle a directory request from the client.
877876
* handle_id: the directory handle (Y= key).
878877
* entry_num: 0 means close the handle; >=1 means read that entry (x= key, 1-based).

kitty/dnd.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ void drop_move_on_child(Window *w, const char **mimes, size_t num_mimes, bool is
1414
void drop_left_child(Window *w);
1515
void drop_free_data(Window *w);
1616
void drop_send_einval(Window *w);
17-
void drop_request_uri_data(Window *w, const char *payload, size_t payload_sz);
1817
void drop_handle_dir_request(Window *w, uint32_t handle_id, int32_t entry_num);
1918
void drop_enqueue_request(Window *w, int32_t cell_x, int32_t cell_y, int32_t pixel_y);
2019
void drop_set_status(Window *w, int operation, const char *payload, size_t payload_sz, bool more);

0 commit comments

Comments
 (0)