OSDN Git Service

MacGui: Remove Target Size as a rate control option as it doesn't really work correct...
[handbrake-jp/handbrake-jp-git.git] / gtk / src / main.c
index a9c07fa..72617eb 100644 (file)
@@ -1,7 +1,7 @@
 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
 /*
  * main.c
- * Copyright (C) John Stebbins 2008 <stebbins@stebbins>
+ * Copyright (C) John Stebbins 2008-2011 <stebbins@stebbins>
  * 
  * main.c is free software.
  * 
 
 #include <gtk/gtk.h>
 
-#if !defined(_WIN32)
+#if defined(_ENABLE_GST)
 #include <gst/gst.h>
+#endif
+
+#if !defined(_WIN32)
 #include <libnotify/notify.h>
+#include <dbus/dbus-glib.h>
 #else
 #include <windows.h>
 #include <io.h>
-//#include <pthread/pthread.h>
 #define pipe(phandles) _pipe (phandles, 4096, _O_BINARY)
 #endif
 
@@ -152,7 +155,7 @@ MyConnect(
     g_return_if_fail(handler_name != NULL);
     g_return_if_fail(signal_name != NULL);
 
-       //const gchar *name = gtk_widget_get_name((GtkWidget*)object);
+       //const gchar *name = ghb_get_setting_key((GtkWidget*)object);
        //g_message("\n\nname %s", name);
        g_debug("handler_name %s", handler_name);
        g_debug("signal_name %s", signal_name);
@@ -192,7 +195,7 @@ change_font(GtkWidget *widget, gpointer data)
     font_desc = pango_font_description_from_string(font);
     if (font_desc == NULL) exit(1);
     gtk_widget_modify_font(widget, font_desc);
-    name = gtk_widget_get_name(widget);
+    name = ghb_get_setting_key(widget);
     g_debug("changing font for widget %s\n", name);
     if (GTK_IS_CONTAINER(widget))
     {
@@ -330,38 +333,41 @@ bind_audio_tree_model (signal_user_data_t *ud)
        selection = gtk_tree_view_get_selection (treeview);
        // 12 columns in model.  6 are visible, the other 6 are for storing
        // values that I need
-       treestore = gtk_list_store_new(12, G_TYPE_STRING, G_TYPE_STRING, 
-                                                                  G_TYPE_STRING, G_TYPE_STRING, 
-                                                                  G_TYPE_STRING, G_TYPE_STRING,
+       treestore = gtk_list_store_new(6, G_TYPE_STRING, G_TYPE_STRING, 
                                                                   G_TYPE_STRING, G_TYPE_STRING, 
-                                                                  G_TYPE_STRING, G_TYPE_STRING,
-                                                                  G_TYPE_STRING, G_TYPE_DOUBLE);
+                                                                  G_TYPE_STRING, G_TYPE_STRING);
        gtk_tree_view_set_model(treeview, GTK_TREE_MODEL(treestore));
 
        cell = gtk_cell_renderer_text_new();
        column = gtk_tree_view_column_new_with_attributes(
                                                                        _("Track"), cell, "text", 0, NULL);
     gtk_tree_view_append_column(treeview, GTK_TREE_VIEW_COLUMN(column));
+       gtk_tree_view_column_set_min_width (column, 200);
+       gtk_tree_view_column_set_max_width (column, 200);
 
        cell = gtk_cell_renderer_text_new();
        column = gtk_tree_view_column_new_with_attributes(
                                                                        _("Codec"), cell, "text", 1, NULL);
     gtk_tree_view_append_column(treeview, GTK_TREE_VIEW_COLUMN(column));
+       gtk_tree_view_column_set_min_width (column, 110);
 
        cell = gtk_cell_renderer_text_new();
        column = gtk_tree_view_column_new_with_attributes(
                                                                        _("Bitrate"), cell, "text", 2, NULL);
     gtk_tree_view_append_column(treeview, GTK_TREE_VIEW_COLUMN(column));
+       gtk_tree_view_column_set_min_width (column, 50);
 
        cell = gtk_cell_renderer_text_new();
        column = gtk_tree_view_column_new_with_attributes(
                                                                        _("Sample Rate"), cell, "text", 3, NULL);
     gtk_tree_view_append_column(treeview, GTK_TREE_VIEW_COLUMN(column));
+       gtk_tree_view_column_set_min_width (column, 100);
 
        cell = gtk_cell_renderer_text_new();
        column = gtk_tree_view_column_new_with_attributes(
                                                                        _("Mix"), cell, "text", 4, NULL);
     gtk_tree_view_append_column(treeview, GTK_TREE_VIEW_COLUMN(column));
+       gtk_tree_view_column_set_min_width (column, 115);
 
        cell = gtk_cell_renderer_text_new();
        column = gtk_tree_view_column_new_with_attributes(
@@ -379,6 +385,7 @@ bind_audio_tree_model (signal_user_data_t *ud)
 extern G_MODULE_EXPORT void subtitle_list_selection_changed_cb(void);
 extern G_MODULE_EXPORT void subtitle_forced_toggled_cb(void);
 extern G_MODULE_EXPORT void subtitle_burned_toggled_cb(void);
+extern G_MODULE_EXPORT void subtitle_default_toggled_cb(void);
 
 // Create and bind the tree model to the tree view for the subtitle track list
 // Also, connect up the signal that lets us know the selection has changed
@@ -395,39 +402,50 @@ bind_subtitle_tree_model (signal_user_data_t *ud)
        g_debug("bind_subtitle_tree_model ()\n");
        treeview = GTK_TREE_VIEW(GHB_WIDGET (ud->builder, "subtitle_list"));
        selection = gtk_tree_view_get_selection (treeview);
-       // 6 columns in model.  4 are visible, the other 2 is for storing
+       // 6 columns in model.  5 are visible, the other 1 is for storing
        // values that I need
-       treestore = gtk_list_store_new(8, 
+       // Track, force, burn, default, type, srt offset, track short, source
+       // force visible, burn visible, offset visible
+       treestore = gtk_list_store_new(10, 
                                                                        G_TYPE_STRING,
                                                                        G_TYPE_BOOLEAN, G_TYPE_BOOLEAN,
-                                                                       G_TYPE_STRING,  G_TYPE_STRING, 
-                                                                       G_TYPE_STRING,  G_TYPE_INT, 
-                                                                       G_TYPE_INT);
+                                                                       G_TYPE_BOOLEAN,
+                                                                       G_TYPE_INT,     G_TYPE_STRING,
+                                                                       G_TYPE_INT,
+                                                                       G_TYPE_BOOLEAN, G_TYPE_BOOLEAN,
+                                                                       G_TYPE_BOOLEAN);
        gtk_tree_view_set_model(treeview, GTK_TREE_MODEL(treestore));
 
        cell = gtk_cell_renderer_text_new();
-       column = gtk_tree_view_column_new_with_attributes( _("Track"), cell, 
-                               "text", 0, "foreground", 5, "weight", 6, "style", 7, NULL);
+       column = gtk_tree_view_column_new_with_attributes(
+                                                                       _("Track"), cell, "text", 0, NULL);
        gtk_tree_view_append_column(treeview, GTK_TREE_VIEW_COLUMN(column));
+       gtk_tree_view_column_set_min_width (column, 350);
+       gtk_tree_view_column_set_max_width (column, 350);
 
        cell = gtk_cell_renderer_toggle_new();
        column = gtk_tree_view_column_new_with_attributes(
-                                                                       _("Forced Only"), cell, "active", 1, NULL);
-       gtk_tree_view_column_set_max_width (column, 50);
+                       _("Forced Only"), cell, "active", 1, "visible", 7, NULL);
        gtk_tree_view_append_column(treeview, GTK_TREE_VIEW_COLUMN(column));
        g_signal_connect(cell, "toggled", subtitle_forced_toggled_cb, ud);
 
        cell = gtk_cell_renderer_toggle_new();
        gtk_cell_renderer_toggle_set_radio(GTK_CELL_RENDERER_TOGGLE(cell), TRUE);
        column = gtk_tree_view_column_new_with_attributes(
-                               _("Burned In"), cell, "active", 2, "cell-background", 5, NULL);
-       gtk_tree_view_column_set_max_width (column, 50);
+                       _("Burned In"), cell, "active", 2, "visible", 8, NULL);
        gtk_tree_view_append_column(treeview, GTK_TREE_VIEW_COLUMN(column));
        g_signal_connect(cell, "toggled", subtitle_burned_toggled_cb, ud);
 
+       cell = gtk_cell_renderer_toggle_new();
+       gtk_cell_renderer_toggle_set_radio(GTK_CELL_RENDERER_TOGGLE(cell), TRUE);
+       column = gtk_tree_view_column_new_with_attributes(
+                               _("Default"), cell, "active", 3, NULL);
+       gtk_tree_view_append_column(treeview, GTK_TREE_VIEW_COLUMN(column));
+       g_signal_connect(cell, "toggled", subtitle_default_toggled_cb, ud);
+
        cell = gtk_cell_renderer_text_new();
        column = gtk_tree_view_column_new_with_attributes(
-                                                                       _("Type"), cell, "text", 3, NULL);
+                       _("Srt Offset"), cell, "text", 4, "visible", 9, NULL);
        gtk_tree_view_append_column(treeview, GTK_TREE_VIEW_COLUMN(column));
 
 
@@ -442,6 +460,7 @@ bind_subtitle_tree_model (signal_user_data_t *ud)
 extern G_MODULE_EXPORT void presets_list_selection_changed_cb(void);
 extern G_MODULE_EXPORT void presets_drag_cb(void);
 extern G_MODULE_EXPORT void presets_drag_motion_cb(void);
+extern G_MODULE_EXPORT void preset_edited_cb(void);
 extern void presets_row_expanded_cb(void);
 
 // Create and bind the tree model to the tree view for the preset list
@@ -462,13 +481,17 @@ bind_presets_tree_model (signal_user_data_t *ud)
        g_debug("bind_presets_tree_model ()\n");
        treeview = GTK_TREE_VIEW(GHB_WIDGET (ud->builder, "presets_list"));
        selection = gtk_tree_view_get_selection (treeview);
-       treestore = gtk_tree_store_new(5, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT, 
-                                                                  G_TYPE_STRING, G_TYPE_STRING);
+       treestore = gtk_tree_store_new(6, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT, 
+                                                                 G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN);
        gtk_tree_view_set_model(treeview, GTK_TREE_MODEL(treestore));
 
        cell = gtk_cell_renderer_text_new();
        column = gtk_tree_view_column_new_with_attributes(_("Preset Name"), cell, 
-                                       "text", 0, "weight", 1, "style", 2, "foreground", 3, NULL);
+               "text", 0, "weight", 1, "style", 2, 
+               "foreground", 3, "editable", 5, NULL);
+
+       g_signal_connect(cell, "edited", preset_edited_cb, ud);
+
     gtk_tree_view_append_column(treeview, GTK_TREE_VIEW_COLUMN(column));
        gtk_tree_view_column_set_expand (column, TRUE);
        gtk_tree_view_set_tooltip_column (treeview, 4);
@@ -489,11 +512,121 @@ bind_presets_tree_model (signal_user_data_t *ud)
 }
 
 static void
+clean_old_logs()
+{
+#if !defined(_WIN32)
+       const gchar *file;
+       gchar *config;
+
+       config = ghb_get_user_config_dir(NULL);
+
+       if (g_file_test(config, G_FILE_TEST_IS_DIR))
+       {
+               GDir *gdir = g_dir_open(config, 0, NULL);
+               file = g_dir_read_name(gdir);
+               while (file)
+               {
+                       if (strncmp(file, "Activity.log.", 13) == 0)
+                       {
+                               gchar *path;
+                               int fd, lock = 1;
+                               int pid;
+
+                               sscanf(file, "Activity.log.%d", &pid);
+
+                               path = g_strdup_printf("%s/ghb.pid.%d", config, pid);
+                               if (g_file_test(path, G_FILE_TEST_EXISTS))
+                               {
+                                       fd = open(path, O_RDWR);
+                                       if (fd >= 0)
+                                       {
+                                               lock = lockf(fd, F_TLOCK, 0);
+                                       }
+                                       g_free(path);
+                                       close(fd);
+                                       if (lock == 0)
+                                       {
+                                               path = g_strdup_printf("%s/%s", config, file);
+                                               g_unlink(path);
+                                               g_free(path);
+                                       }
+                               }
+                               else
+                               {
+                                       g_free(path);
+                                       path = g_strdup_printf("%s/%s", config, file);
+                                       g_unlink(path);
+                                       g_free(path);
+                               }
+                       }
+                       file = g_dir_read_name(gdir);
+               }
+               g_dir_close(gdir);
+       }
+       g_free(config);
+#else
+       const gchar *file;
+       gchar *config;
+
+       config = ghb_get_user_config_dir(NULL);
+
+       if (g_file_test(config, G_FILE_TEST_IS_DIR))
+       {
+               GDir *gdir = g_dir_open(config, 0, NULL);
+               file = g_dir_read_name(gdir);
+               while (file)
+               {
+                       if (strncmp(file, "Activity.log.", 13) == 0)
+                       {
+                               gchar *path;
+                               int pid;
+
+                               sscanf(file, "Activity.log.%d", &pid);
+
+#if 0
+                               int fd, lock = 1;
+
+                               path = g_strdup_printf("%s/ghb.pid.%d", config, pid);
+                               if (g_file_test(path, G_FILE_TEST_EXISTS))
+                               {
+                                       fd = open(path, O_RDWR);
+                                       if (fd >= 0)
+                                       {
+                                               lock = lockf(fd, F_TLOCK, 0);
+                                       }
+                                       g_free(path);
+                                       close(fd);
+                                       if (lock == 0)
+                                       {
+                                               path = g_strdup_printf("%s/%s", config, file);
+                                               g_unlink(path);
+                                               g_free(path);
+                                       }
+                               }
+                               else
+#endif
+                               {
+                                       //g_free(path);
+                                       path = g_strdup_printf("%s/%s", config, file);
+                                       g_unlink(path);
+                                       g_free(path);
+                               }
+                       }
+                       file = g_dir_read_name(gdir);
+               }
+               g_dir_close(gdir);
+       }
+       g_free(config);
+#endif
+}
+
+static void
 IoRedirect(signal_user_data_t *ud)
 {
        GIOChannel *channel;
        gint pfd[2];
-       gchar *config, *path;
+       gchar *config, *path, *str;
+       pid_t pid;
 
        // I'm opening a pipe and attaching the writer end to stderr
        // The reader end will be polled by main event loop and I'll get
@@ -503,21 +636,26 @@ IoRedirect(signal_user_data_t *ud)
                g_warning("Failed to redirect IO. Logging impaired\n");
                return;
        }
+       clean_old_logs();
        // Open activity log.
-       // TODO: Put this in the same directory as the encode destination
        config = ghb_get_user_config_dir(NULL);
-       path = g_strdup_printf("%s/%s", config, "Activity.log");
+       pid = getpid();
+       path = g_strdup_printf("%s/Activity.log.%d", config, pid);
        ud->activity_log = g_io_channel_new_file (path, "w", NULL);
        ud->job_activity_log = NULL;
-       ghb_ui_update(ud, "activity_location", ghb_string_value(path));
+       str = g_strdup_printf("<big><b>%s</b></big>", path);
+       ghb_ui_update(ud, "activity_location", ghb_string_value(str));
+       g_free(str);
        g_free(path);
        g_free(config);
        // Set encoding to raw.
        g_io_channel_set_encoding (ud->activity_log, NULL, NULL);
-#if !defined(_WIN32)
-       stderr->_fileno = pfd[1];
-#else
+       // redirect stderr to the writer end of the pipe
+#if defined(_WIN32)
+       // dup2 doesn't work on windows for some stupid reason
        stderr->_file = pfd[1];
+#else
+       dup2(pfd[1], /*stderr*/2);
 #endif
        setvbuf(stderr, NULL, _IONBF, 0);
        channel = g_io_channel_unix_new (pfd[0]);
