summaryrefslogtreecommitdiffstats
path: root/gtk2/main.c
blob: d4e4d5d8696ca8d240b1d01cc48b11b1ba112877 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*
 * Initial main.c file generated by Glade. Edit as required.
 * Glade will not overwrite this file.
 */

#include "../core/HandBrake.h"

#include <gtk/gtk.h>

#include "interface.h"
#include "support.h"

#include "status.h"

#define GLADE_HOOKUP_OBJECT(component,widget,name) \
  g_object_set_data_full (G_OBJECT (component), name, \
    gtk_widget_ref (widget), (GDestroyNotify) gtk_widget_unref)

GtkWidget *hb_win_main = NULL;
HBHandle  *hb_handle = NULL;
HBStatus  *hb_status = NULL;

static int HBMain( void *data );

/* libhb callbacks */
static void Scanning( void * data, int title, int titleCount );
static void ScanDone( void * data, HBList * titleList );
static void Encoding( void * data, float position, int pass,
                      int passCount, float frameRate,
                      float avgFrameRate, int remainingTime );
static void RipDone( void * data, int result );


int main (int argc, char **argv )
{
    HBCallbacks callbacks;
    GtkWidget   *widget;
    GList       *items = NULL;

    int i_dummy;

    /* Create HB stuff */
    if( ( hb_handle = HBInit( 1, 0 ) ) == NULL )
    {
        g_print( "HBInit: failed\n" );
        return -1;
    }

    /* Emulate a HBStatus */
    hb_status = malloc( sizeof( HBStatus ) );
    hb_status->i_state = HB_STATE_NEED_DEVICE;
    hb_status->b_new = 1;
    hb_status->i_title = 0;
    hb_status->i_title_count = 0;
    hb_status->titleList = NULL;
    hb_status->position = 0.0;
    hb_status->i_remaining = 0;
    hb_status->fps = 0.0;

    /* Set libhb callbacks */
    callbacks.data     = NULL;
    callbacks.scanning = Scanning;
    callbacks.scanDone = ScanDone;
    callbacks.encoding = Encoding;
    callbacks.ripDone  = RipDone;
    HBSetCallbacks( hb_handle, callbacks );

    gtk_set_locale ();
    gtk_init (&argc, &argv);

    //add_pixmap_directory (PACKAGE_DATA_DIR "/" PACKAGE "/pixmaps");

    /* Create the main window */
    hb_win_main = create_win_main();
    gtk_window_set_title( GTK_WINDOW(hb_win_main), "HandBrake " HB_VERSION );

    widget = lookup_widget( hb_win_main, "combo1" );
    gtk_combo_disable_activate( GTK_COMBO( widget ) );

    widget = lookup_widget( hb_win_main, "combo2" );
    gtk_combo_disable_activate( GTK_COMBO( widget ) );

    widget = lookup_widget( hb_win_main, "optionmenu_bitrate" );
    gtk_option_menu_set_history( GTK_OPTION_MENU(widget), 2 );

    /* build dvd list */
    items = g_list_append (items, "" );
    items = g_list_append (items, "/dev/dvd" );
    gtk_combo_set_popdown_strings( GTK_COMBO(lookup_widget( hb_win_main, "combo1") ), items );

    /* Display the main window */
    gtk_widget_show( hb_win_main );

    i_dummy = gtk_timeout_add( 100, (GtkFunction)HBMain, NULL );
    gtk_main();
    gtk_timeout_remove( i_dummy );

    return 0;
}

static void StatusBarUpdate( char *msg )
{
    GtkWidget *widget = lookup_widget( hb_win_main, "statusbar" );

    gtk_statusbar_push( GTK_STATUSBAR(widget),
                        gtk_statusbar_get_context_id(GTK_STATUSBAR(widget), "status" ),
                        msg );
}

static void DisplayError( char *title, char *msg )
{
    GtkWidget *dialog = gtk_message_dialog_new( NULL, GTK_DIALOG_MODAL,
                                                GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
                                                "<b>%s</b>\n\n%s", title, msg );
    gtk_label_set_use_markup( GTK_LABEL( GTK_MESSAGE_DIALOG(dialog)->label), TRUE );
    gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK );
    gtk_window_set_modal( GTK_WINDOW(dialog), TRUE );
    gtk_dialog_run( GTK_DIALOG(dialog) );
    gtk_widget_destroy( dialog );
}

static void DisplayInfo( char *msg )
{
    GtkWidget *dialog = gtk_message_dialog_new( NULL, GTK_DIALOG_MODAL,
                                                GTK_MESSAGE_INFO, GTK_BUTTONS_OK, msg );
    gtk_label_set_use_markup( GTK_LABEL( GTK_MESSAGE_DIALOG(dialog)->label), TRUE );
    gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_OK );
    gtk_window_set_modal( GTK_WINDOW(dialog), TRUE );
    gtk_dialog_run( GTK_DIALOG(dialog) );
    gtk_widget_destroy( dialog );
}

