@crossbind/extension-power 0.0.0-bootstrap.0 → 2.0.0-beta.69
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +21 -3
- package/crossbind.extension.js +12 -0
- package/native/darwin.mm +21 -0
- package/native/linux.cpp +22 -0
- package/native/power.cpp +15 -0
- package/native/power.h +17 -0
- package/native/windows.cpp +45 -0
- package/package.json +38 -6
- package/page.d.ts +10 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Buğra Sarı and crossbind contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
# @crossbind/extension-power
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
package that exists, so it reserves the name for the crossbind release workflow.
|
|
3
|
+
Tells a desktop page when the system sleeps and wakes.
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
An app has it by having `@crossbind/core-app-host`, which lists it as a dependency: there is nothing to install or list.
|
|
6
|
+
|
|
7
|
+
## Use
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
import { events } from 'crossbind/app';
|
|
11
|
+
|
|
12
|
+
events.on('suspend', () => saveDraft());
|
|
13
|
+
events.on('resume', () => reconnect());
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Platforms
|
|
17
|
+
|
|
18
|
+
macOS, Linux and Windows, from the time the app's first window opens.
|
|
19
|
+
|
|
20
|
+
## Replace or leave out
|
|
21
|
+
|
|
22
|
+
It holds the role `power`. An extension that gives `replaces: 'power'` takes it; `app: { without: ['power'] }` in `crossbind.config.js` leaves it out. [The guide](https://crossbind.dev/guide/extensions/#replace) shows how, and [extensions.md](https://github.com/crossbind/crossbind/blob/main/docs/api/extensions.md#the-hosts-own-extensions) what a replacement answers.
|
|
23
|
+
|
|
24
|
+
[app.md](https://github.com/crossbind/crossbind/blob/main/docs/api/app.md#events-and-callbacks) has the app's other events.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export default function power() {
|
|
2
|
+
return {
|
|
3
|
+
name: 'power',
|
|
4
|
+
contract: 1,
|
|
5
|
+
root: import.meta.dirname,
|
|
6
|
+
app: {
|
|
7
|
+
macos: { sources: ['native/power.cpp', 'native/darwin.mm'] },
|
|
8
|
+
linux: { sources: ['native/power.cpp', 'native/linux.cpp'] },
|
|
9
|
+
windows: { sources: ['native/power.cpp', 'native/windows.cpp'], libraries: ['comctl32'] },
|
|
10
|
+
},
|
|
11
|
+
};
|
|
12
|
+
}
|
package/native/darwin.mm
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#import <Cocoa/Cocoa.h>
|
|
2
|
+
|
|
3
|
+
#include "power.h"
|
|
4
|
+
|
|
5
|
+
#include "crossbind/app.h"
|
|
6
|
+
|
|
7
|
+
void crossbind::power::listen() {
|
|
8
|
+
NSNotificationCenter *center = NSWorkspace.sharedWorkspace.notificationCenter;
|
|
9
|
+
[center addObserverForName:NSWorkspaceWillSleepNotification
|
|
10
|
+
object:nil
|
|
11
|
+
queue:nil
|
|
12
|
+
usingBlock:^(NSNotification *) {
|
|
13
|
+
crossbind::app::emitEvent("suspend");
|
|
14
|
+
}];
|
|
15
|
+
[center addObserverForName:NSWorkspaceDidWakeNotification
|
|
16
|
+
object:nil
|
|
17
|
+
queue:nil
|
|
18
|
+
usingBlock:^(NSNotification *) {
|
|
19
|
+
crossbind::app::emitEvent("resume");
|
|
20
|
+
}];
|
|
21
|
+
}
|
package/native/linux.cpp
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#include "power.h"
|
|
2
|
+
|
|
3
|
+
#include "crossbind/app.h"
|
|
4
|
+
|
|
5
|
+
#include <gio/gio.h>
|
|
6
|
+
|
|
7
|
+
// logind says the system goes to sleep and wakes over the system bus, which a container may not have. It sleeps without
|
|
8
|
+
// waiting for the page.
|
|
9
|
+
void crossbind::power::listen() {
|
|
10
|
+
GDBusConnection *bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, nullptr);
|
|
11
|
+
if (!bus) return;
|
|
12
|
+
// GDBus ends the app by default when the bus goes away.
|
|
13
|
+
g_dbus_connection_set_exit_on_close(bus, FALSE);
|
|
14
|
+
g_dbus_connection_signal_subscribe(
|
|
15
|
+
bus, "org.freedesktop.login1", "org.freedesktop.login1.Manager", "PrepareForSleep", "/org/freedesktop/login1", nullptr, G_DBUS_SIGNAL_FLAGS_NONE,
|
|
16
|
+
+[](GDBusConnection *, const gchar *, const gchar *, const gchar *, const gchar *, GVariant *parameters, gpointer) {
|
|
17
|
+
gboolean isSleeping = FALSE;
|
|
18
|
+
g_variant_get(parameters, "(b)", &isSleeping);
|
|
19
|
+
crossbind::app::emitEvent(isSleeping ? "suspend" : "resume");
|
|
20
|
+
},
|
|
21
|
+
nullptr, nullptr);
|
|
22
|
+
}
|
package/native/power.cpp
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// The system's sleep and waking, which a desktop app's pages hear of as the events suspend and resume: each platform
|
|
2
|
+
// hears of them its own way, once, from the time the app's first window opens.
|
|
3
|
+
#include "power.h"
|
|
4
|
+
|
|
5
|
+
#include <utility>
|
|
6
|
+
|
|
7
|
+
void crossbind_extension_power(crossbind::Registry ®istry) {
|
|
8
|
+
registry.onWindow([](const crossbind::Window &) {
|
|
9
|
+
static bool isListening = false;
|
|
10
|
+
if (!std::exchange(isListening, true)) crossbind::power::listen();
|
|
11
|
+
});
|
|
12
|
+
#ifdef _WIN32
|
|
13
|
+
registry.onWindowClosed([](const crossbind::Window &window) { crossbind::power::windowClosed(window.native()); });
|
|
14
|
+
#endif
|
|
15
|
+
}
|
package/native/power.h
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
// What each desktop platform does to tell the page when the system goes to sleep and when it wakes, as the events suspend
|
|
4
|
+
// and resume.
|
|
5
|
+
#include "crossbind/extension.h"
|
|
6
|
+
|
|
7
|
+
namespace crossbind::power {
|
|
8
|
+
|
|
9
|
+
// Starts hearing of them, on the main thread once the app's first window has opened.
|
|
10
|
+
void listen();
|
|
11
|
+
|
|
12
|
+
#ifdef _WIN32
|
|
13
|
+
// The window that hears of them for the app closed, on the main thread: the oldest left hears of them from now on.
|
|
14
|
+
void windowClosed(void *window);
|
|
15
|
+
#endif
|
|
16
|
+
|
|
17
|
+
} // namespace crossbind::power
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
#include "power.h"
|
|
2
|
+
|
|
3
|
+
#include "crossbind/app.h"
|
|
4
|
+
|
|
5
|
+
#include <windows.h>
|
|
6
|
+
|
|
7
|
+
#include <commctrl.h>
|
|
8
|
+
|
|
9
|
+
#include <cstdio>
|
|
10
|
+
|
|
11
|
+
namespace crossbind::power {
|
|
12
|
+
namespace {
|
|
13
|
+
|
|
14
|
+
constexpr UINT_PTR SUBCLASS = 1;
|
|
15
|
+
|
|
16
|
+
// Windows tells the app's windows the system goes to sleep and wakes; one with Modern Standby may tell neither.
|
|
17
|
+
LRESULT CALLBACK powerProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, UINT_PTR, DWORD_PTR) {
|
|
18
|
+
if (message == WM_POWERBROADCAST && (wParam == PBT_APMSUSPEND || wParam == PBT_APMRESUMEAUTOMATIC)) {
|
|
19
|
+
crossbind::app::emitEvent(wParam == PBT_APMSUSPEND ? "suspend" : "resume");
|
|
20
|
+
}
|
|
21
|
+
return DefSubclassProc(hwnd, message, wParam, lParam);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// The one window that hears of them, as each of the app's would.
|
|
25
|
+
HWND listener = nullptr;
|
|
26
|
+
|
|
27
|
+
void listenOn(HWND window) {
|
|
28
|
+
listener = nullptr;
|
|
29
|
+
if (!window) return;
|
|
30
|
+
if (!SetWindowSubclass(window, powerProcedure, SUBCLASS, 0)) {
|
|
31
|
+
std::fprintf(stderr, "crossbind: the app cannot hear of the system's sleep: Windows error %lu\n", GetLastError());
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
listener = window;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
} // namespace
|
|
38
|
+
|
|
39
|
+
void listen() { listenOn(static_cast<HWND>(appWindow())); }
|
|
40
|
+
|
|
41
|
+
void windowClosed(void *window) {
|
|
42
|
+
if (window == listener) listenOn(static_cast<HWND>(appWindow()));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
} // namespace crossbind::power
|
package/package.json
CHANGED
|
@@ -1,7 +1,39 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
2
|
+
"name": "@crossbind/extension-power",
|
|
3
|
+
"version": "2.0.0-beta.69",
|
|
4
|
+
"description": "crossbind app extension: tells a desktop page when the system sleeps and wakes.",
|
|
5
|
+
"homepage": "https://github.com/crossbind/crossbind/tree/main/extensions/power#readme",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/crossbind/crossbind.git"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"type": "module",
|
|
12
|
+
"exports": {
|
|
13
|
+
"./page": {
|
|
14
|
+
"types": "./page.d.ts"
|
|
15
|
+
},
|
|
16
|
+
"./crossbind.extension.js": "./crossbind.extension.js"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"crossbind.extension.js",
|
|
20
|
+
"native",
|
|
21
|
+
"page.d.ts"
|
|
22
|
+
],
|
|
23
|
+
"crossbind": {
|
|
24
|
+
"extension": "./crossbind.extension.js"
|
|
25
|
+
},
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"crossbind": "^2.0.0-beta.69"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"crossbind": "^2.0.0-beta.69"
|
|
31
|
+
},
|
|
32
|
+
"keywords": [
|
|
33
|
+
"crossbind-extension",
|
|
34
|
+
"crossbind",
|
|
35
|
+
"system",
|
|
36
|
+
"power",
|
|
37
|
+
"app"
|
|
38
|
+
]
|
|
39
|
+
}
|
package/page.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export {};
|
|
2
|
+
|
|
3
|
+
declare module 'crossbind/app/core' {
|
|
4
|
+
interface AppEvents {
|
|
5
|
+
/** Calls listener as the system of a desktop app goes to sleep, which it may do before the page has run, until the function it returns is called. */
|
|
6
|
+
suspend: void;
|
|
7
|
+
/** Calls listener as the system of a desktop app wakes from sleep, until the function it returns is called. */
|
|
8
|
+
resume: void;
|
|
9
|
+
}
|
|
10
|
+
}
|