Tizen 2.1 release
[platform/core/uifw/e17.git] / src / modules / conf_randr / e_int_config_randr_orientation.c
1 #include "e_int_config_randr.h"
2 #include "e_randr.h"
3
4 #ifndef  Ecore_X_Randr_Unset
5 #define Ecore_X_Randr_Unset          -1
6 #endif
7
8 static void _orientation_widget_mouse_up_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);
9 static void _orientation_widget_radio_add_callbacks(void);
10 extern E_Config_Dialog_Data *e_config_runtime_info;
11 extern char _theme_file_path[];
12
13 /*
14 static const char *_ORIENTATION_STRINGS[] = {
15     "Normal",
16     "Rotated, 90°",
17     "Rotated, 180°",
18     "Rotated, 270°",
19     "Flipped, Horizontally",
20     "Flipped, Vertically" };
21     */
22
23 Eina_Bool
24 orientation_widget_create_data(E_Config_Dialog_Data *cfdata)
25 {
26    E_Config_Randr_Dialog_Output_Dialog_Data *odd;
27    Eina_List *iter;
28
29    if (!cfdata || !cfdata->output_dialog_data_list) return EINA_FALSE;
30
31    EINA_LIST_FOREACH(cfdata->output_dialog_data_list, iter, odd)
32      {
33         odd->new_orientation = Ecore_X_Randr_Unset;
34         odd->previous_orientation = odd->crtc ? odd->crtc->current_orientation : (Ecore_X_Randr_Orientation) Ecore_X_Randr_Unset;
35      }
36
37    return EINA_TRUE;
38 }
39
40 void
41 orientation_widget_free_cfdata(E_Config_Dialog *cfd __UNUSED__, E_Config_Dialog_Data *cfdata)
42 {
43    evas_object_event_callback_del(cfdata->gui.widgets.orientation.radio_reflect_vertical, EVAS_CALLBACK_MOUSE_UP, _orientation_widget_mouse_up_cb);
44    evas_object_event_callback_del(cfdata->gui.widgets.orientation.radio_reflect_horizontal, EVAS_CALLBACK_MOUSE_UP, _orientation_widget_mouse_up_cb);
45    evas_object_event_callback_del(cfdata->gui.widgets.orientation.radio_rot270, EVAS_CALLBACK_MOUSE_UP, _orientation_widget_mouse_up_cb);
46    evas_object_event_callback_del(cfdata->gui.widgets.orientation.radio_rot180, EVAS_CALLBACK_MOUSE_UP, _orientation_widget_mouse_up_cb);
47    evas_object_event_callback_del(cfdata->gui.widgets.orientation.radio_rot90, EVAS_CALLBACK_MOUSE_UP, _orientation_widget_mouse_up_cb);
48    evas_object_event_callback_del(cfdata->gui.widgets.orientation.radio_normal, EVAS_CALLBACK_MOUSE_UP, _orientation_widget_mouse_up_cb);
49 }
50
51 Evas_Object *
52 orientation_widget_basic_create_widgets(Evas *canvas)
53 {
54    Evas_Object *widget;
55    E_Radio_Group *rg;
56    //char signal[29];
57
58    if (!canvas || !e_config_runtime_info) return NULL;
59    if (e_config_runtime_info->gui.widgets.orientation.widget) return e_config_runtime_info->gui.widgets.orientation.widget;
60
61    if (!(widget = e_widget_framelist_add(canvas, _("Display Orientation"), EINA_FALSE))) return NULL;
62
63    // Add radio buttons
64    if (!(rg = e_widget_radio_group_new(&e_config_runtime_info->gui.widgets.orientation.radio_val))) goto _orientation_widget_radio_add_fail;
65
66    //IMPROVABLE: use enum to determine objects via 'switch'-statement
67    e_config_runtime_info->gui.widgets.orientation.radio_normal = e_widget_radio_add(canvas, _("Normal"), ECORE_X_RANDR_OUTPUT_POLICY_ABOVE, rg);
68    e_widget_framelist_object_append(widget, e_config_runtime_info->gui.widgets.orientation.radio_normal);
69
70    e_config_runtime_info->gui.widgets.orientation.radio_rot90 = e_widget_radio_add(canvas, _("Rotated, 90°"), ECORE_X_RANDR_OUTPUT_POLICY_RIGHT, rg);
71    e_widget_framelist_object_append(widget, e_config_runtime_info->gui.widgets.orientation.radio_rot90);
72
73    e_config_runtime_info->gui.widgets.orientation.radio_rot180 = e_widget_radio_add(canvas, _("Rotated, 180°"), ECORE_X_RANDR_OUTPUT_POLICY_BELOW, rg);
74    e_widget_framelist_object_append(widget, e_config_runtime_info->gui.widgets.orientation.radio_rot180);
75
76    e_config_runtime_info->gui.widgets.orientation.radio_rot270 = e_widget_radio_add(canvas, _("Rotated, 270°"), ECORE_X_RANDR_OUTPUT_POLICY_LEFT, rg);
77    e_widget_framelist_object_append(widget, e_config_runtime_info->gui.widgets.orientation.radio_rot270);
78
79    e_config_runtime_info->gui.widgets.orientation.radio_reflect_horizontal = e_widget_radio_add(canvas, _("Flipped, horizontally"), ECORE_X_RANDR_OUTPUT_POLICY_CLONE, rg);
80    e_widget_framelist_object_append(widget, e_config_runtime_info->gui.widgets.orientation.radio_reflect_horizontal);
81
82    e_config_runtime_info->gui.widgets.orientation.radio_reflect_vertical = e_widget_radio_add(canvas, _("Flipped, vertically"), ECORE_X_RANDR_OUTPUT_POLICY_NONE, rg);
83    e_widget_framelist_object_append(widget, e_config_runtime_info->gui.widgets.orientation.radio_reflect_vertical);
84
85    _orientation_widget_radio_add_callbacks();
86
87    /*
88       // Add orientation demonstration edje
89       if (!(e_config_runtime_info->gui.widgets.orientation.swallowing_edje = edje_object_add(canvas)))
90       goto _orientation_widget_edje_add_fail;
91       if (!edje_object_file_set(e_config_runtime_info->gui.widgets.orientation.swallowing_edje, _theme_file_path, "e/conf/randr/dialog/widget/orientation"))
92       goto _orientation_widget_edje_set_fail;
93
94       e_widget_table_object_align_append(widget, e_config_runtime_info->gui.widgets.orientation.swallowing_edje, 1, 0, 1, 1, 1, 1, 1, 1, 1.0, 1.0);
95     */
96
97    //disable widgets, if no CRTC is selected
98    orientation_widget_update_radio_buttons(e_config_runtime_info->gui.selected_output_dd);
99
100    //evas_object_show(e_config_runtime_info->gui.widgets.orientation.swallowing_edje);
101
102    return widget;
103
104    /*
105       _orientation_widget_edje_set_fail:
106       evas_object_del(ol);
107       evas_object_del(e_config_runtime_info->gui.widgets.orientation.swallowing_edje);
108       _orientation_widget_edje_add_fail:
109       fprintf(stderr, "CONF_RANDR: Couldn't set edj for orientation widget!\n");
110       evas_object_del(widget);
111       return NULL;
112     */
113 _orientation_widget_radio_add_fail:
114    evas_object_del(widget);
115    fprintf(stderr, "CONF_RANDR: Could not add radio group!\n");
116    return NULL;
117 }
118
119 static void
120 _orientation_widget_mouse_up_cb(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void *event_info __UNUSED__)
121 {
122    //char signal[40];
123    int orientation = Ecore_X_Randr_Unset;
124
125    if (!e_config_runtime_info->gui.selected_output_dd)
126      return;
127
128    /*
129     * IMPROVABLE:
130     * "sadly" the evas callbacks are called before radio_val is set to its new
131     * value. If that is ever changed, remove the used code below and just use the
132     * 1-liner below.
133     * snprintf(signal, sizeof(signal), "conf,randr,dialog,orientation,%d", e_config_runtime_info->gui.widgets.orientation.radio_val);
134     */
135    if (obj == e_config_runtime_info->gui.widgets.orientation.radio_normal) orientation = ECORE_X_RANDR_ORIENTATION_ROT_0;
136    if (obj == e_config_runtime_info->gui.widgets.orientation.radio_rot90) orientation = ECORE_X_RANDR_ORIENTATION_ROT_90;
137    if (obj == e_config_runtime_info->gui.widgets.orientation.radio_rot180) orientation = ECORE_X_RANDR_ORIENTATION_ROT_180;
138    if (obj == e_config_runtime_info->gui.widgets.orientation.radio_rot270) orientation = ECORE_X_RANDR_ORIENTATION_ROT_270;
139    if (obj == e_config_runtime_info->gui.widgets.orientation.radio_reflect_horizontal) orientation = ECORE_X_RANDR_ORIENTATION_FLIP_X;
140    if (obj == e_config_runtime_info->gui.widgets.orientation.radio_reflect_vertical) orientation = ECORE_X_RANDR_ORIENTATION_FLIP_Y;
141
142    e_config_runtime_info->gui.selected_output_dd->new_orientation = orientation;
143
144    arrangement_widget_rep_update(e_config_runtime_info->gui.selected_output_dd);
145
146    //edje_object_signal_emit(e_config_runtime_info->gui.widgets.orientation.swallowing_edje, signal, "e");
147
148    //fprintf(stderr, "CONF_RANDR: mouse button released. Emitted signal to orientation: %s\n", signal);
149 }
150
151 void
152 orientation_widget_update_radio_buttons(E_Config_Randr_Dialog_Output_Dialog_Data *odd)
153 {
154    Ecore_X_Randr_Orientation supported_oris, ori;
155
156    //disable widgets, if no crtc is selected
157    if (!odd)
158      {
159         e_widget_disabled_set(e_config_runtime_info->gui.widgets.orientation.radio_normal, EINA_TRUE);
160         e_widget_disabled_set(e_config_runtime_info->gui.widgets.orientation.radio_rot90, EINA_TRUE);
161         e_widget_disabled_set(e_config_runtime_info->gui.widgets.orientation.radio_rot180, EINA_TRUE);
162         e_widget_disabled_set(e_config_runtime_info->gui.widgets.orientation.radio_rot270, EINA_TRUE);
163         e_widget_disabled_set(e_config_runtime_info->gui.widgets.orientation.radio_reflect_horizontal, EINA_TRUE);
164         e_widget_disabled_set(e_config_runtime_info->gui.widgets.orientation.radio_reflect_vertical, EINA_TRUE);
165         return;
166      }
167
168    if (odd->crtc)
169      {
170         //enabled monitor
171         supported_oris = odd->crtc->orientations;
172         ori = (odd->new_orientation != (Ecore_X_Randr_Orientation) Ecore_X_Randr_Unset) ? odd->new_orientation : odd->crtc->current_orientation;
173      }
174    else
175      {
176         //disabled monitor
177         //assume all orientations are supported
178         supported_oris = Ecore_X_Randr_Unset;
179         ori = Ecore_X_Randr_Unset;
180      }
181
182    if (supported_oris & ECORE_X_RANDR_ORIENTATION_ROT_0)
183      e_widget_disabled_set(e_config_runtime_info->gui.widgets.orientation.radio_normal, EINA_FALSE);
184    else
185      e_widget_disabled_set(e_config_runtime_info->gui.widgets.orientation.radio_normal, EINA_TRUE);
186
187    if (supported_oris & ECORE_X_RANDR_ORIENTATION_ROT_90)
188      e_widget_disabled_set(e_config_runtime_info->gui.widgets.orientation.radio_rot90, EINA_FALSE);
189    else
190      e_widget_disabled_set(e_config_runtime_info->gui.widgets.orientation.radio_rot90, EINA_TRUE);
191
192    if (supported_oris & ECORE_X_RANDR_ORIENTATION_ROT_180)
193      e_widget_disabled_set(e_config_runtime_info->gui.widgets.orientation.radio_rot180, EINA_FALSE);
194    else
195      e_widget_disabled_set(e_config_runtime_info->gui.widgets.orientation.radio_rot180, EINA_TRUE);
196
197    if (supported_oris & ECORE_X_RANDR_ORIENTATION_ROT_270)
198      e_widget_disabled_set(e_config_runtime_info->gui.widgets.orientation.radio_rot270, EINA_FALSE);
199    else
200      e_widget_disabled_set(e_config_runtime_info->gui.widgets.orientation.radio_rot270, EINA_TRUE);
201
202    if (supported_oris & ECORE_X_RANDR_ORIENTATION_FLIP_X)
203      e_widget_disabled_set(e_config_runtime_info->gui.widgets.orientation.radio_reflect_horizontal, EINA_FALSE);
204    else
205      e_widget_disabled_set(e_config_runtime_info->gui.widgets.orientation.radio_reflect_horizontal, EINA_TRUE);
206
207    if (supported_oris & ECORE_X_RANDR_ORIENTATION_FLIP_Y)
208      e_widget_disabled_set(e_config_runtime_info->gui.widgets.orientation.radio_reflect_vertical, EINA_FALSE);
209    else
210      e_widget_disabled_set(e_config_runtime_info->gui.widgets.orientation.radio_reflect_vertical, EINA_TRUE);
211
212    //toggle the switch of the currently used orientation
213    switch (ori)
214      {
215       case ECORE_X_RANDR_ORIENTATION_ROT_0:
216         e_widget_radio_toggle_set(e_config_runtime_info->gui.widgets.orientation.radio_normal, EINA_TRUE);
217         break;
218
219       case ECORE_X_RANDR_ORIENTATION_ROT_90:
220         e_widget_radio_toggle_set(e_config_runtime_info->gui.widgets.orientation.radio_rot90, EINA_TRUE);
221         break;
222
223       case ECORE_X_RANDR_ORIENTATION_ROT_180:
224         e_widget_radio_toggle_set(e_config_runtime_info->gui.widgets.orientation.radio_rot180, EINA_TRUE);
225         break;
226
227       case ECORE_X_RANDR_ORIENTATION_ROT_270:
228         e_widget_radio_toggle_set(e_config_runtime_info->gui.widgets.orientation.radio_rot270, EINA_TRUE);
229         break;
230
231       case ECORE_X_RANDR_ORIENTATION_FLIP_X:
232         e_widget_radio_toggle_set(e_config_runtime_info->gui.widgets.orientation.radio_reflect_horizontal, EINA_TRUE);
233         break;
234
235       case ECORE_X_RANDR_ORIENTATION_FLIP_Y:
236         e_widget_radio_toggle_set(e_config_runtime_info->gui.widgets.orientation.radio_reflect_vertical, EINA_TRUE);
237         break;
238
239       default:
240         break;
241      }
242 }
243
244 /*
245 void
246 orientation_widget_update_edje(E_Config_Randr_Dialog_Output_Dialog_Data *odd)
247 {
248    Ecore_X_Randr_Orientation supported_oris, ori;
249    char signal[40];
250
251    if (!odd)
252      return;
253
254    if (odd->crtc)
255      {
256         //enabled monitor
257         supported_oris = odd->crtc->orientations;
258         ori = odd->crtc->current_orientation;
259      }
260    else
261      {
262         //disabled monitor
263         //assume all orientations are supported
264         //#define RANDR_DIALOG_ORIENTATION_ALL (ECORE_X_RANDR_ORIENTATION_ROT_0 | ECORE_X_RANDR_ORIENTATION_ROT_90 | ECORE_X_RANDR_ORIENTATION_ROT_180 | ECORE_X_RANDR_ORIENTATION_ROT_270 | ECORE_X_RANDR_ORIENTATION_ROT_270 | ECORE_X_RANDR_ORIENTATION_FLIP_X | ECORE_X_RANDR_ORIENTATION_FLIP_Y)
265         supported_oris = RANDR_DIALOG_ORIENTATION_ALL;
266         ori = ECORE_X_RANDR_ORIENTATION_ROT_0;
267      }
268    //Send signal to the edje, to represent the supported and current set orientation
269    snprintf(signal, sizeof(signal), "conf,randr,dialog,orientation,supported,%d", supported_oris);
270    edje_object_signal_emit(crtc, signal, "e");
271    snprintf(signal, sizeof(signal), "conf,randr,dialog,orientation,current,%d", ori);
272    edje_object_signal_emit(crtc, signal, "e");
273 }
274 */
275
276 Eina_Bool
277 orientation_widget_basic_apply_data(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata)
278 {
279    E_Config_Randr_Dialog_Output_Dialog_Data *odd;
280    Eina_List *iter;
281    Eina_Bool success = EINA_TRUE;
282
283    if (!cfdata) return EINA_FALSE;
284
285    EINA_LIST_FOREACH(cfdata->output_dialog_data_list, iter, odd)
286      {
287         if (!odd || !odd->crtc || ((int)odd->new_orientation == Ecore_X_Randr_Unset))
288           continue;
289         fprintf(stderr, "CONF_RANDR: Change orientation of crtc %d to %d.\n", odd->crtc->xid, odd->new_orientation);
290         if (!ecore_x_randr_crtc_orientation_set(cfd->con->manager->root, odd->crtc->xid, odd->new_orientation))
291           success = EINA_FALSE;
292      }
293
294    return success;
295 }
296
297 Eina_Bool
298 orientation_widget_basic_check_changed(E_Config_Dialog *cfd __UNUSED__, E_Config_Dialog_Data *cfdata)
299 {
300    E_Config_Randr_Dialog_Output_Dialog_Data *odd;
301    Eina_List *iter;
302
303    if (!cfdata) return EINA_FALSE;
304
305    EINA_LIST_FOREACH(cfdata->output_dialog_data_list, iter, odd)
306      {
307         if (!odd || !odd->crtc || ((int)odd->previous_orientation == Ecore_X_Randr_Unset) || ((int)odd->new_orientation == Ecore_X_Randr_Unset))
308           continue;
309         if (odd->previous_orientation != odd->new_orientation)
310           return EINA_TRUE;
311      }
312
313    return EINA_FALSE;
314 }
315
316 void
317 orientation_widget_keep_changes(E_Config_Dialog_Data *cfdata)
318 {
319    E_Config_Randr_Dialog_Output_Dialog_Data *odd;
320    Eina_List *iter;
321
322    if (!cfdata) return;
323
324    EINA_LIST_FOREACH(cfdata->output_dialog_data_list, iter, odd)
325      {
326         if (!odd || ((int)odd->previous_orientation == Ecore_X_Randr_Unset))
327           continue;
328         odd->previous_orientation = odd->new_orientation;
329         odd->new_orientation = Ecore_X_Randr_Unset;
330      }
331 }
332
333 void
334 orientation_widget_discard_changes(E_Config_Dialog_Data *cfdata)
335 {
336    E_Config_Randr_Dialog_Output_Dialog_Data *odd;
337    Eina_List *iter;
338
339    if (!cfdata) return;
340
341    EINA_LIST_FOREACH(cfdata->output_dialog_data_list, iter, odd)
342      {
343         if (!odd->crtc || ((int)odd->previous_orientation == Ecore_X_Randr_Unset))
344           continue;
345         ecore_x_randr_crtc_orientation_set(cfdata->manager->root, odd->crtc->xid, odd->previous_orientation);
346         odd->new_orientation =  Ecore_X_Randr_Unset;
347      }
348 }
349
350    static void
351 _orientation_widget_radio_add_callbacks(void)
352 {
353    evas_object_event_callback_add(e_config_runtime_info->gui.widgets.orientation.radio_reflect_vertical, EVAS_CALLBACK_MOUSE_UP, _orientation_widget_mouse_up_cb, NULL);
354    evas_object_event_callback_add(e_config_runtime_info->gui.widgets.orientation.radio_reflect_horizontal, EVAS_CALLBACK_MOUSE_UP, _orientation_widget_mouse_up_cb, NULL);
355    evas_object_event_callback_add(e_config_runtime_info->gui.widgets.orientation.radio_rot270, EVAS_CALLBACK_MOUSE_UP, _orientation_widget_mouse_up_cb, NULL);
356    evas_object_event_callback_add(e_config_runtime_info->gui.widgets.orientation.radio_rot180, EVAS_CALLBACK_MOUSE_UP, _orientation_widget_mouse_up_cb, NULL);
357    evas_object_event_callback_add(e_config_runtime_info->gui.widgets.orientation.radio_rot90, EVAS_CALLBACK_MOUSE_UP, _orientation_widget_mouse_up_cb, NULL);
358    evas_object_event_callback_add(e_config_runtime_info->gui.widgets.orientation.radio_normal, EVAS_CALLBACK_MOUSE_UP, _orientation_widget_mouse_up_cb, NULL);
359 }