summaryrefslogtreecommitdiff
path: root/src/bin/edje/edje_player.c
blob: cacfdf297471bcb698a43cec55c6acf2eb95a874 (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
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>

#include <Evas.h>
#include <Ecore.h>
#include <Ecore_Getopt.h>
#include <Ecore_Evas.h>
#include <Edje.h>

struct opts
{
   char          *file;
   char          *group;
   Eina_Bool      list_groups;
   char          *engine;
   Eina_Rectangle size;
   unsigned char  color[3];
   Eina_Bool      borderless;
   Eina_Bool      sticky;
   Eina_Bool      shaped;
   Eina_Bool      alpha;
   Eina_Bool      print;
   Eina_Bool      slave_mode;
   double         scale;
   char          *title;
};

static Eina_Bool _edje_load_or_show_error(Evas_Object *edje, const char *file, const char *group);

static Ecore_Evas *win;

static void
_win_title_set(const char *group, const char *file)
{
   char buf[1024];
   snprintf(buf, sizeof(buf), "Edje_Player - %s of %s", group, file);
   ecore_evas_title_set(win, buf);
}

static char *
_slave_mode_tok(char **p_arg)
{
   char *s, *e;
   Eina_Bool is_quoted;

   if (!*p_arg) return NULL;

   s = *p_arg;
   while (isspace(*s))
     s++;

   if (*s == '\0')
     {
        *p_arg = NULL;
        return NULL;
     }
   else if (*s == '"')
     {
        is_quoted = EINA_TRUE;
        s++;
        *p_arg = s;
     }
   else
     {
        is_quoted = EINA_FALSE;
        *p_arg = s;
     }

   for (e = s; *e != '\0'; e++)
     {
        if ((!is_quoted) && (isspace(*e)))
          break;
        else if ((is_quoted) && (*e == '"'))
          break;
     }

   if (*e == '\0') return NULL;

   *e = '\0';
   return e + 1;
}

static void
_slave_mode_signal(Evas_Object *edje, char *args)
{
   char *emission, *source;

   emission = args;
   source = _slave_mode_tok(&emission);
   _slave_mode_tok(&source);

   if ((!emission) || (!source))
     {
        fputs("ERROR: Invalid command arguments.\n", stderr);
        return;
     }

   edje_object_signal_emit(edje, emission, source);
}

static void
_slave_mode_message_string(Evas_Object *edje, int id, char *arg)
{
   Edje_Message_String msg;
   msg.str = arg;
   edje_object_message_send(edje, EDJE_MESSAGE_STRING, id, &msg);
}

static void
_slave_mode_message_int(Evas_Object *edje, int id, char *arg)
{
   Edje_Message_Int msg;
   msg.val = atoi(arg);
   edje_object_message_send(edje, EDJE_MESSAGE_INT, id, &msg);
}

static void
_slave_mode_message_float(Evas_Object *edje, int id, char *arg)
{
   Edje_Message_Float msg;
   msg.val = atof(arg);
   edje_object_message_send(edje, EDJE_MESSAGE_FLOAT, id, &msg);
}

static void
_slave_mode_message_string_set(Evas_Object *edje, int id, char *arg,
                               char *extra_args)
{
   Edje_Message_String_Set *msg;
   int count, i;

   count = atoi(arg);
   msg = alloca(sizeof(Edje_Message_String_Set) + (count - 1) * sizeof(char *));

   for (i = 0; i < count; i++)
     {
        char *next = _slave_mode_tok(&extra_args);
        if (!extra_args)
          {
             fputs("ERROR: Message missing arg.\n", stderr);
             return;
          }
        msg->str[i] = extra_args;
        extra_args = next;
     }

   msg->count = count;
   edje_object_message_send(edje, EDJE_MESSAGE_STRING_SET, id, msg);
}

static void
_slave_mode_message_int_set(Evas_Object *edje, int id, char *arg,
                            char *extra_args)
{
   Edje_Message_Int_Set *msg;
   int count, i;

   count = atoi(arg);
   msg = alloca(sizeof(Edje_Message_Int_Set) + (count - 1) * sizeof(int));

   for (i = 0; i < count; i++)
     {
        char *next = _slave_mode_tok(&extra_args);
        if (!extra_args)
          {
             fputs("ERROR: Message missing arg.\n", stderr);
             return;
          }
        msg->val[i] = atoi(extra_args);
        extra_args = next;
     }

   msg->count = count;
   edje_object_message_send(edje, EDJE_MESSAGE_INT_SET, id, msg);
}

static void
_slave_mode_message_float_set(Evas_Object *edje, int id, char *arg,
                              char *extra_args)
{
   Edje_Message_Float_Set *msg;
   int count, i;

   count = atoi(arg);
   msg = alloca(sizeof(Edje_Message_Float_Set) + (count - 1) * sizeof(double));

   for (i = 0; i < count; i++)
     {
        char *next = _slave_mode_tok(&extra_args);
        if (!extra_args)
          {
             fputs("ERROR: Message missing arg.\n", stderr);
             return;
          }
        msg->val[i] = atof(extra_args);
        extra_args = next;
     }

   msg->count = count;
   edje_object_message_send(edje, EDJE_MESSAGE_FLOAT_SET, id, msg);
}

static void
_slave_mode_message_string_int(Evas_Object *edje, int id, char *arg,
                               char *extra_args)
{
   Edje_Message_String_Int msg;

   if (!extra_args)
     {
        fputs("ERROR: Message STRING_INT requires integer arg.\n", stderr);
        return;
     }
   _slave_mode_tok(&extra_args);

   msg.str = arg;
   msg.val = atoi(extra_args);

   edje_object_message_send(edje, EDJE_MESSAGE_STRING_INT, id, &msg);
}

static void
_slave_mode_message_string_float(Evas_Object *edje, int id, char *arg,
                                 char *extra_args)
{
   Edje_Message_String_Float msg;

   if (!extra_args)
     {
        fputs("ERROR: Message STRING_FLOAT requires float arg.\n", stderr);
        return;
     }
   _slave_mode_tok(&extra_args);

   msg.str = arg;
   msg.val = atof(extra_args);

   edje_object_message_send(edje, EDJE_MESSAGE_STRING_FLOAT, id, &msg);
}

static void
_slave_mode_message_string_int_set(Evas_Object *edje, int id, char *arg,
                                   char *extra_args)
{
   Edje_Message_String_Int_Set *msg;
   int count, i;
   char *val;

   if (!extra_args)
     {
        fputs("ERROR: Message STRING_INT_SET requires int args.\n", stderr);
        return;
     }

   val = _slave_mode_tok(&extra_args);
   count = atoi(extra_args);
   msg = alloca(sizeof(Edje_Message_String_Int_Set) +
                (count - 1) * sizeof(int));

   for (i = 0; i < count; i++)
     {
        char *next = _slave_mode_tok(&val);
        if (!val)
          {
             fputs("ERROR: Message missing arg.\n", stderr);
             return;
          }
        msg->val[i] = atoi(val);
        val = next;
     }

   msg->count = count;
   msg->str = arg;
   edje_object_message_send(edje, EDJE_MESSAGE_STRING_INT_SET, id, msg);
}

static void
_slave_mode_message_string_float_set(Evas_Object *edje, int id, char *arg,
                                     char *extra_args)
{
   Edje_Message_String_Float_Set *msg;
   int count, i;
   char *val;

   if (!extra_args)
     {
        fputs("ERROR: Message STRING_FLOAT_SET requires float set.\n", stderr);
        return;
     }

   val = _slave_mode_tok(&extra_args);
   count = atoi(extra_args);
   msg = alloca(sizeof(Edje_Message_String_Float_Set) +
                (count - 1) * sizeof(double));

   for (i = 0; i < count; i++)
     {
        char *next = _slave_mode_tok(&val);
        if (!val)
          {
             fputs("ERROR: Message missing arg.\n", stderr);
             return;
          }
        msg->val[i] = atof(val);
        val = next;
     }

   msg->count = count;
   msg->str = arg;
   edje_object_message_send(edje, EDJE_MESSAGE_STRING_FLOAT_SET, id, msg);
}

static void
_slave_mode_message(Evas_Object *edje, char *args)
{
   char *id_str, *type, *message_arg, *extra_args;
   int id;

   id_str = args;
   type = _slave_mode_tok(&id_str);
   message_arg = _slave_mode_tok(&type);
   extra_args = _slave_mode_tok(&message_arg);

   if (!id_str)
     {
        fputs("ERROR: Message id is required.\n", stderr);
        return;
     }

   id = atoi(id_str);

   if (!type)
     {
        fputs("ERROR: Message type is required.\n", stderr);
        return;
     }

   if (!message_arg)
     {
        fputs("ERROR: Missing message argument.\n", stderr);
        return;
     }

   if (!strcmp(type, "STRING"))
     {
        _slave_mode_message_string(edje, id, message_arg);
        return;
     }

   if (!strcmp(type, "INT"))
     {
        _slave_mode_message_int(edje, id, message_arg);
        return;
     }

   if (!strcmp(type, "FLOAT"))
     {
        _slave_mode_message_float(edje, id, message_arg);
        return;
     }

   if (!strcmp(type, "STRING_SET"))
     {
        _slave_mode_message_string_set(edje, id, message_arg, extra_args);
        return;
     }

   if (!strcmp(type, "INT_SET"))
     {
        _slave_mode_message_int_set(edje, id, message_arg, extra_args);
        return;
     }

   if (!strcmp(type, "FLOAT_SET"))
     {
        _slave_mode_message_float_set(edje, id, message_arg, extra_args);
        return;
     }

   if (!strcmp(type, "STRING_INT"))
     {
        _slave_mode_message_string_int(edje, id, message_arg, extra_args);
        return;
     }

   if (!strcmp(type, "STRING_FLOAT"))
     {
        _slave_mode_message_string_float(edje, id, message_arg, extra_args);
        return;
     }

   if (!strcmp(type, "STRING_INT_SET"))
     {
        _slave_mode_message_string_int_set(edje, id, message_arg, extra_args);
        return;
     }

   if (!strcmp(type, "STRING_FLOAT_SET"))
     {
        _slave_mode_message_string_float_set(edje, id, message_arg, extra_args);
        return;
     }

   fputs("ERROR: Invalid type. Check types list using \"help\".\n", stderr);
}

static void
_slave_mode_info(Evas_Object *edje, char *args)
{
   _slave_mode_tok(&args);

   if (!args)
     {
        fputs("ERROR: Invalid command arguments.\n", stderr);
        return;
     }

   if (!edje_object_part_exists(edje, args))
     {
        printf("INFO: \"%s\" does not exist.\n", args);
     }
   else
     {
        Evas_Coord x, y, w, h;
        edje_object_part_geometry_get(edje, args, &x, &y, &w, &h);
        printf("INFO: \"%s\" %d,%d,%d,%d\n", args, x, y, w, h);
     }
}

static void
_slave_mode_text(Evas_Object *edje, char *args)
{
   char *part, *text, *p;

   if (!args) return;
   p = strchr(args, ' ');
   if (!p) return;
   part = malloc(p - args + 1);
   strncpy(part, args, p - args);
   part[p - args] = 0;
   text = p + 1;
   edje_object_part_text_set(edje, part, text);
   free(part);
}

static void
_slave_mode_quit(Evas_Object *edje EINA_UNUSED, char *args EINA_UNUSED)
{
   puts("Bye!");
   ecore_main_loop_quit();
}

static void
_slave_mode_help(Evas_Object *edje EINA_UNUSED, char *args EINA_UNUSED)
{
   puts("Help:\n"
        "One command per line, arguments separated by space.\n"
        "Strings may have spaces if enclosed in quotes (\").\n"
        "\n"
        "\t<command> [arguments]\n"
        "\n"
        "Available commands:\n"
        "\tsignal <emission> <source>\n"
        "\t   sends a signal to edje\n"
        "\tmessage <id> <type> <args list>\n"
        "\t   sends a message to edje.\n"
        "\t   Possible types (and args):\n"
        "\t     * STRING \"string\"\n"
        "\t     * INT integer\n"
        "\t     * FLOAT float\n"
        "\t     * STRING_SET <set length> \"string1\" \"string2\" ...\n"
        "\t     * INT_SET <set length> integer1 integer2 integer3 ...\n"
        "\t     * FLOAT_SET <set length> float1 float2 float3 ...\n"
        "\t     * STRING_INT \"message\" integer\n"
        "\t     * STRING_FLOAT \"message\" float\n"
        "\t     * STRING_INT_SET \"string\" <set length> integer1 ...\n"
        "\t     * STRING_FLOAT_SET \"string\" <set length> float1 float2 ...\n"
        "\tinfo <part>\n"
        "\t   Print part geometry: <x>,<y>,<w>,<h>\n"
        "\ttext <part> <text string>\n"
        "\t   Set text of named part\n"
        "\tquit\n"
        "\t   exit edje player.\n"
        "\thelp\n"
        "\t   shows this message.\n");
   /*
    * Extension ideas (are they useful?):
    *  - data: show data value
    *  - color_class: set color class values (maybe also list?)
    *  - text_class: set text class values (maybe also list?)
    *  - play_set: change play state
    *  - animation_set: change animation state
    */
}

struct slave_cmd
{
   const char *cmd;
   void        (*func)(Evas_Object *edje, char *args);
} _slave_mode_commands[] = {
   {"signal", _slave_mode_signal},
   {"message", _slave_mode_message},
   {"info", _slave_mode_info},
   {"text", _slave_mode_text},
   {"quit", _slave_mode_quit},
   {"help", _slave_mode_help},
   {NULL, NULL}
};

#ifndef _WIN32
static Eina_Bool
_slave_mode(void *data, Ecore_Fd_Handler *fd_handler)
{
   Evas_Object *edje = data;
   char buf[1024], *p;
   const struct slave_cmd *itr;
   size_t len;

   if (ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_ERROR))
     {
        fputs("ERROR: error on stdin! Exit.\n", stderr);
        ecore_main_loop_quit();
        return ECORE_CALLBACK_CANCEL;
     }
   if (!ecore_main_fd_handler_active_get(fd_handler, ECORE_FD_READ))
     return ECORE_CALLBACK_RENEW;

   if (!fgets(buf, sizeof(buf), stdin))
     {
        fputs("ERROR: end of stdin! Exit.\n", stderr);
        ecore_main_loop_quit();
        return ECORE_CALLBACK_CANCEL;
     }

   len = strlen(buf);
   if (len < 1)
     {
        fputs("ERROR: no input! Try: help\n", stderr);
        return ECORE_CALLBACK_RENEW;
     }
   if (buf[len - 1] == '\n')
     {
        len--;
        buf[len] = '\0';
     }

   p = strchr(buf, ' ');
   if (p)
     {
        *p = '\0';
        p++;

        while (isspace(*p))
          p++;
        if (*p == '\0')
          p = NULL;

        if (p)
          {
             char *q = p + strlen(p) - 1;
             while (isspace(*q))
               {
                  *q = '\0';
                  q--;
               }
          }
     }

   for (itr = _slave_mode_commands; itr->cmd; itr++)
     {
        if (strcasecmp(itr->cmd, buf) == 0)
          {
             itr->func(edje, p);
             break;
          }
     }

   return ECORE_CALLBACK_RENEW;
}

