aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/elementary/elm_app_server_view.c
blob: fcd4708c6b092e1cd6e050ef62a2ec685490673b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#ifdef HAVE_CONFIG_H
# include "elementary_config.h"
#endif

#include <Elementary.h>
#include "elm_priv.h"

#define MY_CLASS ELM_APP_SERVER_VIEW_CLASS

#define MY_CLASS_NAME "Elm_App_Server_View"

typedef struct
{
   Eldbus_Connection *conn;
   Eldbus_Service_Interface *iface;

   Eina_Stringshare *id;
   Eina_Stringshare *title;
   Eina_Stringshare *icon_name;
   int new_events;
   short progress;
   Elm_App_View_State state;
   int window_id;
   struct {
      unsigned int w, h;
      Eina_Bool has_alpha;
      unsigned char *raw;
   } image_raw;
} Elm_App_Server_View_Data;

static void
_state_set(Elm_App_Server_View_Data *data, Elm_App_View_State state)
{
   if (data->state != state)
     eldbus_service_property_changed(data->iface, "State");
   data->state = state;
}

static Eldbus_Message *
_method_close(const Eldbus_Service_Interface *iface, const Eldbus_Message *message)
{
   Eo *eo = eldbus_service_object_data_get(iface, MY_CLASS_NAME);
   Elm_App_Server_View_Data *data = efl_data_scope_get(eo, MY_CLASS);

   _state_set(data, ELM_APP_VIEW_STATE_CLOSED);
   efl_event_callback_call(eo, ELM_APP_SERVER_VIEW_EVENT_CLOSED, NULL);

   return eldbus_message_method_return_new(message);
}

static Eldbus_Message *
_method_pause(const Eldbus_Service_Interface *iface, const Eldbus_Message *message)
{
   Eo *eo = eldbus_service_object_data_get(iface, MY_CLASS_NAME);
   Elm_App_Server_View_Data *data = efl_data_scope_get(eo, MY_CLASS);

   _state_set(data, ELM_APP_VIEW_STATE_PAUSED);
   efl_event_callback_call(eo, ELM_APP_SERVER_VIEW_EVENT_PAUSED, NULL);

   return eldbus_message_method_return_new(message);
}

static Eldbus_Message *
_method_resume(const Eldbus_Service_Interface *iface, const Eldbus_Message *message)
{
   Eo *eo = eldbus_service_object_data_get(iface, MY_CLASS_NAME);
   Elm_App_Server_View_Data *data = efl_data_scope_get(eo, MY_CLASS);

   _state_set(data, ELM_APP_VIEW_STATE_LIVE);
   efl_event_callback_call(eo, ELM_APP_SERVER_VIEW_EVENT_RESUMED, NULL);

   return eldbus_message_method_return_new(message);
}

static const Eldbus_Method _methods[] = {
   { "Close", NULL, NULL, _method_close, 0 },
   { "Pause", NULL, NULL, _method_pause, 0 },
   { "Resume", NULL, NULL, _method_resume, 0 },
   { NULL, NULL, NULL, NULL, 0 }
};

static Eina_Bool
_prop_title_get(const Eldbus_Service_Interface *iface, const char *propname EINA_UNUSED, Eldbus_Message_Iter *iter, const Eldbus_Message *request_msg EINA_UNUSED, Eldbus_Message **error EINA_UNUSED)
{
   Eo *eo = eldbus_service_object_data_get(iface, MY_CLASS_NAME);
   Elm_App_Server_View_Data *data = efl_data_scope_get(eo, MY_CLASS);

   eldbus_message_iter_basic_append(iter, 's', data->title);
   return EINA_TRUE;
}