@@ -581,10 +719,6 @@ watch_volumes(signal_user_data_t *ud)
 #endif
 }
 
-// Hack to avoid a segfault in libavcodec
-extern int mm_flags;
-int mm_support();
-
 G_MODULE_EXPORT void x264_entry_changed_cb(GtkWidget *widget, signal_user_data_t *ud);
 void preview_window_expose_cb(void);
 
@@ -613,10 +747,15 @@ const gchar *hud_rcstyle =
 "fg[PRELIGHT]=\"white\"\n"
 "}\n"
 "widget_class \"*.GtkComboBox.GtkToggleButton\" style \"ghb-combo\"\n"
+"widget_class \"*.GtkScaleButton\" style \"ghb-combo\"\n"
 "widget_class \"*.GtkEntry\" style \"ghb-entry\"\n"
 "widget \"preview_window.*.preview_hud.*\" style \"ghb-hud\"\n"
 "widget \"preview_window\" style \"ghb-preview\"\n";
 
+#if GTK_CHECK_VERSION(2, 16, 0)
+extern G_MODULE_EXPORT void status_icon_query_tooltip_cb(void);
+#endif
+
 int
 main (int argc, char *argv[])
 {
@@ -625,32 +764,33 @@ main (int argc, char *argv[])
        GError *error = NULL;
        GOptionContext *context;
 
-       mm_flags = mm_support();
 #ifdef ENABLE_NLS
        bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
        bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
        textdomain (GETTEXT_PACKAGE);
 #endif
 
-#ifdef PTW32_STATIC_LIB
-       pthread_win32_process_attach_np();
-       pthread_win32_thread_attach_np();
-#endif
-
        if (!g_thread_supported())
                g_thread_init(NULL);
        context = g_option_context_new ("- Rip and encode DVD or MPEG file");
        g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
        g_option_context_add_group (context, gtk_get_option_group (TRUE));
-#if !defined(_WIN32)
+#if defined(_ENABLE_GST)
        g_option_context_add_group (context, gst_init_get_option_group ());
 #endif
        g_option_context_parse (context, &argc, &argv, &error);
        g_option_context_free(context);
+
+       if (argc > 1 && dvd_device == NULL && argv[1][0] != '-')
+       {
+               dvd_device = argv[1];
+       }
        
        gtk_set_locale ();
        gtk_init (&argc, &argv);
        gtk_rc_parse_string(hud_rcstyle);
+       g_type_class_unref(g_type_class_ref(GTK_TYPE_BUTTON));
+       g_object_set(gtk_settings_get_default(), "gtk-button-images", TRUE, NULL);
 #if !defined(_WIN32)
        notify_init("HandBrake");
 #endif
@@ -659,9 +799,11 @@ main (int argc, char *argv[])
        ghb_load_icons();
 
 #if !defined(_WIN32)
-       ghb_hal_init();
+       dbus_g_thread_init();
 #endif
+       ghb_udev_init();
 
+       ghb_write_pid_file();
        ud = g_malloc0(sizeof(signal_user_data_t));
        ud->debug = ghb_debug;
        g_log_set_handler (NULL, G_LOG_LEVEL_DEBUG, debug_log_handler, ud);
@@ -675,11 +817,24 @@ main (int argc, char *argv[])
        //GtkWidget *widget = GHB_WIDGET(ud->builder, "PictureDetelecineCustom");
        //gtk_entry_set_inner_border(widget, 2);
 
+       // Since GtkBuilder no longer assigns object ids to widget names
+       // Assign a few that are necessary for style overrides to work
+       GtkWidget *widget;
+#if defined(_NO_UPDATE_CHECK)
+       widget = GHB_WIDGET(ud->builder, "check_updates_box");
+       gtk_widget_hide(widget);
+#endif
+
+       widget = GHB_WIDGET(ud->builder, "preview_hud");
+       gtk_widget_set_name(widget, "preview_hud");
+       widget = GHB_WIDGET(ud->builder, "preview_window");
+       gtk_widget_set_name(widget, "preview_window");
+
        // Set up the "hud" control overlay for the preview window
        GtkWidget *draw, *hud, *blender, *align;
 
        align = GHB_WIDGET(ud->builder, "preview_window_alignment");
-       draw = GHB_WIDGET(ud->builder, "preview_image");
+       draw = GHB_WIDGET(ud->builder, "preview_image_align");
        hud = GHB_WIDGET(ud->builder, "preview_hud");
 
        // Set up compositing for hud
@@ -723,7 +878,7 @@ main (int argc, char *argv[])
        // Load all internal settings
        ghb_settings_init(ud);
        // Load the presets files
-       ghb_presets_load();
+       ghb_presets_load(ud);
        ghb_prefs_load(ud);
 
        ghb_prefs_to_ui(ud);
@@ -766,23 +921,51 @@ main (int argc, char *argv[])
        if (dvd_device != NULL)
        {
                // Source overridden from command line option
-               ghb_settings_set_string(ud->settings, "source", dvd_device);
+               ghb_settings_set_string(ud->settings, "scan_source", dvd_device);
+               g_idle_add((GSourceFunc)ghb_idle_scan, ud);
        }
        // Reload and check status of the last saved queue
        g_idle_add((GSourceFunc)ghb_reload_queue, ud);
-       if (ghb_settings_get_boolean(ud->settings, "check_updates"))
-       {
-               g_thread_create((GThreadFunc)ghb_check_update, ud, FALSE, NULL);
-       }
+
        // Start timer for monitoring libhb status, 500ms
        g_timeout_add (500, ghb_timer_cb, (gpointer)ud);
 
        // Add dvd devices to File menu
-       g_idle_add((GSourceFunc)ghb_file_menu_add_dvd, ud);
+       ghb_volname_cache_init();
+       g_thread_create((GThreadFunc)ghb_cache_volnames, ud, FALSE, NULL);
 
+       GtkStatusIcon *si;
+       si = GTK_STATUS_ICON(GHB_OBJECT(ud->builder, "hb_status"));
+
+       gtk_status_icon_set_visible(si,
+                       ghb_settings_get_boolean(ud->settings, "show_status"));
+
+#if GTK_CHECK_VERSION(2, 16, 0)
+       gtk_status_icon_set_has_tooltip(si, TRUE);
+       g_signal_connect(si, "query-tooltip", 
+                                       status_icon_query_tooltip_cb, ud);
+#else
+       gtk_status_icon_set_tooltip(si, "HandBrake");
+#endif
+
+       // Ugly hack to keep subtitle table from bouncing around as I change
+       // which set of controls are visible
+       GtkRequisition req;
+       gint height;
+       
+       widget = GHB_WIDGET(ud->builder, "SrtCodeset");
+       gtk_widget_size_request( widget, &req );
+       height = req.height;
+       widget = GHB_WIDGET(ud->builder, "srt_code_label");
+       gtk_widget_size_request( widget, &req );
+       height += req.height;
+       widget = GHB_WIDGET(ud->builder, "subtitle_table");
+       gtk_widget_set_size_request(widget, -1, height);
+       
        // Everything should be go-to-go.  Lets rock!
 
        gtk_main ();
+       gtk_status_icon_set_visible(si, FALSE);
        ghb_backend_close();
        if (ud->queue)
                ghb_value_free(ud->queue);
@@ -793,9 +976,6 @@ main (int argc, char *argv[])
        notify_uninit();
 #endif
        g_free(ud);
-#ifdef PTW32_STATIC_LIB
-       pthread_win32_thread_detach_np();
-       pthread_win32_process_detach_np();
-#endif
+
        return 0;
 }