-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwallpaperinstance.cpp
More file actions
212 lines (171 loc) · 7.03 KB
/
wallpaperinstance.cpp
File metadata and controls
212 lines (171 loc) · 7.03 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "wallpaperinstance.h"
void WallpaperInstance::loopVideo(bool loop)
{
isVlcLoopEnabled = loop;
}
void WallpaperInstance::muteVideoSound(bool mute)
{
isVlcSoundMuted = mute;
libvlc_audio_set_mute(vlcPlayer, isVlcSoundMuted);
}
void WallpaperInstance::setVideoSoundVolume(int vol)
{
if(vol >=0 || vol <=100)
{
if(isVlcSoundMuted==true)
muteVideoSound(false);
videoSoundVolume = vol;
libvlc_audio_set_volume(vlcPlayer, videoSoundVolume);
}
else
{
qInfo() << " invalid volume value";
}
}
void WallpaperInstance::pauseResume()
{
// libvlc_media_player_pause(vlcPlayer);
// libvlc_media_player_set_pause(vlcPlayer, 1);
// libvlc_media_player_set_pause(vlcPlayer, 0);
// if (!vlcPlayer || !container)
// return;
if (libvlc_media_player_is_playing(vlcPlayer)) {
libvlc_media_player_set_pause(vlcPlayer, 1); // pause
} else {
// Force re-set the output window just in case
libvlc_media_player_set_xwindow(vlcPlayer, container->winId());
libvlc_media_player_set_pause(vlcPlayer, 0); // resume
}
container->update();
container->repaint();
}
WallpaperInstance::WallpaperInstance(QScreen *screen, const QString &filePath, Display *display, Window desktopWindow)
{
//set default values but in parent (wallpapaerManager) will set these values from settings/config
isVlcLoopEnabled=true;
isVlcSoundMuted=false;
videoSoundVolume=50;
QRect geo = screen->geometry();
if (filePath.endsWith(".gif", Qt::CaseInsensitive)) {
type = Type::GIF;
gifLabel = new QLabel(nullptr);
gifLabel->setWindowFlags(Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint);
gifLabel->setAttribute(Qt::WA_TransparentForMouseEvents);
gifLabel->setFocusPolicy(Qt::NoFocus);
gifLabel->setAttribute(Qt::WA_Disabled);
gifLabel->resize(geo.size());
gifLabel->move(geo.topLeft());
gifMovie = new QMovie(filePath);
gifMovie->setScaledSize(geo.size());
gifLabel->setMovie(gifMovie);
gifLabel->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
gifLabel->setAttribute(Qt::WA_X11NetWmWindowTypeDesktop);
gifMovie->start();
XMapWindow(display, gifLabel->winId());
gifLabel->show();
}
else if (filePath.endsWith(".mp4", Qt::CaseInsensitive) ||
filePath.endsWith(".avi", Qt::CaseInsensitive) ||
filePath.endsWith(".mkv", Qt::CaseInsensitive) ||
filePath.endsWith(".mov", Qt::CaseInsensitive) ||
filePath.endsWith(".webm", Qt::CaseInsensitive))
{
// Video case
type = Type::Video;
container = new QWidget(nullptr);
container->setWindowFlags(Qt::FramelessWindowHint);
container->setAttribute(Qt::WA_TransparentForMouseEvents);
container->setAttribute(Qt::WA_Disabled);
container->setFocusPolicy(Qt::NoFocus);
container->resize(geo.size());
container->move(geo.topLeft());
container->setAttribute(Qt::WA_X11NetWmWindowTypeDesktop);
// Set the native parent (this requires using X11 calls)
Window xid = container->winId();
Display* dpy = display;
// XMapWindow(display, container->winId());
// Reparent the VLC widget (container) to the desktop window
XReparentWindow(dpy, xid, desktopWindow, geo.x(), geo.y());
// Map the window after reparenting
XMapWindow(dpy, xid);
container->show();
// Make the VLC video window input transparent using X11 Shape extension
XRectangle rect;
rect.x = 0;
rect.y = 0;
rect.width = 0; // 0 width => no input
rect.height = 0;
XShapeCombineRectangles(display, xid, ShapeInput, 0, 0, nullptr, 0, ShapeSet, 0);
// VLC setup
const char* const vlc_args[] = {"--quiet"}; //"--no-xlib"
vlcInstance = libvlc_new(1, vlc_args);
if (!vlcInstance) {
qWarning() << "Failed to create VLC instance.";
return;
}
QByteArray filePathBytes = QFile::encodeName(filePath);
vlcMedia = libvlc_media_new_path(vlcInstance, filePathBytes.constData());
vlcPlayer = libvlc_media_player_new_from_media(vlcMedia);
libvlc_event_manager_t* eventManager = libvlc_media_player_event_manager(vlcPlayer);
libvlc_event_attach(eventManager, libvlc_MediaPlayerEndReached,
[](const libvlc_event_t* event, void* userData) {
WallpaperInstance* instance = static_cast<WallpaperInstance*>(userData);
if (instance && instance->isVlcLoopEnabled) {
// Post to Qt main thread with a slight delay
QMetaObject::invokeMethod(QCoreApplication::instance(), [instance]() {
libvlc_media_player_stop(instance->vlcPlayer);
// libvlc_media_player_set_media(instance->vlcPlayer, instance->vlcMedia);
libvlc_media_player_play(instance->vlcPlayer);
qInfo() << "Restarted video on main thread";
}, Qt::QueuedConnection);
}
}, this);
// make vlc loop second approach to do loop, but its not smooth n optimize
// loopTimer = new QTimer();
// loopTimer->setInterval(0); // check every 1 second
// QObject::connect(loopTimer, &QTimer::timeout, [this]() {
// if (vlcPlayer && libvlc_media_player_get_state(vlcPlayer) == libvlc_Ended)
// {
// libvlc_media_player_stop(vlcPlayer); // ✅ reset internal state
// libvlc_media_player_set_media(vlcPlayer, vlcMedia); // ⬅ required if media is lost
// libvlc_media_player_play(vlcPlayer); // ✅ restart
// qInfo() << "qtimer found video is eneded";
// }
// });
// loopTimer->start();
qInfo() << "Using libVLC version:" << libvlc_get_version();
libvlc_media_player_set_xwindow(vlcPlayer, container->winId());
libvlc_media_player_play(vlcPlayer);
}
else
{
qFatal() << "unsupported format.";
}
}
WallpaperInstance::~WallpaperInstance() {
if (type == Type::GIF) {
if (gifMovie) {
gifMovie->stop();
delete gifMovie;
}
if (gifLabel) {
gifLabel->hide();
delete gifLabel;
}
} else {
if (vlcPlayer) {
libvlc_media_player_stop(vlcPlayer);
libvlc_media_player_release(vlcPlayer);
}
if (vlcMedia) {
libvlc_media_release(vlcMedia);
}
if (vlcInstance) {
libvlc_release(vlcInstance);
}
if (loopTimer) {
loopTimer->stop();
delete loopTimer;
}
}
}