static Eina_Bool
_prop_icon_get(const Eldbus_Service_Interface *iface, const char *propname EINA_UNUSED, Eldbus_Message_Iter *iter, const Eldbus_Message *request_msg EINA_UNUSED, Eldbus_Message **error EINA_UNUSED)
{
   Eo *eo = eldbus_service_object_data_get(iface, MY_CLASS_NAME);
   Elm_App_Server_View_Data *data = efl_data_scope_get(eo, MY_CLASS);

   eldbus_message_iter_basic_append(iter, 's', data->icon_name);
   return EINA_TRUE;
}

/*static Eina_Bool
_prop_icon_pixels_get(const Eldbus_Service_Interface *iface, const char *propname EINA_UNUSED, Eldbus_Message_Iter *iter, const Eldbus_Message *request_msg EINA_UNUSED, Eldbus_Message **error EINA_UNUSED)
{
   //TODO
   return EINA_FALSE;
}*/

static Eina_Bool
_prop_events_get(const Eldbus_Service_Interface *iface, const char *propname EINA_UNUSED, Eldbus_Message_Iter *iter, const Eldbus_Message *request_msg EINA_UNUSED, Eldbus_Message **error EINA_UNUSED)
{
   Eo *eo = eldbus_service_object_data_get(iface, MY_CLASS_NAME);
   Elm_App_Server_View_Data *data = efl_data_scope_get(eo, MY_CLASS);

   eldbus_message_iter_basic_append(iter, 'i', data->new_events);
   return EINA_TRUE;
}

static Eina_Bool
_prop_progress_get(const Eldbus_Service_Interface *iface, const char *propname EINA_UNUSED, Eldbus_Message_Iter *iter, const Eldbus_Message *request_msg EINA_UNUSED, Eldbus_Message **error EINA_UNUSED)
{
   Eo *eo = eldbus_service_object_data_get(iface, MY_CLASS_NAME);
   Elm_App_Server_View_Data *data = efl_data_scope_get(eo, MY_CLASS);

   eldbus_message_iter_basic_append(iter, 'n', data->progress);
   return EINA_TRUE;
}

static Eina_Bool
_prop_state_get(const Eldbus_Service_Interface *iface, const char *propname EINA_UNUSED, Eldbus_Message_Iter *iter, const Eldbus_Message *request_msg EINA_UNUSED, Eldbus_Message **error EINA_UNUSED)
{
   Eo *eo = eldbus_service_object_data_get(iface, MY_CLASS_NAME);
   Elm_App_Server_View_Data *data = efl_data_scope_get(eo, MY_CLASS);

   eldbus_message_iter_basic_append(iter, 's', _state_id_to_string(data->state));
   return EINA_TRUE;
}

static Eina_Bool
_prop_window_get(const Eldbus_Service_Interface *iface, const char *propname EINA_UNUSED, Eldbus_Message_Iter *iter, const Eldbus_Message *request_msg EINA_UNUSED, Eldbus_Message **error EINA_UNUSED)
{
   Eo *eo = eldbus_service_object_data_get(iface, MY_CLASS_NAME);
   Elm_App_Server_View_Data *data = efl_data_scope_get(eo, MY_CLASS);

   eldbus_message_iter_basic_append(iter, 'i', data->window_id);
   return EINA_TRUE;
}

static const Eldbus_Property _props[] = {
   { "Title", "s", _prop_title_get, NULL, 0 },
   { "IconName", "s", _prop_icon_get, NULL, 0 },
   //{ "IconPixels", "(uubay)", _prop_icon_pixels_get, NULL, 0 },
   { "NewEvents", "i", _prop_events_get, NULL, 0 },
   { "Progress", "n", _prop_progress_get, NULL, 0 },
   { "State", "s", _prop_state_get, NULL, 0 },
   { "WindowId", "i", _prop_window_get, NULL, 0 },
   { NULL, NULL, NULL, NULL, 0 }
};

static const Eldbus_Signal _signals[] = {
   { "Result", ELDBUS_ARGS({"a{sv}", "arguments"}), 0 },
   { NULL, NULL, 0}
};

