summaryrefslogtreecommitdiff
path: root/legacy/emotion/src/modules/emotion_xine.c
blob: c5328a6944454d898ef9048117a81a96f1fa4224 (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
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
#include "Emotion.h"
#include "emotion_private.h"
#include "emotion_xine.h"

static unsigned char em_init(Evas_Object *obj, void **emotion_video);
static int em_shutdown(void *video);
static unsigned char em_file_open(const char *file, Evas_Object *obj, void *video);
static void em_file_close(void *ef);
static void em_play(void *ef, double pos);
static void em_stop(void *ef);
static void em_size_get(void *ef, int *w, int *h);
static void em_pos_set(void *ef, double pos);
static void em_vis_set(void  *video, Emotion_Vis vis);
static double em_len_get(void *ef);
static int em_fps_num_get(void *ef);
static int em_fps_den_get(void *ef);
static double em_fps_get(void *ef);
static double em_pos_get(void *ef);
static Emotion_Vis em_vis_get(void *video);
static double em_ratio_get(void *ef);
static int em_seekable(void *ef);
static void em_frame_done(void *ef);
static Emotion_Format em_format_get(void *ef);
static void em_video_data_size_get(void *ef, int *w, int *h);
static int em_yuv_rows_get(void *ef, int w, int h, unsigned char **yrows, unsigned char **urows, unsigned char **vrows);
static int em_bgra_data_get(void *ef, unsigned char **bgra_data);
static void em_event_feed(void *ef, int event);
static void em_event_mouse_button_feed(void *ef, int button, int x, int y);
static void em_event_mouse_move_feed(void *ef, int x, int y);
static int em_video_channel_count(void *ef);
static void em_video_channel_set(void *ef, int channel);
static int em_video_channel_get(void *ef);
static const char *em_video_channel_name_get(void *ef, int channel);
static void em_video_channel_mute_set(void *ef, int mute);
static int em_video_channel_mute_get(void *ef);
static int em_audio_channel_count(void *ef);
static void em_audio_channel_set(void *ef, int channel);
static int em_audio_channel_get(void *ef);
static const char *em_audio_channel_name_get(void *ef, int channel);
static void em_audio_channel_mute_set(void *ef, int mute);
static int em_audio_channel_mute_get(void *ef);
static void em_audio_channel_volume_set(void *ef, double vol);
static double em_audio_channel_volume_get(void *ef);
static int em_spu_channel_count(void *ef);
static void em_spu_channel_set(void *ef, int channel);
static int em_spu_channel_get(void *ef);
static const char *em_spu_channel_name_get(void *ef, int channel);
static void em_spu_channel_mute_set(void *ef, int mute);
static int em_spu_channel_mute_get(void *ef);
static int em_chapter_count(void *ef);
static void em_chapter_set(void *ef, int chapter);
static int em_chapter_get(void *ef);
static const char *em_chapter_name_get(void *ef, int chapter);
static void em_speed_set(void *ef, double speed);
static double em_speed_get(void *ef);
static int em_eject(void *ef);
static const char *em_meta_get(Emotion_Xine_Video *ev, int meta);

static void *_em_seek        (void *par);
static int   _em_fd_active   (void *data, Ecore_Fd_Handler *fdh);
static void  _em_event       (void *data, const xine_event_t *event);
static int   _em_fd_ev_active(void *data, Ecore_Fd_Handler *fdh);
static int   _em_timer       (void *data);
static void *_em_get_pos_len_th(void *par);
static void  _em_get_pos_len (Emotion_Xine_Video *ev);

extern plugin_info_t emotion_xine_plugin_info[];
  
static unsigned char
em_init(Evas_Object *obj, void **emotion_video)
{
   Emotion_Xine_Video *ev;
	int fds[2];

   if (!emotion_video)
      return 0;
   
   ev = calloc(1, sizeof(Emotion_Xine_Video));
   if (!ev) return 0;
   ev->obj = obj;

   ev->decoder = xine_new();
   if (!ev->decoder)
     {
	free(ev);
	return 0;
     }
   xine_init(ev->decoder);
   xine_register_plugins(ev->decoder, emotion_xine_plugin_info);
   if (1)
     {
	xine_cfg_entry_t cf;
	if (xine_config_lookup_entry(ev->decoder, "input.dvd_use_readahead", &cf))
	  {
	     cf.num_value = 1; // 0 or 1
	     xine_config_update_entry(ev->decoder, &cf);
	  }
     }
   /* some notes on parameters we could swizzle for certain inputs */
   if (0)
     {
	xine_cfg_entry_t cf;
	
	if (xine_config_lookup_entry(ev->decoder, "video.num_buffers", &cf))
	  {
	     cf.num_value = 1;
	     xine_config_update_entry(ev->decoder, &cf);
	  }
	if (xine_config_lookup_entry(ev->decoder, "input.dvd_device", &cf))
	  {
	     cf.str_value = "/dev/dvd";
	     xine_config_update_entry(ev->decoder, &cf);
	  }
	if (xine_config_lookup_entry(ev->decoder, "input.css_decryption_method", &cf))
	  {
	     cf.str_value = "key"; // "key" "disk" "title"
	     xine_config_update_entry(ev->decoder, &cf);
	  }
	if (xine_config_lookup_entry(ev->decoder, "input.dvd_region", &cf))
	  {
	     cf.num_value = 0; // 0 ... 1 - 8
	     xine_config_update_entry(ev->decoder, &cf);
	  }
	if (xine_config_lookup_entry(ev->decoder, "input.dvd_use_readahead", &cf))
	  {
	     cf.num_value = 1; // 0 or 1
	     xine_config_update_entry(ev->decoder, &cf);
	  }
	// these are used any time in runtime - so changing affects all dvd's
	if (xine_config_lookup_entry(ev->decoder, "input.dvd_skip_behaviour", &cf))
	  {
	     cf.str_value = "skip program"; // "skip program" "skip part" "skip title"
	     xine_config_update_entry(ev->decoder, &cf);
	  }
	// these are used any time in runtime - so changing affects all dvd's
	if (xine_config_lookup_entry(ev->decoder, "input.dvd_seek_behaviour", &cf))
	  {
	     cf.str_value = "seek in program chain"; // "seek in program chain" "seek in program"
	     xine_config_update_entry(ev->decoder, &cf);
	  }
	if (xine_config_lookup_entry(ev->decoder, "input.v4l_video_device_path", &cf))
	  {
	     cf.str_value = "/dev/video0";
	     xine_config_update_entry(ev->decoder, &cf);
	  }
	if (xine_config_lookup_entry(ev->decoder, "input.cdda_use_cddb", &cf))
	  {
	     cf.num_value = 0; // 0 or 1
	     xine_config_update_entry(ev->decoder, &cf);
	  }
	if (xine_config_lookup_entry(ev->decoder, "input.cdda_device", &cf))
	  {
	     cf.str_value = "/dev/cdrom";
	     xine_config_update_entry(ev->decoder, &cf);
	  }
	if (xine_config_lookup_entry(ev->decoder, "audio.oss_device_name", &cf))
	  {
	     cf.str_value = "/dev/dsp";
	     xine_config_update_entry(ev->decoder, &cf);
	  }
	if (xine_config_lookup_entry(ev->decoder, "audio.oss_device_number", &cf))
	  {
	     cf.num_value = -1; // -1 or 0 1 2 ...
	     xine_config_update_entry(ev->decoder, &cf);
	  }
	if (xine_config_lookup_entry(ev->decoder, "audio.alsa_mmap_enable", &cf))
	  {
	     cf.num_value = 1; // 0 or 1
	     xine_config_update_entry(ev->decoder, &cf);
	  }
	if (xine_config_lookup_entry(ev->decoder, "codec.a52_surround_downmix", &cf))
	  {
	     cf.num_value = 1; // 0 or 1
	     xine_config_update_entry(ev->decoder, &cf);
	  }
	if (xine_config_lookup_entry(ev->decoder, "vcd.default_device", &cf))
	  {
	     cf.str_value = "/dev/cdrom";
	     xine_config_update_entry(ev->decoder, &cf);
	  }
     }
   if (0)
     {
	xine_mrl_t **mrls;
	int mrls_num;
	
	mrls = xine_get_browse_mrls(ev->decoder, "dvd", "dvd://", &mrls_num);
	printf("mrls = %p\n", mrls);
	if (mrls)
	  {
	     int i;
	     
	     for (i = 0; i < mrls_num; i++)
	       {
		  printf("MRL: origin \"%s\" mrl \"%s\" link \"%s\" type %x size %i\n", 
			 mrls[i]->origin, mrls[i]->mrl, mrls[i]->link,
			 (int)mrls[i]->type, (int)mrls[i]->size);
	       }
	  }
     }
   if (0)
     {
	char **auto_play_mrls;
	int auto_play_num;
	
	auto_play_mrls = xine_get_autoplay_mrls(ev->decoder, "dvd", &auto_play_num);
	printf("auto_play_mrls = %p\n", auto_play_mrls);
	if (auto_play_mrls)
	  {
	     int i;
	     
	     for (i = 0; i < auto_play_num; i++)
	       printf("MRL: %s\n", auto_play_mrls[i]);
	  }
     }
   
   if (pipe(fds) == 0)
     {
	ev->fd_read = fds[0];
	ev->fd_write = fds[1];
	fcntl(ev->fd_read, F_SETFL, O_NONBLOCK);
	ev->fd_handler = ecore_main_fd_handler_add(ev->fd_read,
						   ECORE_FD_READ, _em_fd_active, ev, NULL, NULL);
	ecore_main_fd_handler_active_set(ev->fd_handler, ECORE_FD_READ);
     }
   
   if (pipe(fds) == 0)
     {
	ev->fd_ev_read = fds[0];
	ev->fd_ev_write = fds[1];
	fcntl(ev->fd_ev_read, F_SETFL, O_NONBLOCK);
	ev->fd_ev_handler = ecore_main_fd_handler_add(ev->fd_ev_read,
						      ECORE_FD_READ, _em_fd_ev_active, ev, NULL, NULL);
	ecore_main_fd_handler_active_set(ev->fd_ev_handler, ECORE_FD_READ);
     }
   ev->fd = ev->fd_write;

   printf("OPEN VIDEO PLUGIN...\n");
   ev->video = xine_open_video_driver(ev->decoder, "emotion",
				      XINE_VISUAL_TYPE_NONE, ev);
   printf("RESULT: xine_open_video_driver() = %p\n", ev->video);
   // Let xine autodetect the best audio output driver
   ev->audio = xine_open_audio_driver(ev->decoder, NULL, ev);
//   ev->audio = xine_open_audio_driver(ev->decoder, "oss", ev);
// dont use alsa - alsa has oss emulation.   
//   ev->audio = xine_open_audio_driver(ev->decoder, "alsa", ev);
//   ev->audio = xine_open_audio_driver(ev->decoder, "arts", ev);
//   ev->audio = xine_open_audio_driver(ev->decoder, "esd", ev);
   ev->stream = xine_stream_new(ev->decoder, ev->audio, ev->video);
   ev->queue = xine_event_new_queue(ev->stream);
   xine_event_create_listener_thread(ev->queue, _em_event, ev);

   ev->delete_me = 0;
   ev->get_pos_thread_deleted = 0;
   ev->seek_thread_deleted = 0;
   pthread_cond_init(&(ev->seek_cond), NULL);
   pthread_cond_init(&(ev->get_pos_len_cond), NULL);
   pthread_mutex_init(&(ev->seek_mutex), NULL);
   pthread_mutex_init(&(ev->get_pos_len_mutex), NULL);
   pthread_create(&ev->seek_th, NULL, _em_seek, ev);
   pthread_create(&ev->get_pos_len_th, NULL, _em_get_pos_len_th, ev);

   *emotion_video = ev;
   
   return 1;
}

static int
em_shutdown(void *video)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)video;
   
   ev->delete_me = 1;