#endif

static void
_print_signal(void *data EINA_UNUSED, Evas_Object *o EINA_UNUSED, const char *emission, const char *source)
{
   printf("SIGNAL: \"%s\" \"%s\"\n", emission, source);
}

static void
_print_message(void *data EINA_UNUSED, Evas_Object *edje EINA_UNUSED, Edje_Message_Type type, int id, void *msg)
{
   const char *typestr;
   char buf[64];

   switch (type)
     {
      case EDJE_MESSAGE_NONE:
        typestr = "NONE";
        break;

      case EDJE_MESSAGE_SIGNAL:
        typestr = "SIGNAL";
        break;

      case EDJE_MESSAGE_STRING:
        typestr = "STRING";
        break;

      case EDJE_MESSAGE_INT:
        typestr = "INT";
        break;

      case EDJE_MESSAGE_FLOAT:
        typestr = "FLOAT";
        break;

      case EDJE_MESSAGE_STRING_SET:
        typestr = "STRING_SET";
        break;

      case EDJE_MESSAGE_INT_SET:
        typestr = "INT_SET";
        break;

      case EDJE_MESSAGE_FLOAT_SET:
        typestr = "FLOAT_SET";
        break;

      case EDJE_MESSAGE_STRING_INT:
        typestr = "STRING_INT";
        break;

      case EDJE_MESSAGE_STRING_FLOAT:
        typestr = "STRING_FLOAT";
        break;

      case EDJE_MESSAGE_STRING_INT_SET:
        typestr = "STRING_INT_SET";
        break;

      case EDJE_MESSAGE_STRING_FLOAT_SET:
        typestr = "STRING_FLOAT_SET";
        break;

      default:
        snprintf(buf, sizeof(buf), "UNKNOWN(%d)", type);
        typestr = buf;
     }

   printf("MESSAGE: type=%s, id=%d", typestr, id);

   switch (type)
     {
      case EDJE_MESSAGE_NONE: break;

      case EDJE_MESSAGE_SIGNAL: break;

      case EDJE_MESSAGE_STRING:
      {
         Edje_Message_String *m = msg;
         printf(" \"%s\"", m->str);
      }
      break;

      case EDJE_MESSAGE_INT:
      {
         Edje_Message_Int *m = msg;
         printf(" %d", m->val);
      }
      break;

      case EDJE_MESSAGE_FLOAT:
      {
         Edje_Message_Float *m = msg;
         printf(" %f", m->val);
      }
      break;

      case EDJE_MESSAGE_STRING_SET:
      {
         Edje_Message_String_Set *m = msg;
         int i;
         for (i = 0; i < m->count; i++)
           printf(" \"%s\"", m->str[i]);
      }
      break;

      case EDJE_MESSAGE_INT_SET:
      {
         Edje_Message_Int_Set *m = msg;
         int i;
         for (i = 0; i < m->count; i++)
           printf(" %d", m->val[i]);
      }
      break;

      case EDJE_MESSAGE_FLOAT_SET:
      {
         Edje_Message_Float_Set *m = msg;
         int i;
         for (i = 0; i < m->count; i++)
           printf(" %f", m->val[i]);
      }
      break;

      case EDJE_MESSAGE_STRING_INT:
      {
         Edje_Message_String_Int *m = msg;
         printf(" \"%s\" %d", m->str, m->val);
      }
      break;

      case EDJE_MESSAGE_STRING_FLOAT:
      {
         Edje_Message_String_Float *m = msg;
         printf(" \"%s\" %f", m->str, m->val);
      }
      break;

      case EDJE_MESSAGE_STRING_INT_SET:
      {
         Edje_Message_String_Int_Set *m = msg;
         int i;
         printf(" \"%s\"", m->str);
         for (i = 0; i < m->count; i++)
           printf(" %d", m->val[i]);
      }
      break;

      case EDJE_MESSAGE_STRING_FLOAT_SET:
      {
         Edje_Message_String_Float_Set *m = msg;
         int i;
         printf(" \"%s\"", m->str);
         for (i = 0; i < m->count; i++)
           printf(" %f", m->val[i]);
      }
      break;

      default:
        break;
     }

   putchar('\n');
}

