summaryrefslogblamecommitdiff
path: root/src/bin/sources_view.c
blob: 470dfbacd324e9f3e8f3c998cf0f94dff054c986 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11

                         
                 


                                 




                               

  

                         

                                  
                                  

 

                                     

                                  
                             
 

                                                              

 

                                       

                                  

                                                                   
 
            
      


                                                          



                                            

                                                         
      

 

                                       

                                  


                                                                   
 
               
      
                                                     
           






                                                                   

                   
                                               
      


           
                                                              
 















                                           
      
 
 




                                                                                




                                                                          
                              









                                    
                                                                            
 
                              
                                        

                      
 



                                                                      
 

                                          





                                                        
                              
 
                                              





                                                                 
                              


                               


                                                   
                                                        



                                                       
                                                              





























                                                                         
                                                            






                                                                          
                                           





                                                  











                                                                  





                
#include "sources_view.h"

#include <emix.h>

#define SOURCES_KEY "sources.key"

struct Sources_View
{
   Evas_Object *self;
   Evas_Object *genlist;
   Elm_Genlist_Item_Class *itc;
};

static void
_disconnected(void *data)
{
   struct Sources_View *sv = data;
   elm_genlist_clear(sv->genlist);
}

static void
_source_added(void *data, void *info)
{
   struct Sources_View *sv = data;
   Emix_Source *source= info;

   elm_genlist_item_append(sv->genlist, sv->itc, source, NULL,
                           ELM_GENLIST_ITEM_NONE, NULL, sv);
}

static void
_source_removed(void *data, void *info)
{
   struct Sources_View *sv = data;
   Emix_Source *source = info;
   Elm_Object_Item *item = elm_genlist_first_item_get(sv->genlist);

   if (item)
     {
        do
          {
             if (source == elm_object_item_data_get(item))
               {
                  elm_object_item_del(item);
                  break;
               }
          }
        while ((item = elm_genlist_item_next_get(item)));
     }
}

static void
_source_changed(void *data, void *info)
{
   struct Sources_View *sv = data;
   Emix_Source *source = info;
   Elm_Object_Item *item = elm_genlist_first_item_get(sv->genlist);
   Evas_Object *it;

   while (item)
     {
        if (source == elm_object_item_data_get(item))
          {
             it = elm_object_item_part_content_get(item, "item");
             if (it)
               elm_slider_value_set(it, source->volume.volumes[0]);

             it = elm_object_item_part_content_get(item, "mute");
             if (it)
               elm_check_state_set(it, source->mute);
             break;
          }
        item = elm_genlist_item_next_get(item);
     }
}

static void
_events_cb(void *data, enum Emix_Event type, void *event_info)
{
   switch (type)
     {
      case EMIX_SOURCE_ADDED_EVENT:
         _source_added(data, event_info);
         break;
      case EMIX_SOURCE_CHANGED_EVENT:
         _source_changed(data, event_info);
         break;
      case EMIX_SOURCE_REMOVED_EVENT:
         _source_removed(data, event_info);
         break;
      case EMIX_DISCONNECTED_EVENT:
         _disconnected(data);
         break;
      default:
         break;
     }
}

static void
_del_cb(void *data EINA_UNUSED, Evas *e EINA_UNUSED, Evas_Object *o EINA_UNUSED,
        void *event_info EINA_UNUSED)
{
   emix_event_callback_del(_events_cb);
}

static char *
_item_text_get(void *data, Evas_Object *obj EINA_UNUSED, const char *part)
{
   Emix_Source *source = data;

   if (!strcmp(part, "name"))
     {
        return strdup(source->name);
     }

   return NULL;
}

static void
_volume_changed_cb(void *data, Evas_Object *o, void *event_info EINA_UNUSED)
{
   Emix_Source *source = data;
   double val = elm_slider_value_get(o);
   Emix_Volume volume;
   unsigned int i;

   volume.channel_count = source->volume.channel_count;
   volume.volumes = calloc(source->volume.channel_count, sizeof(int));
   for (i = 0; i < volume.channel_count; i++)
      volume.volumes[i] = val;

   emix_source_volume_set(source, volume);
   free(volume.volumes);
}

static void
_mute_changed_cb(void *data, Evas_Object *o EINA_UNUSED,
                 void *event_info EINA_UNUSED)
{
   Emix_Source *source = data;

   emix_source_mute_set(source, source->mute);
}

static Evas_Object *
_item_content_get(void *data, Evas_Object *obj, const char *part)
{
   Evas_Object *item = NULL;
   Emix_Source *source = data;

   if (!strcmp(part, "slider"))
     {
        item = elm_slider_add(obj);
        EINA_SAFETY_ON_NULL_RETURN_VAL(item, NULL);

        elm_slider_step_set(item, 1.0/BASE_VOLUME_STEP);
        elm_slider_unit_format_set(item, "%1.0f");
        elm_slider_indicator_format_set(item, "%1.0f");
        elm_slider_span_size_set(item, 120);
        elm_slider_min_max_set(item, 0.0, 100.0);
        elm_slider_value_set(item, source->volume.volumes[0]);
        evas_object_smart_callback_add(item, "delay,changed",
                                       _volume_changed_cb, source);
     }
   else if (!strcmp(part, "mute"))
     {
        item = elm_check_add(obj);
        EINA_SAFETY_ON_NULL_RETURN_VAL(item, NULL);

        elm_object_style_set(item, "toggle");
        elm_object_part_text_set(item, "off", "Mute");
        elm_object_part_text_set(item, "on", "Unmute");

        elm_check_state_set(item, source->mute);
        elm_check_state_pointer_set(item, &source->mute);
        evas_object_smart_callback_add(item, "changed", _mute_changed_cb,
                                       source);
     }

   return item;
}

Evas_Object *
sources_view_add(Evas_Object *parent)
{
   Evas_Object *layout;
   struct Sources_View *sv;

   sv = calloc(1, sizeof(struct Sources_View));
   EINA_SAFETY_ON_NULL_RETURN_VAL(sv, NULL);

   layout = emixer_layout_add(parent, "sources", "default");
   EINA_SAFETY_ON_NULL_GOTO(layout, err);

   evas_object_event_callback_add(layout, EVAS_CALLBACK_DEL, _del_cb, sv);

   sv->genlist = elm_genlist_add(layout);
   EINA_SAFETY_ON_NULL_GOTO(sv->genlist, err_genlist);

   emix_event_callback_add(_events_cb, sv);

   sv->itc = elm_genlist_item_class_new();
   EINA_SAFETY_ON_NULL_GOTO(sv->itc, err_genlist);
   sv->itc->item_style = "sources";
   sv->itc->func.text_get = _item_text_get;
   sv->itc->func.content_get = _item_content_get;

   evas_object_data_set(layout, SOURCES_KEY, sv);
   elm_layout_content_set(layout, "list", sv->genlist);

   evas_object_size_hint_weight_set(sv->genlist, EVAS_HINT_EXPAND,
                                    EVAS_HINT_EXPAND);
   evas_object_size_hint_align_set(sv->genlist, EVAS_HINT_FILL,
                                   EVAS_HINT_FILL);

   return layout;

 err_genlist:
   free(layout);
 err:
   free(sv);

   return NULL;
}