//   pthread_mutex_lock(&(ev->seek_mutex));
   if (!ev->seek_thread_deleted)
     {
	printf("closing seek thread\n");
	pthread_cond_broadcast(&(ev->seek_cond));
	while (ev->seek_to);
     }

//   pthread_mutex_lock(&(ev->get_pos_len_mutex));
   if (!ev->get_pos_thread_deleted)
     {
	printf("closing get_pos thread\n");
	pthread_cond_broadcast(&(ev->get_pos_len_cond));
	while (ev->get_poslen);
     }

   printf("EX dispose\n");
   xine_dispose(ev->stream);
   printf("EX dispose evq\n");
   xine_event_dispose_queue(ev->queue);
   printf("EX close video drv\n");
   if (ev->video) xine_close_video_driver(ev->decoder, ev->video);
   printf("EX close audio drv\n");
   if (ev->audio) xine_close_audio_driver(ev->decoder, ev->audio);
   printf("EX del fds\n");
   ecore_main_fd_handler_del(ev->fd_handler);
   close(ev->fd_write);
   close(ev->fd_read);
   ecore_main_fd_handler_del(ev->fd_ev_handler);
   close(ev->fd_ev_write);
   close(ev->fd_ev_read);
   xine_exit(ev->decoder);
   free(ev);
   return 1;
}

