]> git.tdb.fi Git - xinema.git/blob - source/xinema.cpp
A simple application using libxine to play a video file
[xinema.git] / source / xinema.cpp
1 #include <sigc++/bind.h>
2 #include <msp/core/getopt.h>
3 #include <msp/fs/dir.h>
4 #include <msp/graphics/display_private.h>
5 #include <msp/graphics/window_private.h>
6 #include <msp/io/print.h>
7 #include "xinema.h"
8
9 using namespace std;
10 using namespace Msp;
11
12 Xinema::EarlyInit::EarlyInit()
13 {
14         XInitThreads();
15 }
16
17
18 Xinema::Xinema(int argc, char **argv):
19         window(display, 1920, 1080)
20 {
21         GetOpt getopt;
22         getopt.add_argument("filename", filename, GetOpt::REQUIRED_ARG);
23         getopt(argc, argv);
24
25         window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &Xinema::exit), 0));
26 }
27
28 int Xinema::main()
29 {
30         xine = xine_new();
31
32         FS::Path config_fn = FS::get_home_dir()/".xine"/"config";
33         xine_config_load(xine, config_fn.c_str());
34
35         xine_init(xine);
36
37         xine_audio = xine_open_audio_driver(xine, "auto", 0);
38
39         XLockDisplay(display.get_private().display);
40         window.show();
41         XSync(display.get_private().display, false);
42         XUnlockDisplay(display.get_private().display);
43
44         xine_visual.display = display.get_private().display;
45         xine_visual.screen = 0;
46         xine_visual.d = window.get_private().window;
47         xine_visual.user_data = this;
48         xine_visual.dest_size_cb = &dest_size_cb;
49         xine_visual.frame_output_cb = &frame_output_cb;
50
51         xine_video = xine_open_video_driver(xine, "auto", XINE_VISUAL_TYPE_X11, &xine_visual);
52
53         xine_stream = xine_stream_new(xine, xine_audio, xine_video);
54         xine_open(xine_stream, filename.c_str());
55         xine_play(xine_stream, 0, 0);
56
57         xine_queue = xine_event_new_queue(xine_stream);
58
59         Application::main();
60
61         xine_close(xine_stream);
62         xine_event_dispose_queue(xine_queue);
63         xine_dispose(xine_stream);
64         xine_close_video_driver(xine, xine_video);
65         xine_close_audio_driver(xine, xine_audio);
66         xine_exit(xine);
67
68         return exit_code;
69 }
70
71 void Xinema::tick()
72 {
73         XLockDisplay(display.get_private().display);
74         display.tick();
75         XUnlockDisplay(display.get_private().display);
76
77         while(xine_event_t *event = xine_event_get(xine_queue))
78         {
79                 switch(event->type)
80                 {
81                 case XINE_EVENT_PROGRESS:
82                         {
83                                 xine_progress_data_t *data = reinterpret_cast<xine_progress_data_t *>(event->data);
84                                 IO::print("%s [%d%%]\n", data->description, data->percent);
85                         }
86                         break;
87                 }
88         }
89 }
90
91 void Xinema::dest_size_cb(void *user_data, int, int, double, int *dest_width, int *dest_height, double *dest_pixel_aspect)
92 {
93         Xinema &xinema = *reinterpret_cast<Xinema *>(user_data);
94         *dest_width = xinema.window.get_width();
95         *dest_height = xinema.window.get_height();
96         *dest_pixel_aspect = 1.0;
97 }
98
99 void Xinema::frame_output_cb(void *user_data, int, int, double, int *dest_x, int *dest_y, int *dest_width, int *dest_height, double *dest_pixel_aspect, int *win_x, int *win_y)
100 {
101         Xinema &xinema = *reinterpret_cast<Xinema *>(user_data);
102         *dest_x = 0;
103         *dest_y = 0;
104         *dest_width = xinema.window.get_width();
105         *dest_height = xinema.window.get_height();
106         *dest_pixel_aspect = 1.0;
107         *win_x = 0;
108         *win_y = 0;
109 }