aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/conf_randr/e_smart_monitor.c
blob: 8bb3cd5e1a13eb594970afa7b57de2b4a8a4ff0e (plain)
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
#include "e.h"
#include "e_mod_main.h"
#include "e_smart_monitor.h"

/* local structure */
typedef struct _E_Smart_Data E_Smart_Data;
struct _E_Smart_Data
{
   /* reference to the actual canvas */
   Evas *evas;

   /* object geometry */
   Evas_Coord x, y, w, h;

   /* base object */
   Evas_Object *o_base;

   /* frame object */
   Evas_Object *o_frame;

   /* stand object */
   Evas_Object *o_stand;

   /* thumbnail object */
   Evas_Object *o_thumb;

   /* refresh rate object */
   Evas_Object *o_refresh;

   /* changed flag */
   Eina_Bool changed : 1;

   /* visible flag */
   Eina_Bool visible : 1;

   /* enabled flag */
   Eina_Bool enabled : 1;

   /* moving flag */
   Eina_Bool moving : 1;

   /* resizing flag */
   Eina_Bool resizing : 1;

   /* rotating flag */
   Eina_Bool rotating : 1;

   /* handler for bg updates */
   Ecore_Event_Handler *bg_update_hdl;

   /* list of randr 'modes' */
   Eina_List *modes;

   /* min & max resolutions */
   struct 
     {
        int w, h;
     } min, max;

   /* refresh rate */
   int refresh_rate;

   /* reference to the RandR Output Info */
   E_Randr_Output_Info *output;

   /* reference to the Layout widget */
   struct 
     {
        Evas_Object *obj; /* the actual layout widget */
        Evas_Coord x, y; /* the layout widget's position */
        Evas_Coord vw, vh; /* the layout widget's virtual size */
     } layout;

   /* reference to the Container number */
   int con_num;

   /* reference to the Zone number */
   int zone_num;
};

/* local function prototypes */
static void _e_smart_add(Evas_Object *obj);
static void _e_smart_del(Evas_Object *obj);
static void _e_smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y);
static void _e_smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h);
static void _e_smart_show(Evas_Object *obj);
static void _e_smart_hide(Evas_Object *obj);
static void _e_smart_clip_set(Evas_Object *obj, Evas_Object *clip);
static void _e_smart_clip_unset(Evas_Object *obj);

static void _e_smart_monitor_refresh_rates_fill(E_Smart_Data *sd);
static void _e_smart_monitor_modes_fill(E_Smart_Data *sd);
static int _e_smart_monitor_modes_sort(const void *data1, const void *data2);
static void _e_smart_monitor_background_set(E_Smart_Data *sd, Evas_Coord dx, Evas_Coord dy);
static Eina_Bool _e_smart_monitor_background_update(void *data, int type, void *event);

static void _e_smart_monitor_move_event(E_Smart_Data *sd, Evas_Object *mon, void *event);
static void _e_smart_monitor_resize_event(E_Smart_Data *sd, Evas_Object *mon, void *event);

/* local callback prototypes */
static void _e_smart_monitor_frame_cb_mouse_move(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event);
static void _e_smart_monitor_frame_cb_resize_in(void *data __UNUSED__, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__);
static void _e_smart_monitor_frame_cb_resize_out(void *data __UNUSED__, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__);
static void _e_smart_monitor_frame_cb_resize_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__);
static void _e_smart_monitor_frame_cb_resize_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__);
static void _e_smart_monitor_frame_cb_rotate_in(void *data __UNUSED__, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__);
static void _e_smart_monitor_frame_cb_rotate_out(void *data __UNUSED__, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__);
static void _e_smart_monitor_frame_cb_indicator_in(void *data __UNUSED__, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__);
static void _e_smart_monitor_frame_cb_indicator_out(void *data __UNUSED__, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__);
static void _e_smart_monitor_frame_cb_indicator_toggle(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__);

static void _e_smart_monitor_thumb_cb_mouse_in(void *data __UNUSED__, Evas *evas __UNUSED__, Evas_Object *obj, void *event __UNUSED__);
static void _e_smart_monitor_thumb_cb_mouse_out(void *data __UNUSED__, Evas *evas __UNUSED__, Evas_Object *obj, void *event __UNUSED__);
static void _e_smart_monitor_thumb_cb_mouse_down(void *data, Evas *evas __UNUSED__, Evas_Object *obj, void *event);
static void _e_smart_monitor_thumb_cb_mouse_up(void *data, Evas *evas __UNUSED__, Evas_Object *obj, void *event);

static void _e_smart_monitor_layout_cb_move(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event __UNUSED__);