static unsigned char
em_file_open(const char *file, Evas_Object *obj, void *video)
{
   Emotion_Xine_Video *ev = (Emotion_Xine_Video *)video;
   int pos_stream = 0;
   int pos_time = 0;
   int length_time = 0;
   uint32_t v;
   
   if (!ev)
     return 0;
	 
   if (!xine_open(ev->stream, file))
     return 0;
   if (xine_get_pos_length(ev->stream, &pos_stream, &pos_time, &length_time))
     {
	if (length_time == 0)
	  {
	     ev->pos = (double)pos_stream / 65535;
	     ev->len = 1.0;
	     ev->no_time = 1;
	  }
	else
	  {
	     ev->pos = 0.0;
	     ev->len = (double)length_time / 1000.0;
	  }
     }
   else
     {
	ev->pos = 0.0;
	ev->len = 1.0;
     }
   v = xine_get_stream_info(ev->stream, XINE_STREAM_INFO_FRAME_DURATION);
   if (v > 0) ev->fps = 90000.0 / (double)v;
   v = xine_get_stream_info(ev->stream, XINE_STREAM_INFO_VIDEO_WIDTH);
   ev->w = v;
   v = xine_get_stream_info(ev->stream, XINE_STREAM_INFO_VIDEO_HEIGHT);
   ev->h = v;
   v = xine_get_stream_info(ev->stream, XINE_STREAM_INFO_VIDEO_RATIO);
   ev->ratio = (double)v / 10000.0;
   ev->just_loaded = 1;
   ev->get_poslen = 0;
   ev->seek_to = 0;
   
//   em_debug(ev);
   return 1;
}

static void
em_file_close(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   if (!ev) return;
   printf("EX pause end...\n");
   if (!emotion_object_play_get(ev->obj))
//   if (xine_get_param(ev->stream, XINE_PARAM_SPEED) == XINE_SPEED_PAUSE)
     {
	printf("  ... unpause\n");
	xine_set_param(ev->stream, XINE_PARAM_SPEED, XINE_SPEED_NORMAL);
     }
   printf("EX stop\n");
   xine_stop(ev->stream);
   printf("EX close\n");
   xine_close(ev->stream);
   printf("EX del timer\n");
   if (ev->timer)
     {
	ecore_timer_del(ev->timer);
	ev->timer = NULL;
     }
}

static void
em_play(void *ef, double pos)
{
   Emotion_Xine_Video *ev;
   int pos_stream = 0;
   int pos_time = 0;
   int length_time = 0;
   
   ev = (Emotion_Xine_Video *)ef;
   ev->play = 1;
   if ((xine_get_param(ev->stream, XINE_PARAM_SPEED) == XINE_SPEED_PAUSE) &&
       (pos == ev->pos) &&
       (!ev->just_loaded))
     {
	xine_set_param(ev->stream, XINE_PARAM_SPEED, XINE_SPEED_NORMAL);
     }
   else
     {
	ev->seek_to_pos = -0.1;
	em_pos_set(ef, pos);
     }
   ev->just_loaded = 0;
   
   if (xine_get_pos_length(ev->stream,
			   &pos_stream,
			   &pos_time,
			   &length_time))
     {
	if (length_time == 0)
	  {
	     ev->pos = (double)pos_stream / 65535;
	     ev->len = 1.0;
	     ev->no_time = 1;
	  }
	else
	  {
	     ev->pos = (double)pos_time / 1000.0;
	     ev->len = (double)length_time / 1000.0;
	  }
     }
   
   if ((xine_get_stream_info(ev->stream, XINE_STREAM_INFO_HAS_VIDEO)) &&
       (xine_get_stream_info(ev->stream, XINE_STREAM_INFO_VIDEO_HANDLED)))
     _emotion_frame_new(ev->obj);
   _emotion_video_pos_update(ev->obj, ev->pos, ev->len);
   if ((!xine_get_stream_info(ev->stream, XINE_STREAM_INFO_HAS_VIDEO)) ||
       (!xine_get_stream_info(ev->stream, XINE_STREAM_INFO_VIDEO_HANDLED)))
     {
	if (!ev->timer)
	  ev->timer = ecore_timer_add(1.0 / 30.0, _em_timer, ev);
     }
}

static void
em_stop(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   ev->play = 0;
   xine_set_param(ev->stream, XINE_PARAM_SPEED, XINE_SPEED_PAUSE);
   if (ev->timer)
     {
	ecore_timer_del(ev->timer);
	ev->timer = NULL;
     }
}

static void 
em_size_get(void *ef, int *w, int *h)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   if (w) *w = ev->w;
   if (h) *h = ev->h;
}

static void
em_pos_set(void *ef, double pos)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;

   if (ev->seek_to_pos == pos) return;
//   if (xine_get_stream_info(ev->stream, XINE_STREAM_INFO_SEEKABLE))
     {
	ev->seek_to_pos = pos;
	ev->seek_to = 1;
	pthread_cond_broadcast(&(ev->seek_cond));
     }
}

static void
em_vis_set(void       *ef,
	   Emotion_Vis vis)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;

   if (ev->vis == vis) return;
   ev->vis = vis;
}

static double
em_len_get(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   return ev->len;
}

