forked from nurupo/vlc-pause-click-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpause_click.c
More file actions
469 lines (399 loc) · 16.6 KB
/
pause_click.c
File metadata and controls
469 lines (399 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*****************************************************************************
* pause_click.c : A filter that allows to pause/play a video by a mouse click
*****************************************************************************
* Copyright (C) 2014-2020 Maxim Biro
*
* Authors: Maxim Biro <nurupo.contributions@gmail.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#ifdef HAVE_CONFIG_H
# include "config.h"
#else
# define N_(str) (str)
#endif
#include <vlc/libvlc_version.h>
#if LIBVLC_VERSION_MAJOR >= 3
# define VLC_MODULE_LICENSE VLC_LICENSE_LGPL_2_1_PLUS
# define VLC_MODULE_COPYRIGHT "Copyright (C) 2014-2020 Maxim Biro"
#endif
#include <vlc_atomic.h>
#include <vlc_common.h>
#include <vlc_filter.h>
#include <vlc_input.h>
#include <vlc_mouse.h>
#if LIBVLC_VERSION_MAJOR >= 4
# include <vlc_player.h>
#endif
#include <vlc_playlist.h>
#include <vlc_plugin.h>
#if LIBVLC_VERSION_MAJOR == 2
# include <vlc_spu.h>
#endif
#include <vlc_threads.h>
#if LIBVLC_VERSION_MAJOR >= 3
# include <vlc_vout.h>
#endif
#include <vlc_vout_osd.h>
#if LIBVLC_VERSION_MAJOR == 2 && LIBVLC_VERSION_MINOR == 1
# include "vlc_interface-2.1.0-git.h"
#elif LIBVLC_VERSION_MAJOR == 2 && LIBVLC_VERSION_MINOR == 2
# include "vlc_interface-2.2.0-git.h"
#elif LIBVLC_VERSION_MAJOR >= 3 && LIBVLC_VERSION_MINOR >= 0
# include <vlc_interface.h>
#else
# error "VLC version " LIBVLC_VERSION_MAJOR "." LIBVLC_VERSION_MINOR " is too old and won't be supported"
#endif
#define UNUSED(x) (void)(x)
static const char *const mouse_button_names[] = {N_("None"), N_("Left Button"), N_("Middle Button"), N_("Right Button"), N_("Scroll Up"), N_("Scroll Down"), N_("Scroll Left"), N_("Scroll Right")};
static const int mouse_button_values_index[] = {0, 1, 2, 3, 4, 5, 6, 7};
static const int mouse_button_values[] = {-1, MOUSE_BUTTON_LEFT, MOUSE_BUTTON_CENTER, MOUSE_BUTTON_RIGHT, MOUSE_BUTTON_WHEEL_UP, MOUSE_BUTTON_WHEEL_DOWN, MOUSE_BUTTON_WHEEL_LEFT, MOUSE_BUTTON_WHEEL_RIGHT};
#define CFG_PREFIX "pause-click-"
#define MOUSE_BUTTON_CFG CFG_PREFIX "mouse-button"
#define MOUSE_BUTTON_DEFAULT 1 // MOUSE_BUTTON_LEFT
#define DOUBLE_CLICK_DELAY_CFG CFG_PREFIX "double-click-delay"
#define DOUBLE_CLICK_DELAY_DEFAULT 300
#define ENABLE_DOUBLE_CLICK_DELAY_CFG CFG_PREFIX "enable-double-click-delay"
#define ENABLE_DOUBLE_CLICK_DELAY_DEFAULT false
#define IGNORE_DOUBLE_CLICK_CFG CFG_PREFIX "ignore-double-click"
#define IGNORE_DOUBLE_CLICK_DEFAULT false
#define DISABLE_FS_TOGGLE_CFG CFG_PREFIX "disable-fs-toggle"
#define DISABLE_FS_TOGGLE_DEFAULT false
#define FS_TOGGLE_MOUSE_BUTTON_CFG CFG_PREFIX "fs-toggle-mouse-button"
#define FS_TOGGLE_MOUSE_BUTTON_DEFAULT 0 // None
#define DISABLE_CONTEXT_MENU_TOGGLE_CFG CFG_PREFIX "disable-context-menu-toggle"
#define DISABLE_CONTEXT_MENU_TOGGLE_DEFAULT false
#define CONTEXT_MENU_TOGGLE_MOUSE_BUTTON_CFG CFG_PREFIX "context-menu-toggle-mouse-button"
#define CONTEXT_MENU_TOGGLE_MOUSE_BUTTON_DEFAULT 0 // None
#define DISPLAY_ICON_CFG CFG_PREFIX "display-icon"
#define DISPLAY_ICON_DEFAULT true
static int OpenFilter(vlc_object_t *);
static void CloseFilter(vlc_object_t *);
static int OpenInterface(vlc_object_t *);
static void CloseInterface(vlc_object_t *);
static intf_thread_t *p_intf = NULL;
static vlc_timer_t timer;
static bool timer_initialized = false;
static atomic_bool timer_scheduled;
vlc_module_begin()
set_description(N_("Pause/Play video on mouse click"))
set_shortname(N_("Pause click"))
#if LIBVLC_VERSION_MAJOR == 2
set_capability("video filter2", 0)
#elif LIBVLC_VERSION_MAJOR >= 3
set_capability("video filter", 0)
#endif
set_category(CAT_VIDEO)
set_subcategory(SUBCAT_VIDEO_VFILTER)
set_callbacks(OpenFilter, CloseFilter)
set_section(N_("General"), NULL)
add_integer(MOUSE_BUTTON_CFG, MOUSE_BUTTON_DEFAULT,
N_("Pause/play mouse button"),
N_("Defines the mouse button that will pause/play the video. "
"Note that the scroll buttons might not work, that's a bug in VLC."), false)
vlc_config_set(VLC_CONFIG_LIST, (size_t)(sizeof(mouse_button_values_index)/sizeof(int))-1,
mouse_button_values_index+1, mouse_button_names+1);
add_bool(DISPLAY_ICON_CFG, DISPLAY_ICON_DEFAULT,
N_("Show pause/play icon animations"),
N_("Overlay pause and play icons on the video when it's paused and "
"played respectively."), false)
set_section(N_("Double click behavior"), NULL)
add_integer_with_range(DOUBLE_CLICK_DELAY_CFG, DOUBLE_CLICK_DELAY_DEFAULT,
20, 5000, N_("Custom double click interval (milliseconds)"),
N_("Two clicks made during this time interval will "
"be treated as a double click."), false)
add_bool(ENABLE_DOUBLE_CLICK_DELAY_CFG, ENABLE_DOUBLE_CLICK_DELAY_DEFAULT,
N_("Enable the custom double click interval"),
N_("Ignore system's double click interval and use our own instead. "
"Useful when you want to make the time interval of double clicking "
"to fullscreen be longer or shorter."), false)
add_bool(IGNORE_DOUBLE_CLICK_CFG, IGNORE_DOUBLE_CLICK_DEFAULT,
N_("Prevent pause/play from triggering on double click*"),
N_("Useful if you don't want the video to pause/play when double "
"clicking to fullscreen. Note that enabling this will delay "
"pause/play action by the double click interval, so the experience "
"might not be as snappy as with this option disabled."
"\n\n*Forces the use of the custom double click interval."), false)
set_section(N_("Mouse button assignment"), NULL)
add_bool(DISABLE_FS_TOGGLE_CFG, DISABLE_FS_TOGGLE_DEFAULT,
N_("Disable fullscreen toggle on double click"),
N_("The video will no longer fullscreen if you double click on it."), false)
add_integer(FS_TOGGLE_MOUSE_BUTTON_CFG, FS_TOGGLE_MOUSE_BUTTON_DEFAULT,
N_("Assign fullscreen toggle to"),
N_("Assigns fullscreen toggle to a mouse button."), false)
change_integer_list(mouse_button_values_index, mouse_button_names)
add_bool(DISABLE_CONTEXT_MENU_TOGGLE_CFG, DISABLE_CONTEXT_MENU_TOGGLE_DEFAULT,
N_("Disable context menu toggle on right click"),
N_("The context menu will no longer pop up if you right click on the video. "
"Useful if you want to pause/play or full screen on right click."), false)
add_integer(CONTEXT_MENU_TOGGLE_MOUSE_BUTTON_CFG, CONTEXT_MENU_TOGGLE_MOUSE_BUTTON_DEFAULT,
N_("Assign context menu toggle to"),
N_("Assigns context menu toggle to a mouse button."), false)
change_integer_list(mouse_button_values_index, mouse_button_names)
add_submodule()
set_capability("interface", 0)
set_category(CAT_INTERFACE)
set_subcategory(SUBCAT_INTERFACE_CONTROL)
set_callbacks(OpenInterface, CloseInterface)
vlc_module_end()
static bool is_in_menu(void) {
if (!p_intf) {
return false;
}
#if 2 <= LIBVLC_VERSION_MAJOR && LIBVLC_VERSION_MAJOR <= 3
playlist_t* p_playlist = pl_Get(p_intf);
input_title_t* p_title = NULL;
int i_title_id = -1;
input_thread_t* p_input = playlist_CurrentInput(p_playlist);
if (!p_input) {
return false;
}
if (input_Control(p_input, INPUT_GET_TITLE_INFO, &p_title, &i_title_id) != VLC_SUCCESS) {
vlc_object_release(p_input);
return false;
}
vlc_object_release(p_input);
if (!p_title) {
return false;
}
if (
#if LIBVLC_VERSION_MAJOR == 2
p_title->b_menu
#elif LIBVLC_VERSION_MAJOR >= 3
p_title->i_flags & INPUT_TITLE_MENU || p_title->i_flags & INPUT_TITLE_INTERACTIVE
#endif
) {
vlc_input_title_Delete(p_title);
return true;
}
vlc_input_title_Delete(p_title);
return false;
#elif LIBVLC_VERSION_MAJOR >= 4
vlc_player_t* player = vlc_playlist_GetPlayer(vlc_intf_GetMainPlaylist(p_intf));
vlc_player_Lock(player);
const struct vlc_player_title* title = vlc_player_GetSelectedTitle(player);
if (!title) {
vlc_player_Unlock(player);
return false;
}
if (title->flags & VLC_PLAYER_TITLE_MENU || title->flags & VLC_PLAYER_TITLE_INTERACTIVE) {
vlc_player_Unlock(player);
return true;
}
vlc_player_Unlock(player);
return false;
#endif
}
static void display_icon(short icon) {
if (!p_intf) {
return;
}
#if 2 <= LIBVLC_VERSION_MAJOR && LIBVLC_VERSION_MAJOR <= 3
playlist_t* p_playlist = pl_Get(p_intf);
input_thread_t* p_input = playlist_CurrentInput(p_playlist);
if (!p_input) {
return;
}
vout_thread_t** pp_vout;
size_t i_vout;
if (input_Control(p_input, INPUT_GET_VOUTS, &pp_vout, &i_vout) != VLC_SUCCESS) {
vlc_object_release(p_input);
return;
}
for (size_t i = 0; i < i_vout; i ++) {
vout_OSDIcon(pp_vout[i],
#if LIBVLC_VERSION_MAJOR == 2
SPU_DEFAULT_CHANNEL,
#elif LIBVLC_VERSION_MAJOR >= 3
VOUT_SPU_CHANNEL_OSD,
#endif
icon);
vlc_object_release((vlc_object_t *)pp_vout[i]);
}
vlc_object_release(p_input);
free(pp_vout);
#elif LIBVLC_VERSION_MAJOR >= 4
vlc_player_t* player = vlc_playlist_GetPlayer(vlc_intf_GetMainPlaylist(p_intf));
vlc_player_Lock(player);
vout_thread_t** pp_vout;
size_t i_vout;
pp_vout = vlc_player_vout_HoldAll(player, &i_vout);
if (!pp_vout) {
vlc_player_Unlock(player);
return;
}
for (size_t i = 0; i < i_vout; i ++) {
vout_OSDIcon(pp_vout[i], VOUT_SPU_CHANNEL_OSD, icon);
vout_Release(pp_vout[i]);
}
vlc_player_Unlock(player);
free(pp_vout);
#endif
}
static void pause_play(void)
{
if (!p_intf) {
return;
}
if (is_in_menu()) {
return;
}
#if 2 <= LIBVLC_VERSION_MAJOR && LIBVLC_VERSION_MAJOR <= 3
playlist_t* p_playlist = pl_Get(p_intf);
playlist_status_t status = playlist_Status(p_playlist);
playlist_Control(p_playlist, status == PLAYLIST_RUNNING ? PLAYLIST_PAUSE : PLAYLIST_PLAY , 0);
if (var_InheritBool(p_intf, DISPLAY_ICON_CFG)) {
display_icon(status == PLAYLIST_RUNNING ? OSD_PAUSE_ICON : OSD_PLAY_ICON);
}
#elif LIBVLC_VERSION_MAJOR >= 4
vlc_player_t* player = vlc_playlist_GetPlayer(vlc_intf_GetMainPlaylist(p_intf));
vlc_player_Lock(player);
int state = vlc_player_GetState(player);
state == VLC_PLAYER_STATE_PLAYING ? vlc_player_Pause(player) : vlc_player_Resume(player);
vlc_player_Unlock(player);
if (var_InheritBool(p_intf, DISPLAY_ICON_CFG)) {
display_icon(state == VLC_PLAYER_STATE_PLAYING ? OSD_PAUSE_ICON : OSD_PLAY_ICON);
}
#endif
}
static void timer_callback(void* data)
{
if (!atomic_load(&timer_scheduled)) {
return;
}
filter_t *p_filter = (filter_t *) data;
if (var_InheritBool(p_filter, IGNORE_DOUBLE_CLICK_CFG)) {
pause_play();
}
atomic_store(&timer_scheduled, false);
}
static int cfg_get_mouse_button(vlc_object_t *p_obj, const char *cfg, int default_value) {
int mouse_button = mouse_button_values[default_value];
int mouse_button_index = var_InheritInteger(p_obj, cfg);
if (mouse_button_index >= 0 && (size_t)mouse_button_index <= sizeof(mouse_button_values)-1) {
mouse_button = mouse_button_values[mouse_button_index];
}
return mouse_button;
}
static int mouse(filter_t *p_filter, vlc_mouse_t *p_mouse_out, const vlc_mouse_t *p_mouse_old, const vlc_mouse_t *p_mouse_new)
{
// we don't want to process anything if no mouse button was clicked
if (p_mouse_new->i_pressed == 0 && !p_mouse_new->b_double_click) {
return VLC_EGENERIC;
}
// get mouse button from settings. updates if user changes the setting
const int mouse_button = cfg_get_mouse_button((vlc_object_t *)p_filter, MOUSE_BUTTON_CFG, MOUSE_BUTTON_DEFAULT);
bool filter = false;
*p_mouse_out = *p_mouse_new;
// manually control double click to fullscreen if directly requested or if the ignore double click option is set
if ((var_InheritBool(p_filter, ENABLE_DOUBLE_CLICK_DELAY_CFG) || var_InheritBool(p_filter, IGNORE_DOUBLE_CLICK_CFG)) &&
mouse_button == MOUSE_BUTTON_LEFT) {
p_mouse_out->b_double_click = 0;
filter = true;
}
// react only on the configured mouse button click
if (vlc_mouse_HasPressed(p_mouse_old, p_mouse_new, mouse_button) ||
// treat the double click as the left mouse button click
(p_mouse_new->b_double_click && mouse_button == MOUSE_BUTTON_LEFT)) {
// pause/play on every click unless told not to
if (!var_InheritBool(p_filter, IGNORE_DOUBLE_CLICK_CFG)) {
pause_play();
}
// do the double click logic
if ((var_InheritBool(p_filter, IGNORE_DOUBLE_CLICK_CFG) || var_InheritBool(p_filter, ENABLE_DOUBLE_CLICK_DELAY_CFG)) &&
mouse_button == MOUSE_BUTTON_LEFT && timer_initialized) {
if (atomic_load(&timer_scheduled)) {
// it's a double click -- cancel the scheduled timer
atomic_store(&timer_scheduled, false);
vlc_timer_schedule(timer, false, 0, 0);
// and set fullscreen
p_mouse_out->b_double_click = 1;
filter = true;
} else {
// it might be a single click -- schedule a timer
atomic_store(&timer_scheduled, true);
vlc_timer_schedule(timer, false, var_InheritInteger(p_filter, DOUBLE_CLICK_DELAY_CFG)*1000, 0);
}
}
}
// prevent fullscreen from toggling on double click
if (var_InheritBool(p_filter, DISABLE_FS_TOGGLE_CFG) && p_mouse_new->b_double_click) {
p_mouse_out->b_double_click = 0;
filter = true;
}
// toggle fullscreen on specified mouse click
const int fs_mouse_button = cfg_get_mouse_button((vlc_object_t *)p_filter, FS_TOGGLE_MOUSE_BUTTON_CFG,
FS_TOGGLE_MOUSE_BUTTON_DEFAULT);
if (fs_mouse_button != -1 && vlc_mouse_HasPressed(p_mouse_old, p_mouse_new, fs_mouse_button)) {
p_mouse_out->b_double_click = 1;
filter = true;
}
// prevent the context menu from toggling on right click
if (var_InheritBool(p_filter, DISABLE_CONTEXT_MENU_TOGGLE_CFG) &&
vlc_mouse_IsPressed(p_mouse_new, MOUSE_BUTTON_RIGHT)) {
p_mouse_out->i_pressed = 0;
filter = true;
}
// toggle context menu on specified mouse click
const int context_menu_mouse_button =
cfg_get_mouse_button((vlc_object_t *)p_filter,CONTEXT_MENU_TOGGLE_MOUSE_BUTTON_CFG,
CONTEXT_MENU_TOGGLE_MOUSE_BUTTON_DEFAULT);
if (context_menu_mouse_button != -1 &&
vlc_mouse_HasPressed(p_mouse_old, p_mouse_new, context_menu_mouse_button)) {
vlc_mouse_SetPressed(p_mouse_out, MOUSE_BUTTON_RIGHT);
filter = true;
}
return filter ? VLC_SUCCESS : VLC_EGENERIC;
}
static picture_t *filter(filter_t *p_filter, picture_t *p_pic_in)
{
UNUSED(p_filter);
// don't alter picture
return p_pic_in;
}
static int OpenFilter(vlc_object_t *p_this)
{
filter_t *p_filter = (filter_t *) p_this;
p_filter->pf_video_filter = filter;
p_filter->pf_video_mouse = mouse;
if (vlc_timer_create(&timer, &timer_callback, p_filter)) {
return VLC_EGENERIC;
}
timer_initialized = true;
atomic_store(&timer_scheduled, false);
return VLC_SUCCESS;
}
static void CloseFilter(vlc_object_t *p_this)
{
UNUSED(p_this);
if (timer_initialized) {
vlc_timer_destroy(timer);
timer_initialized = false;
atomic_store(&timer_scheduled, false);
}
}
static int OpenInterface(vlc_object_t *p_this)
{
p_intf = (intf_thread_t*) p_this;
return VLC_SUCCESS;
}
static void CloseInterface(vlc_object_t *p_this)
{
UNUSED(p_this);
p_intf = NULL;
}