/* external functions exposed by this widget */
Evas_Object *
e_smart_monitor_add(Evas *evas)
{
   static Evas_Smart *smart = NULL;
   static const Evas_Smart_Class sc = 
     {
        "smart_monitor", EVAS_SMART_CLASS_VERSION, 
        _e_smart_add, _e_smart_del, _e_smart_move, _e_smart_resize, 
        _e_smart_show, _e_smart_hide, NULL, 
        _e_smart_clip_set, _e_smart_clip_unset, 
        NULL, NULL, NULL, NULL, NULL, NULL, NULL
     };

   /* if we have never created the smart class, do it now */
   if (!smart)
     if (!(smart = evas_smart_class_new(&sc)))
       return NULL;

   /* return a newly created smart randr widget */
   return evas_object_smart_add(evas, smart);
}

void 
e_smart_monitor_output_set(Evas_Object *obj, E_Randr_Output_Info *output)
{
   E_Smart_Data *sd;
   Evas_Coord mw = 0, mh = 0;
   Evas_Coord cx = 0, cy = 0;
   Evas_Coord fw = 0, fh = 0;
   E_Container *con;
   E_Zone *zone;
   E_Desk *desk;

   /* try to get the objects smart data */
   if (!(sd = evas_object_smart_data_get(obj))) return;

   /* set this monitor's output reference */
   sd->output = output;

   /* fill the list of 'modes' for this monitor
    * 
    * NB: This clears old modes and also sets the min & max resolutions */
   _e_smart_monitor_modes_fill(sd);

   if (sd->layout.obj)
     {
        /* with the min & max resolutions, we can now set the thumbnail size.
         * get largest resolution and convert to largest canvas size */
        e_layout_coord_virtual_to_canvas(sd->layout.obj, 
                                         sd->max.w, sd->max.h, &mw, &mh);
     }

   /* set thumbnail size based on largest canvas size */
   mh = (mw * mh) / mw;
   if (sd->o_thumb) 
     e_livethumb_vsize_set(sd->o_thumb, mw, mh);

   /* if we have a crtc, get the x/y location of it and current refresh rate
    * 
    * NB: Used to determine the proper container */
   if ((sd->output) && (sd->output->crtc))
     {
        Ecore_X_Randr_Mode_Info *mode = NULL;

        cx = sd->output->crtc->geometry.x;
        cy = sd->output->crtc->geometry.y;

        if ((mode = sd->output->crtc->current_mode))
          {
             if ((mode->hTotal) && (mode->vTotal))
               {
                  /* calculate rate */
                  sd->refresh_rate = 
                    (int)((float)mode->dotClock / 
                          ((float)mode->hTotal * (float)mode->vTotal));
               }
          }
     }
   else
     {
        /* FIXME: NB: TODO: Handle case of output not having crtc */
     }

   /* fill in list of refresh rates
    * 
    * NB: This has to be done after the 'current' refresh rate is calculated 
    * above or else the radio widgets do not get properly selected */
   _e_smart_monitor_refresh_rates_fill(sd);

   /* get the current desktop at this crtc coordinate */
   con = e_container_current_get(e_manager_current_get());
   zone = e_container_zone_at_point_get(con, cx, cy);
   if (!(desk = e_desk_at_xy_get(zone, cx, cy)))
     desk = e_desk_current_get(zone);

   /* set references to the container & zone number
    * 
    * NB: Used later if background gets updated */
   sd->con_num = con->num;
   sd->zone_num = zone->num;

   /* set the background image */
   _e_smart_monitor_background_set(sd, desk->x, desk->y);

   /* if we have an output, set the monitor name */
   if (sd->output) 
     {
        E_Randr_Monitor_Info *monitor = NULL;
        const char *name = NULL;

        if ((monitor = sd->output->monitor))
          name = ecore_x_randr_edid_display_name_get(monitor->edid, 
                                                     monitor->edid_length);
        if (!name) name = sd->output->name;
        edje_object_part_text_set(sd->o_frame, "e.text.name", name);
     }

   /* if we have an output, set the resolution name */
   if ((sd->output) && (sd->output->crtc))
     {
        Ecore_X_Randr_Mode_Info *mode;

        if ((mode = sd->output->crtc->current_mode))
          {
             char buff[1024];

             snprintf(buff, sizeof(buff), "%d x %d", 
                      mode->width, mode->height);
             edje_object_part_text_set(sd->o_frame, "e.text.resolution", buff);
          }
     }

   /* check if enabled */
   if ((sd->output) && (sd->output->crtc))
     {
        if (sd->output->crtc->current_mode)
          sd->enabled = EINA_TRUE;
     }

   /* send enabled/disabled signals */
   if (sd->enabled)
     edje_object_signal_emit(sd->o_frame, "e,state,enabled", "e");
   else
     edje_object_signal_emit(sd->o_frame, "e,state,disabled", "e");

   /* with everything all setup, calculate the smallest frame width */
   edje_object_size_min_get(sd->o_frame, &fw, &fh);

   if (sd->layout.obj)
     {
        /* get smallest resolution and convert to smallest canvas size */
        e_layout_coord_virtual_to_canvas(sd->layout.obj, 
                                         sd->min.w, sd->min.h, &mw, &mh);
     }

   /* if min resolution is smaller than the frame, then set the 
    * objects min size to frame size */
   if (mw < fw) mw = fw;
   if (mh < fh) mh = fh;
   evas_object_size_hint_min_set(obj, mw, mh);
}

