2bd06b0b9a4b8c898657222223eb970180206b31
[melted_gui] / src / instance.c
1 /*
2 * instance.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 #ifndef _GNU_SOURCE
21 #define _GNU_SOURCE
22 #endif
23
24 #ifdef HAVE_CONFIG_H
25 # include <config.h>
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
35 #include "instance.h"
36 #include "ui.h"
37 #include "opts.h"
38 #include "timecode.h"
39 #include "player.h"
40 #include "library.h"
41 #include "playlist.h"
42
43 GtkTargetEntry drag_targets[] = { { (char*) "application/playlist_item_t", 0, 0 } };
44
45 gboolean instance_button_click(instance_t* app, control_buttons_t button)
46 {
47 #if 0
48 switch(button)
49 {
50 case BUTTON_PLAYLIST_ITEM_ADD:
51 omnplay_playlist_item_add(app, 0);
52 break;
53 case BUTTON_PLAYLIST_ITEM_DEL:
54 omnplay_playlist_item_del(app);
55 break;
56 case BUTTON_PLAYLIST_ITEM_EDIT:
57 omnplay_playlist_item_edit(app);
58 break;
59 case BUTTON_PLAYLIST_LOAD:
60 omnplay_playlist_load(app);
61 break;
62 case BUTTON_PLAYLIST_SAVE:
63 omnplay_playlist_save(app);
64 break;
65 case BUTTON_PLAYLIST_BLOCK_SINGLE:
66 case BUTTON_PLAYLIST_BLOCK_LOOP:
67 omnplay_playlist_block(app, button);
68 break;
69 case BUTTON_PLAYLIST_ITEM_UP:
70 omnplay_playlist_item_swap(app, -1);
71 break;
72 case BUTTON_PLAYLIST_ITEM_DOWN:
73 omnplay_playlist_item_swap(app, +1);
74 break;
75 case BUTTON_PLAYER_CUE:
76 case BUTTON_PLAYER_PLAY:
77 case BUTTON_PLAYER_PAUSE:
78 case BUTTON_PLAYER_STOP:
79 omnplay_ctl(app, button);
80 break;
81 case BUTTON_LIBRARY_ADD:
82 omnplay_library_add(app, 0);
83 break;
84 case BUTTON_LIBRARY_REFRESH:
85 omnplay_library_refresh(app);
86 break;
87 case BUTTON_LIBRARY_FIND:
88 omnplay_library_search(app, 0);
89 break;
90 case BUTTON_LIBRARY_FIND_NEXT:
91 omnplay_library_search(app, 1);
92 break;
93 case BUTTON_PLAYLIST_RELINK:
94 omnplay_playlist_relink(app);
95 break;
96 };
97 #endif
98 return TRUE;
99 };
100
101 static gboolean on_button_click(GtkWidget *button, gpointer user_data)
102 {
103 int i;
104 instance_t* app = (instance_t*)user_data;
105
106 for(i = 1; i < BUTTON_LAST; i++)
107 if(app->buttons[i] == button)
108 return instance_button_click(app, (control_buttons_t)i);
109
110 return FALSE;
111 };
112
113 static gboolean on_main_window_delete_event( GtkWidget *widget, GdkEvent *event, gpointer user_data )
114 {
115 g_print ("delete event occurred [start]\n");
116 gdk_threads_leave();
117 instance_release((instance_t*)user_data);
118 gdk_threads_enter();
119 g_print ("delete event occurred [finish]\n");
120
121 return FALSE;
122 }
123
124 static void on_main_window_destroy( GtkWidget *widget, gpointer user_data )
125 {
126 g_print ("destroy occurred\n");
127 gtk_main_quit();
128 }
129
130 instance_t* instance_create(int argc, char** argv)
131 {
132 int i, c;
133 instance_t* app;
134
135 /* prepare application instance */
136 app = (instance_t*)malloc(sizeof(instance_t));
137 memset(app, 0, sizeof(instance_t));
138
139 /* load parameters from command line */
140 if(!instance_opt(argc, argv, app) && app->players.count)
141 app->window = ui_create(app);
142 else
143 instance_usage();
144
145 return app;
146 };
147
148 void instance_destroy(instance_t* app)
149 {
150 free(app);
151 };
152
153 void instance_init(instance_t* app)
154 {
155 int i;
156 pthread_mutexattr_t attr;
157
158 gtk_signal_connect( GTK_OBJECT( app->window ), "delete-event",
159 GTK_SIGNAL_FUNC(on_main_window_delete_event), app);
160
161 gtk_signal_connect( GTK_OBJECT( app->window ), "destroy",
162 GTK_SIGNAL_FUNC(on_main_window_destroy), app);
163
164 #if 0
165 gtk_widget_add_events(app->playlist_grid, GDK_BUTTON_PRESS_MASK);
166 gtk_widget_add_events(app->playlist_grid, GDK_KEY_PRESS_MASK);
167 gtk_signal_connect(GTK_OBJECT(app->playlist_grid), "key-press-event",
168 GTK_SIGNAL_FUNC(on_playlist_grid_key), app);
169
170 gtk_widget_add_events(app->library_grid, GDK_BUTTON_PRESS_MASK);
171 gtk_widget_add_events(app->library_grid, GDK_KEY_PRESS_MASK);
172 gtk_signal_connect(GTK_OBJECT(app->library_grid), "key-press-event",
173 GTK_SIGNAL_FUNC(on_library_grid_key), app);
174
175 gtk_signal_connect(GTK_OBJECT(app->playlist_grid), "button-press-event",
176 GTK_SIGNAL_FUNC(on_playlist_grid_button), app);
177
178 gtk_signal_connect(GTK_OBJECT(app->library_grid), "button-press-event",
179 GTK_SIGNAL_FUNC(on_library_grid_button), app);
180 #endif
181
182 /* create lock */
183 pthread_mutexattr_init(&attr);
184 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
185 pthread_mutex_init(&app->players.lock, &attr);
186 pthread_mutex_init(&app->playlist.lock, &attr);
187 pthread_mutex_init(&app->library.lock, &attr);
188 pthread_mutexattr_destroy(&attr);
189
190 /* run unit monitoring threads */
191 for(i = 0; i < app->players.count; i++)
192 player_run(app, i);
193
194 /* attach buttons click */
195 for(i = 1; i < BUTTON_LAST; i++)
196 gtk_signal_connect(GTK_OBJECT(app->buttons[i]), "clicked",
197 GTK_SIGNAL_FUNC( on_button_click), app );
198
199 /* init library */
200 library_init(app);
201
202 /* init playlist */
203 playlist_init(app);
204 };
205
206 void instance_release(instance_t* app)
207 {
208 int i;
209
210 app->f_exit = 1;
211
212 /* stop unit monitoring threads */
213 for(i = 0; i < app->players.count; i++)
214 player_stop(app, i);
215
216 /* release laylist */
217 playlist_release(app);
218
219 /* release library */
220 library_release(app);
221
222 /* destroy lock */
223 pthread_mutex_destroy(&app->players.lock);
224
225 /* destroy lock */
226 pthread_mutex_destroy(&app->playlist.lock);
227
228 /* destroy library lock */
229 pthread_mutex_destroy(&app->library.lock);
230 };