Skip to content

Commit

Permalink
server: Move screen saver code to a class
Browse files Browse the repository at this point in the history
Issue: #131
  • Loading branch information
plfiorini committed Mar 9, 2014
1 parent fc7a665 commit 3489496
Show file tree
Hide file tree
Showing 5 changed files with 286 additions and 171 deletions.
1 change: 1 addition & 0 deletions src/server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ set(SOURCES
wl_hawaii/hawaiiworkspace.cpp
wl_hawaii/panelmanager.cpp
wl_hawaii/panelsurface.cpp
wl_hawaii/screensaver.cpp
wl_hawaii/shellwindow.cpp
wl_shell/wlshell.cpp
wl_shell/wlshellsurface.cpp
Expand Down
155 changes: 19 additions & 136 deletions src/server/wl_hawaii/desktop-shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include "desktop-shell.h"
#include "wayland-hawaii-server-protocol.h"
#include "wayland-notification-daemon-server-protocol.h"
#include "wayland-screensaver-server-protocol.h"
#include "shellsurface.h"
#include "binding.h"
#include "inputpanel.h"
Expand All @@ -52,6 +51,7 @@
#include "wl_hawaii/hawaiiclientwindow.h"
#include "wl_hawaii/hawaiiworkspace.h"
#include "wl_hawaii/panelmanager.h"
#include "wl_hawaii/screensaver.h"
#include "wl_hawaii/shellwindow.h"

#include <stdio.h>
Expand Down Expand Up @@ -277,10 +277,6 @@ DesktopShell::DesktopShell(struct weston_compositor *ec)
, m_inputPanel(nullptr)
, m_splash(nullptr)
, m_sessionManager(nullptr)
, m_screenSaverBinding(nullptr)
, m_screenSaverEnabled(false)
, m_screenSaverPath(INSTALL_LIBEXECDIR "/hawaii-screensaver")
, m_screenSaverDuration(5*60*1000)
, m_panelManagerBinding(nullptr)
, m_notificationsEdge(DesktopShell::EdgeRight)
, m_notificationsCornerAnchor(DesktopShell::CornerTopRight)
Expand All @@ -290,8 +286,6 @@ DesktopShell::DesktopShell(struct weston_compositor *ec)
, m_locked(false)
, m_lockSurface(nullptr)
{
m_screenSaverChild.shell = this;
m_screenSaverChild.process.pid = 0;
}

DesktopShell::~DesktopShell()
Expand Down Expand Up @@ -333,10 +327,6 @@ void DesktopShell::init()
[](struct wl_client *client, void *data, uint32_t version, uint32_t id) { static_cast<DesktopShell *>(data)->bindNotifications(client, version, id); }))
return;

if (!wl_global_create(compositor()->wl_display, &wl_screensaver_interface, 1, this,
[](struct wl_client *client, void *data, uint32_t version, uint32_t id) { static_cast<DesktopShell *>(data)->bindScreenSaver(client, version, id); }))
return;

if (!wl_global_create(compositor()->wl_display, &wl_hawaii_shell_surface_interface, 1, this,
[](struct wl_client *client, void *data, uint32_t version, uint32_t id) { static_cast<DesktopShell *>(data)->bindDesktopShellSurface(client, version, id); }))
return;
Expand All @@ -351,10 +341,6 @@ void DesktopShell::init()
m_wakeListener.listen(&compositor()->wake_signal);
m_wakeListener.signal->connect(this, &DesktopShell::wake);

struct wl_event_loop *loop = wl_display_get_event_loop(compositor()->wl_display);
m_screenSaverTimer = wl_event_loop_add_timer(loop, [](void *data) {
return static_cast<DesktopShell *>(data)->screenSaverTimeout(); }, this);

m_moveBinding = new Binding();
m_moveBinding->buttonTriggered.connect(this, &DesktopShell::moveBinding);
m_resizeBinding = new Binding();
Expand All @@ -374,6 +360,7 @@ void DesktopShell::init()
Shell::quit();
});

addInterface(new ScreenSaver);
addInterface(new PanelManager);
WlShell *wls = new WlShell;
wls->surfaceResponsivenessChangedSignal.connect(this, &DesktopShell::surfaceResponsivenessChanged);
Expand Down Expand Up @@ -433,7 +420,8 @@ void DesktopShell::lockSession()
// TODO: Disable bindings that are not supposed to work while locked

// Run screensaver or sleep
launchScreenSaverProcess();
ScreenSaver *screenSaver = findInterface<ScreenSaver>();
screenSaver->launchProcess();

// All this must be undone in resumeDesktop()
}
Expand Down Expand Up @@ -464,7 +452,8 @@ void DesktopShell::unlockSession()