static int
em_fps_num_get(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   return (int)(ev->fps * 10000.0);
}

static int
em_fps_den_get(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   return 10000;
}

static double
em_fps_get(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   return ev->fps;
}

static double
em_pos_get(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   return ev->pos;
}

static Emotion_Vis
em_vis_get(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;

   return ev->vis;
}

static double
em_ratio_get(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   return ev->ratio;
}

static int
em_video_handled(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   return (xine_get_stream_info(ev->stream, XINE_STREAM_INFO_HAS_VIDEO) &&
      xine_get_stream_info(ev->stream, XINE_STREAM_INFO_VIDEO_HANDLED));
}

static int
em_audio_handled(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   return (xine_get_stream_info(ev->stream, XINE_STREAM_INFO_HAS_AUDIO) &&
      xine_get_stream_info(ev->stream, XINE_STREAM_INFO_AUDIO_HANDLED));
}

static int
em_seekable(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   return xine_get_stream_info(ev->stream, XINE_STREAM_INFO_SEEKABLE);
}

static void
em_frame_done(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   if (ev->cur_frame)
     {
	if (ev->cur_frame->done_func)
	  ev->cur_frame->done_func(ev->cur_frame->done_data);
	ev->cur_frame = NULL;
     }
}

static Emotion_Format em_format_get(void *ef)
{
   Emotion_Xine_Video *ev;
   Emotion_Xine_Video_Frame *fr;
   
   ev = (Emotion_Xine_Video *)ef;
   fr = ev->cur_frame;

   if (fr)
     return fr->format;
   return EMOTION_FORMAT_YV12;
}

static void
em_video_data_size_get(void *ef, int *w, int *h)
{
   Emotion_Xine_Video *ev;
   Emotion_Xine_Video_Frame *fr;
   
   ev = (Emotion_Xine_Video *)ef;
   fr = ev->cur_frame;
   if (!fr)
     {
	if (w) *w = 0;
	if (h) *h = 0;
	return;
     }
   if (w) *w = fr->w;
   if (h) *h = fr->h;
}

static int
em_yuv_rows_get(void *ef, int w, int h, unsigned char **yrows, unsigned char **urows, unsigned char **vrows)
{
   Emotion_Xine_Video *ev;
   Emotion_Xine_Video_Frame *fr;
   
   ev = (Emotion_Xine_Video *)ef;
   fr = ev->cur_frame;
   if (!fr) return 0;
   if (fr->y)
     {
	int i;
	
	for (i = 0; i < h; i++) yrows[i] = fr->y + (i * fr->y_stride);
	for (i = 0; i < (h / 2); i++) urows[i] = fr->u + (i * fr->u_stride);
	for (i = 0; i < (h / 2); i++) vrows[i] = fr->v + (i * fr->v_stride);
	return 1;
     }
   return 0;
}

static int
em_bgra_data_get(void *ef, unsigned char **bgra_data)
{
   Emotion_Xine_Video *ev;
   Emotion_Xine_Video_Frame *fr;
   
   ev = (Emotion_Xine_Video *)ef;
   fr = ev->cur_frame;
   if (!fr) return 0;
   if (fr->bgra_data)
     {
   *bgra_data = fr->bgra_data;
	return 1;
     }
   return 0;
}

static void
em_event_feed(void *ef, int event)
{
   Emotion_Xine_Video *ev;
   xine_event_t xine_event;

   ev = (Emotion_Xine_Video *)ef;
   xine_event.data_length = 0;
   xine_event.data        = NULL;
   xine_event.stream      = ev->stream;
   gettimeofday(&xine_event.tv, NULL);
   switch (event)
     {
      case EMOTION_EVENT_MENU1:
	xine_event.type = XINE_EVENT_INPUT_MENU1;
	break;
      case EMOTION_EVENT_MENU2:
	xine_event.type = XINE_EVENT_INPUT_MENU2;
	break;
      case EMOTION_EVENT_MENU3:
	xine_event.type = XINE_EVENT_INPUT_MENU3;
	break;
      case EMOTION_EVENT_MENU4:
	xine_event.type = XINE_EVENT_INPUT_MENU4;
	break;
      case EMOTION_EVENT_MENU5:
	xine_event.type = XINE_EVENT_INPUT_MENU5;
	break;
      case EMOTION_EVENT_MENU6:
	xine_event.type = XINE_EVENT_INPUT_MENU6;
	break;
      case EMOTION_EVENT_MENU7:
	xine_event.type = XINE_EVENT_INPUT_MENU7;
	break;
      case EMOTION_EVENT_UP:
	xine_event.type = XINE_EVENT_INPUT_UP;
	break;
      case EMOTION_EVENT_DOWN:
	xine_event.type = XINE_EVENT_INPUT_DOWN;
	break;
      case EMOTION_EVENT_LEFT:
	xine_event.type = XINE_EVENT_INPUT_LEFT;
	break;
      case EMOTION_EVENT_RIGHT:
	xine_event.type = XINE_EVENT_INPUT_RIGHT;
	break;
      case EMOTION_EVENT_SELECT:
	xine_event.type = XINE_EVENT_INPUT_SELECT;
	break;
      case EMOTION_EVENT_NEXT:
	xine_event.type = XINE_EVENT_INPUT_NEXT;
	break;
      case EMOTION_EVENT_PREV:
	xine_event.type = XINE_EVENT_INPUT_PREVIOUS;
	break;
      case EMOTION_EVENT_ANGLE_NEXT:
	xine_event.type = XINE_EVENT_INPUT_ANGLE_NEXT;
	break;
      case EMOTION_EVENT_ANGLE_PREV:
	xine_event.type = XINE_EVENT_INPUT_ANGLE_PREVIOUS;
	break;
      case EMOTION_EVENT_FORCE:
	xine_event.type = XINE_EVENT_INPUT_BUTTON_FORCE;
	break;
      case EMOTION_EVENT_0:
	xine_event.type = XINE_EVENT_INPUT_NUMBER_0;
	break;
      case EMOTION_EVENT_1:
	xine_event.type = XINE_EVENT_INPUT_NUMBER_1;
	break;
      case EMOTION_EVENT_2:
	xine_event.type = XINE_EVENT_INPUT_NUMBER_2;
	break;
      case EMOTION_EVENT_3:
	xine_event.type = XINE_EVENT_INPUT_NUMBER_3;
	break;
      case EMOTION_EVENT_4:
	xine_event.type = XINE_EVENT_INPUT_NUMBER_4;
	break;
      case EMOTION_EVENT_5:
	xine_event.type = XINE_EVENT_INPUT_NUMBER_5;
	break;
      case EMOTION_EVENT_6:
	xine_event.type = XINE_EVENT_INPUT_NUMBER_6;
	break;
      case EMOTION_EVENT_7:
	xine_event.type = XINE_EVENT_INPUT_NUMBER_7;
	break;
      case EMOTION_EVENT_8:
	xine_event.type = XINE_EVENT_INPUT_NUMBER_8;
	break;
      case EMOTION_EVENT_9:
	xine_event.type = XINE_EVENT_INPUT_NUMBER_9;
	break;
      case EMOTION_EVENT_10:
	xine_event.type = XINE_EVENT_INPUT_NUMBER_10_ADD;
	break;
      default:
	return;
	break;
     }
   xine_event_send(ev->stream, &xine_event);
}

