Skip to content

Commit

Permalink
feat: pause mpv when fullscreen
Browse files Browse the repository at this point in the history
  • Loading branch information
moetayuko committed Oct 26, 2024
1 parent 66cc6d9 commit 739a7ec
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 28 deletions.
52 changes: 29 additions & 23 deletions src/WallpaperEngine/Application/CWallpaperApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void CWallpaperApplication::setupContainer (CCombinedContainer& container, const
try {
container.add (new CDirectory (this->m_context.settings.general.assets));
} catch (CAssetLoadException&) {
sLog.exception("Cannot find a valid assets folder, resolved to ", this->m_context.settings.general.assets);
sLog.exception ("Cannot find a valid assets folder, resolved to ", this->m_context.settings.general.assets);
}

// add two possible patches directories to the container
Expand Down Expand Up @@ -173,8 +173,9 @@ void CWallpaperApplication::setupContainer (CCombinedContainer& container, const
}

void CWallpaperApplication::loadBackgrounds () {
if (this->m_context.settings.render.mode == CApplicationContext::NORMAL_WINDOW || this->m_context.settings.render.mode == CApplicationContext::EXPLICIT_WINDOW) {
this->m_backgrounds ["default"] = this->loadBackground(this->m_context.settings.general.defaultBackground);
if (this->m_context.settings.render.mode == CApplicationContext::NORMAL_WINDOW ||
this->m_context.settings.render.mode == CApplicationContext::EXPLICIT_WINDOW) {
this->m_backgrounds ["default"] = this->loadBackground (this->m_context.settings.general.defaultBackground);
return;
}

Expand Down Expand Up @@ -244,9 +245,9 @@ void CWallpaperApplication::takeScreenshot (const std::filesystem::path& filenam
int xfinal = x + xoffset;
int yfinal = this->m_renderContext->getOutput ().renderVFlip () ? (viewport->viewport.w - y) : y;

bitmap[yfinal * width * 3 + xfinal * 3] = *pixel++;
bitmap[yfinal * width * 3 + xfinal * 3 + 1] = *pixel++;
bitmap[yfinal * width * 3 + xfinal * 3 + 2] = *pixel++;
bitmap [yfinal * width * 3 + xfinal * 3] = *pixel++;
bitmap [yfinal * width * 3 + xfinal * 3 + 1] = *pixel++;
bitmap [yfinal * width * 3 + xfinal * 3 + 2] = *pixel++;
}
}

Expand All @@ -257,14 +258,14 @@ void CWallpaperApplication::takeScreenshot (const std::filesystem::path& filenam
delete [] buffer;
}

auto extension = filename.extension();
auto extension = filename.extension ();

if (extension == ".bmp") {
stbi_write_bmp (filename.c_str(), width, height, 3, bitmap);
stbi_write_bmp (filename.c_str (), width, height, 3, bitmap);
} else if (extension == ".png") {
stbi_write_png (filename.c_str(), width, height, 3, bitmap, width * 3);
stbi_write_png (filename.c_str (), width, height, 3, bitmap, width * 3);
} else if (extension == ".jpg" || extension == ".jpeg") {
stbi_write_jpg (filename.c_str(), width, height, 3, bitmap, 100);
stbi_write_jpg (filename.c_str (), width, height, 3, bitmap, 100);
}
}

Expand All @@ -276,14 +277,14 @@ void CWallpaperApplication::show () {
"Cannot read environment variable XDG_SESSION_TYPE, window server detection failed. Please ensure proper values are set");
}

sLog.debug("Checking for window servers: ");
sLog.debug ("Checking for window servers: ");

#ifdef ENABLE_WAYLAND
sLog.debug("\twayland");
sLog.debug ("\twayland");
#endif // ENABLE_WAYLAND

#ifdef ENABLE_X11
sLog.debug("\tx11");
sLog.debug ("\tx11");
#endif // ENABLE_X11

#ifdef ENABLE_WAYLAND
Expand Down Expand Up @@ -349,8 +350,9 @@ void CWallpaperApplication::show () {
new WallpaperEngine::Render::Drivers::Detectors::CFullScreenDetector (this->m_context);
}

