Coverage Report

Created: 2025-06-24 07:38

/src/mpv/input/event.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * This file is part of mpv.
3
 *
4
 * mpv is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * mpv is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
#include "event.h"
19
#include "input.h"
20
#include "common/msg.h"
21
#include "player/external_files.h"
22
23
void mp_event_drop_files(struct input_ctx *ictx, int num_files, char **files,
24
                         enum mp_dnd_action action)
25
0
{
26
0
    bool all_sub = true;
27
0
    for (int i = 0; i < num_files; i++)
28
0
        all_sub &= mp_might_be_subtitle_file(files[i]);
29
30
0
    if (all_sub) {
31
0
        for (int i = 0; i < num_files; i++) {
32
0
            const char *cmd[] = {
33
0
                "osd-auto",
34
0
                "sub-add",
35
0
                files[i],
36
0
                NULL
37
0
            };
38
0
            mp_input_run_cmd(ictx, cmd);
39
0
        }
40
0
    } else if (action == DND_INSERT_NEXT) {
41
        /* To insert the entries in the correct order, we iterate over them
42
           backwards */
43
0
        for (int i = num_files - 1; i >= 0; i--) {
44
0
            const char *cmd[] = {
45
0
                "osd-auto",
46
0
                "loadfile",
47
0
                files[i],
48
                /* Since we're inserting in reverse, wait til the final item
49
                   is added to start playing */
50
0
                (i > 0) ? "insert-next" : "insert-next-play",
51
0
                NULL
52
0
            };
53
0
            mp_input_run_cmd(ictx, cmd);
54
0
        }
55
0
    } else {
56
0
        for (int i = 0; i < num_files; i++) {
57
0
            const char *cmd[] = {
58
0
                "osd-auto",
59
0
                "loadfile",
60
0
                files[i],
61
                /* Either start playing the dropped files right away
62
                   or add them to the end of the current playlist */
63
0
                (i == 0 && action == DND_REPLACE) ? "replace" : "append-play",
64
0
                NULL
65
0
            };
66
0
            mp_input_run_cmd(ictx, cmd);
67
0
        }
68
0
    }
69
0
}
70
71
int mp_event_drop_mime_data(struct input_ctx *ictx, const char *mime_type,
72
                            bstr data, enum mp_dnd_action action)
73
0
{
74
    // (text lists are the only format supported right now)
75
0
    if (mp_event_get_mime_type_score(ictx, mime_type) >= 0) {
76
0
        void *tmp = talloc_new(NULL);
77
0
        int num_files = 0;
78
0
        char **files = NULL;
79
0
        while (data.len) {
80
0
            bstr line = bstr_getline(data, &data);
81
0
            line = bstr_strip_linebreaks(line);
82
0
            if (bstr_startswith0(line, "#") || !line.start[0])
83
0
                continue;
84
0
            char *s = bstrto0(tmp, line);
85
0
            MP_TARRAY_APPEND(tmp, files, num_files, s);
86
0
        }
87
0
        mp_event_drop_files(ictx, num_files, files, action);
88
0
        talloc_free(tmp);
89
0
        return num_files > 0;
90
0
    } else {
91
0
        return -1;
92
0
    }
93
0
}
94
95
int mp_event_get_mime_type_score(struct input_ctx *ictx, const char *mime_type)
96
0
{
97
    // X11 and Wayland file list format.
98
0
    if (strcmp(mime_type, "text/uri-list") == 0)
99
0
        return 10;
100
    // Just text; treat it the same for convenience.
101
0
    if (strcmp(mime_type, "text/plain;charset=utf-8") == 0)
102
0
        return 5;
103
0
    if (strcmp(mime_type, "text/plain") == 0)
104
0
        return 4;
105
0
    if (strcmp(mime_type, "text") == 0)
106
0
        return 0;
107
0
    return -1;
108
0
}