History

video/out/wayland_common: extract data offer to a struct

The data offers can be either dnd or selection offers. Add a separate struct for it so that handling of selection offers can be added.

nanahi committed 2 years ago

Commit eb0c4b91 parent 68d9ed22 files · +71 −61Browse files

Unified · Split

2 files
  1. video/out/wayland_common.c +68 −55
  2. video/out/wayland_common.h +3 −6
video/out/wayland_common.c+68−55
@@ -221,6 +221,14 @@ struct vo_wayland_tranche { · 220 unmodified lines
221221 struct wl_list link;
222222 };
223223
224+struct vo_wayland_data_offer {
225+ struct wl_data_offer *offer;
226+ int action; // actually enum mp_dnd_action
227+ char *mime_type;
228+ int fd;
229+ int mime_score;
230+};
231+
224232 static bool single_output_spanned(struct vo_wayland_state *wl);
225233
226234 static int check_for_resize(struct vo_wayland_state *wl, int edge_pixels,
@@ -688,11 +696,12 @@ static void data_offer_handle_offer(void *data, struct wl_data_offer *offer, · 461 unmodified lines
688696 {
689697 struct vo_wayland_seat *s = data;
690698 struct vo_wayland_state *wl = s->wl;
699+ struct vo_wayland_data_offer *o = wl->dnd_offer;
691700 int score = mp_event_get_mime_type_score(wl->vo->input_ctx, mime_type);
692− if (score > wl->dnd_mime_score && wl->opts->drag_and_drop != -2) {
693− wl->dnd_mime_score = score;
694− talloc_replace(wl, wl->dnd_mime_type, mime_type);
695− MP_VERBOSE(wl, "Given DND offer with mime type %s\n", wl->dnd_mime_type);
701+ if (score > o->mime_score && wl->opts->drag_and_drop != -2) {
702+ o->mime_score = score;
703+ talloc_replace(wl, o->mime_type, mime_type);
704+ MP_VERBOSE(wl, "Given DND offer with mime type %s\n", o->mime_type);
696705 }
697706 }
698707
@@ -704,12 +713,13 @@ static void data_offer_action(void *data, struct wl_data_offer *wl_data_offer, u · 5 unmodified lines
704713 {
705714 struct vo_wayland_seat *s = data;
706715 struct vo_wayland_state *wl = s->wl;
716+ struct vo_wayland_data_offer *o = wl->dnd_offer;
707717 if (dnd_action && wl->opts->drag_and_drop != -2) {
708718 if (wl->opts->drag_and_drop >= 0) {
709− wl->dnd_action = wl->opts->drag_and_drop;
719+ o->action = wl->opts->drag_and_drop;
710720 } else {
711− wl->dnd_action = dnd_action & WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY ?
712− DND_REPLACE : DND_APPEND;
721+ o->action = dnd_action & WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY ?
722+ DND_REPLACE : DND_APPEND;
713723 }
714724
715725 static const char * const dnd_action_names[] = {
@@ -718,7 +728,7 @@ static void data_offer_action(void *data, struct wl_data_offer *wl_data_offer, u · 2 unmodified lines
718728 [DND_INSERT_NEXT] = "DND_INSERT_NEXT",
719729 };
720730
721− MP_VERBOSE(wl, "DND action is %s\n", dnd_action_names[wl->dnd_action]);
731+ MP_VERBOSE(wl, "DND action is %s\n", dnd_action_names[o->action]);
722732 }
723733 }
724734
@@ -733,10 +743,11 @@ static void data_device_handle_data_offer(void *data, struct wl_data_device *wl_ · 8 unmodified lines
733743 {
734744 struct vo_wayland_seat *s = data;
735745 struct vo_wayland_state *wl = s->wl;
736− if (wl->dnd_offer)
737− wl_data_offer_destroy(wl->dnd_offer);
746+ struct vo_wayland_data_offer *o = wl->dnd_offer;
747+ if (o->offer)
748+ wl_data_offer_destroy(o->offer);
738749
739− wl->dnd_offer = id;
750+ o->offer = id;
740751 wl_data_offer_add_listener(id, &data_offer_listener, s);
741752 }
742753
@@ -747,7 +758,8 @@ static void data_device_handle_enter(void *data, struct wl_data_device *wl_ddev, · 4 unmodified lines
747758 {
748759 struct vo_wayland_seat *s = data;
749760 struct vo_wayland_state *wl = s->wl;
750− if (wl->dnd_offer != id) {
761+ struct vo_wayland_data_offer *o = wl->dnd_offer;
762+ if (o->offer != id) {
751763 MP_FATAL(wl, "DND offer ID mismatch!\n");
752764 return;
753765 }
@@ -756,8 +768,8 @@ static void data_device_handle_enter(void *data, struct wl_data_device *wl_ddev, · 2 unmodified lines
756768 wl_data_offer_set_actions(id, WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY |
757769 WL_DATA_DEVICE_MANAGER_DND_ACTION_MOVE,
758770 WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY);
759− wl_data_offer_accept(id, serial, wl->dnd_mime_type);
760− MP_VERBOSE(wl, "Accepting DND offer with mime type %s\n", wl->dnd_mime_type);
771+ wl_data_offer_accept(id, serial, o->mime_type);
772+ MP_VERBOSE(wl, "Accepting DND offer with mime type %s\n", o->mime_type);
761773 }
762774
763775 }
@@ -766,19 +778,20 @@ static void data_device_handle_leave(void *data, struct wl_data_device *wl_ddev) · 2 unmodified lines
766778 {
767779 struct vo_wayland_seat *s = data;
768780 struct vo_wayland_state *wl = s->wl;
781+ struct vo_wayland_data_offer *o = wl->dnd_offer;
769782
770− if (wl->dnd_offer) {
771− if (wl->dnd_fd != -1)
783+ if (o->offer) {
784+ if (o->fd != -1)
772785 return;
773− wl_data_offer_destroy(wl->dnd_offer);
774− wl->dnd_offer = NULL;
786+ wl_data_offer_destroy(o->offer);
787+ o->offer = NULL;
775788 }
776789
777790 if (wl->opts->drag_and_drop != -2) {
778− MP_VERBOSE(wl, "Releasing DND offer with mime type %s\n", wl->dnd_mime_type);
779− if (wl->dnd_mime_type)
780− TA_FREEP(&wl->dnd_mime_type);
781− wl->dnd_mime_score = 0;
791+ MP_VERBOSE(wl, "Releasing DND offer with mime type %s\n", o->mime_type);
792+ if (o->mime_type)
793+ TA_FREEP(&o->mime_type);
794+ o->mime_score = 0;
782795 }
783796 }
784797
@@ -787,13 +800,15 @@ static void data_device_handle_motion(void *data, struct wl_data_device *wl_ddev · 2 unmodified lines
787800 {
788801 struct vo_wayland_seat *s = data;
789802 struct vo_wayland_state *wl = s->wl;
790− wl_data_offer_accept(wl->dnd_offer, time, wl->dnd_mime_type);
803+ struct vo_wayland_data_offer *o = wl->dnd_offer;
804+ wl_data_offer_accept(o->offer, time, o->mime_type);
791805 }
792806
793807 static void data_device_handle_drop(void *data, struct wl_data_device *wl_ddev)
794808 {
795809 struct vo_wayland_seat *s = data;
796810 struct vo_wayland_state *wl = s->wl;
811+ struct vo_wayland_data_offer *o = wl->dnd_offer;
797812
798813 int pipefd[2];
799814
@@ -803,12 +818,12 @@ static void data_device_handle_drop(void *data, struct wl_data_device *wl_ddev) · 3 unmodified lines
803818 }
804819
805820 if (wl->opts->drag_and_drop != -2) {
806− MP_VERBOSE(wl, "Receiving DND offer with mime %s\n", wl->dnd_mime_type);
807− wl_data_offer_receive(wl->dnd_offer, wl->dnd_mime_type, pipefd[1]);
821+ MP_VERBOSE(wl, "Receiving DND offer with mime %s\n", o->mime_type);
822+ wl_data_offer_receive(o->offer, o->mime_type, pipefd[1]);
808823 }
809824
810825 close(pipefd[1]);
811− wl->dnd_fd = pipefd[0];
826+ o->fd = pipefd[0];
812827 }
813828
814829 static void data_device_handle_selection(void *data, struct wl_data_device *wl_ddev,
@@ -816,10 +831,11 @@ static void data_device_handle_selection(void *data, struct wl_data_device *wl_d · 1 unmodified line
816831 {
817832 struct vo_wayland_seat *s = data;
818833 struct vo_wayland_state *wl = s->wl;
834+ struct vo_wayland_data_offer *o = wl->dnd_offer;
819835
820− if (wl->dnd_offer) {
821− wl_data_offer_destroy(wl->dnd_offer);
822− wl->dnd_offer = NULL;
836+ if (o->offer) {
837+ wl_data_offer_destroy(o->offer);
838+ o->offer = NULL;
823839 MP_VERBOSE(wl, "Received a new DND offer. Releasing the previous offer.\n");
824840 }
825841
@@ -1830,21 +1846,23 @@ static void apply_keepaspect(struct vo_wayland_state *wl, int *width, int *heigh · 1004 unmodified lines
18301846 }
18311847 }
18321848
1833−static void free_dnd_data(struct vo_wayland_state *wl)
1849+static void destroy_offer(struct vo_wayland_data_offer *o)
18341850 {
1835− // caller should close wl->dnd_fd if appropriate
1836−
1837− wl->dnd_action = -1;
1838− TA_FREEP(&wl->dnd_mime_type);
1839− wl->dnd_mime_score = 0;
1851+ TA_FREEP(&o->mime_type);
1852+ if (o->fd != -1)
1853+ close(o->fd);
1854+ if (o->offer)
1855+ wl_data_offer_destroy(o->offer);
1856+ *o = (struct vo_wayland_data_offer){.fd = -1, .action = -1};
18401857 }
18411858
18421859 static void check_dnd_fd(struct vo_wayland_state *wl)
18431860 {
1844− if (wl->dnd_fd == -1)
1861+ struct vo_wayland_data_offer *o = wl->dnd_offer;
1862+ if (o->fd == -1)
18451863 return;
18461864
1847− struct pollfd fdp = { wl->dnd_fd, POLLIN | POLLHUP, 0 };
1865+ struct pollfd fdp = { o->fd, POLLIN | POLLHUP, 0 };
18481866 if (poll(&fdp, 1, 0) <= 0)
18491867 return;
18501868
@@ -1856,7 +1874,7 @@ static void check_dnd_fd(struct vo_wayland_state *wl) · 5 unmodified lines
18561874 };
18571875
18581876 while (1) {
1859− data_read = read(wl->dnd_fd, file_list.start + file_list.len, chunk_size);
1877+ data_read = read(o->fd, file_list.start + file_list.len, chunk_size);
18601878 if (data_read == -1 && errno == EINTR)
18611879 continue;
18621880 else if (data_read <= 0)
@@ -1871,24 +1889,24 @@ static void check_dnd_fd(struct vo_wayland_state *wl) · 8 unmodified lines
18711889 } else {
18721890 MP_VERBOSE(wl, "Read %zu bytes from the DND fd\n", file_list.len);
18731891
1874− if (wl->dnd_offer)
1875− wl_data_offer_finish(wl->dnd_offer);
1892+ if (o->offer)
1893+ wl_data_offer_finish(o->offer);
18761894
1877− assert(wl->dnd_action >= 0);
1878− mp_event_drop_mime_data(wl->vo->input_ctx, wl->dnd_mime_type,
1879− file_list, wl->dnd_action);
1895+ assert(o->action >= 0);
1896+ mp_event_drop_mime_data(wl->vo->input_ctx, o->mime_type,
1897+ file_list, o->action);
18801898 }
18811899
18821900 talloc_free(file_list.start);
1883− free_dnd_data(wl);
1901+ free_offer_data(wl->dnd_offer);
18841902 }
18851903
18861904 if (fdp.revents & (POLLIN | POLLERR | POLLHUP)) {
1887− if (wl->dnd_action >= 0)
1905+ if (o->action >= 0)
18881906 MP_VERBOSE(wl, "DND aborted (hang up or error)\n");
1889− free_dnd_data(wl);
1890− close(wl->dnd_fd);
1891− wl->dnd_fd = -1;
1907+ free_offer_data(wl->dnd_offer);
1908+ close(o->fd);
1909+ o->fd = -1;
18921910 }
18931911 }
18941912
@@ -3040,7 +3058,7 @@ bool vo_wayland_init(struct vo *vo) · 1145 unmodified lines
30403058 .scaling = WAYLAND_SCALE_FACTOR,
30413059 .wakeup_pipe = {-1, -1},
30423060 .display_fd = -1,
3043− .dnd_fd = -1,
3061+ .dnd_offer = talloc_zero(wl, struct vo_wayland_data_offer),
30443062 .cursor_visible = true,
30453063 .opts_cache = m_config_cache_alloc(wl, vo->global, &vo_sub_opts),
30463064 };
@@ -3269,9 +3287,7 @@ void vo_wayland_uninit(struct vo *vo) · 222 unmodified lines
32693287 if (!wl)
32703288 return;
32713289
3272− if (wl->dnd_fd != -1)
3273− close(wl->dnd_fd);
3274− free_dnd_data(wl);
3290+ destroy_offer(wl->dnd_offer);
32753291
32763292 mp_input_put_key(wl->vo->input_ctx, MP_INPUT_RELEASE_ALL);
32773293
@@ -3313,9 +3329,6 @@ void vo_wayland_uninit(struct vo *vo) · 35 unmodified lines
33133329 if (wl->dnd_devman)
33143330 wl_data_device_manager_destroy(wl->dnd_devman);
33153331
3316− if (wl->dnd_offer)
3317− wl_data_offer_destroy(wl->dnd_offer);
3318−
33193332 if (wl->fback_pool)
33203333 clean_feedback_pool(wl->fback_pool);
33213334
video/out/wayland_common.h+3−6
@@ -27,6 +27,7 @@ · 26 unmodified lines
2727 struct compositor_format;
2828 struct vo_wayland_seat;
2929 struct vo_wayland_tranche;
30+struct vo_wayland_data_offer;
3031
3132 struct drm_format {
3233 uint32_t format;
@@ -160,13 +161,9 @@ struct vo_wayland_state { · 127 unmodified lines
160161 struct wl_list seat_list;
161162 struct xkb_context *xkb_context;
162163
163− /* DND */
164+ /* Data offer */
164165 struct wl_data_device_manager *dnd_devman;
165− struct wl_data_offer *dnd_offer;
166− int dnd_action; // actually enum mp_dnd_action
167− char *dnd_mime_type;
168− int dnd_fd;
169− int dnd_mime_score;
166+ struct vo_wayland_data_offer *dnd_offer;
170167
171168 /* Cursor */
172169 struct wl_cursor_theme *cursor_theme;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t