static const Eldbus_Service_Interface_Desc iface_desc = {
   "org.enlightenment.ApplicationView1", _methods, _signals, _props, NULL, NULL
};

EOLIAN static void
_elm_app_server_view_resume(Eo *obj, Elm_App_Server_View_Data *data)
{
   _state_set(data, ELM_APP_VIEW_STATE_LIVE);
   efl_event_callback_call(obj, ELM_APP_SERVER_VIEW_EVENT_RESUMED, NULL);
}

EOLIAN static void
_elm_app_server_view_pause(Eo *obj, Elm_App_Server_View_Data *data)
{
   _state_set(data, ELM_APP_VIEW_STATE_PAUSED);
   efl_event_callback_call(obj, ELM_APP_SERVER_VIEW_EVENT_PAUSED, NULL);
}

EOLIAN static void
_elm_app_server_view_close(Eo *obj, Elm_App_Server_View_Data *data)
{
   _state_set(data, ELM_APP_VIEW_STATE_CLOSED);
   efl_event_callback_call(obj, ELM_APP_SERVER_VIEW_EVENT_CLOSED, NULL);
}

EOLIAN static void
_elm_app_server_view_shallow(Eo *obj, Elm_App_Server_View_Data *data)
{
   _state_set(data, ELM_APP_VIEW_STATE_SHALLOW);
   efl_event_callback_call(obj, ELM_APP_SERVER_VIEW_EVENT_SHALLOW, NULL);
}

EOLIAN static Elm_App_View_State
_elm_app_server_view_state_get(Eo *obj EINA_UNUSED, Elm_App_Server_View_Data *data)
{
   return data->state;
}

EOLIAN static void
_elm_app_server_view_window_set(Eo *obj EINA_UNUSED, Elm_App_Server_View_Data *data, Evas_Object *win)
{
   int before = data->window_id;

   data->window_id = elm_win_window_id_get(win);

   if (data->window_id != before)
     eldbus_service_property_changed(data->iface, "WindowId");
}

EOLIAN static void
_elm_app_server_view_title_set(Eo *obj EINA_UNUSED, Elm_App_Server_View_Data *data, const char *title)
{
   title = title ? title : "";

   if (eina_stringshare_replace(&data->title, title))
     eldbus_service_property_changed(data->iface, "Title");
}

EOLIAN static const char*
_elm_app_server_view_title_get(Eo *obj EINA_UNUSED, Elm_App_Server_View_Data *data)
{
   return data->title;
}

EOLIAN static void
_elm_app_server_view_icon_set(Eo *obj EINA_UNUSED, Elm_App_Server_View_Data *data, const char *icon)
{
   icon = icon ? icon : "";

   if (eina_stringshare_replace(&data->icon_name, icon))
     eldbus_service_property_changed(data->iface, "IconName");
}

EOLIAN static const char*
_elm_app_server_view_icon_get(Eo *obj EINA_UNUSED, Elm_App_Server_View_Data *data)
{
   return data->icon_name;
}

EOLIAN static void
_elm_app_server_view_pixels_set(Eo *obj EINA_UNUSED, Elm_App_Server_View_Data *data,
      unsigned int w EINA_UNUSED, unsigned int h EINA_UNUSED,
      Eina_Bool has_alpha EINA_UNUSED, const unsigned char *pixels EINA_UNUSED)
{
   //TODO
   eldbus_service_property_changed(data->iface, "IconPixels");
}

EOLIAN static void
_elm_app_server_view_pixels_get(Eo *obj EINA_UNUSED, Elm_App_Server_View_Data *pd EINA_UNUSED,
      unsigned int *w EINA_UNUSED, unsigned int *h EINA_UNUSED,
      Eina_Bool *has_alpha EINA_UNUSED, const unsigned char **pixels EINA_UNUSED)
{
   //TODO
}

