Evdev joystick plugin
[lsnes.git] / generic / avsnoop.cpp
blob529a8fd1bc65cccc02f55754e7166e1403f3995e
1 #include "avsnoop.hpp"
2 #include "misc.hpp"
3 #include "globalwrap.hpp"
5 namespace
7 globalwrap<std::list<av_snooper*>> snoopers;
8 std::list<av_snooper::dump_notification*> notifiers;
9 std::string s_gamename;
10 std::string s_rerecords;
11 double s_gametime;
12 std::list<std::pair<std::string, std::string>> s_authors;
13 bool gameinfo_set;
14 uint32_t srate_n;
15 uint32_t srate_d;
18 av_snooper::av_snooper() throw(std::bad_alloc)
20 snoopers().push_back(this);
21 for(auto i : notifiers)
22 i->dump_starting();
25 av_snooper::~av_snooper() throw()
27 for(auto i = snoopers().begin(); i != snoopers().end(); i++)
28 if(*i == this) {
29 snoopers().erase(i);
30 break;
32 for(auto i : notifiers)
33 i->dump_ending();
36 void av_snooper::set_sound_rate(uint32_t rate_n, uint32_t rate_d)
38 srate_n = rate_n;
39 srate_d = rate_d;
40 uint32_t g = gcd(srate_n, srate_d);
41 srate_n /= g;
42 srate_d /= g;
45 std::pair<uint32_t, uint32_t> av_snooper::get_sound_rate()
47 return std::make_pair(srate_n, srate_d);
50 void av_snooper::frame(struct lcscreen& _frame, uint32_t fps_n, uint32_t fps_d, bool dummy) throw(std::bad_alloc)
52 for(auto i : snoopers())
53 try {
54 i->frame(_frame, fps_n, fps_d);
55 } catch(std::bad_alloc& e) {
56 throw;
57 } catch(std::exception& e) {
58 try {
59 messages << "Error dumping frame: " << e.what() << std::endl;
60 } catch(...) {
65 void av_snooper::sample(short l, short r, bool dummy) throw(std::bad_alloc)
67 for(auto i : snoopers())
68 try {
69 i->sample(l, r);
70 } catch(std::bad_alloc& e) {
71 throw;
72 } catch(std::exception& e) {
73 try {
74 messages << "Error dumping sample: " << e.what() << std::endl;
75 } catch(...) {
80 void av_snooper::end(bool dummy) throw(std::bad_alloc)
82 for(auto i : snoopers())
83 try {
84 i->end();
85 } catch(std::bad_alloc& e) {
86 throw;
87 } catch(std::exception& e) {
88 try {
89 messages << "Error ending dump: " << e.what() << std::endl;
90 } catch(...) {
95 void av_snooper::gameinfo(const std::string& gamename, const std::list<std::pair<std::string, std::string>>&
96 authors, double gametime, const std::string& rerecords, bool dummy) throw(std::bad_alloc)
98 s_gamename = gamename;
99 s_authors = authors;
100 s_gametime = gametime;
101 s_rerecords = rerecords;
102 gameinfo_set = true;
103 for(auto i : snoopers())
104 i->send_gameinfo();
107 void av_snooper::send_gameinfo() throw()
109 if(gameinfo_set)
110 try {
111 gameinfo(s_gamename, s_authors, s_gametime, s_rerecords);
112 } catch(...) {
116 bool av_snooper::dump_in_progress() throw()
118 return !snoopers().empty();
121 av_snooper::dump_notification::~dump_notification() throw()
125 void av_snooper::dump_notification::dump_starting() throw()
129 void av_snooper::dump_notification::dump_ending() throw()
133 void av_snooper::add_dump_notifier(av_snooper::dump_notification& notifier) throw(std::bad_alloc)
135 notifiers.push_back(&notifier);
138 void av_snooper::remove_dump_notifier(av_snooper::dump_notification& notifier) throw()
140 for(auto i = notifiers.begin(); i != notifiers.end(); i++)
141 if(*i == &notifier) {
142 notifiers.erase(i);
143 return;