E_Randr_Output_Info *
e_smart_monitor_output_get(Evas_Object *obj)
{
   E_Smart_Data *sd;

   /* try to get the objects smart data */
   if (!(sd = evas_object_smart_data_get(obj))) return NULL;

   /* return the monitor's referenced output */
   return sd->output;
}

void 
e_smart_monitor_layout_set(Evas_Object *obj, Evas_Object *layout)
{
   E_Smart_Data *sd;

   /* try to get the objects smart data */
   if (!(sd = evas_object_smart_data_get(obj))) return;

   /* set this monitor's layout reference */
   sd->layout.obj = layout;

   /* get the layout's virtual size */
   e_layout_virtual_size_get(layout, &sd->layout.vw, &sd->layout.vh);

   evas_object_event_callback_add(layout, EVAS_CALLBACK_MOVE, 
                                  _e_smart_monitor_layout_cb_move, sd);
}

Evas_Object *
e_smart_monitor_layout_get(Evas_Object *obj)
{
   E_Smart_Data *sd;

   /* try to get the objects smart data */
   if (!(sd = evas_object_smart_data_get(obj))) return NULL;

   /* return the monitor's referenced layout widget */
   return sd->layout.obj;
}

void 
e_smart_monitor_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
{
   if (!obj) return;
   e_layout_child_move(obj, x, y);
}

void 
e_smart_monitor_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
{
   if (!obj) return;
   e_layout_child_resize(obj, w, h);
}

/* local functions */
static void 
_e_smart_add(Evas_Object *obj)
{
   E_Smart_Data *sd;

   /* try to allocate the smart data structure */
   if (!(sd = E_NEW(E_Smart_Data, 1))) return;

   /* grab the canvas */
   sd->evas = evas_object_evas_get(obj);

   /* create the base object */
   sd->o_base = edje_object_add(sd->evas);
   e_theme_edje_object_set(sd->o_base, "base/theme/widgets", 
                           "e/conf/randr/main/monitor");
   evas_object_smart_member_add(sd->o_base, obj);

   /* create monitor 'frame' */
   sd->o_frame = edje_object_add(sd->evas);
   e_theme_edje_object_set(sd->o_frame, "base/theme/widgets", 
                           "e/conf/randr/main/frame");
   edje_object_part_swallow(sd->o_base, "e.swallow.frame", sd->o_frame);
   evas_object_event_callback_add(sd->o_frame, EVAS_CALLBACK_MOUSE_MOVE, 
                                  _e_smart_monitor_frame_cb_mouse_move, obj);

   /* create the preview */
   sd->o_thumb = e_livethumb_add(sd->evas);
   edje_object_part_swallow(sd->o_frame, "e.swallow.preview", sd->o_thumb);
   evas_object_event_callback_add(sd->o_thumb, EVAS_CALLBACK_MOUSE_IN, 
                                  _e_smart_monitor_thumb_cb_mouse_in, NULL);
   evas_object_event_callback_add(sd->o_thumb, EVAS_CALLBACK_MOUSE_OUT, 
                                  _e_smart_monitor_thumb_cb_mouse_out, NULL);
   evas_object_event_callback_add(sd->o_thumb, EVAS_CALLBACK_MOUSE_UP, 
                                  _e_smart_monitor_thumb_cb_mouse_up, obj);
   evas_object_event_callback_add(sd->o_thumb, EVAS_CALLBACK_MOUSE_DOWN, 
                                  _e_smart_monitor_thumb_cb_mouse_down, obj);

   /* create monitor stand */
   sd->o_stand = edje_object_add(sd->evas);
   e_theme_edje_object_set(sd->o_stand, "base/theme/widgets", 
                           "e/conf/randr/main/stand");
   edje_object_part_swallow(sd->o_base, "e.swallow.stand", sd->o_stand);

   /* add callbacks for resize signals */
   edje_object_signal_callback_add(sd->o_frame, "e,action,resize,in", "e", 
                                   _e_smart_monitor_frame_cb_resize_in, NULL);
   edje_object_signal_callback_add(sd->o_frame, "e,action,resize,out", "e", 
                                   _e_smart_monitor_frame_cb_resize_out, NULL);
   edje_object_signal_callback_add(sd->o_frame, "e,action,resize,start", "e", 
                                   _e_smart_monitor_frame_cb_resize_start, obj);
   edje_object_signal_callback_add(sd->o_frame, "e,action,resize,stop", "e", 
                                   _e_smart_monitor_frame_cb_resize_stop, obj);

   /* add callbacks for rotate signals */
   edje_object_signal_callback_add(sd->o_frame, "e,action,rotate,in", "e", 
                                   _e_smart_monitor_frame_cb_rotate_in, sd);
   edje_object_signal_callback_add(sd->o_frame, "e,action,rotate,out", "e", 
                                   _e_smart_monitor_frame_cb_rotate_out, sd);

   /* add callbacks for indicator signals */
   edje_object_signal_callback_add(sd->o_frame, "e,action,indicator,in", "e", 
                                   _e_smart_monitor_frame_cb_indicator_in, sd);
   edje_object_signal_callback_add(sd->o_frame, "e,action,indicator,out", "e", 
                                   _e_smart_monitor_frame_cb_indicator_out, sd);
   edje_object_signal_callback_add(sd->o_frame, 
                                   "e,action,indicator,toggle", "e", 
                                   _e_smart_monitor_frame_cb_indicator_toggle, 
                                   obj);

   /* NB: Not entirely sure this is needed */
   /* evas_object_stack_below(sd->o_stand, sd->o_frame); */

   /* add handler for bg updates */
   sd->bg_update_hdl = 
     ecore_event_handler_add(E_EVENT_BG_UPDATE, 
                             _e_smart_monitor_background_update, sd);

   /* set the objects smart data */
   evas_object_smart_data_set(obj, sd);
}