static void
em_event_mouse_button_feed(void *ef, int button, int x, int y)
{
   Emotion_Xine_Video *ev;
   xine_event_t xine_event;
   xine_input_data_t xine_input;

   ev = (Emotion_Xine_Video *)ef;
   xine_event.stream      = ev->stream;
   gettimeofday(&xine_event.tv, NULL);
   xine_event.type = XINE_EVENT_INPUT_MOUSE_BUTTON;
   xine_input.button = 1;
   xine_input.x = x;
   xine_input.y = y;
   xine_event.data = &xine_input;
   xine_event.data_length = sizeof(xine_input);
   xine_event_send(ev->stream, &xine_event);
}

static void
em_event_mouse_move_feed(void *ef, int x, int y)
{
   Emotion_Xine_Video *ev;
   xine_event_t xine_event;
   xine_input_data_t xine_input;

   ev = (Emotion_Xine_Video *)ef;
   xine_event.stream      = ev->stream;
   gettimeofday(&xine_event.tv, NULL);
   xine_event.type = XINE_EVENT_INPUT_MOUSE_MOVE;
   xine_input.button = 0;
   xine_input.x = x;
   xine_input.y = y;
   xine_event.data = &xine_input;
   xine_event.data_length = sizeof(xine_input);
   xine_event_send(ev->stream, &xine_event);
}

static int
em_video_channel_count(void *ef)
{
   Emotion_Xine_Video *ev;
   int v;
   
   ev = (Emotion_Xine_Video *)ef;
   v = xine_get_stream_info(ev->stream, XINE_STREAM_INFO_VIDEO_CHANNELS);
   if ((v < 1) &&
       xine_get_stream_info(ev->stream, XINE_STREAM_INFO_HAS_VIDEO)) return 1;
   return v;
}

static void
em_video_channel_set(void *ef, int channel)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   if (channel < 0) channel = 0;
   xine_set_param(ev->stream, XINE_PARAM_VIDEO_CHANNEL, channel);
}

static int
em_video_channel_get(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   return xine_get_param(ev->stream, XINE_PARAM_VIDEO_CHANNEL);
}

static const char *
em_video_channel_name_get(void *ef, int channel)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   return NULL;
}

static void
em_video_channel_mute_set(void *ef, int mute)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   ev->video_mute = mute;
}

static int
em_video_channel_mute_get(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   return ev->video_mute;
}

static int
em_audio_channel_count(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   return xine_get_stream_info(ev->stream, XINE_STREAM_INFO_MAX_AUDIO_CHANNEL);
}

static void
em_audio_channel_set(void *ef, int channel)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   if (channel < -1) channel = -1;
   xine_set_param(ev->stream, XINE_PARAM_AUDIO_CHANNEL_LOGICAL, channel);
}

static int
em_audio_channel_get(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   return xine_get_param(ev->stream, XINE_PARAM_AUDIO_CHANNEL_LOGICAL);
}

static const char *
em_audio_channel_name_get(void *ef, int channel)
{
   Emotion_Xine_Video *ev;
   static char lang[XINE_LANG_MAX + 1];
   
   ev = (Emotion_Xine_Video *)ef;
   lang[0] = 0;
   if (xine_get_audio_lang(ev->stream, channel, lang)) return lang;
   return NULL;
}

static void
em_audio_channel_mute_set(void *ef, int mute)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   ev->audio_mute = mute;
   xine_set_param(ev->stream, XINE_PARAM_AUDIO_MUTE, ev->audio_mute);
}

static int
em_audio_channel_mute_get(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   return ev->audio_mute;
}

static void
em_audio_channel_volume_set(void *ef, double vol)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   if (vol < 0.0) vol = 0.0;
   if (vol > 1.0) vol = 1.0;
   xine_set_param(ev->stream, XINE_PARAM_AUDIO_VOLUME, vol * 100);
}

static double
em_audio_channel_volume_get(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   return (double)xine_get_param(ev->stream, XINE_PARAM_AUDIO_VOLUME) / 100.0;
}

static int
em_spu_channel_count(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   return xine_get_stream_info(ev->stream, XINE_STREAM_INFO_MAX_SPU_CHANNEL);
}

static void
em_spu_channel_set(void *ef, int channel)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   if (channel < 0) channel = 0;
   xine_set_param(ev->stream, XINE_PARAM_SPU_CHANNEL, channel);
}

static int
em_spu_channel_get(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   return xine_get_param(ev->stream, XINE_PARAM_SPU_CHANNEL);
}

static const char *
em_spu_channel_name_get(void *ef, int channel)
{
   Emotion_Xine_Video *ev;
   static char lang[XINE_LANG_MAX + 1];
   
   ev = (Emotion_Xine_Video *)ef;
   lang[0] = 0;
   if (xine_get_spu_lang(ev->stream, channel, lang)) return lang;
   return NULL;
}

static void
em_spu_channel_mute_set(void *ef, int mute)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   ev->spu_mute = mute;
   xine_set_param(ev->stream, XINE_PARAM_IGNORE_SPU, ev->spu_mute);
}