void DesktopShell::resumeDesktop()
{
terminateScreenSaverProcess();
ScreenSaver *screenSaver = findInterface<ScreenSaver>();
screenSaver->terminateProcess();

// Restore layers order
m_lockLayer.insert(&compositor()->cursor_layer);
Expand Down Expand Up @@ -569,6 +558,11 @@ void DesktopShell::addPanelSurfaceToLayer(weston_view *view)
configure_static_view_no_position(view, &m_panelsLayer);
}

void DesktopShell::prependViewToLockLayer(weston_view *view)
{
m_lockLayer.prependSurface(view);
}

IRect2D DesktopShell::windowsArea(struct weston_output *output) const
{
for (Output o: m_outputs) {
Expand Down Expand Up @@ -620,6 +614,14 @@ void DesktopShell::recalculateAvailableGeometry()
}
}

void DesktopShell::centerSurfaceOnOutput(weston_view *ev, weston_output *output)
{
float x = output->x + (output->width - ev->surface->width) / 2;
float y = output->y + (output->height - ev->surface->height) / 2;

weston_view_set_position(ev, x, y);
}

void DesktopShell::trustedClientDestroyed(void *data)
{
wl_client *client = static_cast<wl_client *>(data);
Expand Down Expand Up @@ -676,14 +678,6 @@ void DesktopShell::sendInitEvents()
}
}

void DesktopShell::centerSurfaceOnOutput(weston_view *ev, weston_output *output)
{
float x = output->x + (output->width - ev->surface->width) / 2;
float y = output->y + (output->height - ev->surface->height) / 2;

weston_view_set_position(ev, x, y);
}

void DesktopShell::workspaceAdded(HawaiiWorkspace *ws)
{
wl_hawaii_shell_send_workspace_added(m_child.desktop_shell, ws->resource(), ws->workspace()->isActive());
Expand Down Expand Up @@ -759,26 +753,6 @@ void DesktopShell::unbindDesktopShellSurface(struct wl_resource *resource)
m_shellSurfaceBindings.remove(resource);
}

void DesktopShell::bindScreenSaver(wl_client *client, uint32_t version, uint32_t id)
{
struct wl_resource *resource = wl_resource_create(client, &wl_screensaver_interface, version, id);

if (!m_screenSaverBinding) {
wl_resource_set_implementation(resource, &m_screenSaverImpl, this,
[](struct wl_resource *resource) { static_cast<DesktopShell *>(resource->data)->unbindScreenSaver(resource); });
m_screenSaverBinding = resource;
return;
}

wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT, "permission to bind wl_notification_daemon denied");
wl_resource_destroy(resource);
}

void DesktopShell::unbindScreenSaver(struct wl_resource *resource)
{
m_screenSaverBinding = nullptr;
}

void DesktopShell::moveBinding(struct weston_seat *seat, uint32_t time, uint32_t button)
{
weston_view *view = seat->pointer->focus;
Expand Down Expand Up @@ -1493,97 +1467,6 @@ const struct wl_notification_daemon_interface DesktopShell::m_notificationDaemon
wrapInterface(&DesktopShell::addNotificationSurface)
};

int DesktopShell::screenSaverTimeout()
{
weston_log("screensaver timeout...\n");
fadeOut();
return 1;
}

void DesktopShell::screenSaverSigChild(int status)
{
m_screenSaverChild.process.pid = 0;
m_screenSaverChild.client = nullptr; // already destroyed by wayland

if (m_locked)
weston_compositor_sleep(compositor());
}

void DesktopShell::launchScreenSaverProcess()
{
if (m_screenSaverBinding)
return;

if (m_screenSaverPath.empty() || !m_screenSaverEnabled) {
weston_compositor_sleep(compositor());
return;
}

if (m_screenSaverChild.process.pid != 0) {
weston_log("old screensaver still running\n");
return;
}

m_screenSaverChild.client = weston_client_launch(compositor(),
&m_screenSaverChild.process,
m_screenSaverPath.c_str(),
[](struct weston_process *process, int status) {
ScreenSaverChild *child = container_of(process, ScreenSaverChild, process);
child->shell->screenSaverSigChild(status);
});

if (!m_screenSaverChild.client)
weston_log("not able to start %s\n", m_screenSaverPath.c_str());
}

void DesktopShell::terminateScreenSaverProcess()
{
if (m_screenSaverChild.process.pid == 0)
return;

::kill(m_screenSaverChild.process.pid, SIGTERM);
}

void DesktopShell::screenSaverConfigure(weston_surface *es, int32_t sx, int32_t sy)
{
// Starting screensaver beforehand doesn't work
if (!m_locked)
return;

weston_view *view = container_of(es->views.next, weston_view, surface_link);

centerSurfaceOnOutput(view, es->output);

if (wl_list_empty(&view->layer_link)) {
m_lockLayer.prependSurface(view);
weston_view_update_transform(view);
wl_event_source_timer_update(m_screenSaverTimer, m_screenSaverDuration);
fadeIn();
}
}