static void 
_e_smart_del(Evas_Object *obj)
{
   E_Smart_Data *sd;

   /* try to get the objects smart data */
   if (!(sd = evas_object_smart_data_get(obj))) return;

   /* delete the list of modes */
   if (sd->modes) eina_list_free(sd->modes);

   /* delete the bg update handler */
   if (sd->bg_update_hdl) ecore_event_handler_del(sd->bg_update_hdl);

   /* delete the stand object */
   if (sd->o_stand) evas_object_del(sd->o_stand);

   /* delete the preview */
   if (sd->o_thumb) 
     {
        evas_object_event_callback_del(sd->o_thumb, EVAS_CALLBACK_MOUSE_IN, 
                                       _e_smart_monitor_thumb_cb_mouse_in);
        evas_object_event_callback_del(sd->o_thumb, EVAS_CALLBACK_MOUSE_OUT, 
                                       _e_smart_monitor_thumb_cb_mouse_out);
        evas_object_event_callback_del(sd->o_thumb, EVAS_CALLBACK_MOUSE_UP, 
                                       _e_smart_monitor_thumb_cb_mouse_up);
        evas_object_event_callback_del(sd->o_thumb, EVAS_CALLBACK_MOUSE_DOWN, 
                                       _e_smart_monitor_thumb_cb_mouse_down);
        evas_object_del(sd->o_thumb);
     }

   /* delete the frame object */
   if (sd->o_frame) 
     {
        edje_object_signal_callback_del(sd->o_frame, 
                                        "e,action,resize,in", "e", 
                                        _e_smart_monitor_frame_cb_resize_in);
        edje_object_signal_callback_del(sd->o_frame, 
                                        "e,action,resize,out", "e", 
                                        _e_smart_monitor_frame_cb_resize_out);
        edje_object_signal_callback_del(sd->o_frame, 
                                        "e,action,resize,start", "e", 
                                        _e_smart_monitor_frame_cb_resize_start);
        edje_object_signal_callback_del(sd->o_frame, 
                                        "e,action,resize,stop", "e", 
                                        _e_smart_monitor_frame_cb_resize_stop);
        edje_object_signal_callback_del(sd->o_frame, 
                                        "e,action,rotate,in", "e", 
                                        _e_smart_monitor_frame_cb_rotate_in);
        edje_object_signal_callback_del(sd->o_frame, 
                                        "e,action,rotate,out", "e", 
                                        _e_smart_monitor_frame_cb_rotate_out);
        edje_object_signal_callback_del(sd->o_frame, 
                                        "e,action,indicator,in", "e", 
                                        _e_smart_monitor_frame_cb_indicator_in);
        edje_object_signal_callback_del(sd->o_frame, 
                                        "e,action,indicator,out", "e", 
                                        _e_smart_monitor_frame_cb_indicator_out);
        edje_object_signal_callback_del(sd->o_frame, 
                                        "e,action,indicator,toggle", "e", 
                                        _e_smart_monitor_frame_cb_indicator_toggle);
        evas_object_event_callback_del(sd->o_frame, EVAS_CALLBACK_MOUSE_MOVE, 
                                       _e_smart_monitor_frame_cb_mouse_move);
        evas_object_del(sd->o_frame);
     }

   /* delete the base object */
   if (sd->o_base) evas_object_del(sd->o_base);

   /* try to free the allocated structure */
   E_FREE(sd);

   /* set the objects smart data to null */
   evas_object_smart_data_set(obj, NULL);
}