static int
em_spu_channel_mute_get(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   return ev->spu_mute;
}

static int
em_chapter_count(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   if (xine_get_stream_info(ev->stream, XINE_STREAM_INFO_HAS_CHAPTERS))
     return 99;
   return 0;
}

static void
em_chapter_set(void *ef, int chapter)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
}

static int
em_chapter_get(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   return 0;
}

static const char *
em_chapter_name_get(void *ef, int chapter)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   return NULL;
}

static void
em_speed_set(void *ef, double speed)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
}

static double
em_speed_get(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   return 1.0;
}

static int
em_eject(void *ef)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)ef;
   xine_eject(ev->stream);
   return 1;
}

static const char *
em_meta_get(Emotion_Xine_Video *ev, int meta)
{
   switch (meta)
     {
      case META_TRACK_TITLE:
	return xine_get_meta_info(ev->stream, XINE_META_INFO_TITLE);
	break;
      case META_TRACK_ARTIST:
	return xine_get_meta_info(ev->stream, XINE_META_INFO_ARTIST);
	break;
      case META_TRACK_GENRE:
	return xine_get_meta_info(ev->stream, XINE_META_INFO_GENRE);
	break;
      case META_TRACK_COMMENT:
	return xine_get_meta_info(ev->stream, XINE_META_INFO_COMMENT);
	break;
      case META_TRACK_ALBUM:
	return xine_get_meta_info(ev->stream, XINE_META_INFO_ALBUM);
	break;
      case META_TRACK_YEAR:
	return xine_get_meta_info(ev->stream, XINE_META_INFO_YEAR);
	break;
      case META_TRACK_DISCID:
	return xine_get_meta_info(ev->stream, XINE_META_INFO_CDINDEX_DISCID);
	break;
      default:
	break;
     }
   return NULL;
}

static void *
_em_seek(void *par)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)par;
   pthread_mutex_lock(&(ev->seek_mutex));
   for (;;)
     {
	double ppos;
	
	pthread_cond_wait(&(ev->seek_cond), &(ev->seek_mutex));
	if (ev->seek_to)
	  {
	     ppos = ev->seek_to_pos;
	     if (ppos > ev->len)
	       ppos = ev->len;
	     if (ev->no_time)
	       xine_play(ev->stream, ppos * 65535, 0);
	     else
	       xine_play(ev->stream, 0, ppos * 1000);
	     ev->seek_to = 0;
	     
	     if (!ev->play)
	       xine_set_param(ev->stream, XINE_PARAM_SPEED, XINE_SPEED_PAUSE);
	  }
	if (ev->delete_me)
	  {
	     ev->seek_thread_deleted = 1;
	     return NULL;
	  }
     }
   return NULL;
}

static int
_em_fd_active(void *data, Ecore_Fd_Handler *fdh)
{
   void *buf;
   int fd, len;
   Emotion_Xine_Video_Frame *fr;
   Emotion_Xine_Video *ev;
   
   ev = data;
   fd = ecore_main_fd_handler_fd_get(fdh);
   while ((len = read(fd, &buf, sizeof(buf))) > 0)
     {
	if (len == sizeof(buf))
	  {
	     fr = buf;
	     ev = _emotion_video_get(fr->obj);
	     if (ev)
	       {
		  if (ev->cur_frame)
		    {
		       if (ev->cur_frame->done_func)
			 ev->cur_frame->done_func(ev->cur_frame->done_data);
		    }
		  ev->cur_frame = fr;
		  _em_get_pos_len(ev);
		  if ((xine_get_stream_info(ev->stream, XINE_STREAM_INFO_HAS_VIDEO)) &&
		      (xine_get_stream_info(ev->stream, XINE_STREAM_INFO_VIDEO_HANDLED)))
		    {
 
		       if (ev->video_mute)
			 {
			    if (ev->cur_frame->done_func)
			      ev->cur_frame->done_func(ev->cur_frame->done_data);
			    ev->cur_frame = NULL;
			 }
		       else
			 _emotion_frame_new(fr->obj);
		    }
		  _emotion_frame_resize(fr->obj, fr->w, fr->h, fr->ratio);
		  _emotion_video_pos_update(fr->obj, ev->pos, ev->len);
	       }
	  }
     }
   return 1;
}

static void
_em_event(void *data, const xine_event_t *event)
{
   void *buf[2];
   Emotion_Xine_Event *new_ev;
   Emotion_Xine_Video *ev;
   
   ev = data;
   new_ev = calloc(1, sizeof(Emotion_Xine_Event));
   if (!new_ev) return;
   new_ev->type = event->type;
   if (event->data)
     {
	new_ev->xine_event = malloc(event->data_length);
	if (!new_ev->xine_event)
	  {
	     free(new_ev);
	     return;
	  }
	memcpy(new_ev->xine_event, event->data, event->data_length);
     }
   buf[0] = data;
   buf[1] = new_ev;
   write(ev->fd_ev_write, buf, sizeof(buf));
}

