fix README
[melted_gui] / src / control.c
1 /*
2 * control.c -- GTK+ 2 melted gui
3 * Copyright (C) 2012 Maksym Veremeyenko <verem@m1stereo.tv>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #ifndef _GNU_SOURCE
25 #define _GNU_SOURCE
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <gtk/gtk.h>
32 #include <gdk/gdkkeysyms.h>
33 #include <pthread.h>
34 #include <string.h>
35
36 #include <mvcp/mvcp.h>
37 #include <mvcp/mvcp_remote.h>
38
39 #include "control.h"
40 #include "ui.h"
41 #include "timecode.h"
42 #include "support.h"
43 #include "playlist.h"
44
45 void control_release(instance_t* app)
46 {
47 };
48
49 void control_init(instance_t* app)
50 {
51 };
52
53 typedef struct player_handle_desc
54 {
55 mvcp_parser parser;
56 mvcp command;
57 mvcp status;
58 } player_handle_t;
59
60 int control_route(instance_t* app, control_buttons_t button)
61 {
62 int i;
63 int idx, start, stop;
64 player_t *player;
65 player_handle_t* handle;
66 mvcp_error_code m;
67
68 pthread_mutex_lock(&app->playlist.lock);
69
70 idx = playlist_get_first_selected_item_idx(app);
71
72 if(idx < 0)
73 {
74 pthread_mutex_unlock(&app->playlist.lock);
75 return -1;
76 };
77
78 g_warning("control_route: selected item is %d\n", idx);
79
80 if(playlist_get_block(app, idx, &start, &stop) < 0)
81 {
82 pthread_mutex_unlock(&app->playlist.lock);
83 return -2;
84 };
85
86 g_warning("control_route: range %d -> %d\n", start, stop);
87
88 player = playlist_get_player_at_pos(app, start);
89
90 if(!player)
91 {
92 pthread_mutex_unlock(&app->playlist.lock);
93 return -3;
94 };
95
96 handle = player->handle;
97
98 pthread_mutex_lock(&app->players.lock);
99
100 if(BUTTON_PLAYER_STOP == button || BUTTON_PLAYER_CUE == button)
101 {
102 /* stop */
103 mvcp_unit_stop(handle->command, player->unit);
104
105 /* detach previous clips */
106 player->playlist_length = -1;
107 mvcp_unit_clear(handle->command, player->unit);
108 };
109
110 if(BUTTON_PLAYER_CUE == button)
111 {
112 playlist_item_t* item;
113 int o, c, j;
114
115 /* reset int_idx */
116 for(i = start; i <= stop; i++)
117 app->playlist.item[i].int_idx = -1;
118
119 /* Attach clips to timeline */
120 for(i = 0, c = 0, o = 0; i < (stop - start + 1); i++)
121 {
122 /* calc seq to playlist grid */
123 j = idx + i;
124 if(j > stop)
125 {
126 if(app->playlist.item[start].type & PLAYLIST_BLOCK_LOOP)
127 j = start + j - stop - 1;
128 else
129 continue;
130 };
131 g_warning("control_route: i = %d, j = %d\n", i, j);
132
133 item = &app->playlist.item[j];
134
135 /* append clip */
136 m = mvcp_unit_append(handle->command, player->unit,
137 item->id, item->in, item->in + item->dur - 1);
138
139 if(mvcp_ok != m)
140 {
141 g_warning("cue: failed with %d, %s\n", m, "unknown" /*OmPlrGetErrorString((OmPlrError)r)*/);
142 item->error |= PLAYLIST_ITEM_ERROR_CUE;
143 }
144 else
145 {
146 item->int_idx = i;
147 item->error &= 0xF ^ PLAYLIST_ITEM_ERROR_CUE;
148 c++;
149 };
150 };
151
152 if(c)
153 {
154 mvcp_unit_pause(handle->command, player->unit);
155
156 /* setup loop */
157 if(app->playlist.item[start].type & PLAYLIST_BLOCK_LOOP)
158 mvcp_unit_set(handle->command, player->unit, "eof", "loop");
159 else
160 mvcp_unit_set(handle->command, player->unit, "eof", "pause");
161
162 player->playlist_start = start;
163 player->playlist_length = stop - start + 1;
164 };
165 };
166
167 if(BUTTON_PLAYER_PLAY == button)
168 /* play */
169 mvcp_unit_play(handle->command, player->unit);
170
171 if(BUTTON_PLAYER_PAUSE == button)
172 /* pause */
173 mvcp_unit_pause(handle->command, player->unit);
174
175 pthread_mutex_unlock(&app->players.lock);
176
177 pthread_mutex_unlock(&app->playlist.lock);
178
179 return 0;
180 };