st-handler.c

来自「linux下网络收音机的源码」· C语言 代码 · 共 1,838 行 · 第 1/4 页

C
1,838
字号
  g_return_if_fail(size > 0);  g_return_if_fail(data != NULL);  st_handler_set_icon_from_inline(handler, size, data);}/** * st_handler_set_stock_categories: * @handler: a handler. * @categories: a tree of stock categories. * * Sets the handler stock categories. The "__main" category is * mandatory. * * Special category names: __main and __search. * * The stock categories will not be saved to permanent storage. **/voidst_handler_set_stock_categories (STHandler *handler, GNode *categories){  g_return_if_fail(ST_IS_HANDLER(handler));  g_return_if_fail(categories != NULL);  g_return_if_fail(handler->priv->stock_categories == NULL);  g_node_traverse(categories,		  G_PRE_ORDER,		  G_TRAVERSE_ALL,		  -1,		  st_handler_set_stock_categories_cb,		  handler);  handler->priv->stock_categories = categories;}static gbooleanst_handler_set_stock_categories_cb (GNode *node, gpointer data){  if (node->data)    {      STHandler *handler = data;      STCategoryBag *bag;      bag = st_category_bag_new_from_category(handler, node->data);      node->data = bag;    }  return FALSE;			/* continue */}/** * st_handler_set_flags: * @handler: a handler. * @flags: the handler flags. * * Sets the handler flags. **/voidst_handler_set_flags (STHandler *handler, STHandlerFlags flags){  g_return_if_fail(ST_IS_HANDLER(handler));  handler->priv->flags = flags;}/** * st_handler_set_stream_version: * @handler: a handler: * @version: the stream version. * * Sets the handler stream version. **/voidst_handler_set_stream_version (STHandler *handler, int version){  g_return_if_fail(ST_IS_HANDLER(handler));  handler->priv->stream_version = version;}/** * st_handler_add_field: * @handler: a handler. * @field: a field to add to @handler. * * Add a stream field to @handler. **/voidst_handler_add_field (STHandler *handler, STHandlerField *field){  g_return_if_fail(ST_IS_HANDLER(handler));  g_return_if_fail(field != NULL);  handler->priv->fields = g_slist_append(handler->priv->fields, field);}/** * st_handler_bind: * @handler: a handler. * @event: an event to bind @cb to. * @cb: a callback function to bind to @event. * @data: data to pass to @cb. * * Binds callback function @cb to event @event. If a function is * already bound to @cb, the previous binding will be overriden (only * one function can be bound to an event). **/voidst_handler_bind (STHandler *handler,		 STHandlerEvent event,		 gpointer cb,		 gpointer data){  g_return_if_fail(ST_IS_HANDLER(handler));  g_return_if_fail(event >= 0 && event < ST_HANDLER_N_EVENTS);  g_return_if_fail(cb != NULL);  handler->priv->callbacks[event].cb = cb;  handler->priv->callbacks[event].data = data;}/** * st_handler_notice: * @handler: a handler. * @format: the message format. See the printf() documentation. * @...: the parameters to insert into the format string. * * Outputs a formatted handler notice to the standard error output. **/voidst_handler_notice (STHandler *handler, const char *format, ...){  va_list args;  char *notice;  g_return_if_fail(ST_IS_HANDLER(handler));  g_return_if_fail(format != NULL);  va_start(args, format);  notice = g_strdup_vprintf(format, args);  va_end(args);  st_notice("%s: %s", handler->priv->label, notice);  g_free(notice);}/** * st_handler_config_register: * @handler: a handler. * @pspec: a param spec defining the configuration key to * register. The param spec value type must be #G_TYPE_BOOLEAN, * #G_TYPE_INT, #G_TYPE_UINT, #G_TYPE_DOUBLE, #G_TYPE_STRING or * #G_TYPE_VALUE_ARRAY. The param spec flags must be * #G_PARAM_READWRITE. * * Registers a new configuration key. **/voidst_handler_config_register (STHandler *handler, GParamSpec *pspec){  ConfigEntry *entry;  /* keep in sync with st_config_load_handler_key_value() */  g_return_if_fail(ST_IS_HANDLER(handler));  g_return_if_fail(G_IS_PARAM_SPEC(pspec));  g_return_if_fail(G_PARAM_SPEC_VALUE_TYPE(pspec) == G_TYPE_BOOLEAN		   || G_PARAM_SPEC_VALUE_TYPE(pspec) == G_TYPE_INT		   || G_PARAM_SPEC_VALUE_TYPE(pspec) == G_TYPE_UINT		   || G_PARAM_SPEC_VALUE_TYPE(pspec) == G_TYPE_DOUBLE		   || G_PARAM_SPEC_VALUE_TYPE(pspec) == G_TYPE_STRING		   || G_PARAM_SPEC_VALUE_TYPE(pspec) == G_TYPE_VALUE_ARRAY);  g_return_if_fail((pspec->flags & G_PARAM_READWRITE) != 0);    g_mutex_lock(handler->priv->config_mutex);  entry = g_hash_table_lookup(handler->priv->config, g_param_spec_get_name(pspec));  g_return_if_fail(entry == NULL);  entry = g_new0(ConfigEntry, 1);  entry->pspec = g_param_spec_ref(pspec);  g_param_spec_sink(pspec);  g_value_init(&entry->value, G_PARAM_SPEC_VALUE_TYPE(entry->pspec));  g_param_value_set_default(entry->pspec, &entry->value);  g_hash_table_insert(handler->priv->config, (gpointer) g_param_spec_get_name(pspec), entry);  g_mutex_unlock(handler->priv->config_mutex);}/** * st_handler_config_lookup: * @handler: a handler. * @key: a configuration key. * * Looks up the #GParamSpec of a configuration key. * * Return value: the #GParamSpec of @key, or %NULL if no such * configuration key exists. **/GParamSpec *st_handler_config_lookup (STHandler *handler, const char *key){  const ConfigEntry *entry;  GParamSpec *pspec;  g_return_val_if_fail(ST_IS_HANDLER(handler), NULL);  g_return_val_if_fail(key != NULL, NULL);  g_mutex_lock(handler->priv->config_mutex);  entry = g_hash_table_lookup(handler->priv->config, key);  pspec = entry ? entry->pspec : NULL;  g_mutex_unlock(handler->priv->config_mutex);  return pspec;}/** * st_handler_config_foreach: * @handler: a handler. * @cb: a function to call for each key/value pair. * @data: data to pass to @cb. * * Calls the given function for each key/value pair in the * configuration database of @handler. You must not call any other * st_handler_config_* function while iterating over the database. **/voidst_handler_config_foreach (STHandler *handler,			   STHandlerConfigForeachCallback cb,			   gpointer data){  ConfigForeachInfo info;  g_return_if_fail(ST_IS_HANDLER(handler));  g_return_if_fail(cb != NULL);  info.cb = cb;  info.data = data;  g_mutex_lock(handler->priv->config_mutex);  g_hash_table_foreach(handler->priv->config, st_handler_config_foreach_cb, &info);  g_mutex_unlock(handler->priv->config_mutex);}static voidst_handler_config_foreach_cb (gpointer key,			      gpointer value,			      gpointer user_data){  ConfigForeachInfo *info = user_data;  const ConfigEntry *entry = value;  info->cb(entry->pspec, &entry->value, info->data);}/** * st_handler_config_get_value: * @handler: a handler. * @key: a configuration key previously registered with * st_handler_config_register(). * @value: a location to copy the value of @key to. * * Gets a configuration value. @value must be zero-filled (but not * initialized), and should be unset with g_value_unset() when no * longer needed. **/voidst_handler_config_get_value (STHandler *handler,			     const char *key,			     GValue *value){  const ConfigEntry *entry;  g_return_if_fail(ST_IS_HANDLER(handler));  g_return_if_fail(key != NULL);  g_mutex_lock(handler->priv->config_mutex);  entry = g_hash_table_lookup(handler->priv->config, key);  g_return_if_fail(entry != NULL);  g_value_init(value, G_PARAM_SPEC_VALUE_TYPE(entry->pspec));  g_value_copy(&entry->value, value);  g_mutex_unlock(handler->priv->config_mutex);}/** * st_handler_config_set_value: * @handler: a handler. * @key: a configuration key previously registered with * st_handler_config_register(). * @value: the value to set. * * Sets a configuration value. @value must match the value type of the * param spec of @key. **/voidst_handler_config_set_value (STHandler *handler,			     const char *key,			     const GValue *value){  ConfigEntry *entry;  g_return_if_fail(ST_IS_HANDLER(handler));  g_return_if_fail(key != NULL);  g_return_if_fail(G_IS_VALUE(value));  g_mutex_lock(handler->priv->config_mutex);  entry = g_hash_table_lookup(handler->priv->config, key);  g_return_if_fail(entry != NULL);  g_value_copy(value, &entry->value);  g_param_value_validate(entry->pspec, &entry->value);  g_mutex_unlock(handler->priv->config_mutex);}#define CONFIG_GETTER(_name, _gtype, _ctype, _getter, _failret)		\  _ctype st_handler_config_get_ ## _name (STHandler *handler,		\					  const char *key)		\  {									\    const ConfigEntry *entry;						\    _ctype value;							\									\    g_return_val_if_fail(ST_IS_HANDLER(handler), _failret);		\    g_return_val_if_fail(key != NULL, _failret);			\									\    g_mutex_lock(handler->priv->config_mutex);				\									\    entry = g_hash_table_lookup(handler->priv->config, key);		\    g_return_val_if_fail(entry != NULL, _failret);			\    g_return_val_if_fail(G_VALUE_HOLDS(&entry->value, _gtype), _failret); \									\    value = _getter(&entry->value);					\									\    g_mutex_unlock(handler->priv->config_mutex);			\									\    return value;							\  }#define CONFIG_SETTER(_name, _gtype, _ctype, _setter)		\  void st_handler_config_set_ ## _name (STHandler *handler,	\					const char *key,	\					_ctype value)		\  {								\    ConfigEntry *entry;						\    								\    g_return_if_fail(ST_IS_HANDLER(handler));			\    g_return_if_fail(key != NULL);				\    								\    g_mutex_lock(handler->priv->config_mutex);			\    								\    entry = g_hash_table_lookup(handler->priv->config, key);	\    g_return_if_fail(entry != NULL);				\    g_return_if_fail(G_VALUE_HOLDS(&entry->value, _gtype));	\    								\    _setter(&entry->value, value);				\    g_param_value_validate(entry->pspec, &entry->value);	\    								\    g_mutex_unlock(handler->priv->config_mutex);		\  }CONFIG_GETTER(boolean, G_TYPE_BOOLEAN, gboolean, g_value_get_boolean, FALSE)CONFIG_SETTER(boolean, G_TYPE_BOOLEAN, gboolean, g_value_set_boolean)CONFIG_GETTER(int, G_TYPE_INT, int, g_value_get_int, 0)CONFIG_SETTER(int, G_TYPE_INT, int, g_value_set_int)CONFIG_GETTER(uint, G_TYPE_UINT, unsigned int, g_value_get_uint, 0)CONFIG_SETTER(uint, G_TYPE_UINT, unsigned int, g_value_set_uint)CONFIG_GETTER(double, G_TYPE_DOUBLE, double, g_value_get_double, 0)CONFIG_SETTER(double, G_TYPE_DOUBLE, double, g_value_set_double)CONFIG_GETTER(string, G_TYPE_STRING, char *, g_value_dup_string, NULL)CONFIG_SETTER(string, G_TYPE_STRING, const char *, g_value_set_string)CONFIG_GETTER(value_array, G_TYPE_VALUE_ARRAY, GValueArray *, g_value_dup_boxed, NULL)CONFIG_SETTER(value_array, G_TYPE_VALUE_ARRAY, const GValueArray *, g_value_set_boxed)#undef CONFIG_GETTER#undef CONFIG_SETTER/*** private implementation **************************************************/static voidst_handler_class_init (STHandlerClass *class){  GObjectClass *object_class = G_OBJECT_CLASS(class);  parent_class = g_type_class_peek_parent(class);  g_type_class_add_private(class, sizeof(STHandlerPrivate));  object_class->constructor = st_handler_constructor;  object_class->set_property = st_handler_set_property;  g_object_class_install_property(object_class,				  PROP_NAME,				  g_param_spec_string("name",						      _("Name"),						      _("The handler name"),						      NULL,						      G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));  /* private properties */  g_object_class_install_property(object_class,				  PROP_DUMMY,				  g_param_spec_boolean("dummy",						       NULL,						       NULL,						       FALSE,						       G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));}static voidst_handler_init (STHandler *handler){  handler->priv = G_TYPE_INSTANCE_GET_PRIVATE(handler, ST_TYPE_HANDLER, STHandlerPrivate);}static GObject *st_handler_constructor (GType type,			unsigned int n_construct_properties,			GObjectConstructParam *construct_params){  GObject *object;   STHandler *handler;  object = parent_class->constructor(type, n_construct_properties, construct_params);  handler = ST_HANDLER(object);  handler->priv->paned_position = 150;  handler->priv->config = g_hash_table_new(g_str_hash, g_str_equal);  handler->priv->config_mutex = g_mutex_new();  if (! handler->priv->dummy)    {      handler->priv->streams = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_object_unref);      handler->priv->streams_mutex = g_mutex_new();      /* provide handy defaults for some mandatory callbacks */      st_handler_bind(handler, ST_HANDLER_EVENT_CATEGORY_NEW, st_handler_category_new_default_cb, NULL);      st_handler_bind(handler, ST_HANDLER_EVENT_CATEGORY_FREE, st_handler_category_free_default_cb, NULL);      st_handler_bind(handler, ST_HANDLER_EVENT_STREAM_NEW, st_handler_stream_new_default_cb, NULL);      st_handler_bind(handler, ST_HANDLER_EVENT_STREAM_FREE, st_handler_stream_free_default_cb, NULL);    }    return object;}static voidst_handler_set_property (GObject *object,			 unsigned int prop_id,			 const GValue *value,

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?