static int
_em_fd_ev_active(void *data, Ecore_Fd_Handler *fdh)
{
   Emotion_Xine_Video *ev;
   int fd, len;
   void *buf[2];
   
   ev = data;
   fd = ecore_main_fd_handler_fd_get(fdh);
   while ((len = read(fd, buf, sizeof(buf))) > 0)
     {
	if (len == sizeof(buf))
	  {
	     Emotion_Xine_Event *eev;
	     
	     ev = buf[0];
	     eev = buf[1];
	     switch (eev->type)
	       {
		case XINE_EVENT_UI_PLAYBACK_FINISHED:
		    {
		       if (ev->timer)
			 {
			    ecore_timer_del(ev->timer);
			    ev->timer = NULL;
			 }
		       ev->play = 0;
		       _emotion_decode_stop(ev->obj);
		       _emotion_playback_finished(ev->obj);
		    }
		  break;
		case XINE_EVENT_UI_CHANNELS_CHANGED:
		    {
		       _emotion_channels_change(ev->obj);
		    }
		  break;
		case XINE_EVENT_UI_SET_TITLE:
		    {
		       xine_ui_data_t *e;
		       
		       e = (xine_ui_data_t *)eev->xine_event;
		       _emotion_title_set(ev->obj, e->str);
		    }
		  break;
		case XINE_EVENT_FRAME_FORMAT_CHANGE:
		    {
		       xine_format_change_data_t *e;
		       
		       e = (xine_format_change_data_t *)eev->xine_event;
		    }
		  break;
		case XINE_EVENT_UI_MESSAGE:
		    {
		       xine_ui_message_data_t *e;
		       
		       e = (xine_ui_message_data_t *)eev->xine_event;
		       printf("EV: UI Message [FIXME: break this out to emotion api]\n");
		       // e->type = error type(XINE_MSG_NO_ERROR, XINE_MSG_GENERAL_WARNING, XINE_MSG_UNKNOWN_HOST etc.)
		       // e->messages is a list of messages DOUBLE null terminated
		    }
		  break;
		case XINE_EVENT_AUDIO_LEVEL:
		    {
		       xine_audio_level_data_t *e;
	     
		       e = (xine_audio_level_data_t *)eev->xine_event;
             _emotion_audio_level_change(ev->obj);
		       printf("EV: Audio Level [FIXME: break this out to emotion api]\n");
		       // e->left (0->100) 
		       // e->right
		       // e->mute
		    }
		  break;
		case XINE_EVENT_PROGRESS:
		    {
		       xine_progress_data_t *e;
		       
		       e = (xine_progress_data_t *)eev->xine_event;
		       _emotion_progress_set(ev->obj, (char *)e->description, (double)e->percent / 100.0);
		    }
		  break;
		case XINE_EVENT_MRL_REFERENCE:
		    {
		       xine_mrl_reference_data_t *e;
		       
		       e = (xine_mrl_reference_data_t *)eev->xine_event;
		       _emotion_file_ref_set(ev->obj, e->mrl, e->alternative);
		    }
		  break;
		case XINE_EVENT_UI_NUM_BUTTONS:
		    {
		       xine_ui_data_t *e;
		       
		       e = (xine_ui_data_t *)eev->xine_event;
		       _emotion_spu_button_num_set(ev->obj, e->num_buttons);
		    }
		  break;
		case XINE_EVENT_SPU_BUTTON:
		    {
		       xine_spu_button_t *e;
		       
		       e = (xine_spu_button_t *)eev->xine_event;
		       if (e->direction == 1)
			 _emotion_spu_button_set(ev->obj, e->button);
		       else
			 _emotion_spu_button_set(ev->obj, -1);
		    }
		  break;
		case XINE_EVENT_DROPPED_FRAMES:
		    {
		       xine_dropped_frames_t *e;
		       
		       e = (xine_dropped_frames_t *)eev->xine_event;
		       printf("EV: Dropped Frames (skipped %i) (discarded %i) [FIXME: break this out to the emotion api]\n", e->skipped_frames, e->discarded_frames);
		       // e->skipped_frames = % frames skipped * 10
		       // e->discarded_frames = % frames skipped * 10
		    }
		  break;
		default:
//		  printf("EV: unknown event type %i\n", eev->type);
		  break;
	       }
	     if (eev->xine_event) free(eev->xine_event);
	     free(eev);
	  }
     }
   return 1;
}

static int
_em_timer(void *data)
{
   Emotion_Xine_Video *ev;
   
   ev = data;
   _em_get_pos_len(ev);
   if ((xine_get_stream_info(ev->stream, XINE_STREAM_INFO_HAS_VIDEO)) &&
       (xine_get_stream_info(ev->stream, XINE_STREAM_INFO_VIDEO_HANDLED)))
     _emotion_frame_new(ev->obj);
   _emotion_video_pos_update(ev->obj, ev->pos, ev->len);
   return 1;
}

static void *
_em_get_pos_len_th(void *par)
{
   Emotion_Xine_Video *ev;
   
   ev = (Emotion_Xine_Video *)par;
   
   pthread_mutex_lock(&(ev->get_pos_len_mutex));
   for (;;)
     {
	pthread_cond_wait(&(ev->get_pos_len_cond), &(ev->get_pos_len_mutex));
	if (ev->get_poslen)
	  {
	     int pos_stream = 0;
	     int pos_time = 0;
	     int length_time = 0;
	     
	     if (xine_get_pos_length(ev->stream, &pos_stream, &pos_time, &length_time))
	       {
		  if (length_time == 0)
		    {
		       ev->pos = (double)pos_stream / 65535;
		       ev->len = 1.0;
		       ev->no_time = 1;
		    }
		  else
		    {
		       ev->pos = (double)pos_time / 1000.0;
		       ev->len = (double)length_time / 1000.0;
		       ev->no_time = 0;
		    }
	       }
	     ev->get_poslen = 0;
	     //printf("get pos %3.3f\n", ev->pos);
	  }
	if (ev->delete_me)
	  {
	     ev->get_pos_thread_deleted = 1;
	     return NULL;     
	  }
     }
   return NULL;
}

static void
_em_get_pos_len(Emotion_Xine_Video *ev)
{
   if (ev->get_poslen) return;
   ev->get_poslen = 1;
   pthread_cond_broadcast(&(ev->get_pos_len_cond));
}