static void 
_e_smart_move(Evas_Object *obj, Evas_Coord x, Evas_Coord y)
{
   E_Smart_Data *sd;

   /* try to get the objects smart data */
   if (!(sd = evas_object_smart_data_get(obj))) return;

   if ((sd->x == x) && (sd->y == y)) return;

   sd->x = x;
   sd->y = y;

   /* move the base object */
   if (sd->o_base) evas_object_move(sd->o_base, x, y);
}

static void 
_e_smart_resize(Evas_Object *obj, Evas_Coord w, Evas_Coord h)
{
   E_Smart_Data *sd;

   /* try to get the objects smart data */
   if (!(sd = evas_object_smart_data_get(obj))) return;

   if ((sd->w == h) && (sd->w == h)) return;

   sd->w = w;
   sd->h = h;

   /* resize the base object */
   if (sd->o_base) evas_object_resize(sd->o_base, w, h);
}

static void 
_e_smart_show(Evas_Object *obj)
{
   E_Smart_Data *sd;

   /* try to get the objects smart data */
   if (!(sd = evas_object_smart_data_get(obj))) return;

   /* if it is already visible, get out */
   if (sd->visible) return;

   /* show the grid */
   if (sd->o_base) evas_object_show(sd->o_base);

   /* set visibility flag */
   sd->visible = EINA_TRUE;
}

static void 
_e_smart_hide(Evas_Object *obj)
{
   E_Smart_Data *sd;

   /* try to get the objects smart data */
   if (!(sd = evas_object_smart_data_get(obj))) return;

   /* if it is not visible, we have nothing to do */
   if (!sd->visible) return;

   /* hide the grid */
   if (sd->o_base) evas_object_hide(sd->o_base);

   /* set visibility flag */
   sd->visible = EINA_FALSE;
}

static void 
_e_smart_clip_set(Evas_Object *obj, Evas_Object *clip)
{
   E_Smart_Data *sd;

   /* try to get the objects smart data */
   if (!(sd = evas_object_smart_data_get(obj))) return;

   /* set the clip */
   if (sd->o_stand) evas_object_clip_set(sd->o_stand, clip);
   if (sd->o_frame) evas_object_clip_set(sd->o_frame, clip);
   if (sd->o_base) evas_object_clip_set(sd->o_base, clip);
}

static void 
_e_smart_clip_unset(Evas_Object *obj)
{
   E_Smart_Data *sd;

   /* try to get the objects smart data */
   if (!(sd = evas_object_smart_data_get(obj))) return;

   /* unset the clip */
   if (sd->o_stand) evas_object_clip_unset(sd->o_stand);
   if (sd->o_frame) evas_object_clip_unset(sd->o_frame);
   if (sd->o_base) evas_object_clip_unset(sd->o_base);
}

