Skip to content

Commit

Permalink
Expose screen saver index via $XSCREENSAVER_SAVER_INDEX (#144)
Browse files Browse the repository at this point in the history
Co-authored-by: Joshua Trees <[email protected]>
  • Loading branch information
jtrees and jtrees authored Jul 7, 2022
1 parent d28b145 commit 592c850
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,8 @@ The screen saver module is a separate executable, whose name must start with
`saver_` and be installed together with the included `auth_` modules (default
location: `/usr/local/libexec/xsecurelock/helpers`).
* Input: none.
* Input: receives the 0-based index of the screen saver (remember: one saver
is started per display by the multiplexer) via `$XSCREENSAVER_SAVER_INDEX`.
* Output: it may draw on or create windows below `$XSCREENSAVER_WINDOW`.
* Exit condition: the saver child will receive SIGTERM when the user wishes to
unlock the screen. It should exit promptly.
Expand Down
3 changes: 2 additions & 1 deletion saver_child.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ limitations under the License.

#include "logging.h" // for LogErrno, Log
#include "wait_pgrp.h" // for KillPgrp, WaitPgrp
#include "xscreensaver_api.h" // for ExportWindowID
#include "xscreensaver_api.h" // for ExportWindowID and ExportSaverIndex

//! The PIDs of currently running saver children, or 0 if not running.
static pid_t saver_child_pid[MAX_SAVERS] = {0};
Expand Down Expand Up @@ -65,6 +65,7 @@ void WatchSaverChild(Display* dpy, Window w, int index, const char* executable,
// Child process.
StartPgrp();
ExportWindowID(w);
ExportSaverIndex(index);

{
const char* args[3] = {
Expand Down
11 changes: 11 additions & 0 deletions xscreensaver_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ void ExportWindowID(Window w) {
setenv("XSCREENSAVER_WINDOW", window_id_str, 1);
}

void ExportSaverIndex(int index) {
char saver_index_str[32];
int saver_index_len = snprintf(saver_index_str, sizeof(saver_index_str), "%llu",
(unsigned long long)index);
if (saver_index_len <= 0 || (size_t)saver_index_len >= sizeof(saver_index_str)) {
Log("Saver index doesn't fit into buffer");
return;
}
setenv("XSCREENSAVER_SAVER_INDEX", saver_index_str, 1);
}

Window ReadWindowID(void) {
return GetUnsignedLongLongSetting("XSCREENSAVER_WINDOW", None);
}
8 changes: 8 additions & 0 deletions xscreensaver_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ limitations under the License.
*/
void ExportWindowID(Window w);

/*! \brief Export the given saver index to the environment for a saver/auth child.
*
* This simply sets $XSCREENSAVER_SAVER_INDEX.
*
* \param index The index of the saver.
*/
void ExportSaverIndex(int index);

/*! \brief Reads the window ID to draw on from the environment.
*
* This simply reads $XSCREENSAVER_WINDOW.
Expand Down

0 comments on commit 592c850

Please sign in to comment.