File src/Player.cpp changed (mode: 100644) (index 04928d5..2cabf74) |
19 |
19 |
|
|
20 |
20 |
#include <unistd.h> |
#include <unistd.h> |
21 |
21 |
|
|
|
22 |
|
#include <atomic> |
22 |
23 |
#include <chrono> |
#include <chrono> |
|
24 |
|
#include <memory> |
23 |
25 |
#include <stdexcept> |
#include <stdexcept> |
24 |
26 |
#include <string> |
#include <string> |
25 |
27 |
#include <thread> |
#include <thread> |
|
... |
... |
Player::Player(const ColorScheme& colorScheme, std::string host, int port) |
44 |
46 |
const std::string song = mpdClient.getCurrentSong(); |
const std::string song = mpdClient.getCurrentSong(); |
45 |
47 |
const MpdState state = mpdClient.getState(); |
const MpdState state = mpdClient.getState(); |
46 |
48 |
|
|
47 |
|
// XXX: this might be non thread safe |
|
48 |
|
status = getStateMark(state) + " " + song; |
|
|
49 |
|
std::string newStatus = getStateMark(state) + " " + song; |
|
50 |
|
std::atomic_exchange( |
|
51 |
|
&status, std::make_shared<std::string>(newStatus)); |
49 |
52 |
|
|
50 |
53 |
mpdClient.waitForChanges(); |
mpdClient.waitForChanges(); |
51 |
54 |
} |
} |
52 |
|
} catch (std::runtime_error &e) { |
|
53 |
|
std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
|
54 |
|
|
|
55 |
|
// XXX: this might be non thread safe |
|
56 |
|
status.clear(); |
|
|
55 |
|
} catch (std::runtime_error &) { |
|
56 |
|
std::atomic_exchange(&status, {}); |
57 |
57 |
} |
} |
|
58 |
|
|
|
59 |
|
// Do not consume CPU for nothing while there is no connection. |
|
60 |
|
std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
58 |
61 |
} |
} |
59 |
62 |
}).detach(); |
}).detach(); |
60 |
63 |
} |
} |
|
... |
... |
static std::string getStateMark(MpdState state) |
72 |
75 |
|
|
73 |
76 |
void Player::update() |
void Player::update() |
74 |
77 |
{ |
{ |
75 |
|
Field::setText(status); |
|
|
78 |
|
if (std::shared_ptr<std::string> text = std::atomic_exchange(&status, {})) { |
|
79 |
|
Field::setText(*text); |
|
80 |
|
} |
76 |
81 |
} |
} |