Skip to content

Commit 3836c8f

Browse files
committed
LibLemon: use libfmt for logging, ninja script which calls xbstrap for
convenience
1 parent a61610b commit 3836c8f

File tree

12 files changed

+134
-78
lines changed

12 files changed

+134
-78
lines changed

Applications/AudioPlayer/AudioContext.cpp

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void PlayAudio(AudioContext* ctx) {
4646
#endif
4747

4848
if(write(fd, buffer.data, buffer.samples * channels * sampleSize) < 0) {
49-
Lemon::Logger::Warning("/snd/dev/pcm: Error writing samples: ", strerror(errno));
49+
Lemon::Logger::Warning("/snd/dev/pcm: Error writing samples: {}", strerror(errno));
5050
}
5151

5252
#ifdef AUDIOCONTEXT_TIME_PLAYBACK
@@ -178,6 +178,8 @@ void DecodeAudio(AudioContext* ctx) {
178178
(const uint8_t**)frame->extended_data, frame->nb_samples);
179179

180180
buffer->samples += samplesWritten;
181+
buffer->timestamp = frame->best_effort_timestamp / ctx->m_currentStream->time_base.den;
182+
181183
av_frame_unref(frame);
182184
}
183185

@@ -192,21 +194,21 @@ AudioContext::AudioContext() {
192194
// Open the PCM output device
193195
m_pcmOut = open("/dev/snd/pcm", O_WRONLY);
194196
if (m_pcmOut < 0) {
195-
Lemon::Logger::Error("Failed to open PCM output '/dev/snd/pcm': ", strerror(errno));
197+
Lemon::Logger::Error("Failed to open PCM output '/dev/snd/pcm': {}", strerror(errno));
196198
exit(1);
197199
}
198200

199201
// Get output information
200202
m_pcmChannels = ioctl(m_pcmOut, IoCtlOutputGetNumberOfChannels);
201203
if (m_pcmChannels <= 0) {
202-
Lemon::Logger::Error("/dev/snd/pcm IoCtlOutputGetNumberOfChannels:", strerror(errno));
204+
Lemon::Logger::Error("/dev/snd/pcm IoCtlOutputGetNumberOfChannels: {}", strerror(errno));
203205
exit(1);
204206
}
205207
assert(m_pcmChannels == 1 || m_pcmChannels == 2);
206208

207209
int outputEncoding = ioctl(m_pcmOut, IoCtlOutputGetEncoding);
208210
if (outputEncoding < 0) {
209-
Lemon::Logger::Error("/dev/snd/pcm IoCtlOutputGetEncoding:", strerror(errno));
211+
Lemon::Logger::Error("/dev/snd/pcm IoCtlOutputGetEncoding: {}", strerror(errno));
210212
exit(1);
211213
}
212214

@@ -220,12 +222,12 @@ AudioContext::AudioContext() {
220222

221223
m_pcmSampleRate = ioctl(m_pcmOut, IoCtlOutputGetSampleRate);
222224
if (m_pcmSampleRate < 0) {
223-
Lemon::Logger::Error("/dev/snd/pcm IoCtlOutputGetSampleRate:", strerror(errno));
225+
Lemon::Logger::Error("/dev/snd/pcm IoCtlOutputGetSampleRate: {}", strerror(errno));
224226
exit(1);
225227
}
226228

227229
if (m_pcmBitDepth != 16) {
228-
Lemon::Logger::Error("/dev/snd/pcm Unsupported PCM sample depth of ", m_pcmBitDepth);
230+
Lemon::Logger::Error("/dev/snd/pcm Unsupported PCM sample depth of {}", m_pcmBitDepth);
229231
exit(1);
230232
}
231233

@@ -237,6 +239,14 @@ AudioContext::AudioContext() {
237239
}
238240
}
239241

242+
float AudioContext::PlaybackProgress() const {
243+
if(!m_isDecoderRunning) {
244+
return 0;
245+
}
246+
247+
return sampleBuffers[currentSampleBuffer].timestamp;
248+
}
249+
240250
void AudioContext::PlaybackStop() {
241251
m_isDecoderRunning = false;
242252
m_decoderThread.join();
@@ -270,8 +280,8 @@ int AudioContext::PlayTrack(std::string filepath) {
270280

271281
av_dump_format(m_avfmt, 0, filepath.c_str(), 0);
272282

273-
AVStream* stream = m_avfmt->streams[streamIndex];
274-
const AVCodec* decoder = avcodec_find_decoder(stream->codecpar->codec_id);
283+
m_currentStream = m_avfmt->streams[streamIndex];
284+
const AVCodec* decoder = avcodec_find_decoder(m_currentStream->codecpar->codec_id);
275285
if (!decoder) {
276286
Lemon::Logger::Error("Failed to find codec for '", filepath, "'.");
277287
return 1;
@@ -280,7 +290,7 @@ int AudioContext::PlayTrack(std::string filepath) {
280290
m_avcodec = avcodec_alloc_context3(decoder);
281291
assert(decoder);
282292

283-
if (avcodec_parameters_to_context(m_avcodec, stream->codecpar)) {
293+
if (avcodec_parameters_to_context(m_avcodec, m_currentStream->codecpar)) {
284294
Lemon::Logger::Error("Failed to initialie codec context.");
285295
return 1;
286296
}
@@ -306,6 +316,9 @@ int AudioContext::LoadTrack(std::string filepath, TrackInfo& info) {
306316
return r;
307317
}
308318

319+
int durationSeconds = fmt->duration / 1000000;
320+
info.durationString = std::to_string(durationSeconds / 60) + ":" + std::to_string(durationSeconds % 60);
321+
309322
// Get file metadata
310323
AVDictionaryEntry* tag = nullptr;
311324
tag = av_dict_get(fmt->metadata, "title", tag, AV_DICT_IGNORE_SUFFIX);

Applications/AudioPlayer/AudioContext.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@ class AudioContext {
1414
struct SampleBuffer {
1515
uint8_t* data;
1616
int samples;
17+
18+
// Timestamp in seconds of last frame in buffer
19+
float timestamp;
1720
};
1821

1922
AudioContext();
2023

2124
inline bool IsAudioPlaying() const { return m_isDecoderRunning; }
2225

26+
inline float PlaybackProgress() const;
27+
2328
void PlaybackStart();
2429
void PlaybackPause();
2530
void PlaybackStop();

Applications/AudioPlayer/AudioTrack.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
struct TrackInfo {
66
std::string filepath;
7+
std::string durationString;
78
struct {
89
std::string title;
910
std::string artist;

Applications/AudioPlayer/Main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class TrackSelection : public Container, public DataModel {
196196
} else if (column == 1) {
197197
return track.metadata.artist + " - " + track.metadata.title;
198198
} else if (column == 2) {
199-
return track.metadata.year;
199+
return track.durationString;
200200
}
201201

202202
return 0;

LibLemon/include/Lemon/Core/Logger.h

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,33 @@
33
#include <iostream>
44
#include <mutex>
55

6+
#define FMT_EXCEPTIONS 0
7+
#define FMT_HEADER_ONLY 1
8+
9+
#include <fmt/core.h>
10+
#include <fmt/format.h>
11+
612
namespace Lemon {
713
namespace Logger {
814

9-
template<typename Arg>
10-
void Log(const Arg& arg){
11-
std::cout << arg;
12-
}
13-
1415
const char* GetProgramName();
1516

16-
// Explicit overloads for basic types, no need to pass by const reference
17-
void Log(short arg);
18-
void Log(int arg);
19-
void Log(long arg);
20-
void Log(unsigned short arg);
21-
void Log(unsigned int arg);
22-
void Log(unsigned long arg);
23-
void Log(float arg);
24-
25-
template <typename Arg, typename ...Args>
26-
inline void Log(const Arg& arg, const Args&... args){
27-
Log(arg);
28-
Log(args...);
29-
}
30-
31-
template<typename ...Args>
32-
void Debug(const Args&... args){
33-
Log("[", GetProgramName(), "] [Debug] ", args..., "\n");
17+
template <typename... Args> inline void Debug(fmt::format_string<Args...> f, Args&&... args) {
18+
fmt::print(stderr, "[{}] [Debug] ", GetProgramName());
19+
fmt::vprint(stderr, f, fmt::make_format_args(args...));
20+
fmt::print(stderr, "\n");
3421
}
3522

36-
template<typename ...Args>
37-
void Warning(const Args&... args){
38-
Log("[", GetProgramName(), "] [Warning] ", args..., "\n");
23+
template <typename... Args> inline void Warning(fmt::format_string<Args...> f, Args&&... args) {
24+
fmt::print(stderr, "[{}] [Warning] ", GetProgramName());
25+
fmt::vprint(stderr, f, fmt::make_format_args(args...));
26+
fmt::print(stderr, "\n");
3927
}
4028

41-
template<typename ...Args>
42-
void Error(const Args&... args){
43-
Log("[", GetProgramName(), "] [Error] ", args..., "\n");
29+
template <typename... Args> inline void Error(fmt::format_string<Args...> f, Args&&... args) {
30+
fmt::print(stderr, "[{}] [Error] ", GetProgramName());
31+
fmt::vprint(stderr, f, fmt::make_format_args(args...));
32+
fmt::print(stderr, "\n");
4433
}
4534

4635
} // namespace Logger

LibLemon/src/ConfigManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void ConfigManager::LoadJSONConfig(const std::string& path) {
3737

3838
auto root = parser.Parse();
3939
if(!root.IsObject()){
40-
Logger::Warning("[ConfigManager] Failed to laod JSON config at ", path);
40+
Logger::Warning("[ConfigManager] Failed to laod JSON config at {}", path);
4141
return;
4242
}
4343

LibLemon/src/Logger.cpp

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,5 @@ const char* GetProgramName(){
3434
return programName;
3535
}
3636

37-
void Log(short arg){
38-
std::cerr << arg;
39-
}
40-
41-
void Log(int arg){
42-
std::cerr << arg;
43-
}
44-
45-
void Log(long arg){
46-
std::cerr << arg;
47-
}
48-
49-
void Log(unsigned short arg){
50-
std::cerr << arg;
51-
}
52-
53-
void Log(unsigned int arg){
54-
std::cerr << arg;
55-
}
56-
57-
void Log(unsigned long arg){
58-
std::cerr << arg;
59-
}
60-
61-
void Log(float arg){
62-
std::cerr << arg;
63-
}
64-
6537
} // namespace Logger
6638
} // namespace Lemon

System/LemonWM/Compositor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ void Compositor::SetWallpaper(const std::string& path) {
224224

225225
if (Graphics::LoadImage(path.c_str(), 0, 0, m_displaySurface.width, m_displaySurface.height, &m_wallpaper,
226226
true)) {
227-
Logger::Error("Failed to load wallpaper '", path, "'");
227+
Logger::Error("Failed to load wallpaper '{}'", path);
228228
}
229229

230230
m_renderMutex.lock();

System/LemonWM/WM.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ void WM::OnCreateWindow(const Lemon::Handle& client, int32_t x, int32_t y, int32
344344
void WM::OnDestroyWindow(const Lemon::Handle& client, int64_t windowID) {
345345
WMWindow* win = GetWindowFromID(windowID);
346346
if (!win) {
347-
Lemon::Logger::Warning("OnDestroyWindow: Invalid Window ID: ", windowID);
347+
Lemon::Logger::Warning("OnDestroyWindow: Invalid Window ID: {}", windowID);
348348
return;
349349
}
350350

@@ -354,7 +354,7 @@ void WM::OnDestroyWindow(const Lemon::Handle& client, int64_t windowID) {
354354
void WM::OnSetTitle(const Lemon::Handle& client, int64_t windowID, const std::string& title) {
355355
WMWindow* win = GetWindowFromID(windowID);
356356
if (!win) {
357-
Lemon::Logger::Warning("OnSetTitle: Invalid Window ID: ", windowID);
357+
Lemon::Logger::Warning("OnSetTitle: Invalid Window ID: {}", windowID);
358358
return;
359359
}
360360

@@ -364,7 +364,7 @@ void WM::OnSetTitle(const Lemon::Handle& client, int64_t windowID, const std::st
364364
void WM::OnUpdateFlags(const Lemon::Handle& client, int64_t windowID, uint32_t flags) {
365365
WMWindow* win = GetWindowFromID(windowID);
366366
if (!win) {
367-
Lemon::Logger::Warning("OnUpdateFlags: Invalid Window ID: ", windowID);
367+
Lemon::Logger::Warning("OnUpdateFlags: Invalid Window ID: {}", windowID);
368368
return;
369369
}
370370

@@ -374,7 +374,7 @@ void WM::OnUpdateFlags(const Lemon::Handle& client, int64_t windowID, uint32_t f
374374
void WM::OnRelocate(const Lemon::Handle& client, int64_t windowID, int32_t x, int32_t y) {
375375
WMWindow* win = GetWindowFromID(windowID);
376376
if (!win) {
377-
Lemon::Logger::Warning("OnRelocate: Invalid Window ID: ", windowID);
377+
Lemon::Logger::Warning("OnRelocate: Invalid Window ID: {}", windowID);
378378
return;
379379
}
380380

@@ -384,7 +384,7 @@ void WM::OnRelocate(const Lemon::Handle& client, int64_t windowID, int32_t x, in
384384
void WM::OnGetPosition(const Lemon::Handle& client, int64_t windowID) {
385385
WMWindow* win = GetWindowFromID(windowID);
386386
if (!win) {
387-
Lemon::Logger::Warning("OnGetPosition: Invalid Window ID: ", windowID);
387+
Lemon::Logger::Warning("OnGetPosition: Invalid Window ID: {}", windowID);
388388
return;
389389
}
390390

@@ -395,7 +395,7 @@ void WM::OnGetPosition(const Lemon::Handle& client, int64_t windowID) {
395395
void WM::OnResize(const Lemon::Handle& client, int64_t windowID, int32_t width, int32_t height) {
396396
WMWindow* win = GetWindowFromID(windowID);
397397
if (!win) {
398-
Lemon::Logger::Warning("OnResize: Invalid Window ID: ", windowID);
398+
Lemon::Logger::Warning("OnResize: Invalid Window ID: {}", windowID);
399399
return;
400400
}
401401

@@ -405,7 +405,7 @@ void WM::OnResize(const Lemon::Handle& client, int64_t windowID, int32_t width,
405405
void WM::OnMinimize(const Lemon::Handle& client, int64_t windowID, bool minimized) {
406406
WMWindow* win = GetWindowFromID(windowID);
407407
if (!win) {
408-
Lemon::Logger::Warning("OnMinimize: Invalid Window ID: ", windowID);
408+
Lemon::Logger::Warning("OnMinimize: Invalid Window ID: {}", windowID);
409409
return;
410410
}
411411

@@ -416,7 +416,7 @@ void WM::OnDisplayContextMenu(const Lemon::Handle& client, int64_t windowID, int
416416
const std::string& entries) {
417417
WMWindow* win = GetWindowFromID(windowID);
418418
if (!win) {
419-
Lemon::Logger::Warning("OnDisplayContextMenu: Invalid Window ID: ", windowID);
419+
Lemon::Logger::Warning("OnDisplayContextMenu: Invalid Window ID: {}", windowID);
420420
return;
421421
}
422422

@@ -455,7 +455,7 @@ void WM::OnDisplayContextMenu(const Lemon::Handle& client, int64_t windowID, int
455455
void WM::OnPong(const Lemon::Handle& client, int64_t windowID) {
456456
WMWindow* win = GetWindowFromID(windowID);
457457
if (!win) {
458-
Lemon::Logger::Warning("OnPong: Invalid Window ID: ", windowID);
458+
Lemon::Logger::Warning("OnPong: Invalid Window ID: {}", windowID);
459459
return;
460460
}
461461
}

System/LemonWM/Window.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ void WMWindow::DrawDecorationClip(const Rect& clip, Surface* surface) {
5656
}
5757

5858
void WMWindow::DrawClip(const Rect& clip, Surface* surface) {
59+
m_buffer->drawing = 1;
5960
m_windowSurface.buffer = m_buffer->currentBuffer ? (m_buffer2) : (m_buffer1);
6061

6162
Rect clipCopy = clip;
@@ -66,6 +67,8 @@ void WMWindow::DrawClip(const Rect& clip, Surface* surface) {
6667
} else {
6768
surface->Blit(&m_windowSurface, clip.pos, clipCopy);
6869
}
70+
71+
m_buffer->drawing = 0;
6972
}
7073

7174
int WMWindow::GetResizePoint(Vector2i absolutePosition) const {

0 commit comments

Comments
 (0)