void DesktopShell::setScreenSaverSurface(wl_client *client, wl_resource *resource,
wl_resource *output_resource,
wl_resource *surface_resource)
{
struct weston_output *output = static_cast<weston_output *>(output_resource->data);
struct weston_surface *surface = static_cast<weston_surface *>(surface_resource->data);

weston_view *view, *next;
wl_list_for_each_safe(view, next, &surface->views, surface_link)
weston_view_destroy(view);
view = weston_view_create(surface);

surface->configure = [](struct weston_surface *es, int32_t sx, int32_t sy) {
static_cast<DesktopShell *>(es->configure_private)->screenSaverConfigure(es, sx, sy); };
surface->configure_private = this;
surface->output = output;
}

const struct wl_screensaver_interface DesktopShell::m_screenSaverImpl = {
wrapInterface(&DesktopShell::setScreenSaverSurface)
};

WL_EXPORT int
module_init(struct weston_compositor *ec, int *argc, char *argv[])
{
Expand Down
40 changes: 5 additions & 35 deletions src/server/wl_hawaii/desktop-shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class DesktopShell : public Shell {

weston_view *createBlackSurfaceWithInput(int x, int y, int w, int h, float a);

bool isLocked() const { return m_locked; }

void fadeIn();
void fadeOut();

Expand All @@ -81,11 +83,14 @@ class DesktopShell : public Shell {
bool isTrusted(wl_client *client, const char *interface) const override;

void addPanelSurfaceToLayer(weston_view *view);
void prependViewToLockLayer(weston_view *view);

virtual IRect2D windowsArea(struct weston_output *output) const;

void recalculateAvailableGeometry();

void centerSurfaceOnOutput(weston_view *ev, weston_output *output);

protected:
virtual void init();
virtual void setGrabCursor(Cursor cursor);
Expand All @@ -97,8 +102,6 @@ class DesktopShell : public Shell {

void sendInitEvents();

void centerSurfaceOnOutput(weston_view *ev, weston_output *output);

void workspaceAdded(HawaiiWorkspace *ws);

void surfaceResponsivenessChanged(ShellSurface *shsurf, bool responsive);
Expand All @@ -112,9 +115,6 @@ class DesktopShell : public Shell {
void bindDesktopShellSurface(struct wl_client *client, uint32_t version, uint32_t id);
void unbindDesktopShellSurface(struct wl_resource *resource);

void bindScreenSaver(wl_client *client, uint32_t version, uint32_t id);
void unbindScreenSaver(wl_resource *resource);

void moveBinding(struct weston_seat *seat, uint32_t time, uint32_t button);
void resizeBinding(struct weston_seat *seat, uint32_t time, uint32_t button);
void closeBinding(struct weston_seat *seat, uint32_t time, uint32_t button);
Expand Down Expand Up @@ -188,28 +188,11 @@ class DesktopShell : public Shell {
void addNotificationSurface(wl_client *client, wl_resource *resource,
wl_resource *surface_resource);

/*
* screensaver
*/

void screenSaverSigChild(int status);

void launchScreenSaverProcess();
void terminateScreenSaverProcess();

int screenSaverTimeout();

void screenSaverConfigure(weston_surface *es, int32_t sx, int32_t sy);
void setScreenSaverSurface(wl_client *client, wl_resource *resource,
wl_resource *output_resource,
wl_resource *surface_resource);

static void configurePopup(weston_surface *es, int32_t sx, int32_t sy);

static const struct wl_hawaii_shell_interface m_desktopShellImpl;
static const struct wl_hawaii_shell_surface_interface m_shellSurfaceImpl;
static const struct wl_notification_daemon_interface m_notificationDaemonImpl;
static const struct wl_screensaver_interface m_screenSaverImpl;

WlListener m_idleListener;
WlListener m_wakeListener;
Expand All @@ -235,19 +218,6 @@ class DesktopShell : public Shell {

std::list<wl_resource *> m_shellSurfaceBindings;

wl_resource *m_screenSaverBinding;
wl_event_source *m_screenSaverTimer;
bool m_screenSaverEnabled;
std::string m_screenSaverPath;
int m_screenSaverDuration;

struct ScreenSaverChild {
DesktopShell *shell;
struct weston_process process;
struct wl_client *client;
};
ScreenSaverChild m_screenSaverChild;

wl_resource *m_panelManagerBinding;

Edge m_notificationsEdge;
Expand Down
Loading

0 comments on commit 3489496

Please sign in to comment.