f521f04782c3f8da319860d59078932d8a3572cf
[framework/uifw/e17.git] / src / modules / illume-bluetooth / e_mod_main.c
1 #include "e.h"
2 #include "e_mod_main.h"
3
4 typedef struct _Instance Instance;
5 struct _Instance 
6 {
7    E_Gadcon_Client *gcc;
8    Evas_Object *obj;
9    Ecore_Poller *poller;
10    int on;
11 };
12
13 /* local function prototypes */
14 static E_Gadcon_Client *_gc_init(E_Gadcon *gc, const char *name, const char *id, const char *style);
15 static void _gc_shutdown(E_Gadcon_Client *gcc);
16 static void _gc_orient(E_Gadcon_Client *gcc, E_Gadcon_Orient orient);
17 static char *_gc_label(E_Gadcon_Client_Class *cc);
18 static Evas_Object *_gc_icon(E_Gadcon_Client_Class *cc, Evas *evas);
19 static const char *_gc_id_new(E_Gadcon_Client_Class *cc);
20 static Eina_Bool _cb_poll(void *data);
21 static int _get_interface_class(int iclass);
22
23 /* local variables */
24 static Eina_List *instances = NULL;
25 static const char *_bt_mod_dir = NULL;
26
27 static const E_Gadcon_Client_Class _gc_class = 
28 {
29    GADCON_CLIENT_CLASS_VERSION, "illume-bluetooth", 
30      { _gc_init, _gc_shutdown, _gc_orient, _gc_label, _gc_icon, _gc_id_new, 
31           NULL, e_gadcon_site_is_not_toolbar }, 
32    E_GADCON_CLIENT_STYLE_PLAIN
33 };
34
35 /* local function prototypes */
36 static E_Gadcon_Client *
37 _gc_init(E_Gadcon *gc, const char *name, const char *id, const char *style) 
38 {
39    Instance *inst;
40    char buff[PATH_MAX];
41
42    inst = E_NEW(Instance, 1);
43
44    inst->obj = edje_object_add(gc->evas);
45    if (!e_theme_edje_object_set(inst->obj, "base/theme/modules/illume-bluetooth", 
46                                 "modules/illume-bluetooth/main")) 
47      {
48         snprintf(buff, sizeof(buff), "%s/e-module-illume-bluetooth.edj", 
49                  _bt_mod_dir);
50         edje_object_file_set(inst->obj, buff, "modules/illume-bluetooth/main");
51      }
52    evas_object_show(inst->obj);
53
54    inst->gcc = e_gadcon_client_new(gc, name, id, style, inst->obj);
55    inst->gcc->data = inst;
56
57    inst->on = -1;
58    inst->poller = ecore_poller_add(ECORE_POLLER_CORE, 16, _cb_poll, inst);
59    return inst->gcc;
60 }
61
62 static void 
63 _gc_shutdown(E_Gadcon_Client *gcc) 
64 {
65    Instance *inst;
66
67    if (!(inst = gcc->data)) return;
68    instances = eina_list_remove(instances, inst);
69    if (inst->poller) ecore_poller_del(inst->poller);
70    if (inst->obj) evas_object_del(inst->obj);
71    E_FREE(inst);
72 }
73
74 static void 
75 _gc_orient(E_Gadcon_Client *gcc, E_Gadcon_Orient orient __UNUSED__) 
76 {
77    Instance *inst;
78    int mw, mh, xw, xh;
79
80    inst = gcc->data;
81    edje_object_size_min_get(inst->obj, &mw, &mh);
82    edje_object_size_max_get(inst->obj, &xw, &xh);
83    if ((mw < 1) || (mh < 1))
84      edje_object_size_min_calc(inst->obj, &mw, &mh);
85    if (mw < 4) mw = 4;
86    if (mh < 4) mh = 4;
87    if ((xw > 0) && (xh > 0))
88      e_gadcon_client_aspect_set(gcc, xw, xh);
89    e_gadcon_client_min_size_set(gcc, mw, mh);
90 }
91
92 static char *
93 _gc_label(E_Gadcon_Client_Class *cc __UNUSED__) 
94 {
95    return _("Illume Bluetooth");
96 }
97
98 static Evas_Object *
99 _gc_icon(E_Gadcon_Client_Class *cc __UNUSED__, Evas *evas) 
100 {
101    Evas_Object *o;
102    char buff[PATH_MAX];
103
104    snprintf(buff, sizeof(buff), "%s/e-module-illume-bluetooth.edj", 
105             _bt_mod_dir);
106    o = edje_object_add(evas);
107    edje_object_file_set(o, buff, "icon");
108    return o;
109 }
110
111 static const char *
112 _gc_id_new(E_Gadcon_Client_Class *cc __UNUSED__) 
113 {
114    static char buff[32];
115
116    snprintf(buff, sizeof(buff), "%s.%d", _gc_class.name, 
117             eina_list_count(instances));
118    return buff;
119 }
120
121 static Eina_Bool
122 _cb_poll(void *data) 
123 {
124    Instance *inst;
125    int pon;
126
127    inst = data;
128    pon = inst->on;
129    inst->on = _get_interface_class(0xe0);
130    if (inst->on != pon) 
131      {
132         if (inst->on)
133           edje_object_signal_emit(inst->obj, "e,state,active", "e");
134         else
135           edje_object_signal_emit(inst->obj, "e,state,passive", "e");
136      }
137    return ECORE_CALLBACK_RENEW;
138 }
139
140 static int 
141 _get_interface_class(int iclass) 
142 {
143    Eina_List *devs;
144    char *name;
145
146    devs = ecore_file_ls("/sys/bus/usb/devices");
147    EINA_LIST_FREE(devs, name)
148      {
149         char buf[PATH_MAX];
150         FILE *f;
151
152         snprintf(buf, sizeof(buf), "%s/%s/%s",
153                  "/sys/bus/usb/devices", name, "bInterfaceClass");
154         f = fopen(buf, "r");
155         if (f)
156           {
157              if (fgets(buf, sizeof(buf), f))
158                {
159                   int id = -1;
160
161                   sscanf(buf, "%x", &id);
162                   if (iclass == id)
163                     {
164                        EINA_LIST_FREE(devs, name)
165                          free(name);
166                        fclose(f);
167                        return 1;
168                     }
169                }
170              fclose(f);
171           }
172         free(name);
173      }
174    return 0;
175 }
176
177 EAPI E_Module_Api e_modapi = { E_MODULE_API_VERSION, "Illume Bluetooth" };
178
179 EAPI void *
180 e_modapi_init(E_Module *m) 
181 {
182    _bt_mod_dir = eina_stringshare_add(m->dir);
183    e_gadcon_provider_register(&_gc_class);
184    return m;
185 }
186
187 EAPI int 
188 e_modapi_shutdown(E_Module *m __UNUSED__) 
189 {
190    e_gadcon_provider_unregister(&_gc_class);
191    if (_bt_mod_dir) eina_stringshare_del(_bt_mod_dir);
192    _bt_mod_dir = NULL;
193    return 1;
194 }
195
196 EAPI int 
197 e_modapi_save(E_Module *m __UNUSED__) 
198 {
199    return 1;
200 }