void gmpv_mpv_opt_handle_geometry(GmpvMpv *mpv) { gchar *opt_buf; opt_buf = mpv_get_property_string (mpv->mpv_ctx, "options/geometry"); if(opt_buf) { parse_geom_string(mpv, opt_buf, &mpv->geometry); mpv_free(opt_buf); } }
void gmpv_mpv_opt_handle_fs(GmpvMpv *mpv) { gchar *optbuf; optbuf = gmpv_mpv_get_property_string(mpv, "options/fs"); if(g_strcmp0(optbuf, "yes") == 0) { gmpv_mpv_command_string (mpv, "script-message gmpv-action toggle-fullscreen"); } mpv_free(optbuf); }
void resize_window_to_fit(gmpv_handle *ctx, gdouble multiplier) { gchar *video = mpv_get_property_string(ctx->mpv_ctx, "video"); gint64 width; gint64 height; gint mpv_width_rc; gint mpv_height_rc; mpv_width_rc = mpv_get_property( ctx->mpv_ctx, "dwidth", MPV_FORMAT_INT64, &width ); mpv_height_rc = mpv_get_property( ctx->mpv_ctx, "dheight", MPV_FORMAT_INT64, &height ); if(video && strncmp(video, "no", 3) != 0 && mpv_width_rc >= 0 && mpv_height_rc >= 0) { gint width_margin; gint height_margin; gint new_width; gint new_height; width_margin = main_window_get_width_margin(ctx->gui); height_margin = main_window_get_height_margin(ctx->gui); new_width = (gint)(multiplier*(gdouble)width)+width_margin; new_height = (gint)(multiplier*(gdouble)height)+height_margin; gtk_window_resize( GTK_WINDOW(ctx->gui), new_width, new_height ); } mpv_free(video); }
void gmpv_mpv_opt_handle_msg_level(GmpvMpv *mpv) { const struct { gchar *name; mpv_log_level level; } level_map[] = { {"no", MPV_LOG_LEVEL_NONE}, {"fatal", MPV_LOG_LEVEL_FATAL}, {"error", MPV_LOG_LEVEL_ERROR}, {"warn", MPV_LOG_LEVEL_WARN}, {"info", MPV_LOG_LEVEL_INFO}, {"v", MPV_LOG_LEVEL_V}, {"debug", MPV_LOG_LEVEL_DEBUG}, {"trace", MPV_LOG_LEVEL_TRACE}, {NULL, MPV_LOG_LEVEL_NONE} }; gchar *optbuf = NULL; gchar **tokens = NULL; mpv_log_level min_level = DEFAULT_LOG_LEVEL; gint i; optbuf = mpv_get_property_string(mpv->mpv_ctx, "options/msg-level"); if(optbuf) { tokens = g_strsplit(optbuf, ",", 0); } if(mpv->log_level_list) { g_slist_free_full(mpv->log_level_list, g_free); mpv->log_level_list = NULL; } for(i = 0; tokens && tokens[i]; i++) { gchar **pair = g_strsplit(tokens[i], "=", 2); module_log_level *level = g_malloc(sizeof(module_log_level)); gboolean found = FALSE; gint j; level->prefix = g_strdup(pair[0]); for(j = 0; level_map[j].name && !found; j++) { if(g_strcmp0(pair[1], level_map[j].name) == 0) { level->level = level_map[j].level; found = TRUE; } } /* Ignore if the given level is invalid */ if(found) { /* Lower log levels have higher values */ if(level->level > min_level) { min_level = level->level; } if(g_strcmp0(level->prefix, "all") != 0) { mpv->log_level_list = g_slist_append (mpv->log_level_list, level); } } g_strfreev(pair); } for(i = 0; level_map[i].level != min_level; i++); mpv_check_error (mpv_request_log_messages(mpv->mpv_ctx, level_map[i].name)); mpv_free(optbuf); g_strfreev(tokens); }
void gmpv_mpv_opt_handle_autofit(GmpvMpv *mpv) { gchar *scale_str = NULL; gchar *autofit_str = NULL; gchar *larger_str = NULL; gchar *smaller_str = NULL; gboolean scale_set = FALSE; gboolean autofit_set = FALSE; gboolean larger_set = FALSE; gboolean smaller_set = FALSE; scale_str = mpv_get_property_string (mpv->mpv_ctx, "options/window-scale"); autofit_str = mpv_get_property_string (mpv->mpv_ctx, "options/autofit"); larger_str = mpv_get_property_string (mpv->mpv_ctx, "options/autofit-larger"); smaller_str = mpv_get_property_string (mpv->mpv_ctx, "options/autofit-smaller"); scale_set = scale_str && scale_str[0] != '\0'; autofit_set = autofit_str && autofit_str[0] != '\0'; larger_set = larger_str && larger_str[0] != '\0'; smaller_set = smaller_str && smaller_str[0] != '\0'; if(scale_set || autofit_set || larger_set || smaller_set) { gint64 larger_dim[2] = {G_MAXINT, G_MAXINT}; gint64 smaller_dim[2] = {0, 0}; gint64 autofit_dim[2] = {0, 0}; gint64 vid_dim[2]; gdouble ratio[2]; gdouble scale = 1; gint rc = 0; rc |= mpv_get_property( mpv->mpv_ctx, "dwidth", MPV_FORMAT_INT64, &vid_dim[0] ); rc |= mpv_get_property( mpv->mpv_ctx, "dheight", MPV_FORMAT_INT64, &vid_dim[1] ); if(rc >= 0) { g_debug( "Retrieved video size: " "%" G_GINT64_FORMAT "x" "%" G_GINT64_FORMAT, vid_dim[0], vid_dim[1] ); } if(rc >= 0 && scale_set) { g_debug( "Retrieved option --window-scale=%s", scale_str); /* This should never fail since mpv_set_option() will * refuse to set invalid values. */ scale = g_ascii_strtod(scale_str, NULL); } if(rc >= 0 && larger_set) { g_debug( "Retrieved option --autofit-larger=%s", larger_str); parse_dim_string(larger_str, larger_dim); } if(rc >= 0 && smaller_set) { g_debug( "Retrieved option --autofit-smaller=%s", smaller_str); parse_dim_string(smaller_str, smaller_dim); } if(rc >= 0) { if(autofit_set) { g_debug( "Retrieved option --autofit=%s", autofit_str ); parse_dim_string(autofit_str, autofit_dim); } else { autofit_dim[0] = vid_dim[0]; autofit_dim[1] = vid_dim[1]; } if(scale_set) { autofit_dim[0] = (gint64)(scale*(gdouble)autofit_dim[0]); autofit_dim[1] = (gint64)(scale*(gdouble)autofit_dim[1]); } } if(rc >= 0) { autofit_dim[0] = MIN(autofit_dim[0], larger_dim[0]); autofit_dim[1] = MIN(autofit_dim[1], larger_dim[1]); autofit_dim[0] = MAX(autofit_dim[0], smaller_dim[0]); autofit_dim[1] = MAX(autofit_dim[1], smaller_dim[1]); if(!autofit_set && autofit_dim[0] == vid_dim[0] && autofit_dim[1] == vid_dim[1]) { /* Do not resize if --autofit is not set and the * video size does not exceed the limits imposed * by --autofit-larger and --autofit-smaller. */ ratio[0] = scale_set?scale:0; ratio[1] = scale_set?scale:0; } else { g_debug( "Target video area size: " "%" G_GINT64_FORMAT "x%" G_GINT64_FORMAT, autofit_dim[0], autofit_dim[1] ); ratio[0] = (gdouble)autofit_dim[0]/ (gdouble)vid_dim[0]; ratio[1] = (gdouble)autofit_dim[1]/ (gdouble)vid_dim[1]; } } if(rc >= 0 && ratio[0] > 0 && ratio[1] > 0) { /* Resize the window so that it is as big as possible * while preseving the aspect ratio. */ mpv->autofit_ratio = MIN(ratio[0], ratio[1]); g_debug( "Set video size multiplier to %f", mpv->autofit_ratio ); } else { mpv->autofit_ratio = -1; } } mpv_free(scale_str); mpv_free(autofit_str); mpv_free(larger_str); mpv_free(smaller_str); }
void gmpv_mpv_obj_initialize(GmpvMpvObj *mpv) { GSettings *main_settings = g_settings_new(CONFIG_ROOT); gchar *config_dir = get_config_dir_path(); gchar *mpvopt = NULL; gchar *current_vo = NULL; gchar *mpv_version = NULL; const struct { const gchar *name; const gchar *value; } options[] = { {"osd-level", "1"}, {"softvol", "yes"}, {"force-window", "yes"}, {"input-default-bindings", "yes"}, {"audio-client-name", ICON_NAME}, {"title", "${media-title}"}, {"autofit-larger", "75%"}, {"window-scale", "1"}, {"pause", "no"}, {"ytdl", "yes"}, {"osd-bar", "no"}, {"input-cursor", "no"}, {"cursor-autohide", "no"}, {"softvol-max", "100"}, {"config", "yes"}, {"screenshot-template", "gnome-mpv-shot%n"}, {"config-dir", config_dir}, {NULL, NULL} }; g_assert(mpv->mpv_ctx); for(gint i = 0; options[i].name; i++) { g_debug( "Applying default option --%s=%s", options[i].name, options[i].value ); mpv_set_option_string( mpv->mpv_ctx, options[i].name, options[i].value ); } if(g_settings_get_boolean(main_settings, "mpv-config-enable")) { gchar *mpv_conf = g_settings_get_string (main_settings, "mpv-config-file"); g_info("Loading config file: %s", mpv_conf); mpv_load_config_file(mpv->mpv_ctx, mpv_conf); g_free(mpv_conf); } if(g_settings_get_boolean(main_settings, "mpv-input-config-enable")) { gchar *input_conf = g_settings_get_string (main_settings, "mpv-input-config-file"); g_info("Loading input config file: %s", input_conf); load_input_conf(mpv, input_conf); g_free(input_conf); } else { load_input_conf(mpv, NULL); } mpvopt = g_settings_get_string(main_settings, "mpv-options"); g_debug("Applying extra mpv options: %s", mpvopt); /* Apply extra options */ if(mpv_obj_apply_args(mpv->mpv_ctx, mpvopt) < 0) { const gchar *msg = _("Failed to apply one or more MPV options."); g_signal_emit_by_name(mpv, "mpv-error", msg); } if(mpv->force_opengl) { g_info("Forcing --vo=opengl-cb"); mpv_set_option_string(mpv->mpv_ctx, "vo", "opengl-cb"); } else { g_debug( "Attaching mpv window to wid %#x", (guint)mpv->wid ); mpv_set_option(mpv->mpv_ctx, "wid", MPV_FORMAT_INT64, &mpv->wid); } mpv_observe_property(mpv->mpv_ctx, 0, "aid", MPV_FORMAT_INT64); mpv_observe_property(mpv->mpv_ctx, 0, "chapters", MPV_FORMAT_INT64); mpv_observe_property(mpv->mpv_ctx, 0, "core-idle", MPV_FORMAT_FLAG); mpv_observe_property(mpv->mpv_ctx, 0, "fullscreen", MPV_FORMAT_FLAG); mpv_observe_property(mpv->mpv_ctx, 0, "pause", MPV_FORMAT_FLAG); mpv_observe_property(mpv->mpv_ctx, 0, "length", MPV_FORMAT_DOUBLE); mpv_observe_property(mpv->mpv_ctx, 0, "media-title", MPV_FORMAT_STRING); mpv_observe_property(mpv->mpv_ctx, 0, "playlist-pos", MPV_FORMAT_INT64); mpv_observe_property(mpv->mpv_ctx, 0, "track-list", MPV_FORMAT_NODE); mpv_observe_property(mpv->mpv_ctx, 0, "volume", MPV_FORMAT_DOUBLE); mpv_set_wakeup_callback(mpv->mpv_ctx, wakeup_callback, mpv); mpv_check_error(mpv_initialize(mpv->mpv_ctx)); mpv_version = gmpv_mpv_obj_get_property_string(mpv, "mpv-version"); current_vo = gmpv_mpv_obj_get_property_string(mpv, "current-vo"); g_info("Using %s", mpv_version); if(current_vo && !GDK_IS_X11_DISPLAY(gdk_display_get_default())) { g_info( "The chosen vo is %s but the display is not X11; " "forcing --vo=opengl-cb and resetting", current_vo ); mpv->force_opengl = TRUE; mpv->state.paused = FALSE; gmpv_mpv_obj_reset(mpv); } else { GSettings *win_settings; gdouble volume; win_settings = g_settings_new(CONFIG_WIN_STATE); volume = g_settings_get_double(win_settings, "volume")*100; g_debug("Setting volume to %f", volume); mpv_set_property( mpv->mpv_ctx, "volume", MPV_FORMAT_DOUBLE, &volume ); /* The vo should be opengl-cb if current_vo is NULL*/ if(!current_vo) { mpv->opengl_ctx = mpv_get_sub_api ( mpv->mpv_ctx, MPV_SUB_API_OPENGL_CB ); } gmpv_mpv_opt_handle_msg_level(mpv); gmpv_mpv_opt_handle_fs(mpv); gmpv_mpv_opt_handle_geometry(mpv); mpv->force_opengl = FALSE; mpv->state.ready = TRUE; g_signal_emit_by_name(mpv, "mpv-init"); g_clear_object(&win_settings); } g_clear_object(&main_settings); g_free(config_dir); g_free(mpvopt); mpv_free(current_vo); mpv_free(mpv_version); }
void gmpv_mpv_obj_free(gpointer data) { mpv_free(data); }
void gmpv_mpv_obj_reset(GmpvMpvObj *mpv) { const gchar *quit_cmd[] = {"quit_watch_later", NULL}; gchar *loop_str; gboolean loop; gint64 playlist_pos; gint playlist_pos_rc; g_assert(mpv->mpv_ctx); loop_str = gmpv_mpv_obj_get_property_string(mpv, "loop"); loop = (g_strcmp0(loop_str, "inf") == 0); mpv_free(loop_str); playlist_pos_rc = mpv_get_property( mpv->mpv_ctx, "playlist-pos", MPV_FORMAT_INT64, &playlist_pos ); /* Reset mpv->mpv_ctx */ mpv->state.ready = FALSE; mpv_check_error(gmpv_mpv_obj_command(mpv, quit_cmd)); gmpv_mpv_obj_quit(mpv); mpv->mpv_ctx = mpv_create(); gmpv_mpv_obj_initialize(mpv); gmpv_mpv_obj_set_event_callback ( mpv, mpv->event_callback, mpv->event_callback_data ); gmpv_mpv_obj_set_opengl_cb_callback ( mpv, mpv->opengl_cb_callback, mpv->opengl_cb_callback_data ); gmpv_mpv_obj_set_property_string(mpv, "loop", loop?"inf":"no"); if(mpv->playlist) { if(mpv->state.loaded) { gint rc; rc = mpv_request_event (mpv->mpv_ctx, MPV_EVENT_FILE_LOADED, 0); mpv_check_error(rc); gmpv_mpv_obj_load(mpv, NULL, FALSE, TRUE); rc = mpv_request_event (mpv->mpv_ctx, MPV_EVENT_FILE_LOADED, 1); mpv_check_error(rc); } if(playlist_pos_rc >= 0 && playlist_pos > 0) { gmpv_mpv_obj_set_property( mpv, "playlist-pos", MPV_FORMAT_INT64, &playlist_pos ); } gmpv_mpv_obj_set_property( mpv, "pause", MPV_FORMAT_FLAG, &mpv->state.paused ); } }