static void 
_e_smart_monitor_refresh_rates_fill(E_Smart_Data *sd)
{
   E_Radio_Group *rg;
   Eina_List *m = NULL;
   Ecore_X_Randr_Mode_Info *mode = NULL;
   Evas_Coord mw = 0, mh = 0;

   if (sd->o_refresh)
     {
        /* remove the old refresh list */
        edje_object_part_unswallow(sd->o_frame, sd->o_refresh);
        evas_object_del(sd->o_refresh);
     }

   /* create new refresh list widget */
   sd->o_refresh = e_widget_list_add(sd->evas, 0, 0);

   /* create radio group for rates */
   rg = e_widget_radio_group_new(&sd->refresh_rate);

   /* loop the modes and find the current one */
   EINA_LIST_FOREACH(sd->modes, m, mode)
     {
        /* compare mode names */
        if (!strcmp(mode->name, sd->output->crtc->current_mode->name))
          {
             if ((mode->hTotal) && (mode->vTotal))
               {
                  Evas_Object *ow;
                  double rate = 0.0;
                  char buff[1024];

                  /* calculate rate */
                  rate = ((float)mode->dotClock / 
                          ((float)mode->hTotal * (float)mode->vTotal));

                  snprintf(buff, sizeof(buff), "%.1fHz", rate);

                  /* create the radio widget */
                  ow = e_widget_radio_add(sd->evas, buff, (int)rate, rg);
                  e_widget_list_object_append(sd->o_refresh, ow, 1, 0, 0.5);
               }
          }
     }

   /* calculate minimum size for refresh list and set it */
   e_widget_size_min_get(sd->o_refresh, &mw, &mh);
   edje_extern_object_min_size_set(sd->o_refresh, mw, mh);

   /* swallow refresh list into frame and show it */
   edje_object_part_swallow(sd->o_frame, "e.swallow.refresh", sd->o_refresh);
   evas_object_show(sd->o_refresh);
}

static void 
_e_smart_monitor_modes_fill(E_Smart_Data *sd)
{
   /* clear out any old modes */
   if (sd->modes) eina_list_free(sd->modes);

   /* make sure we have a valid output */
   if (!sd->output) return;

   /* if we have an assigned monitor, copy the modes from that */
   if (sd->output->monitor)
     sd->modes = eina_list_clone(sd->output->monitor->modes);
   else if (sd->output->crtc)
     sd->modes = eina_list_clone(sd->output->crtc->outputs_common_modes);

   /* sort the mode list (smallest to largest) */
   sd->modes = eina_list_sort(sd->modes, 0, _e_smart_monitor_modes_sort);

   /* try to determine the min & max resolutions */
   if (sd->modes)
     {
        Ecore_X_Randr_Mode_Info *mode = NULL;

        /* try to get smallest resolution */
        if ((mode = eina_list_nth(sd->modes, 0)))
          {
             sd->min.w = mode->width;
             sd->min.h = mode->height;
          }

        /* try to get largest resolution */
        if ((mode = eina_list_last_data_get(sd->modes)))
          {
             sd->max.w = mode->width;
             sd->max.h = mode->height;
          }
     }
}

static int 
_e_smart_monitor_modes_sort(const void *data1, const void *data2)
{
   const Ecore_X_Randr_Mode_Info *m1, *m2 = NULL;

   if (!(m1 = data1)) return 1;
   if (!(m2 = data2)) return -1;

   /* second one compares to previous to determine position */
   if (m2->width < m1->width) return 1;
   if (m2->width > m1->width) return -1;

   /* width are same, compare heights */
   if ((m2->width == m1->width))
     {
        if (m2->height < m1->height) return 1;
        if (m2->height > m1->height) return -1;
     }

   return 1;
}

static void 
_e_smart_monitor_background_set(E_Smart_Data *sd, Evas_Coord dx, Evas_Coord dy)
{
   const char *bg = NULL;

   if (!sd->o_thumb) return;

   /* try to get the bg file for this desktop */
   if ((bg = e_bg_file_get(sd->con_num, sd->zone_num, dx, dy)))
     {
        Evas_Object *o;

        /* try to get the livethumb, if not then create an object */
        if (!(o = e_livethumb_thumb_get(sd->o_thumb)))
          o = edje_object_add(e_livethumb_evas_get(sd->o_thumb));

        /* tell the object to use this edje file & group */
        edje_object_file_set(o, bg, "e/desktop/background");

        /* tell the thumbnail to use this object for preview */
        e_livethumb_thumb_set(sd->o_thumb, o);
     }
}

static Eina_Bool 
_e_smart_monitor_background_update(void *data, int type, void *event)
{
   E_Smart_Data *sd;
   E_Event_Bg_Update *ev;

   if (type != E_EVENT_BG_UPDATE) return ECORE_CALLBACK_PASS_ON;
   if (!(sd = data)) return ECORE_CALLBACK_PASS_ON;

   ev = event;

   if (((ev->container < 0) || (ev->container == sd->con_num)) && 
       ((ev->zone < 0) || (ev->zone == sd->zone_num)))
     {
        Evas_Coord cx = 0, cy = 0;

        /* if we have a crtc, get the x/y location of it */
        if ((sd->output) && (sd->output->crtc))
          {
             cx = sd->output->crtc->geometry.x;
             cy = sd->output->crtc->geometry.y;
          }
        else
          {
             /* FIXME: NB: TODO: Handle case of output not having crtc */
          }

        if (((ev->desk_x < 0) || (ev->desk_x == cx)) && 
            ((ev->desk_y < 0) || (ev->desk_y == cy)))
          _e_smart_monitor_background_set(sd, ev->desk_x, ev->desk_y);
     }

   return ECORE_CALLBACK_PASS_ON;
}

