2 * omnplay.c -- GTK+ 2 omnplay
3 * Copyright (C) 2011 Maksym Veremeyenko <verem@m1stereo.tv>
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.
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.
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.
28 #include <gdk/gdkkeysyms.h>
36 #include "omplrclnt.h"
38 static gboolean
on_main_window_delete_event( GtkWidget
*widget
, GdkEvent
*event
, gpointer user_data
)
44 omnplay_instance_t
* omnplay_create(int argc
, char** argv
)
47 omnplay_instance_t
* app
;
49 /* prepare application instance */
50 app
= (omnplay_instance_t
*)malloc(sizeof(omnplay_instance_t
));
51 memset(app
, 0, sizeof(omnplay_instance_t
));
53 /* load parameters from command line */
54 if(!omnplay_opt(argc
, argv
, app
) && app
->players
.count
)
55 app
->window
= ui_omnplay(app
);
62 void omnplay_destroy(omnplay_instance_t
* app
)
67 static void omnplay_update_status(omnplay_player_t
* player
, OmPlrStatus
*prev
, OmPlrStatus
*curr
)
69 char tc_cur
[32], tc_rem
[32], state
[32], status
[32];
74 frames2tc(curr
->pos
- curr
->minPos
, 25.0, tc_cur
);
75 frames2tc(curr
->maxPos
- curr
->pos
, 25.0, tc_rem
);
76 strcpy(status
, "ONLINE");
77 clip
= curr
->currClipName
;
81 case omPlrStateStopped
: strcpy(state
, "STOPPED"); break;
82 case omPlrStateCuePlay
: strcpy(state
, "CUE_PLAY"); break;
83 case omPlrStatePlay
: strcpy(state
, "PLAY"); break;
84 case omPlrStateCueRecord
: strcpy(state
, "CUE_RECORD"); break;
85 case omPlrStateRecord
: strcpy(state
, "RECORD"); break;
94 strcpy(status
, "OFFLINE");
98 gtk_label_set_text(GTK_LABEL (player
->label_tc_cur
), tc_cur
);
99 gtk_label_set_text(GTK_LABEL (player
->label_tc_rem
), tc_rem
);
100 gtk_label_set_text(GTK_LABEL (player
->label_state
), state
);
101 gtk_label_set_text(GTK_LABEL (player
->label_status
), status
);
102 gtk_label_set_text(GTK_LABEL (player
->label_clip
), clip
);
106 memcpy(prev
, curr
, sizeof(OmPlrStatus
));
109 static void* omnplay_thread_proc(void* data
)
112 OmPlrStatus st_curr
, st_prev
;
113 omnplay_player_t
* player
= (omnplay_player_t
*)data
;
116 pthread_mutex_lock(&player
->app
->players
.lock
);
117 r
= OmPlrOpen(player
->host
, player
->name
, (OmPlrHandle
*)&player
->handle
);
118 pthread_mutex_unlock(&player
->app
->players
.lock
);
121 fprintf(stderr
, "ERROR: OmPlrOpen(%s, %s) failed with 0x%.8X\n",
122 player
->host
, player
->name
, r
);
127 /* setup to do not reconnect */
128 pthread_mutex_lock(&player
->app
->players
.lock
);
129 OmPlrSetRetryOpen((OmPlrHandle
)player
->handle
, 0);
130 pthread_mutex_unlock(&player
->app
->players
.lock
);
132 /* setup directory */
133 if(player
->app
->players
.path
[0])
135 pthread_mutex_lock(&player
->app
->players
.lock
);
136 // r = OmPlrClipSetDirectory((OmPlrHandle)player->handle, player->app->players.path);
137 pthread_mutex_unlock(&player
->app
->players
.lock
);
141 fprintf(stderr
, "ERROR: OmPlrClipSetDirectory(%s) failed with 0x%.8X\n",
142 player
->app
->players
.path
, r
);
144 pthread_mutex_lock(&player
->app
->players
.lock
);
145 OmPlrClose((OmPlrHandle
)player
->handle
);
146 pthread_mutex_unlock(&player
->app
->players
.lock
);
153 for(r
= 0 ; !player
->app
->f_exit
&& !r
;)
159 pthread_mutex_lock(&player
->app
->players
.lock
);
160 st_curr
.size
= sizeof(OmPlrStatus
);
161 r
= OmPlrGetPlayerStatus((OmPlrHandle
)player
->handle
, &st_curr
);
162 pthread_mutex_unlock(&player
->app
->players
.lock
);
165 fprintf(stderr
, "ERROR: OmPlrGetPlayerStatus failed with 0x%.8X\n", r
);
167 if(memcmp(&st_curr
, &st_prev
, sizeof(OmPlrStatus
)))
168 omnplay_update_status(player
, &st_prev
, &st_curr
);
171 pthread_mutex_lock(&player
->app
->players
.lock
);
172 OmPlrClose((OmPlrHandle
)player
->handle
);
173 pthread_mutex_unlock(&player
->app
->players
.lock
);
178 static int get_first_selected_item_playlist(omnplay_instance_t
* app
)
183 GtkTreeSelection
*selection
;
185 selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(app
->playlist_grid
));
186 if(selection
&& gtk_tree_selection_get_selected(selection
, &model
, &iter
))
188 gtk_tree_model_get(model
, &iter
, 7, &idx
, -1);
194 static int get_playlist_block(omnplay_instance_t
* app
, int idx
, int* start_ptr
, int* stop_ptr
)
198 for(start
= idx
; start
>= 0; start
--)
199 if(app
->playlist
.item
[start
].type
& OMNPLAY_PLAYLIST_BLOCK_BEGIN
)
202 for(stop
= idx
; stop
< app
->playlist
.count
; stop
++)
203 if(app
->playlist
.item
[stop
].type
& OMNPLAY_PLAYLIST_BLOCK_END
)
206 fprintf(stderr
, "get_playlist_block: range %d -> %d\n", start
, stop
);
208 /* check block range */
209 if(start
>= 0 && stop
< app
->playlist
.count
)
213 return (stop
- start
+ 1);
219 static omnplay_player_t
*get_player_at_pos(omnplay_instance_t
* app
, int pos
)
221 /* check player range */
222 if(app
->playlist
.item
[pos
].player
> -1 && app
->playlist
.item
[pos
].player
< app
->players
.count
)
223 return &app
->players
.item
[app
->playlist
.item
[pos
].player
];
228 static void omnplay_cue(omnplay_instance_t
* app
)
230 int idx
, start
, stop
;
231 omnplay_player_t
*player
;
233 pthread_mutex_lock(&app
->playlist
.lock
);
235 idx
= get_first_selected_item_playlist(app
);
239 pthread_mutex_unlock(&app
->playlist
.lock
);
243 fprintf(stderr
, "cue: selected item is %d\n", idx
);
245 if(get_playlist_block(app
, idx
, &start
, &stop
) < 0)
247 pthread_mutex_unlock(&app
->playlist
.lock
);
251 fprintf(stderr
, "cue: range %d -> %d\n", start
, stop
);
253 player
= get_player_at_pos(app
, start
);
257 pthread_mutex_unlock(&app
->playlist
.lock
);
262 pthread_mutex_lock(&app
->players
.lock
);
263 OmPlrStop((OmPlrHandle
)player
->handle
);
264 player
->playlist_start
= -1;
265 player
->playlist_count
= -1;
266 pthread_mutex_unlock(&app
->players
.lock
);
268 /* 2. detach previous clips */
269 pthread_mutex_lock(&app
->players
.lock
);
270 OmPlrDetachAllClips((OmPlrHandle
)player
->handle
);
271 pthread_mutex_unlock(&app
->players
.lock
);
273 // http://research.m1stereo.tv/viewvc-int/viewvc.cgi/Ingest2Srv/trunk/SrvPlayCtl.cpp?view=markup
275 pthread_mutex_unlock(&app
->playlist
.lock
);
279 static gboolean
omnplay_button_click(omnplay_instance_t
* app
, control_buttons_t button
)
283 case BUTTON_PLAYLIST_ITEM_ADD
:
284 case BUTTON_PLAYLIST_ITEM_DEL
:
285 case BUTTON_PLAYLIST_ITEM_EDIT
:
286 case BUTTON_PLAYLIST_LOAD
:
287 omnplay_playlist_load(app
);
289 case BUTTON_PLAYLIST_SAVE
:
290 omnplay_playlist_save(app
);
292 case BUTTON_PLAYLIST_BLOCK_SINGLE
:
293 case BUTTON_PLAYLIST_BLOCK_LOOP
:
294 case BUTTON_PLAYLIST_ITEM_UP
:
295 case BUTTON_PLAYLIST_ITEM_DOWN
:
296 case BUTTON_PLAYER_CUE
:
298 case BUTTON_PLAYER_PLAY
:
299 case BUTTON_PLAYER_PAUSE
:
300 case BUTTON_PLAYER_STOP
:
301 case BUTTON_LIBRARY_ADD
:
302 case BUTTON_LIBRARY_REFRESH
:
309 static gboolean
on_button_click(GtkWidget
*button
, gpointer user_data
)
312 omnplay_instance_t
* app
= (omnplay_instance_t
*)user_data
;
314 for(i
= 1; i
< BUTTON_LAST
; i
++)
315 if(app
->buttons
[i
] == button
)
316 return omnplay_button_click(app
, (control_buttons_t
)i
);
321 void omnplay_init(omnplay_instance_t
* app
)
325 gtk_signal_connect( GTK_OBJECT( app
->window
), "destroy",
326 GTK_SIGNAL_FUNC(on_main_window_delete_event
), app
);
329 pthread_mutex_init(&app
->players
.lock
, NULL
);
331 /* create a omneon status thread */
332 for(i
= 0; i
< app
->players
.count
; i
++)
333 pthread_create(&app
->players
.item
[i
].thread
, NULL
,
334 omnplay_thread_proc
, &app
->players
.item
[i
]);
337 pthread_mutex_init(&app
->playlist
.lock
, NULL
);
339 /* attach buttons click */
340 for(i
= 1; i
< BUTTON_LAST
; i
++)
341 gtk_signal_connect(GTK_OBJECT(app
->buttons
[i
]), "clicked",
342 GTK_SIGNAL_FUNC( on_button_click
), app
);
346 void omnplay_release(omnplay_instance_t
* app
)
353 for(i
= 0; i
< app
->players
.count
; i
++)
354 /* create a omneon status thread */
355 pthread_join(app
->players
.item
[i
].thread
, &r
);
358 pthread_mutex_destroy(&app
->players
.lock
);
361 pthread_mutex_destroy(&app
->playlist
.lock
);