static int HBMain( void *data )
{
    int        b_new = hb_status->b_new;
    GtkWidget  *widget;
    gchar      msg[1000];

    hb_status->b_new = 0;

    switch( hb_status->i_state )
    {
        case HB_STATE_NEED_DEVICE:
            if( b_new )
            {
                g_print( "HBMain: HB_MODE_NEED_DEVICE\n" );
                gtk_widget_set_sensitive( lookup_widget( hb_win_main, "frame_dvd"), TRUE );
                StatusBarUpdate( "Enter a device" );
            }
            break;

        case HB_STATE_SCANNING:
            if( b_new )
            {
                g_print( "HBMain: HB_MODE_SCANNING\n" );
            }

            gtk_widget_set_sensitive( lookup_widget( hb_win_main, "frame_dvd"), FALSE );

            gtk_progress_bar_pulse( GTK_PROGRESS_BAR(lookup_widget( hb_win_main, "progressbar" ) ));
            if( hb_status->i_title )
            {
                sprintf( msg, "Scanning title %d/%d...", hb_status->i_title, hb_status->i_title_count );
            }
            else
            {
                sprintf( msg, "Opening device..." );
            }
            StatusBarUpdate( msg );
            break;

        case HB_STATE_INVALID_DEVICE:
            if( b_new )
            {
                g_print( "HBMain: HB_MODE_INVALID_DEVICE\n" );

                gtk_widget_set_sensitive( lookup_widget( hb_win_main, "frame_dvd"), TRUE );

                gtk_entry_set_text( GTK_ENTRY(lookup_widget( hb_win_main, "entry_device")), "" );

                gtk_progress_bar_set_fraction( GTK_PROGRESS_BAR(lookup_widget( hb_win_main, "progressbar" )), 0.0 );

                StatusBarUpdate( "Enter a valid device" );

                /* error dialog */
                DisplayError( "Invalid device", "Please select a valide device" );
            }
            break;

        case HB_STATE_READY_TO_RIP:
            if( b_new )
            {
                GtkWidget *optionmenu_title = lookup_widget( hb_win_main, "optionmenu_title" );
                GtkWidget *menu;
                GtkWidget *item;
                int i;

                g_print( "HBMain: HB_MODE_READY_TO_RIP\n" );

                /* create title menu */
                menu = gtk_menu_new();
                for( i = 0; i < HBListCount( hb_status->titleList ); i++ )
                {
                    HBTitle *title = (HBTitle*) HBListItemAt( hb_status->titleList, i );
                    char string[1024];

                    sprintf( string, "%d - %02dh%02dm%02ds",
                             title->title, title->hours, title->minutes,
                             title->seconds );

                    item = gtk_menu_item_new_with_mnemonic( string );
                    gtk_widget_show( item );
                    gtk_menu_shell_append( GTK_MENU_SHELL(menu), item );
                }

                gtk_option_menu_set_menu( GTK_OPTION_MENU(optionmenu_title), menu );
                GLADE_HOOKUP_OBJECT( hb_win_main, menu, "menu_title" );

                /* unhide */
                widget = lookup_widget( hb_win_main, "vbox_parameters" );
                gtk_widget_show( widget );

                gtk_progress_bar_set_fraction( GTK_PROGRESS_BAR(lookup_widget( hb_win_main, "progressbar" )), 0.0 );
                gtk_progress_bar_set_text( GTK_PROGRESS_BAR(lookup_widget( hb_win_main, "progressbar" )), "" );

                StatusBarUpdate( "Select your parameters" );
            }
            break;

        case HB_STATE_ENCODING:
            if( b_new )
            {
                /* change the name to Stop */
                widget = lookup_widget( hb_win_main, "button_start_label" );
                gtk_label_set_text( GTK_LABEL( widget ), "Stop" );

                widget = lookup_widget( hb_win_main, "button_start_image" );
                gtk_image_set_from_stock( GTK_IMAGE(widget), "gtk-cancel", GTK_ICON_SIZE_BUTTON );

                gtk_widget_set_sensitive( lookup_widget(hb_win_main, "frame_video"), FALSE );
                gtk_widget_set_sensitive( lookup_widget(hb_win_main, "frame_audio"), FALSE );
                gtk_widget_set_sensitive( lookup_widget(hb_win_main, "frame_output"), FALSE );
            }

            if( hb_status->position <= 0 )
            {
                StatusBarUpdate( "Starting..." );
            }
            else
            {
                char string[1024];

                /* Status bar */
                sprintf( string, "Encoding: %02dh%02dm%02ds remaining",
                         hb_status->i_remaining / 3600,
                         ( hb_status->i_remaining % 3600 ) / 60,
                         hb_status->i_remaining % 60 );
                StatusBarUpdate( string );

                /* Progressbar */
                sprintf( string, "%.2f %% (%.2f fps)", 100 * hb_status->position, hb_status->fps );
                gtk_progress_bar_set_text( GTK_PROGRESS_BAR(lookup_widget( hb_win_main, "progressbar" )), string );
                gtk_progress_bar_set_fraction( GTK_PROGRESS_BAR(lookup_widget( hb_win_main, "progressbar" )), hb_status->position );
            }
            break;

        case HB_STATE_DONE:
        case HB_STATE_CANCELED:
            if( b_new )
            {
                g_print( "HBMain: HB_MODE_DONE/HB_MODE_CANCELED\n" );

                /* change the name to Start */
                widget = lookup_widget( hb_win_main, "button_start_label" );
                gtk_label_set_text( GTK_LABEL( widget ), "Start" );

                widget = lookup_widget( hb_win_main, "button_start_image" );
                gtk_image_set_from_stock( GTK_IMAGE(widget), "gtk-apply", GTK_ICON_SIZE_BUTTON );

                gtk_progress_bar_set_fraction( GTK_PROGRESS_BAR(lookup_widget( hb_win_main, "progressbar" )), 0.0 );
                gtk_progress_bar_set_text( GTK_PROGRESS_BAR(lookup_widget( hb_win_main, "progressbar" )), "" );

                if( hb_status->i_state == HB_STATE_DONE )
                {
                    StatusBarUpdate( "Done." );
                    DisplayInfo( "Encoding finished with success." );
                }
                else
                {
                    StatusBarUpdate( "Canceled." );
                    DisplayInfo("Encoding canceled.");
                }

                gtk_widget_set_sensitive( lookup_widget(hb_win_main, "frame_video"), TRUE );
                gtk_widget_set_sensitive( lookup_widget(hb_win_main, "frame_audio"), TRUE );
                gtk_widget_set_sensitive( lookup_widget(hb_win_main, "frame_output"), TRUE );
            }
            break;

        case HB_STATE_ERROR:
            if( b_new )
            {
                char *msg = "Unknown";

                g_print( "HBMain: HB_MODE_ERROR\n" );

                /* change the name to Start */
                widget = lookup_widget( hb_win_main, "button_start_label" );
                gtk_label_set_text( GTK_LABEL( widget ), "Start" );

                widget = lookup_widget( hb_win_main, "button_start_image" );
                gtk_image_set_from_stock( GTK_IMAGE(widget), "gtk-apply", GTK_ICON_SIZE_BUTTON );

                gtk_progress_bar_set_fraction( GTK_PROGRESS_BAR(lookup_widget( hb_win_main, "progressbar" )), 0.0 );
                gtk_progress_bar_set_text( GTK_PROGRESS_BAR(lookup_widget( hb_win_main, "progressbar" )), "" );

                StatusBarUpdate( "Error." );
                switch( hb_status->i_error )
                {
#if 0
                    case HB_ERROR_A52_SYNC:
                        msg = "corrupted AC3 data.";
                        break;
                    case HB_ERROR_AVI_WRITE:
                        msg = "could not write to file.";
                        break;
                    case HB_ERROR_DVD_OPEN:
                        msg = "could not open device.";
                        break;
                    case HB_ERROR_DVD_READ:
                        msg = "DVD read failed.";
                        break;
                    case HB_ERROR_MP3_INIT:
                        msg = "could not init MP3 encoder.";
                        break;
                    case HB_ERROR_MP3_ENCODE:
                        msg = "MP3 encoder failed.";
                        break;
                    case HB_ERROR_MPEG4_INIT:
                        msg = "could not init MPEG4 encoder.";
                        break;
#endif
                    default:
                        msg = "Generic error";
                        break;
                }
                DisplayError( "An error occured", msg );

                gtk_widget_set_sensitive( lookup_widget(hb_win_main, "frame_video"), TRUE );
                gtk_widget_set_sensitive( lookup_widget(hb_win_main, "frame_audio"), TRUE );
                gtk_widget_set_sensitive( lookup_widget(hb_win_main, "frame_output"), TRUE );
            }
            break;
    }


    return TRUE;
}


static void Scanning( void * data, int title, int titleCount )
{
    if( hb_status->i_state != HB_STATE_SCANNING )
    {
        hb_status->i_state = HB_STATE_SCANNING;
        hb_status->b_new = 1;
    }
    hb_status->i_title = title;
    hb_status->i_title_count = titleCount;
}

static void ScanDone( void * data, HBList * titleList )
{
    if( titleList == NULL )
    {
        hb_status->i_state = HB_STATE_INVALID_DEVICE;
    }
    else
    {
        hb_status->i_state = HB_STATE_READY_TO_RIP;
    }
    hb_status->b_new = 1;

    hb_status->titleList = titleList;
}

static void Encoding( void * data, float position, int pass,
                      int passCount, float frameRate,
                      float avgFrameRate, int remainingTime )
{
    if( hb_status->i_state != HB_STATE_ENCODING )
    {
        hb_status->i_state = HB_STATE_ENCODING;
        hb_status->b_new = 1;
    }

    hb_status->position = position;
    hb_status->fps = frameRate;
    hb_status->i_remaining = remainingTime;
}

static void RipDone( void * data, int result )
{
    /* TODO check result */
    hb_status->i_state = HB_STATE_DONE;
    hb_status->b_new = 1;
}