static void 
_e_smart_monitor_move_event(E_Smart_Data *sd, Evas_Object *mon, void *event)
{
   Evas_Event_Mouse_Move *ev;
   Evas_Coord dx = 0, dy = 0;
   Evas_Coord nx = 0, ny = 0;
   Evas_Coord mx = 0, my = 0;
   Evas_Coord mw = 0, mh = 0;

   /* check for valid smart data */
   if (!sd) return;

   ev = event;

   /* calculate the difference in mouse movement */
   dx = (ev->cur.canvas.x - ev->prev.canvas.x);
   dy = (ev->cur.canvas.y - ev->prev.canvas.y);

   /* convert these coords to virtual space */
   e_layout_coord_canvas_to_virtual(sd->layout.obj, (sd->layout.x + dx), 
                                    (sd->layout.y + dy), &nx, &ny);

   /* get monitors current geometry */
   e_layout_child_geometry_get(mon, &mx, &my, &mw, &mh);
   nx += mx;
   ny += my;

   /* constrain to the layout bounds */
   if (nx < sd->layout.x) 
     nx = sd->layout.x;
   else if (nx > (sd->layout.x + sd->layout.vw) - mw)
     nx = (sd->layout.x + sd->layout.vw) - mw;

   if (ny < sd->layout.y) 
     ny = sd->layout.y;
   else if (ny > (sd->layout.y + sd->layout.vh) - mh)
     ny = (sd->layout.y + sd->layout.vh) - mh;

   /* actually do the move */
   if ((nx != mx) || (ny != my))
     {
        e_layout_child_move(mon, nx, ny);
        evas_object_smart_callback_call(mon, "monitor_moved", NULL);
     }

   /* TODO: handle changes */
}

static void 
_e_smart_monitor_resize_event(E_Smart_Data *sd, Evas_Object *mon, void *event)
{
   Evas_Event_Mouse_Move *ev;
   Evas_Coord dx = 0, dy = 0;
   Evas_Coord cw = 0, ch = 0;
   Evas_Coord mw = 0, mh = 0;
   Evas_Coord nw = 0, nh = 0;

   ev = event;

   /* calculate resize difference based on mouse movement */
   dx = (ev->cur.output.x - ev->prev.output.x);
   dy = (ev->cur.output.y - ev->prev.output.y);

   /* get size of monitor object */
   e_layout_child_geometry_get(mon, NULL, NULL, &cw, &ch);

   /* convert size to canvas coords */
   e_layout_coord_virtual_to_canvas(sd->layout.obj, cw, ch, &mw, &mh);

   /* factor in the resize difference and convert to virtual coords */
   e_layout_coord_canvas_to_virtual(sd->layout.obj, 
                                    (mw + dx), (mh + dy), &nw, &nh);

   /* max sure we cannot resize beyond the max */
   if ((nw > sd->max.w) || (nh > sd->max.h)) return;

   /* actually resize the monitor */
   e_layout_child_resize(mon, nw, nh);
}

/* local callbacks */
static void 
_e_smart_monitor_frame_cb_mouse_move(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event)
{
   Evas_Object *mon;
   E_Smart_Data *sd;

   /* try to get the objects smart data */
   if (!(mon = data)) return;
   if (!(sd = evas_object_smart_data_get(mon))) return;

   /* TODO: handle moving, resize, rotating */
   if (sd->moving)
     _e_smart_monitor_move_event(sd, mon, event);
   else if (sd->resizing)
     _e_smart_monitor_resize_event(sd, mon, event);
}