m_inputContext = new WallpaperEngine::Input::CInputContext (new WallpaperEngine::Input::Drivers::CGLFWMouseInput (
reinterpret_cast<Render::Drivers::CGLFWOpenGLDriver*> (m_videoDriver)));
m_inputContext =
new WallpaperEngine::Input::CInputContext (new WallpaperEngine::Input::Drivers::CGLFWMouseInput (
reinterpret_cast<Render::Drivers::CGLFWOpenGLDriver*> (m_videoDriver)));
}

if (this->m_context.settings.audio.audioprocessing) {
Expand All @@ -374,9 +376,10 @@ void CWallpaperApplication::show () {

// set all the specific wallpapers required
for (const auto& [background, info] : this->m_backgrounds) {
m_renderContext->setWallpaper (background, WallpaperEngine::Render::CWallpaper::fromWallpaper (
info->getWallpaper (), *m_renderContext, *m_audioContext, m_browserContext,
this->m_context.settings.general.screenScalings [background]));
m_renderContext->setWallpaper (background,
WallpaperEngine::Render::CWallpaper::fromWallpaper (
info->getWallpaper (), *m_renderContext, *m_audioContext, m_browserContext,
this->m_context.settings.general.screenScalings [background]));
}

// wallpapers are setup, free browsesr context if possible
Expand All @@ -401,6 +404,13 @@ void CWallpaperApplication::show () {
m_audioDriver->update ();
// update input information
m_inputContext->update ();
// check for fullscreen windows and wait until there's none fullscreen
if (this->m_fullScreenDetector->anythingFullscreen () && this->m_context.state.general.keepRunning) {
m_renderContext->setPause (true);
while (this->m_fullScreenDetector->anythingFullscreen () && this->m_context.state.general.keepRunning)
usleep (FULLSCREEN_CHECK_WAIT_TIME);
m_renderContext->setPause (false);
}
// process driver events
m_videoDriver->dispatchEventQueue ();

Expand All @@ -420,10 +430,6 @@ void CWallpaperApplication::show () {
}

void CWallpaperApplication::update (Render::Drivers::Output::COutputViewport* viewport) {
// check for fullscreen windows and wait until there's none fullscreen
while (this->m_fullScreenDetector->anythingFullscreen () && this->m_context.state.general.keepRunning)
usleep (FULLSCREEN_CHECK_WAIT_TIME);

// render the scene
m_renderContext->render (viewport);
}
Expand All @@ -443,4 +449,4 @@ CApplicationContext& CWallpaperApplication::getContext () const {
const WallpaperEngine::Render::Drivers::Output::COutput& CWallpaperApplication::getOutput () const {
return this->m_renderContext->getOutput ();
}
} // namespace WallpaperEngine::Application
} // namespace WallpaperEngine::Application
7 changes: 6 additions & 1 deletion src/WallpaperEngine/Render/CRenderContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ void CRenderContext::setWallpaper (const std::string& display, CWallpaper* wallp
this->m_wallpapers.insert_or_assign (display, wallpaper);
}

void CRenderContext::setPause (bool newState) {
for (auto&& wallpaper : this->m_wallpapers)
wallpaper.second->setPause (newState);
}

Input::CInputContext& CRenderContext::getInputContext () const {
return this->m_input;
}
Expand All @@ -61,4 +66,4 @@ const Drivers::Output::COutput& CRenderContext::getOutput () const {
const ITexture* CRenderContext::resolveTexture (const std::string& name) {
return this->m_textureCache->resolve (name);
}
} // namespace WallpaperEngine::Render
} // namespace WallpaperEngine::Render
3 changes: 2 additions & 1 deletion src/WallpaperEngine/Render/CRenderContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class CRenderContext {

void render (Drivers::Output::COutputViewport* viewport);
void setWallpaper (const std::string& display, CWallpaper* wallpaper);
void setPause (bool newState);
[[nodiscard]] Input::CInputContext& getInputContext () const;
[[nodiscard]] const CWallpaperApplication& getApp () const;
[[nodiscard]] const Drivers::CVideoDriver& getDriver () const;
Expand All @@ -54,4 +55,4 @@ class CRenderContext {
CTextureCache* m_textureCache;
};
} // namespace Render
} // namespace WallpaperEngine
} // namespace WallpaperEngine
4 changes: 3 additions & 1 deletion src/WallpaperEngine/Render/CWallpaper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ void CWallpaper::render (glm::ivec4 viewport, bool vflip) {
glDrawArrays (GL_TRIANGLES, 0, 6);
}

