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_ctl(omnplay_instance_t
* app
, control_buttons_t button
)
231 int idx
, start
, stop
;
232 omnplay_player_t
*player
;
234 pthread_mutex_lock(&app
->playlist
.lock
);
236 idx
= get_first_selected_item_playlist(app
);
240 pthread_mutex_unlock(&app
->playlist
.lock
);
244 fprintf(stderr
, "cue: selected item is %d\n", idx
);
246 if(get_playlist_block(app
, idx
, &start
, &stop
) < 0)
248 pthread_mutex_unlock(&app
->playlist
.lock
);
252 fprintf(stderr
, "cue: range %d -> %d\n", start
, stop
);
254 player
= get_player_at_pos(app
, start
);
258 pthread_mutex_unlock(&app
->playlist
.lock
);
262 pthread_mutex_lock(&app
->players
.lock
);
264 if(BUTTON_PLAYER_STOP
== button
|| BUTTON_PLAYER_CUE
== button
)
267 OmPlrStop((OmPlrHandle
)player
->handle
);
269 /* detach previous clips */
270 player
->playlist_start
= -1;
271 player
->playlist_count
= -1;
272 OmPlrDetachAllClips((OmPlrHandle
)player
->handle
);
275 if(BUTTON_PLAYER_CUE
== button
)
279 /* Attach clips to timeline */
280 for(i
= start
, c
= 0, o
= 0; i
<= stop
; i
++)
284 r
= OmPlrAttach((OmPlrHandle
)player
->handle
,
285 app
->playlist
.item
[i
].id
,
286 app
->playlist
.item
[i
].in
,
287 app
->playlist
.item
[i
].in
+ app
->playlist
.item
[i
].dur
,
288 0, omPlrShiftModeAfter
, &l
);
292 fprintf(stderr
, "cue: failed with %d, %s\n", r
, OmPlrGetErrorString((OmPlrError
)r
));
293 app
->playlist
.item
[i
].omn_idx
= -1;
294 app
->playlist
.item
[i
].omn_offset
= -1;
298 app
->playlist
.item
[i
].omn_idx
= c
;
299 app
->playlist
.item
[i
].omn_offset
= o
;
301 /* save selected item offset */
305 o
+= app
->playlist
.item
[i
].dur
;
313 /* Set timeline min/max */
314 OmPlrSetMinPosMin((OmPlrHandle
)player
->handle
);
315 OmPlrSetMaxPosMax((OmPlrHandle
)player
->handle
);
317 /* Set timeline position */
319 hs
.size
= sizeof(OmPlrStatus
);
320 OmPlrGetPlayerStatus((OmPlrHandle
)player
->handle
, &hs
);
321 OmPlrSetPos((OmPlrHandle
)player
->handle
, hs
.minPos
+ p
);
324 OmPlrCuePlay((OmPlrHandle
)player
->handle
, 0.0);
325 OmPlrPlay((OmPlrHandle
)player
->handle
, 0.0);
327 player
->playlist_start
= start
;
328 player
->playlist_count
= stop
- start
+ 1;
332 if(BUTTON_PLAYER_PLAY
== button
)
335 OmPlrPlay((OmPlrHandle
)player
->handle
, 1.0);
338 if(BUTTON_PLAYER_PAUSE
== button
)
340 OmPlrPlay((OmPlrHandle
)player
->handle
, 0.0);
342 pthread_mutex_unlock(&app
->players
.lock
);
344 pthread_mutex_unlock(&app
->playlist
.lock
);
347 static gboolean
omnplay_button_click(omnplay_instance_t
* app
, control_buttons_t button
)
351 case BUTTON_PLAYLIST_ITEM_ADD
:
352 case BUTTON_PLAYLIST_ITEM_DEL
:
353 case BUTTON_PLAYLIST_ITEM_EDIT
:
355 case BUTTON_PLAYLIST_LOAD
:
356 omnplay_playlist_load(app
);
358 case BUTTON_PLAYLIST_SAVE
:
359 omnplay_playlist_save(app
);
361 case BUTTON_PLAYLIST_BLOCK_SINGLE
:
362 case BUTTON_PLAYLIST_BLOCK_LOOP
:
364 case BUTTON_PLAYLIST_ITEM_UP
:
365 case BUTTON_PLAYLIST_ITEM_DOWN
:
367 case BUTTON_PLAYER_CUE
:
368 case BUTTON_PLAYER_PLAY
:
369 case BUTTON_PLAYER_PAUSE
:
370 case BUTTON_PLAYER_STOP
:
371 omnplay_ctl(app
, button
);
373 case BUTTON_LIBRARY_ADD
:
374 case BUTTON_LIBRARY_REFRESH
:
381 static gboolean
on_button_click(GtkWidget
*button
, gpointer user_data
)
384 omnplay_instance_t
* app
= (omnplay_instance_t
*)user_data
;
386 for(i
= 1; i
< BUTTON_LAST
; i
++)
387 if(app
->buttons
[i
] == button
)
388 return omnplay_button_click(app
, (control_buttons_t
)i
);
393 void omnplay_init(omnplay_instance_t
* app
)
397 gtk_signal_connect( GTK_OBJECT( app
->window
), "destroy",
398 GTK_SIGNAL_FUNC(on_main_window_delete_event
), app
);
401 pthread_mutex_init(&app
->players
.lock
, NULL
);
403 /* create a omneon status thread */
404 for(i
= 0; i
< app
->players
.count
; i
++)
405 pthread_create(&app
->players
.item
[i
].thread
, NULL
,
406 omnplay_thread_proc
, &app
->players
.item
[i
]);
409 pthread_mutex_init(&app
->playlist
.lock
, NULL
);
411 /* attach buttons click */
412 for(i
= 1; i
< BUTTON_LAST
; i
++)
413 gtk_signal_connect(GTK_OBJECT(app
->buttons
[i
]), "clicked",
414 GTK_SIGNAL_FUNC( on_button_click
), app
);
418 void omnplay_release(omnplay_instance_t
* app
)
425 for(i
= 0; i
< app
->players
.count
; i
++)
426 /* create a omneon status thread */
427 pthread_join(app
->players
.item
[i
].thread
, &r
);
430 pthread_mutex_destroy(&app
->players
.lock
);
433 pthread_mutex_destroy(&app
->playlist
.lock
);