static Emotion_Video_Module em_module =
{
   em_init, /* init */
     em_shutdown, /* shutdown */
     em_file_open, /* file_open */
     em_file_close, /* file_close */
     em_play, /* play */
     em_stop, /* stop */
     em_size_get, /* size_get */
     em_pos_set, /* pos_set */
     em_vis_set, /* vis_set */
     em_len_get, /* len_get */
     em_fps_num_get, /* fps_num_get */
     em_fps_den_get, /* fps_den_get */
     em_fps_get, /* fps_get */
     em_pos_get, /* pos_get */
     em_vis_get, /* vis_get */
     em_ratio_get, /* ratio_get */
     em_video_handled, /* video_handled */
     em_audio_handled, /* audio_handled */
     em_seekable, /* seekable */
     em_frame_done, /* frame_done */
     em_format_get, /* format_get */
     em_video_data_size_get, /* video_data_size_get */
     em_yuv_rows_get, /* yuv_rows_get */
     em_bgra_data_get, /* bgra_data_get */
     em_event_feed, /* event_feed */
     em_event_mouse_button_feed, /* event_mouse_button_feed */
     em_event_mouse_move_feed, /* event_mouse_move_feed */
     em_video_channel_count, /* video_channel_count */
     em_video_channel_set, /* video_channel_set */
     em_video_channel_get, /* video_channel_get */
     em_video_channel_name_get, /* video_channel_name_get */
     em_video_channel_mute_set, /* video_channel_mute_set */
     em_video_channel_mute_get, /* video_channel_mute_get */
     em_audio_channel_count, /* audio_channel_count */
     em_audio_channel_set, /* audio_channel_set */
     em_audio_channel_get, /* audio_channel_get */
     em_audio_channel_name_get, /* audio_channel_name_get */
     em_audio_channel_mute_set, /* audio_channel_mute_set */
     em_audio_channel_mute_get, /* audio_channel_mute_get */
     em_audio_channel_volume_set, /* audio_channel_volume_set */
     em_audio_channel_volume_get, /* audio_channel_volume_get */
     em_spu_channel_count, /* spu_channel_count */
     em_spu_channel_set, /* spu_channel_set */
     em_spu_channel_get, /* spu_channel_get */
     em_spu_channel_name_get, /* spu_channel_name_get */
     em_spu_channel_mute_set, /* spu_channel_mute_set */
     em_spu_channel_mute_get, /* spu_channel_mute_get */
     em_chapter_count, /* chapter_count */
     em_chapter_set, /* chapter_set */
     em_chapter_get, /* chapter_get */
     em_chapter_name_get, /* chapter_name_get */
     em_speed_set, /* speed_set */
     em_speed_get, /* speed_get */
     em_eject, /* eject */
     em_meta_get /* meta_get */
};

unsigned char
module_open(Evas_Object *obj, Emotion_Video_Module **module, void **video)
{
   if (!module)
      return 0;
   
   if (!em_module.init(obj, video))
      return 0;

   *module = &em_module;
   return 1;
}

void
module_close(Emotion_Video_Module *module, void *video)
{
   em_module.shutdown(video);
}

#if 0
void
em_debug(Emotion_Xine_Video *ev)
{
   int has_chapters = 0;
   int max_spu = 0;
   int max_audio = 0;
   int video_channels = 0;
   int video_streams = 0;
   int video_seekable = 0;
   char *title;
   char *comment;
   char *artist;
   char *genre;
   char *album;
   char *year;
   char *cdindex_discid;
   int video_channel = 0;
   int audio_channel = 0;
   int spu_channel = 0;
   int video_ratio = 0;
   int audio_mode = 0;

//   return;
   has_chapters = xine_get_stream_info(ev->stream, XINE_STREAM_INFO_HAS_CHAPTERS);
   max_spu = xine_get_stream_info(ev->stream, XINE_STREAM_INFO_MAX_SPU_CHANNEL);
   max_audio = xine_get_stream_info(ev->stream, XINE_STREAM_INFO_MAX_AUDIO_CHANNEL);
   video_channels = xine_get_stream_info(ev->stream, XINE_STREAM_INFO_VIDEO_CHANNELS);
   video_streams = xine_get_stream_info(ev->stream, XINE_STREAM_INFO_VIDEO_STREAMS);
   video_seekable = xine_get_stream_info(ev->stream, XINE_STREAM_INFO_SEEKABLE);
   title = xine_get_meta_info(ev->stream, XINE_META_INFO_TITLE);
   comment = xine_get_meta_info(ev->stream, XINE_META_INFO_COMMENT);
   artist = xine_get_meta_info(ev->stream, XINE_META_INFO_ARTIST);
   genre = xine_get_meta_info(ev->stream, XINE_META_INFO_GENRE);
   album = xine_get_meta_info(ev->stream, XINE_META_INFO_ALBUM);
   year = xine_get_meta_info(ev->stream, XINE_META_INFO_YEAR);
   cdindex_discid = xine_get_meta_info(ev->stream, XINE_META_INFO_CDINDEX_DISCID);
   video_channel = xine_get_param(ev->stream, XINE_PARAM_VIDEO_CHANNEL);
   audio_channel = xine_get_param(ev->stream, XINE_PARAM_AUDIO_CHANNEL_LOGICAL);
   spu_channel = xine_get_param(ev->stream, XINE_PARAM_SPU_CHANNEL);
   video_ratio = xine_get_stream_info(ev->stream, XINE_STREAM_INFO_VIDEO_RATIO);
   audio_mode = xine_get_stream_info(ev->stream, XINE_STREAM_INFO_AUDIO_MODE);
   printf("has_chapters = %i\n", has_chapters);
   printf("max_spu = %i\n", max_spu);
   printf("max_audio = %i\n", max_audio);
   printf("video_channels = %i\n", video_channels);
   printf("video_streams = %i\n", video_streams);
   printf("video_seekable = %i\n", video_seekable);
   printf("title = %s\n", title);
   printf("comment = %s\n", comment);
   printf("artist = %s\n", artist);
   printf("genre = %s\n", genre);
   printf("album = %s\n", album);
   printf("year = %s\n", year);
   printf("cdindex_discid = %s\n", cdindex_discid);
   printf("video_channel = %i\n", video_channel);
   printf("audio_channel = %i\n", audio_channel);
   printf("spu_channels = %i\n", spu_channel);
   printf("video_ratio = %i\n", video_ratio);
   printf("audio_mode = %i\n", audio_mode);
     {
	int i;
	
	for (i = 0; i <= max_audio; i++)
	  {
	     char lang[XINE_LANG_MAX + 1];
	     
	     lang[0] = 0;
	     printf("  AUDIO %i = ", i);
	     if (xine_get_audio_lang(ev->stream, i, lang))
	       printf("%s\n", lang);
	     else
	       printf("NONE\n");
	  }
	for (i = 0; i <= max_spu; i++)
	  {
	     char lang[XINE_LANG_MAX + 1];
	     
	     lang[0] = 0;
	     printf("  SPU %i = ", i);
	     if (xine_get_spu_lang(ev->stream, i, lang))
	       printf("%s\n", lang);
	     else
	       printf("NONE\n");
	  }
     }
}
#endif