EOLIAN static void
_elm_app_server_view_progress_set(Eo *obj EINA_UNUSED, Elm_App_Server_View_Data *data, short progress)
{
   if (data->progress != progress)
     eldbus_service_property_changed(data->iface, "Progress");
   data->progress = progress;
}

EOLIAN static short
_elm_app_server_view_progress_get(Eo *obj EINA_UNUSED, Elm_App_Server_View_Data *data)
{
   return data->progress;
}

EOLIAN static void
_elm_app_server_view_new_events_set(Eo *obj EINA_UNUSED, Elm_App_Server_View_Data *data, int events)
{
   if (data->new_events != events)
     eldbus_service_property_changed(data->iface, "NewEvents");
   data->new_events = events;
}

EOLIAN static int
_elm_app_server_view_new_events_get(Eo *obj EINA_UNUSED, Elm_App_Server_View_Data *data)
{
   return data->new_events;
}

EOLIAN static Eina_Stringshare*
_elm_app_server_view_id_get(Eo *obj EINA_UNUSED, Elm_App_Server_View_Data *data)
{
   return data->id;
}

EOLIAN static const char*
_elm_app_server_view_path_get(Eo *obj EINA_UNUSED, Elm_App_Server_View_Data *data)
{
   return eldbus_service_object_path_get(data->iface);
}

EOLIAN static void
_elm_app_server_view_id_set(Eo *obj, Elm_App_Server_View_Data *data, const char *id)
{
   Elm_App_Server *server = NULL;

   if (efl_finalized_get(obj))
     {
        ERR("Can't set id after object has been created.");
        return;
     }

   server = efl_parent_get(obj);
   EINA_SAFETY_ON_TRUE_RETURN(!server || !efl_isa(server, ELM_APP_SERVER_CLASS));

   if (!id)
     {
        int i;
        for (i = 1; i < 99999; i++)
          {
             char buf[64];
             Eina_Bool valid = EINA_FALSE;

             snprintf(buf, sizeof(buf), "view_%d", i);
             valid = elm_app_server_view_check(server, buf);
             if (valid)
               {
                  data->id = eina_stringshare_add(buf);
                  break;
               }
          }
     }
   else
     {
        Eina_Bool valid = EINA_FALSE;
        valid = elm_app_server_view_check(server, id);
        if (valid)
          data->id = eina_stringshare_add(id);
     }
}

EOLIAN static Eo *
_elm_app_server_view_efl_object_finalize(Eo *obj, Elm_App_Server_View_Data *data)
{
   const char *server_path = NULL;
   char view_path[PATH_MAX];
   Elm_App_Server *server = NULL;

   server = efl_parent_get(obj);
   EINA_SAFETY_ON_TRUE_RETURN_VAL(!server || !efl_isa(server, ELM_APP_SERVER_CLASS), NULL);


   if (!data->id)
     {
        ERR("Failed");
        return NULL;
     }

   server_path = elm_app_server_path_get(server);
   snprintf(view_path, sizeof(view_path), "%s/%s", server_path, data->id);

   eldbus_init();
   data->conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
   data->iface = eldbus_service_interface_register(data->conn, view_path,
                                                   &iface_desc);
   eldbus_service_object_data_set(data->iface, MY_CLASS_NAME, obj);

   data->state = ELM_APP_VIEW_STATE_UNKNOWN;
   data->title = eina_stringshare_add("");
   data->icon_name = eina_stringshare_add("");

   return efl_finalize(efl_super(obj, MY_CLASS));
}

EOLIAN static void
_elm_app_server_view_efl_object_destructor(Eo *obj, Elm_App_Server_View_Data *data)
{
   eina_stringshare_del(data->title);
   eina_stringshare_del(data->icon_name);

   eina_stringshare_del(data->id);
   eldbus_service_object_unregister(data->iface);
   eldbus_connection_unref(data->conn);
   eldbus_shutdown();

   efl_destructor(efl_super(obj, MY_CLASS));
}

#include "elm_app_server_view.eo.c"