MacOS: remove some callbacks
[vlc.git] / src / video_output / control.c
blob6e33b458f14b9d7a4bd3d54ac036db8a6ccbff0e
1 /*****************************************************************************
2 * control.c : vout internal control
3 *****************************************************************************
4 * Copyright (C) 2009 Laurent Aimar
5 * $Id$
7 * Authors: Laurent Aimar <fenrir _AT_ videolan _DOT_ org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <vlc_common.h>
29 #include <vlc_vout.h>
30 #include "control.h"
32 /* */
33 void vout_control_cmd_Init(vout_control_cmd_t *cmd, int type)
35 memset(cmd, 0, sizeof(*cmd));
36 cmd->type = type;
39 void vout_control_cmd_Clean(vout_control_cmd_t *cmd)
41 switch (cmd->type) {
42 case VOUT_CONTROL_SUBPICTURE:
43 if (cmd->u.subpicture)
44 subpicture_Delete(cmd->u.subpicture);
45 break;
46 case VOUT_CONTROL_OSD_TITLE:
47 case VOUT_CONTROL_CHANGE_FILTERS:
48 case VOUT_CONTROL_CHANGE_SUB_SOURCES:
49 case VOUT_CONTROL_CHANGE_SUB_FILTERS:
50 free(cmd->u.string);
51 break;
52 default:
53 break;
57 /* */
58 void vout_control_Init(vout_control_t *ctrl)
60 vlc_mutex_init(&ctrl->lock);
61 vlc_cond_init(&ctrl->wait_request);
62 vlc_cond_init(&ctrl->wait_acknowledge);
64 ctrl->is_dead = false;
65 ctrl->is_sleeping = false;
66 ctrl->can_sleep = true;
67 ctrl->is_processing = false;
68 ARRAY_INIT(ctrl->cmd);
71 void vout_control_Clean(vout_control_t *ctrl)
73 /* */
74 for (int i = 0; i < ctrl->cmd.i_size; i++) {
75 vout_control_cmd_t cmd = ARRAY_VAL(ctrl->cmd, i);
76 vout_control_cmd_Clean(&cmd);
78 ARRAY_RESET(ctrl->cmd);
80 vlc_mutex_destroy(&ctrl->lock);
81 vlc_cond_destroy(&ctrl->wait_request);
82 vlc_cond_destroy(&ctrl->wait_acknowledge);
85 void vout_control_Dead(vout_control_t *ctrl)
87 vlc_mutex_lock(&ctrl->lock);
88 ctrl->is_dead = true;
89 vlc_cond_broadcast(&ctrl->wait_acknowledge);
90 vlc_mutex_unlock(&ctrl->lock);
94 void vout_control_WaitEmpty(vout_control_t *ctrl)
96 vlc_mutex_lock(&ctrl->lock);
97 while ((ctrl->cmd.i_size > 0 || ctrl->is_processing) && !ctrl->is_dead)
98 vlc_cond_wait(&ctrl->wait_acknowledge, &ctrl->lock);
99 vlc_mutex_unlock(&ctrl->lock);
102 void vout_control_Push(vout_control_t *ctrl, vout_control_cmd_t *cmd)
104 vlc_mutex_lock(&ctrl->lock);
105 if (!ctrl->is_dead) {
106 ARRAY_APPEND(ctrl->cmd, *cmd);
107 vlc_cond_signal(&ctrl->wait_request);
108 } else {
109 vout_control_cmd_Clean(cmd);
111 vlc_mutex_unlock(&ctrl->lock);
114 void vout_control_Wake(vout_control_t *ctrl)
116 vlc_mutex_lock(&ctrl->lock);
117 ctrl->can_sleep = false;
118 if (ctrl->is_sleeping)
119 vlc_cond_signal(&ctrl->wait_request);
120 vlc_mutex_unlock(&ctrl->lock);
123 void vout_control_PushVoid(vout_control_t *ctrl, int type)
125 vout_control_cmd_t cmd;
127 vout_control_cmd_Init(&cmd, type);
128 vout_control_Push(ctrl, &cmd);
130 void vout_control_PushBool(vout_control_t *ctrl, int type, bool boolean)
132 vout_control_cmd_t cmd;
134 vout_control_cmd_Init(&cmd, type);
135 cmd.u.boolean = boolean;
136 vout_control_Push(ctrl, &cmd);
138 void vout_control_PushInteger(vout_control_t *ctrl, int type, int integer)
140 vout_control_cmd_t cmd;
142 vout_control_cmd_Init(&cmd, type);
143 cmd.u.integer = integer;
144 vout_control_Push(ctrl, &cmd);
146 void vout_control_PushTime(vout_control_t *ctrl, int type, mtime_t time)
148 vout_control_cmd_t cmd;
150 vout_control_cmd_Init(&cmd, type);
151 cmd.u.time = time;
152 vout_control_Push(ctrl, &cmd);
154 void vout_control_PushMessage(vout_control_t *ctrl, int type, int channel, const char *string)
156 vout_control_cmd_t cmd;
158 vout_control_cmd_Init(&cmd, type);
159 cmd.u.message.channel = channel;
160 cmd.u.message.string = strdup(string);
161 vout_control_Push(ctrl, &cmd);
163 void vout_control_PushPair(vout_control_t *ctrl, int type, int a, int b)
165 vout_control_cmd_t cmd;
167 vout_control_cmd_Init(&cmd, type);
168 cmd.u.pair.a = a;
169 cmd.u.pair.b = b;
170 vout_control_Push(ctrl, &cmd);
172 void vout_control_PushString(vout_control_t *ctrl, int type, const char *string)
174 vout_control_cmd_t cmd;
176 vout_control_cmd_Init(&cmd, type);
177 cmd.u.string = strdup(string);
178 vout_control_Push(ctrl, &cmd);
181 int vout_control_Pop(vout_control_t *ctrl, vout_control_cmd_t *cmd,
182 mtime_t deadline, mtime_t timeout)
184 vlc_mutex_lock(&ctrl->lock);
185 if (ctrl->cmd.i_size <= 0) {
186 ctrl->is_processing = false;
187 vlc_cond_broadcast(&ctrl->wait_acknowledge);
189 const mtime_t max_deadline = mdate() + timeout;
191 /* Supurious wake up are perfectly fine */
192 if (deadline <= VLC_TS_INVALID) {
193 ctrl->is_sleeping = true;
194 if (ctrl->can_sleep)
195 vlc_cond_timedwait(&ctrl->wait_request, &ctrl->lock, max_deadline);
196 ctrl->is_sleeping = false;
197 } else {
198 vlc_cond_timedwait(&ctrl->wait_request, &ctrl->lock, __MIN(deadline, max_deadline));
202 bool has_cmd;
203 if (ctrl->cmd.i_size > 0) {
204 has_cmd = true;
205 *cmd = ARRAY_VAL(ctrl->cmd, 0);
206 ARRAY_REMOVE(ctrl->cmd, 0);
208 ctrl->is_processing = true;
209 } else {
210 has_cmd = false;
211 ctrl->can_sleep = true;
213 vlc_mutex_unlock(&ctrl->lock);
215 return has_cmd ? VLC_SUCCESS : VLC_EGENERIC;