static void 
_e_smart_monitor_frame_cb_resize_in(void *data __UNUSED__, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
{
   E_Manager *man;

   if ((man = e_manager_current_get()))
     e_pointer_type_push(man->pointer, obj, "resize_br");
}

static void 
_e_smart_monitor_frame_cb_resize_out(void *data __UNUSED__, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
{
   E_Manager *man;

   if ((man = e_manager_current_get()))
     e_pointer_type_pop(man->pointer, obj, "resize_br");
}

static void 
_e_smart_monitor_frame_cb_resize_start(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
{
   Evas_Object *mon;
   E_Smart_Data *sd;

   /* try to get the objects smart data */
   if (!(mon = data)) return;
   if (!(sd = evas_object_smart_data_get(mon))) return;

   /* set resizing flag */
   sd->resizing = EINA_TRUE;

   /* raise this monitor */
   e_layout_child_raise(mon);
}

static void 
_e_smart_monitor_frame_cb_resize_stop(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
{
   Evas_Object *mon;
   E_Smart_Data *sd;

   /* try to get the objects smart data */
   if (!(mon = data)) return;
   if (!(sd = evas_object_smart_data_get(mon))) return;

   /* set resizing flag */
   sd->resizing = EINA_FALSE;
}

static void 
_e_smart_monitor_frame_cb_rotate_in(void *data __UNUSED__, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
{
   E_Manager *man;

   if ((man = e_manager_current_get()))
     e_pointer_type_push(man->pointer, obj, "rotate");
}

static void 
_e_smart_monitor_frame_cb_rotate_out(void *data __UNUSED__, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
{
   E_Manager *man;

   if ((man = e_manager_current_get()))
     e_pointer_type_pop(man->pointer, obj, "rotate");
}

static void 
_e_smart_monitor_frame_cb_indicator_in(void *data __UNUSED__, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
{
   E_Manager *man;

   if ((man = e_manager_current_get()))
     e_pointer_type_push(man->pointer, obj, "hand");
}

static void 
_e_smart_monitor_frame_cb_indicator_out(void *data __UNUSED__, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__)
{
   E_Manager *man;

   if ((man = e_manager_current_get()))
     e_pointer_type_pop(man->pointer, obj, "hand");
}

static void 
_e_smart_monitor_frame_cb_indicator_toggle(void *data, Evas_Object *obj __UNUSED__, const char *emission __UNUSED__, const char *source __UNUSED__)
{
   Evas_Object *mon;
   E_Smart_Data *sd;

   if (!(mon = data)) return;
   if (!(sd = evas_object_smart_data_get(mon))) return;

   if (sd->enabled)
     {
        sd->enabled = EINA_FALSE;
        edje_object_signal_emit(sd->o_frame, "e,state,disabled", "e");
     }
   else
     {
        sd->enabled = EINA_TRUE;
        edje_object_signal_emit(sd->o_frame, "e,state,enabled", "e");
     }

   /* TODO: Record changes */

   evas_object_smart_callback_call(mon, "monitor_toggled", NULL);
}

static void 
_e_smart_monitor_thumb_cb_mouse_in(void *data __UNUSED__, Evas *evas __UNUSED__, Evas_Object *obj, void *event __UNUSED__)
{
   E_Manager *man;

   if ((man = e_manager_current_get()))
     e_pointer_type_push(man->pointer, obj, "hand");
}

static void 
_e_smart_monitor_thumb_cb_mouse_out(void *data __UNUSED__, Evas *evas __UNUSED__, Evas_Object *obj, void *event __UNUSED__)
{
   E_Manager *man;

   if ((man = e_manager_current_get()))
     e_pointer_type_pop(man->pointer, obj, "hand");
}

static void 
_e_smart_monitor_thumb_cb_mouse_down(void *data, Evas *evas __UNUSED__, Evas_Object *obj, void *event)
{
   Evas_Event_Mouse_Down *ev;

   ev = event;
   if (ev->button == 1)
     {
        E_Manager *man;
        Evas_Object *mon;
        E_Smart_Data *sd;

        if ((man = e_manager_current_get()))
          e_pointer_type_push(man->pointer, obj, "move");

        if (!(mon = data)) return;
        if (!(sd = evas_object_smart_data_get(mon))) return;

        /* set moving flag */
        sd->moving = EINA_TRUE;

        /* raise this monitor */
        e_layout_child_raise(mon);
     }
}

static void 
_e_smart_monitor_thumb_cb_mouse_up(void *data, Evas *evas __UNUSED__, Evas_Object *obj, void *event)
{
   Evas_Event_Mouse_Up *ev;

   ev = event;
   if (ev->button == 1)
     {
        E_Manager *man;
        Evas_Object *mon;
        E_Smart_Data *sd;

        if ((man = e_manager_current_get()))
          e_pointer_type_pop(man->pointer, obj, "move");

        if (!(mon = data)) return;
        if (!(sd = evas_object_smart_data_get(mon))) return;

        /* set moving state */
        sd->moving = EINA_FALSE;

        /* send monitor moved signal */
        evas_object_smart_callback_call(mon, "monitor_moved", NULL);
     }
}

static void 
_e_smart_monitor_layout_cb_move(void *data, Evas *evas __UNUSED__, Evas_Object *obj __UNUSED__, void *event __UNUSED__)
{
   E_Smart_Data *sd;

   if (!(sd = data)) return;

   /* get the layout's geometry */
   evas_object_geometry_get(sd->layout.obj, 
                            &sd->layout.x, &sd->layout.y, NULL, NULL);
}