void CWallpaper::setPause (bool newState) {}

void CWallpaper::setupFramebuffers () {
const uint32_t width = this->getWidth ();
const uint32_t height = this->getHeight ();
Expand Down Expand Up @@ -291,4 +293,4 @@ CWallpaper* CWallpaper::fromWallpaper (Core::CWallpaper* wallpaper, CRenderConte
scalingMode);
else
sLog.exception ("Unsupported wallpaper type");
}
}
8 changes: 7 additions & 1 deletion src/WallpaperEngine/Render/CWallpaper.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ class CWallpaper : public Helpers::CContextAware {
*/
void render (glm::ivec4 viewport, bool vflip);

/**
* Pause the renderer
*/
virtual void setPause (bool newState);

/**
* @return The container to resolve files for this wallpaper
*/
Expand Down Expand Up @@ -141,7 +146,8 @@ class CWallpaper : public Helpers::CContextAware {
* @return
*/
static CWallpaper* fromWallpaper (Core::CWallpaper* wallpaper, CRenderContext& context, CAudioContext& audioContext,
CWebBrowserContext& browserContext, const CWallpaperState::TextureUVsScaling& scalingMode);
CWebBrowserContext& browserContext,
const CWallpaperState::TextureUVsScaling& scalingMode);

protected:
CWallpaper (Core::CWallpaper* wallpaperData, std::string type, CRenderContext& context, CAudioContext& audioContext,
Expand Down
11 changes: 10 additions & 1 deletion src/WallpaperEngine/Render/Wallpapers/CVideo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ CVideo::CVideo (Core::CVideo* video, CRenderContext& context, CAudioContext& aud
CWallpaper (video, Type, context, audioContext, scalingMode),
m_width (16),
m_height (16),
m_paused (false),
m_mpvGl (nullptr) {
double volume = this->getContext ().getApp ().getContext ().settings.audio.volume * 100.0 / 128.0;

Expand All @@ -40,7 +41,7 @@ CVideo::CVideo (Core::CVideo* video, CRenderContext& context, CAudioContext& aud
mpv_set_option (this->m_mpv, "volume", MPV_FORMAT_DOUBLE, &volume);

if (!this->getContext ().getApp ().getContext ().settings.audio.enabled) {
mpv_set_option_string(this->m_mpv, "mute", "yes");
mpv_set_option_string (this->m_mpv, "mute", "yes");
}

// initialize gl context for mpv
Expand Down Expand Up @@ -121,6 +122,14 @@ Core::CVideo* CVideo::getVideo () {
return this->getWallpaperData ()->as<Core::CVideo> ();
}

void CVideo::setPause (bool newState) {
if (m_paused == newState)
return;
m_paused = newState;
int pause = newState;
mpv_set_property (m_mpv, "pause", MPV_FORMAT_FLAG, &pause);
}

int CVideo::getWidth () const {
return this->m_width;
}
Expand Down
2 changes: 2 additions & 0 deletions src/WallpaperEngine/Render/Wallpapers/CVideo.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class CVideo final : public CWallpaper {
[[nodiscard]] int getWidth () const override;
[[nodiscard]] int getHeight () const override;

void setPause (bool newState) override;
void setSize (int width, int height);

protected:
Expand All @@ -31,6 +32,7 @@ class CVideo final : public CWallpaper {
mpv_handle* m_mpv;
mpv_render_context* m_mpvGl;

bool m_paused;
int64_t m_width;
int64_t m_height;
};
Expand Down

0 comments on commit 739a7ec

Please # to comment.