2 * instance.c -- GTK+ 2 melted gui
3 * Copyright (C) 2012 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.
32 #include <gdk/gdkkeysyms.h>
35 #if defined(__MINGW32__)
48 GtkTargetEntry drag_targets
[] = { { (char*) "application/playlist_item_t", 0, 0 } };
50 static gboolean
library_tree_key(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
52 instance_t
* app
= (instance_t
*)data
;
58 if(event
->state
& GDK_CONTROL_MASK
)
61 playlist_item_t
* items
;
63 items
= library_get_selected_items(app
, &count
);
69 for(i
= 0; i
< count
; i
++)
70 app
->clipboard
.item
[i
] = items
[i
];
72 app
->clipboard
.count
= count
;
80 if(event
->state
& GDK_CONTROL_MASK
)
82 g_warning("CTRL+v\n");
88 if(event
->state
& GDK_CONTROL_MASK
)
90 g_warning("CTRL+x\n");
94 case GDK_KEY_BackSpace
:
95 // omnplay_library_add(app, 0);
98 // omnplay_library_refresh(app);
105 static gboolean
playlist_grid_key(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
107 instance_t
* app
= (instance_t
*)data
;
109 switch(event
->keyval
)
113 if(event
->state
& GDK_CONTROL_MASK
)
115 playlist_item_copy(app
);
121 if(event
->state
& GDK_CONTROL_MASK
)
123 playlist_item_paste(app
, 0);
129 if(event
->state
& GDK_CONTROL_MASK
)
131 playlist_item_copy(app
);
132 playlist_delete_selected_items(app
);
138 if(event
->state
& GDK_CONTROL_MASK
)
146 if(event
->state
& GDK_CONTROL_MASK
)
152 case GDK_KEY_uparrow
:
153 if(event
->state
& GDK_CONTROL_MASK
)
155 playlist_item_swap(app
, -1);
159 case GDK_KEY_downarrow
:
160 if(event
->state
& GDK_CONTROL_MASK
)
162 playlist_item_swap(app
, -1);
167 control_route(app
, BUTTON_PLAYER_PLAY
);
170 control_route(app
, BUTTON_PLAYER_CUE
);
173 // omnplay_playlist_item_add(app, 0);
176 playlist_delete_selected_items(app
);
180 playlist_item_edit(app
);
187 static gboolean
library_tree_button(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
189 // g_warning("on_library_grid_button: event->button=%d, event->type=%d", event->button, event->type);
191 if(event
->button
==1 && event
->type
==GDK_2BUTTON_PRESS
)
193 playlist_item_add_from_library((instance_t
* )data
, 0);
200 static gboolean
playlist_grid_button(GtkWidget
*widget
, GdkEventButton
*event
, gpointer data
)
202 instance_t
* app
= (instance_t
*)data
;
204 // g_warning("on_playlist_grid_button");
206 if(event
->button
==1 && event
->type
==GDK_2BUTTON_PRESS
)
208 playlist_item_edit(app
);
215 static gboolean
instance_button_click(instance_t
* app
, control_buttons_t button
)
219 case BUTTON_PLAYLIST_ITEM_ADD
:
220 playlist_item_add(app
, 0);
222 case BUTTON_PLAYLIST_ITEM_DEL
:
223 playlist_delete_selected_items(app
);
225 case BUTTON_PLAYLIST_ITEM_EDIT
:
226 playlist_item_edit(app
);
228 case BUTTON_PLAYLIST_LOAD
:
231 case BUTTON_PLAYLIST_SAVE
:
234 case BUTTON_PLAYLIST_BLOCK_SINGLE
:
235 case BUTTON_PLAYLIST_BLOCK_LOOP
:
236 playlist_block(app
, (BUTTON_PLAYLIST_BLOCK_LOOP
== button
)?
1:0);
238 case BUTTON_PLAYLIST_ITEM_UP
:
239 playlist_item_swap(app
, -1);
241 case BUTTON_PLAYLIST_ITEM_DOWN
:
242 playlist_item_swap(app
, +1);
244 case BUTTON_PLAYER_CUE
:
245 case BUTTON_PLAYER_PLAY
:
246 case BUTTON_PLAYER_PAUSE
:
247 case BUTTON_PLAYER_STOP
:
248 control_route(app
, button
);
250 case BUTTON_LIBRARY_ADD
:
251 playlist_item_add_from_library(app
, 0);
253 case BUTTON_LIBRARY_REFRESH
:
254 // omnplay_library_refresh(app);
256 case BUTTON_LIBRARY_FIND
:
257 // omnplay_library_search(app, 0);
259 case BUTTON_LIBRARY_FIND_NEXT
:
260 // omnplay_library_search(app, 1);
262 case BUTTON_PLAYLIST_RELINK
:
263 // omnplay_playlist_relink(app);
266 g_warning("instance_button_click: unknow button clicked");
271 static gboolean
on_button_click(GtkWidget
*button
, gpointer user_data
)
274 instance_t
* app
= (instance_t
*)user_data
;
276 for(i
= 1; i
< BUTTON_LAST
; i
++)
277 if(app
->buttons
[i
] == button
)
278 return instance_button_click(app
, (control_buttons_t
)i
);
283 static gboolean
on_main_window_delete_event( GtkWidget
*widget
, GdkEvent
*event
, gpointer user_data
)
285 g_print ("delete event occurred [start]\n");
287 instance_release((instance_t
*)user_data
);
289 g_print ("delete event occurred [finish]\n");
294 static void on_main_window_destroy( GtkWidget
*widget
, gpointer user_data
)
296 g_print ("destroy occurred\n");
300 instance_t
* instance_create(int argc
, char** argv
)
305 /* prepare application instance */
306 app
= (instance_t
*)malloc(sizeof(instance_t
));
307 memset(app
, 0, sizeof(instance_t
));
309 /* load parameters from command line */
310 if(!instance_opt(argc
, argv
, app
) && app
->players
.count
)
311 app
->window
= ui_create(app
);
318 void instance_destroy(instance_t
* app
)
323 void instance_init(instance_t
* app
)
326 pthread_mutexattr_t attr
;
328 #if defined(__MINGW32__)
329 WORD wVersionRequested
;
331 wVersionRequested
= MAKEWORD(2, 2);
332 WSAStartup(wVersionRequested
, &wsaData
);
335 gtk_signal_connect( GTK_OBJECT( app
->window
), "delete-event",
336 GTK_SIGNAL_FUNC(on_main_window_delete_event
), app
);
338 gtk_signal_connect( GTK_OBJECT( app
->window
), "destroy",
339 GTK_SIGNAL_FUNC(on_main_window_destroy
), app
);
341 gtk_widget_add_events(app
->playlist_grid
, GDK_BUTTON_PRESS_MASK
);
342 gtk_widget_add_events(app
->playlist_grid
, GDK_KEY_PRESS_MASK
);
343 gtk_signal_connect(GTK_OBJECT(app
->playlist_grid
), "key-press-event",
344 GTK_SIGNAL_FUNC(playlist_grid_key
), app
);
346 gtk_widget_add_events(app
->library_tree
, GDK_BUTTON_PRESS_MASK
);
347 gtk_widget_add_events(app
->library_tree
, GDK_KEY_PRESS_MASK
);
348 gtk_signal_connect(GTK_OBJECT(app
->library_tree
), "key-press-event",
349 GTK_SIGNAL_FUNC(library_tree_key
), app
);
351 gtk_signal_connect(GTK_OBJECT(app
->playlist_grid
), "button-press-event",
352 GTK_SIGNAL_FUNC(playlist_grid_button
), app
);
354 gtk_signal_connect(GTK_OBJECT(app
->library_tree
), "button-press-event",
355 GTK_SIGNAL_FUNC(library_tree_button
), app
);
358 pthread_mutexattr_init(&attr
);
359 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_RECURSIVE
);
360 pthread_mutex_init(&app
->players
.lock
, &attr
);
361 pthread_mutex_init(&app
->playlist
.lock
, &attr
);
362 pthread_mutex_init(&app
->library
.lock
, &attr
);
363 pthread_mutexattr_destroy(&attr
);
365 /* run unit monitoring threads */
366 for(i
= 0; i
< app
->players
.count
; i
++)
369 /* attach buttons click */
370 for(i
= 1; i
< BUTTON_LAST
; i
++)
371 gtk_signal_connect(GTK_OBJECT(app
->buttons
[i
]), "clicked",
372 GTK_SIGNAL_FUNC( on_button_click
), app
);
381 void instance_release(instance_t
* app
)
387 /* stop unit monitoring threads */
388 for(i
= 0; i
< app
->players
.count
; i
++)
391 /* release laylist */
392 playlist_release(app
);
394 /* release library */
395 library_release(app
);
398 pthread_mutex_destroy(&app
->players
.lock
);
401 pthread_mutex_destroy(&app
->playlist
.lock
);
403 /* destroy library lock */
404 pthread_mutex_destroy(&app
->library
.lock
);