static void
_reset_size_hints(void *data, Evas *e EINA_UNUSED, Evas_Object *stack, void *event_info EINA_UNUSED)
{
   Evas_Coord minw, minh;
   Evas_Object *edje = data;

   edje_object_size_min_get(edje, &minw, &minh);
   if ((minw <= 0) && (minh <= 0))
     edje_object_size_min_calc(edje, &minw, &minh);

   evas_object_size_hint_min_set(stack, minw, minh);
}

static void
_key_down(void *data, Evas *e EINA_UNUSED, Evas_Object *stack EINA_UNUSED, void *event_info)
{
   Evas_Event_Key_Down *ev = event_info;
   struct opts *opts = data;

   if ((!strcmp(ev->keyname, "equal")) ||
       (!strcmp(ev->keyname, "plus")))
     opts->scale += 0.1;
   else if ((!strcmp(ev->keyname, "minus")) ||
            (!strcmp(ev->keyname, "underscore")))
     opts->scale -= 0.1;
   else if ((!strcmp(ev->keyname, "0")))
     opts->scale = 1.0;
   if (opts->scale < 0.1) opts->scale = 0.1;
   else if (opts->scale > 10.0)
     opts->scale = 1.0;
   edje_scale_set(opts->scale);
}

static Evas_Object *
_create_stack(Evas *evas, const struct opts *opts)
{
   Evas_Object *stack = evas_object_box_add(evas);
   if (!stack)
     {
        fputs("ERROR: could not create object stack (box).\n", stderr);
        return NULL;
     }
   evas_object_box_layout_set(stack, evas_object_box_layout_stack, NULL, NULL);
   evas_object_resize(stack, opts->size.w, opts->size.h);
   evas_object_size_hint_weight_set(stack, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
   evas_object_size_hint_align_set(stack, EVAS_HINT_FILL, EVAS_HINT_FILL);
   evas_object_show(stack);
   return stack;
}

static Evas_Object *
_create_bg(Evas *evas, const struct opts *opts)
{
   const unsigned char *color = opts->color;
   Evas_Object *bg = evas_object_rectangle_add(evas);
   if (!bg)
     {
        fputs("ERROR: could not create background.\n", stderr);
        return NULL;
     }
   evas_object_resize(bg, opts->size.w, opts->size.h);
   evas_object_color_set(bg, color[0], color[1], color[2], 255);
   evas_object_size_hint_weight_set(bg, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
   evas_object_size_hint_align_set(bg, EVAS_HINT_FILL, EVAS_HINT_FILL);
   evas_object_show(bg);
   return bg;
}

static void
_edje_reload(void *data EINA_UNUSED, Evas_Object *obj, const char *emission EINA_UNUSED, const char *source EINA_UNUSED)
{
   const char *file;
   const char *group;
   edje_object_signal_callback_del(obj, "edje,change,file", "edje", _edje_reload);

   edje_object_file_get(obj, &file, &group);
   _edje_load_or_show_error(obj, file, group);
}

static void
_edje_circul(void *data, Evas_Object *obj EINA_UNUSED, void *event_info)
{
   char buf[1024] = "";
   Eina_List *parts = event_info;
   Eina_List *l;
   char *part_name;
   char *group = data;

   part_name = eina_list_data_get(eina_list_last(parts));
   strncpy(buf, part_name, sizeof(buf) - 1);
   part_name[sizeof(buf) - 1] = 0;
   EINA_LIST_FOREACH(parts, l, part_name)
     {
        strncat(buf, " -> ", sizeof(buf) - strlen(buf) - 1);
        part_name[sizeof(buf) - 1] = 0;
        strncat(buf, part_name, sizeof(buf) - strlen(buf) - 1);
        part_name[sizeof(buf) - 1] = 0;
     }

   fprintf(stderr, "Group '%s' have a circul dependency between parts: %s\n",
           group, buf);
}

static Eina_Bool
_edje_load_or_show_error(Evas_Object *edje, const char *file, const char *group)
{
   Eina_File *f = NULL;
   const char *errmsg;
   int err;
   Evas_Canvas3D_Scene *scene = NULL;
   Evas_Canvas3D_Node *root_node = NULL;

   f = eina_file_open(file, EINA_FALSE);
   if (!(edje_mmap_3d_has(f, group)))
     {
        if (edje_object_file_set(edje, file, group))
          {
             edje_object_signal_callback_add(edje, "edje,change,file", "edje", _edje_reload, NULL);
             evas_object_focus_set(edje, EINA_TRUE);
             eina_file_close(f);
             return EINA_TRUE;
          }
     }
   else
     {
        if (edje_object_file_set(edje, file, group))
          {
             if (edje_3d_object_add(edje, &root_node, scene))
               {
                  edje_object_signal_callback_add(edje, "edje,change,file", "edje", _edje_reload, NULL);
                  evas_object_focus_set(edje, EINA_TRUE);
                  eina_file_close(f);
                  return EINA_TRUE;
               }
          }
     }

   err = edje_object_load_error_get(edje);
   errmsg = edje_load_error_str(err);
   eina_file_close(f);
   fprintf(stderr, "ERROR: could not load edje file '%s', group '%s': %s\n",
           file, group, errmsg);
   return EINA_FALSE;
}

static Evas_Object *
_create_edje(Evas *evas, const struct opts *opts)
{
   Evas_Coord minw, minh, maxw, maxh;
   Evas_Object *edje = edje_object_add(evas);
   if (!edje)
     {
        fputs("ERROR: could not create edje.\n", stderr);
        return NULL;
     }

   if (opts->group)
     {
        if (!_edje_load_or_show_error(edje, opts->file, opts->group))
          {
             evas_object_del(edje);
             return NULL;
          }
        if (!opts->title) _win_title_set(opts->group, opts->file);
     }
   else
     {
        if (edje_file_group_exists(opts->file, "main"))
          {
             if (!_edje_load_or_show_error(edje, opts->file, "main"))
               {
                  evas_object_del(edje);
                  return NULL;
               }
             if (!opts->title) _win_title_set("main", opts->file);
          }
        else
          {
             Eina_List *groups = edje_file_collection_list(opts->file);
             const char *group;
             if (!groups)
               {
                  fprintf(stderr, "ERROR: file '%s' has no groups!\n",
                          opts->file);
                  evas_object_del(edje);
                  return NULL;
               }
             group = groups->data;
             if (!_edje_load_or_show_error(edje, opts->file, group))
               {
                  edje_file_collection_list_free(groups);
                  evas_object_del(edje);
                  return NULL;
               }
             if (!opts->title) _win_title_set(group, opts->file);
             edje_file_collection_list_free(groups);
          }
     }
   evas_object_smart_callback_add(edje, "circular,dependency", _edje_circul, opts->group);

   edje_object_size_max_get(edje, &maxw, &maxh);
   edje_object_size_min_get(edje, &minw, &minh);
   if ((minw <= 0) && (minh <= 0))
     edje_object_size_min_calc(edje, &minw, &minh);

   evas_object_size_hint_max_set(edje, maxw, maxh);
   evas_object_size_hint_min_set(edje, minw, minh);

   evas_object_size_hint_weight_set(edje, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
   evas_object_size_hint_align_set(edje, EVAS_HINT_FILL, EVAS_HINT_FILL);
   evas_object_show(edje);

   return edje;
}

static unsigned char
_parse_color(EINA_UNUSED const Ecore_Getopt *parser, EINA_UNUSED const Ecore_Getopt_Desc *desc, const char *str, EINA_UNUSED void *data, Ecore_Getopt_Value *storage)
{
   unsigned char *color = (unsigned char *)storage->ptrp;

   if (sscanf(str, "%hhu,%hhu,%hhu", color, color + 1, color + 2) != 3)
     {
        fprintf(stderr, "ERROR: incorrect color value '%s'\n", str);
        return 0;
     }

   return 1;
}

static void
_cb_delete(EINA_UNUSED Ecore_Evas *ee)
{
   ecore_main_loop_quit();
}

const Ecore_Getopt optdesc = {
   "edje_player",
   "%prog [options] <filename.edj>",
   PACKAGE_VERSION,
   "(C) 2010 Enlightenment",
   "BSD with advertisement clause",
   "Simple application to view edje files.",
   0,
   {
      ECORE_GETOPT_STORE_STR
        ('g', "group", "The edje group to view (defaults to 'main')."),
      ECORE_GETOPT_STORE_TRUE
        ('G', "list-groups", "The groups in the given file."),
      ECORE_GETOPT_STORE_STR
        ('e', "engine", "The Ecore-Evas engine to use (see --list-engines)"),
      ECORE_GETOPT_CALLBACK_NOARGS
        ('E', "list-engines", "list Ecore-Evas engines",
        ecore_getopt_callback_ecore_evas_list_engines, NULL),
      ECORE_GETOPT_CALLBACK_ARGS
        ('Z', "size", "size to use in wxh form.", "WxH",
        ecore_getopt_callback_size_parse, NULL),
      ECORE_GETOPT_CALLBACK_ARGS
        ('c', "bg-color", "Color of the background (if not shaped or alpha)",
        "RRGGBB", _parse_color, NULL),
      ECORE_GETOPT_STORE_TRUE
        ('b', "borderless", "Display window without border."),
      ECORE_GETOPT_STORE_TRUE
        ('y', "sticky", "Display window sticky."),
      ECORE_GETOPT_STORE_TRUE
        ('s', "shaped", "Display window shaped."),
      ECORE_GETOPT_STORE_TRUE
        ('a', "alpha", "Display window with alpha channel "
                       "(needs composite manager!)"),
      ECORE_GETOPT_STORE_STR
        ('t', "title", "Define the window title string"),
      ECORE_GETOPT_STORE_TRUE
        ('p', "print", "Print signals and messages to stdout"),
      ECORE_GETOPT_STORE_TRUE
        ('S', "slave-mode", "Listen for commands on stdin"),
      ECORE_GETOPT_STORE_DOUBLE
        ('z', "scale", "Set scale factor"),
      ECORE_GETOPT_LICENSE('L', "license"),
      ECORE_GETOPT_COPYRIGHT('C', "copyright"),
      ECORE_GETOPT_VERSION('V', "version"),
      ECORE_GETOPT_HELP('h', "help"),
      ECORE_GETOPT_SENTINEL
   }
};

int
main(int argc, char **argv)
{
   Evas *evas;
   Evas_Object *stack, *edje;
   struct opts opts;
   Eina_Bool quit_option = EINA_FALSE;
   int args;
   Eina_List *groups;
   Eina_File *f = NULL;
   const char *group;
   Ecore_Getopt_Value values[] = {
      ECORE_GETOPT_VALUE_STR(opts.group),
      ECORE_GETOPT_VALUE_BOOL(opts.list_groups),
      ECORE_GETOPT_VALUE_STR(opts.engine),
      ECORE_GETOPT_VALUE_BOOL(quit_option),
      ECORE_GETOPT_VALUE_PTR_CAST(opts.size),
      ECORE_GETOPT_VALUE_PTR_CAST(opts.color),
      ECORE_GETOPT_VALUE_BOOL(opts.borderless),
      ECORE_GETOPT_VALUE_BOOL(opts.sticky),
      ECORE_GETOPT_VALUE_BOOL(opts.shaped),
      ECORE_GETOPT_VALUE_BOOL(opts.alpha),
      ECORE_GETOPT_VALUE_STR(opts.title),
      ECORE_GETOPT_VALUE_BOOL(opts.print),
      ECORE_GETOPT_VALUE_BOOL(opts.slave_mode),
      ECORE_GETOPT_VALUE_DOUBLE(opts.scale),
      ECORE_GETOPT_VALUE_BOOL(quit_option),
      ECORE_GETOPT_VALUE_BOOL(quit_option),
      ECORE_GETOPT_VALUE_BOOL(quit_option),
      ECORE_GETOPT_VALUE_BOOL(quit_option),
      ECORE_GETOPT_VALUE_NONE
   };

   memset(&opts, 0, sizeof(opts));
   opts.scale = 1.0;

   if (!ecore_evas_init())
     return EXIT_FAILURE;
   if (!edje_init())
     goto shutdown_ecore_evas;
   edje_frametime_set(1.0 / 60.0);

   args = ecore_getopt_parse(&optdesc, values, argc, argv);
   if (args < 0)
     {
        fputs("Could not parse arguments.\n", stderr);
        goto shutdown_edje;
     }
   else if (quit_option)
     {
        goto end;
     }
   else if (args >= argc)
     {
        fputs("Missing edje file to load.\n", stderr);
        goto shutdown_edje;
     }

   ecore_app_args_set(argc, (const char **)argv);
   edje_scale_set(opts.scale);

   // check if the given edj file is there
   if (access(argv[args], R_OK) == -1)
     {
        int e = errno;
        fprintf(stderr, "ERROR: file '%s' not accessible, error %d (%s).\n",
                argv[args], e, strerror(e));
        goto end;
     }

   opts.file = argv[args];
   groups = edje_file_collection_list(opts.file);
   if (opts.list_groups)
     {
        Eina_List *n;
        printf("%d groups in file '%s':\n", eina_list_count(groups), opts.file);
        EINA_LIST_FOREACH(groups, n, group)
          printf("\t'%s'\n", group);
        edje_file_collection_list_free(groups);
        goto end;
     }

   group = eina_list_data_get(eina_list_last(groups));

   f = eina_file_open(opts.file, EINA_FALSE);
   if (edje_mmap_3d_has(f, group))
     {
        Eina_List *engine_list, *n;
        Eina_Bool opengl_x11_has = EINA_FALSE, wayland_egl_has = EINA_FALSE;
        const char *engine;
        engine_list = ecore_evas_engines_get();
        EINA_LIST_FOREACH(engine_list, n, engine)
          {
             if (!strcmp(engine, "wayland_egl"))
               {
                  wayland_egl_has = EINA_TRUE;
                  break;
               }
             else if (!strcmp(engine, "opengl_x11"))
               opengl_x11_has = EINA_TRUE;
          }
        if (wayland_egl_has)
          opts.engine = "wayland_egl";
        else if (opengl_x11_has)
          opts.engine = "opengl_x11";
     }
   eina_file_close(f);
   edje_file_collection_list_free(groups);

   if (opts.size.w <= 0) opts.size.w = 320;
   if (opts.size.h <= 0) opts.size.h = 240;
   win = ecore_evas_new(opts.engine, 0, 0, opts.size.w, opts.size.h, NULL);
   if (!win)
     {
        fprintf(stderr,
                "ERROR: could not create window of "
                "size %dx%d using engine %s.\n",
                opts.size.w, opts.size.h, opts.engine ? opts.engine : "(auto)");
        goto shutdown_edje;
     }

   ecore_evas_callback_delete_request_set(win, _cb_delete);
   evas = ecore_evas_get(win);
   stack = _create_stack(evas, &opts);
   if (!stack)
     {
        goto free_ecore_evas;
     }

   ecore_evas_object_associate(win, stack, ECORE_EVAS_OBJECT_ASSOCIATE_BASE);

   if (opts.alpha)
     ecore_evas_alpha_set(win, EINA_TRUE);
   else if (opts.shaped)
     ecore_evas_shaped_set(win, EINA_TRUE);
   else
     {
        Evas_Object *bg = _create_bg(evas, &opts);
        if (bg) evas_object_box_append(stack, bg);
     }

   edje = _create_edje(evas, &opts);
   if (edje)
     evas_object_box_append(stack, edje);
   else
     {
        goto free_ecore_evas;
     }

   evas_object_focus_set(stack, EINA_TRUE);
   evas_object_event_callback_add(stack, EVAS_CALLBACK_KEY_DOWN,
                                  _key_down, &opts);
   evas_object_event_callback_add(stack, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
                                  _reset_size_hints, edje);

   if (opts.print)
     {
        edje_object_signal_callback_add(edje, "*", "*", _print_signal, NULL);
        edje_object_message_handler_set(edje, _print_message, NULL);
     }

   if (opts.slave_mode)
     {
#ifndef _WIN32
        int flags;
        flags = fcntl(STDIN_FILENO, F_GETFL, 0);
        flags |= O_NONBLOCK;
        if (fcntl(STDIN_FILENO, F_SETFL, flags) < 0)
          {
             fprintf(stderr, "ERROR: Could not set stdin to non-block: %s\n",
                     strerror(errno));
             goto free_ecore_evas;
          }
        ecore_main_fd_handler_add(STDIN_FILENO, ECORE_FD_READ | ECORE_FD_ERROR,
                                  _slave_mode, edje, NULL, NULL);
#else
        /* TODO: port the code above to Windows */
        fprintf(stderr, "ERROR: slave mode not working on Windows\n");
        goto free_ecore_evas;
#endif
     }

   ecore_evas_borderless_set(win, opts.borderless);
   ecore_evas_sticky_set(win, opts.sticky);
   if (opts.title)
     ecore_evas_title_set(win, opts.title);

   ecore_evas_show(win);
   ecore_main_loop_begin();

   ecore_evas_free(win);
end:
   edje_shutdown();
   ecore_evas_shutdown();

   return 0;

free_ecore_evas:
   ecore_evas_free(win);
shutdown_edje:
   edje_shutdown();
shutdown_ecore_evas:
   ecore_evas_shutdown();
   return EXIT_FAILURE;
}