forked from snarfed/p4sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.cpp
More file actions
147 lines (121 loc) · 4.13 KB
/
player.cpp
File metadata and controls
147 lines (121 loc) · 4.13 KB
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
/**
* p4sync
* http://snarfed.org/p4sync
*
* Copyright 2005 Ryan Barrett <p4sync@ryanb.org>
*
* @file player.cpp
*
* Defines generic player plugin code - connecting or starting the server on
* init, disconnecting or stopping the server on teardown, setting auto-sync
* timeouts, etc.
*
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "player.h"
#include "client.h"
#include "server.h"
#include "settings.h"
#include "trace.h"
#include <cassert>
// callbacks
bool server_callback();
bool sync_callback();
bool play_callback();
void cplayer::plugin_enabled()
{
assert(!enabled);
ctrace::instance() << acquire("player") << "plugin_enabled" << release();
enabled = true;
if (csettings::instance().get<bool>("run_server")) {
server_init(csettings::instance().get<int>("server_port"));
add_timer(csettings::instance().get<int>("serve_freq_ms"),
server_callback);
} else {
add_timer(100, play_callback);
int ret = client_connect(csettings::instance().get<string>("sync_to_host"),
csettings::instance().get<int>("sync_to_port"));
// only start the re-synching timer if we're connected
if (ret == 0) {
int sync_freq = csettings::instance().get<int>("sync_freq_ms");
if (sync_freq > 0)
add_timer(sync_freq, sync_callback);
}
}
}
void cplayer::plugin_disabled()
{
assert(enabled);
ctrace::instance() << acquire("player") << "plugin_disabled" << release();
enabled = false;
if (csettings::instance().get<bool>("run_server"))
server_shutdown();
else
client_disconnect();
csettings::instance().write();
}
// if the plugin is enabled and this player is the server, this callback is
// called every server_freq_ms
bool server_callback()
{
if (!cplayer::instance().is_plugin_enabled() ||
!csettings::instance().get<bool>("run_server"))
return false;
serve();
return true;
}
// if the plugin is enabled and this player is a client, this callback is
// called every sync_freq_ms
bool sync_callback()
{
if (!cplayer::instance().is_plugin_enabled() ||
csettings::instance().get<bool>("run_server"))
return false;
sync_player();
return true;
}
// if the plugin is enabled and this player is a client, this callback is
// called every 100ms or so. it checks to see whether the play button was
// pressed. if it was, it tries to connect and sync.
bool play_callback()
{
static string last_state = cplayer::kplaying();
static int last_song = -1;
static int last_pos = -1;
if (!cplayer::instance().is_plugin_enabled() ||
csettings::instance().get<bool>("run_server"))
return false;
// detect whether they pressed play. if the player was previously stopped or
// paused, this is easy; we just compare the current and last states. if it
// was playing, though, we have to check whether it's on the same song, but
// it restarted from the beginning.
const string &cur_state = cplayer::instance().get_state();
const int &cur_song = cplayer::instance().get_cur_song();
const int &cur_pos = cplayer::instance().get_pos_ms();
if (cur_state == cplayer::kplaying()) {
bool restarted = (last_song == cur_song &&
cur_pos < last_pos && cur_pos <= 1000);
if (restarted || last_state != cplayer::kplaying()) {
ctrace::instance() << acquire("player") << "pressed play!" << release();
client_connect();
sync_player();
}
}
last_state = cur_state;
last_song = cur_song;
last_pos = cur_pos;
return true;
}