1 /**
2  * Ecore example illustrating the basics of ecore evas extn plug usage.
3  *
4  * For checking with ecore evas extn socket, please run with ecore_evas_extn_socket_example.
5  *
6  * @verbatim
7  * gcc -o ecore_evas_extn_plug_example ecore_evas_extn_plug_example.c `pkg-config --libs --cflags evas ecore ecore-evas`
8  * @endverbatim
9  */
10 
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #else
14 #define EINA_UNUSED
15 #endif
16 
17 #include <Ecore.h>
18 #include <Ecore_Evas.h>
19 #include <unistd.h>
20 // procotol version - change this as needed
21 #define MSG_DOMAIN_CONTROL_OBJECT 0x1004
22 #define MSG_ID_BG_COLOR 0x1005
23 #define MSG_ID_TEXT 0x1006
24 
25 typedef struct _Msg_Color Msg_Color;
26 
27 struct _Msg_Color
28 {
29    int r;
30    int g;
31    int b;
32    int a;
33 };
34 
35 static void
_on_delete(Ecore_Evas * ee)36 _on_delete(Ecore_Evas *ee)
37 {
38    Msg_Color *color = NULL;
39 
40    color = ecore_evas_data_get(ee, "color");
41    if (color) free(color);
42    ecore_main_loop_quit();
43 }
44 
45 static void
_button_1_up(void * data,Evas * e EINA_UNUSED,Evas_Object * obj EINA_UNUSED,void * event_info EINA_UNUSED)46 _button_1_up(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_info EINA_UNUSED)
47 {
48    Ecore_Evas *ee;
49    Msg_Color *color = NULL;
50    int r = 0, g = 0, b = 0, a = 0;
51 
52    ee = data;
53    color = ecore_evas_data_get(ee, "color");
54 
55    printf("Plug's button pressed\n");
56    if (!color)
57      {
58         color = malloc(sizeof(Msg_Color));
59         r = 255;
60         g = 0;
61         b = 0;
62         a = 255;
63 	    ecore_evas_data_set(ee, "color", color);
64      }
65    else
66      {
67         r = ((color->r) + 100) % 255;
68         g = ((color->g) + 100) % 255;
69         b = ((color->b) + 100) % 255;
70         a = color->a;
71      }
72    printf("Send color info (%x,%x,%x,%x)\n", r, g, b, a);
73 
74    color->r = r;
75    color->g = g;
76    color->b = b;
77    color->a = a;
78 
79    ecore_evas_msg_parent_send(ee, MSG_DOMAIN_CONTROL_OBJECT, MSG_ID_BG_COLOR, color, sizeof(Msg_Color));
80 }
81 
82 static void
_ecore_evas_msg_handle(Ecore_Evas * ee,int msg_domain,int msg_id,void * data,int size)83 _ecore_evas_msg_handle(Ecore_Evas *ee, int msg_domain, int msg_id, void *data, int size)
84 {
85    if (!data) return;
86    printf("Receive msg from server msg_domain=%x msg_id=%x size=%d\n", msg_domain, msg_id, size);
87 
88    if (msg_domain == MSG_DOMAIN_CONTROL_OBJECT)
89      {
90         if (msg_id == MSG_ID_TEXT)
91           {
92              Evas_Object *text = NULL;
93              char *txt = data;
94              int len = 0;
95              len = strlen(txt);
96              printf("data len= (%d).\n", len);
97 
98              text = ecore_evas_data_get(ee, "text");
99              if (text && (size == (int)strlen(txt)))
100                {
101                   printf("Receive msg is text (%s).\n", txt);
102                   evas_object_text_text_set(text, txt);
103                }
104           }
105      }
106 }
107 
108 int
main(void)109 main(void)
110 {
111    Ecore_Evas *ee, *ee_plug;
112    Evas *canvas;
113    Evas_Object *bg, *button1, *text, *noti_text;
114    Evas_Object *plug;
115    int w, h;
116    int x1, x2, y; //for button position
117    int plug_x, plug_y, plug_w = 0, plug_h = 0; //for button position
118 
119    if (ecore_evas_init() <= 0)
120      return 1;
121 
122    w = 480;
123    h = 500;
124    x1 = 20;
125    x2 = 170;
126    y = 100;
127    plug_x = 10;
128    plug_y = y + (h / 4) + 20;
129    plug_w = 460;
130    plug_h = 200;
131 
132    ee = ecore_evas_new(NULL, 0, 0, w, h, NULL);
133    ecore_evas_title_set(ee, "Ecore Evas Extn Plug Example");
134    ecore_evas_show(ee);
135 
136    ecore_evas_callback_delete_request_set(ee, _on_delete);
137 
138    printf("Using %s engine! ee=%p\n", ecore_evas_engine_name_get(ee), ee);
139 
140    //create ecore evas to show info
141    canvas = ecore_evas_get(ee);
142    if (ecore_evas_ecore_evas_get(canvas) == ee)
143      printf("Everything is sane!\n");
144 
145    bg = evas_object_rectangle_add(canvas);
146    evas_object_color_set(bg, 255, 0, 255, 255);
147    evas_object_resize(bg, w, h);
148    evas_object_show(bg);
149 
150    //button to change socket's bg
151    button1 = evas_object_rectangle_add(canvas);
152    evas_object_color_set(button1, 0, 255, 255, 255);
153    evas_object_resize(button1, w/4, 100);
154    evas_object_move(button1, x1, y);
155    evas_object_show(button1);
156 
157    text = evas_object_text_add(canvas);
158    evas_object_color_set(text, 0, 0, 0, 255);
159    evas_object_text_style_set(text, EVAS_TEXT_STYLE_PLAIN);
160    evas_object_text_font_set(text, "Sans", 15);
161    evas_object_text_text_set(text, "Chagne bg!!");
162    evas_object_move(text, x1 + 5, y + 10);
163    evas_object_show(text);
164 
165 
166    //button to send msg1 to socket
167    bg = evas_object_rectangle_add(canvas);
168    evas_object_color_set(bg, 0, 255, 255, 255);
169    evas_object_resize(bg, w/2, 100);
170    evas_object_move(bg, x2, y);
171    evas_object_show(bg);
172    text = evas_object_text_add(canvas);
173    evas_object_color_set(text, 0, 0, 0, 255);
174    evas_object_text_style_set(text, EVAS_TEXT_STYLE_PLAIN);
175    evas_object_text_font_set(text, "Sans", 15);
176    evas_object_text_text_set(text, "No Message from server!!");
177    evas_object_move(text, x2 + 5, y + 10);
178    evas_object_show(text);
179 
180    //text to noti plug area
181    noti_text = evas_object_text_add(canvas);
182    evas_object_color_set(noti_text, 0, 0, 0, 255);
183    evas_object_text_style_set(noti_text, EVAS_TEXT_STYLE_PLAIN);
184    evas_object_text_font_set(noti_text, "Sans", 15);
185    evas_object_text_text_set(noti_text, "Below is the plug area!!");
186    evas_object_move(noti_text, x1, plug_y - 25);
187    evas_object_show(noti_text);
188 
189    //create ecore evas extn plug(image object) show socket area
190    plug = ecore_evas_extn_plug_new(ee);
191    ecore_evas_data_set(ee, "plug", plug);
192    if (!plug)
193      {
194         printf("Fail to create ecore extn plug!\n");
195 		return 0;
196      }
197 
198    ee_plug = ecore_evas_object_ecore_evas_get(plug);
199    ecore_evas_data_set(ee_plug, "text", text);
200 
201    if(!ecore_evas_extn_plug_connect(plug, "socket_exam_service", 0, EINA_FALSE))
202      {
203         printf("Fail to connect socket_exam_service!\n");
204 		return 0;
205      }
206 
207    evas_object_event_callback_add(button1, EVAS_CALLBACK_MOUSE_UP, _button_1_up, ee_plug);
208    //callback to deal with extn socket message
209    ecore_evas_callback_msg_handle_set(ee_plug, _ecore_evas_msg_handle);
210 
211    ecore_evas_data_set(ee_plug, "text", text);
212 
213    evas_object_resize(plug, plug_w, plug_h);
214    evas_object_move(plug, plug_x, plug_y);
215    evas_object_show(plug);
216 
217    ecore_main_loop_begin();
218 
219    ecore_evas_free(ee);
220    ecore_evas_shutdown();
221 
222    return 0;
223 }
224 
225