claude-hook-notify 1.2.2 → 1.3.1

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/README.md CHANGED
@@ -89,7 +89,7 @@ npx claude-hook-notify setup --events Stop,TaskCompleted,Notification,PostToolUs
89
89
  | macOS | `osascript` | 无(系统自带) |
90
90
  | macOS | `terminal-notifier` | 可选: `brew install terminal-notifier`(更好) |
91
91
  | Linux | `notify-send` | `sudo apt-get install libnotify-bin` |
92
- | Windows | PowerShell | 无(系统自带) |
92
+ | Windows | Toast 通知 (PowerShell) | 无(Windows 10+ 系统自带) |
93
93
 
94
94
  ## 原理
95
95
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claude-hook-notify",
3
- "version": "1.2.2",
3
+ "version": "1.3.1",
4
4
  "description": "🔔 Claude Code 任务完成桌面通知 — 一键安装,跨平台支持",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -18,13 +18,22 @@
18
18
  "notify-send",
19
19
  "cli"
20
20
  ],
21
- "author": "",
21
+ "author": "Mr-zj388",
22
22
  "license": "MIT",
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "https://github.com/Mr-zj388/claude-code-notify.git"
26
+ },
27
+ "homepage": "https://github.com/Mr-zj388/claude-code-notify#readme",
28
+ "bugs": {
29
+ "url": "https://github.com/Mr-zj388/claude-code-notify/issues"
30
+ },
23
31
  "engines": {
24
32
  "node": ">=16.0.0"
25
33
  },
26
34
  "files": [
27
35
  "src/",
36
+ "vendor/",
28
37
  "README.md"
29
38
  ]
30
39
  }
@@ -0,0 +1,82 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * 后台点击监听脚本
5
+ *
6
+ * 由主进程 spawn 为 detached 后台进程。
7
+ * 执行通知命令(带等待标志),检测用户点击后激活终端窗口。
8
+ *
9
+ * 参数格式:
10
+ * node activate-watcher.js <JSON配置>
11
+ *
12
+ * JSON 配置:
13
+ * {
14
+ * "platform": "win32" | "linux",
15
+ * "notifyCommand": "...",
16
+ * "notifyArgs": ["..."],
17
+ * "activateCommand": "...",
18
+ * "activateArgs": ["..."],
19
+ * "activateFallbackCommand": "...", // 可选
20
+ * "activateFallbackArgs": ["..."], // 可选
21
+ * "timeout": 120000
22
+ * }
23
+ */
24
+
25
+ const { spawn } = require("child_process");
26
+
27
+ // 120 秒超时自动退出,防僵尸进程
28
+ const config = parseConfig();
29
+ const selfTimeout = setTimeout(() => process.exit(0), config.timeout || 120000);
30
+ selfTimeout.unref();
31
+
32
+ function parseConfig() {
33
+ try {
34
+ return JSON.parse(process.argv[2] || "{}");
35
+ } catch {
36
+ process.exit(1);
37
+ }
38
+ }
39
+
40
+ function activateTerminal() {
41
+ const { activateCommand, activateArgs, activateFallbackCommand, activateFallbackArgs } = config;
42
+ if (!activateCommand) return;
43
+
44
+ try {
45
+ execFileSync(activateCommand, activateArgs || [], { stdio: "ignore", timeout: 5000 });
46
+ } catch {
47
+ // 主命令失败,尝试回退
48
+ if (activateFallbackCommand) {
49
+ try {
50
+ execFileSync(activateFallbackCommand, activateFallbackArgs || [], { stdio: "ignore", timeout: 5000 });
51
+ } catch {
52
+ // 回退也失败,静默退出
53
+ }
54
+ }
55
+ }
56
+ }
57
+
58
+ async function main() {
59
+ const { platform, notifyCommand, notifyArgs } = config;
60
+ if (!notifyCommand) process.exit(1);
61
+
62
+ if (platform === "linux") {
63
+ // notify-send --action 模式:stdout 输出 action 名
64
+ const child = spawn(notifyCommand, notifyArgs, {
65
+ stdio: ["ignore", "pipe", "ignore"],
66
+ timeout: 120000,
67
+ });
68
+
69
+ let output = "";
70
+ child.stdout.on("data", (chunk) => {
71
+ output += chunk.toString();
72
+ });
73
+
74
+ child.on("close", () => {
75
+ if (output.trim() === "default") {
76
+ activateTerminal();
77
+ }
78
+ });
79
+ }
80
+ }
81
+
82
+ main().catch(() => process.exit(1));
@@ -0,0 +1,220 @@
1
+ const { execSync, execFileSync } = require("child_process");
2
+ const os = require("os");
3
+
4
+ /**
5
+ * 终端进程名到 macOS bundleId 的映射
6
+ */
7
+ const MAC_BUNDLE_MAP = {
8
+ "Terminal": "com.apple.Terminal",
9
+ "iTerm2": "com.googlecode.iterm2",
10
+ "iTerm2-v3": "com.googlecode.iterm2",
11
+ "Electron": null, // 需进一步判断
12
+ "Code": "com.microsoft.VSCode",
13
+ "Code - Insiders": "com.microsoft.VSCodeInsiders",
14
+ "Cursor": "com.todesktop.230313mzl4w4u92",
15
+ "WarpTerminal": "dev.warp.Warp-Stable",
16
+ "Alacritty": "org.alacritty",
17
+ "kitty": "net.kovidgoyal.kitty",
18
+ "Hyper": "co.zeit.hyper",
19
+ "Tabby": "org.tabby",
20
+ };
21
+
22
+ /**
23
+ * Windows 终端进程名(小写,不含 .exe)
24
+ */
25
+ const WIN_TERMINAL_NAMES = new Set([
26
+ "windowsterminal",
27
+ "code",
28
+ "cursor",
29
+ "claude",
30
+ "cmd",
31
+ "powershell",
32
+ "pwsh",
33
+ "mintty",
34
+ "alacritty",
35
+ "wezterm-gui",
36
+ "hyper",
37
+ "tabby",
38
+ "conhost",
39
+ ]);
40
+
41
+ /**
42
+ * Linux 终端进程名
43
+ */
44
+ const LINUX_TERMINALS = new Set([
45
+ "gnome-terminal-server",
46
+ "gnome-terminal",
47
+ "konsole",
48
+ "xfce4-terminal",
49
+ "mate-terminal",
50
+ "tilix",
51
+ "terminator",
52
+ "alacritty",
53
+ "kitty",
54
+ "wezterm-gui",
55
+ "foot",
56
+ "code",
57
+ "cursor",
58
+ "hyper",
59
+ "tabby",
60
+ ]);
61
+
62
+ /**
63
+ * 沿进程树向上查找终端进程
64
+ */
65
+ function detectTerminal() {
66
+ const platform = os.platform();
67
+ try {
68
+ if (platform === "darwin") return detectMac();
69
+ if (platform === "win32") return detectWindows();
70
+ if (platform === "linux") return detectLinux();
71
+ } catch {
72
+ // 检测失败
73
+ }
74
+ return null;
75
+ }
76
+
77
+ function detectMac() {
78
+ let pid = process.ppid;
79
+ for (let i = 0; i < 20 && pid > 1; i++) {
80
+ try {
81
+ const info = execSync(`ps -o ppid=,comm= -p ${pid}`, { encoding: "utf-8" }).trim();
82
+ const match = info.match(/^\s*(\d+)\s+(.+)$/);
83
+ if (!match) break;
84
+ const ppid = parseInt(match[1], 10);
85
+ const comm = match[2].trim().split("/").pop();
86
+
87
+ if (MAC_BUNDLE_MAP[comm] !== undefined) {
88
+ const bundleId = MAC_BUNDLE_MAP[comm];
89
+ if (bundleId) {
90
+ return { name: comm, bundleId, pid, processName: comm };
91
+ }
92
+ }
93
+ pid = ppid;
94
+ } catch {
95
+ break;
96
+ }
97
+ }
98
+ // 回退:检查 TERM_PROGRAM 环境变量
99
+ const termProgram = process.env.TERM_PROGRAM;
100
+ if (termProgram) {
101
+ const map = {
102
+ "Apple_Terminal": { name: "Terminal", bundleId: "com.apple.Terminal" },
103
+ "iTerm.app": { name: "iTerm2", bundleId: "com.googlecode.iterm2" },
104
+ "vscode": { name: "Code", bundleId: "com.microsoft.VSCode" },
105
+ "WarpTerminal": { name: "Warp", bundleId: "dev.warp.Warp-Stable" },
106
+ "Hyper": { name: "Hyper", bundleId: "co.zeit.hyper" },
107
+ };
108
+ if (map[termProgram]) {
109
+ return { ...map[termProgram], pid: 0, processName: termProgram };
110
+ }
111
+ }
112
+ return null;
113
+ }
114
+
115
+ /**
116
+ * Windows: 优先环境变量,回退单次 PowerShell 批量查询
117
+ */
118
+ function detectWindows() {
119
+ // 1. 环境变量快速检测(仅保留精确 PID 的路径)
120
+ if (process.env.VSCODE_PID) {
121
+ const pid = parseInt(process.env.VSCODE_PID, 10);
122
+ if (pid > 0) return { name: "Code", pid, processName: "code.exe" };
123
+ }
124
+
125
+ // 2. WMI 遍历进程树(OS 级 ParentProcessId 始终完整,不受 spawn 方式影响)
126
+ try {
127
+ const script = [
128
+ "$cpid=" + process.pid,
129
+ "for($i=0;$i -lt 30 -and $cpid -gt 0;$i++){",
130
+ " $w=Get-CimInstance Win32_Process -Filter \"ProcessId=$cpid\" -EA SilentlyContinue",
131
+ " if(-not $w){break}",
132
+ " Write-Output \"$($w.Name)|$($w.ProcessId)|$($w.ParentProcessId)\"",
133
+ " $cpid=$w.ParentProcessId",
134
+ "}",
135
+ ].join(";");
136
+ const output = execFileSync("powershell", ["-NoProfile", "-Command", script], {
137
+ encoding: "utf-8",
138
+ timeout: 8000,
139
+ }).trim();
140
+
141
+ // 遍历整棵进程树,取最高层级(最靠近根)的终端匹配
142
+ // 避免误取 powershell/cmd 等 shell 进程 —— 它们在 Windows Terminal 下不拥有窗口
143
+ let bestMatch = null;
144
+ for (const line of output.split("\n")) {
145
+ const parts = line.trim().split("|");
146
+ if (parts.length < 3) continue;
147
+ // WMI 返回的 Name 带 .exe 后缀,去掉再匹配
148
+ const name = parts[0].replace(/\.exe$/i, "").toLowerCase();
149
+ const pid = parseInt(parts[1], 10);
150
+ if (WIN_TERMINAL_NAMES.has(name)) {
151
+ bestMatch = { name: parts[0].replace(/\.exe$/i, ""), pid, processName: parts[0] };
152
+ }
153
+ }
154
+ return bestMatch;
155
+ } catch {
156
+ // PowerShell 调用失败
157
+ }
158
+ return null;
159
+ }
160
+
161
+ function detectLinux() {
162
+ // 1. 环境变量快速检测
163
+ if (process.env.VSCODE_PID) {
164
+ const pid = parseInt(process.env.VSCODE_PID, 10);
165
+ if (pid > 0) return { name: "code", pid, processName: "code" };
166
+ }
167
+
168
+ // 2. 进程树遍历
169
+ let pid = process.ppid;
170
+ for (let i = 0; i < 20 && pid > 1; i++) {
171
+ try {
172
+ const info = execSync(`ps -o ppid=,comm= -p ${pid}`, { encoding: "utf-8" }).trim();
173
+ const match = info.match(/^\s*(\d+)\s+(.+)$/);
174
+ if (!match) break;
175
+ const ppid = parseInt(match[1], 10);
176
+ const comm = match[2].trim();
177
+
178
+ if (LINUX_TERMINALS.has(comm)) {
179
+ return { name: comm, pid, processName: comm };
180
+ }
181
+ pid = ppid;
182
+ } catch {
183
+ break;
184
+ }
185
+ }
186
+ return null;
187
+ }
188
+
189
+ /**
190
+ * 获取激活终端窗口的命令和参数
191
+ */
192
+ function getActivateCommand(terminalInfo) {
193
+ if (!terminalInfo) return null;
194
+ const platform = os.platform();
195
+
196
+ if (platform === "darwin") {
197
+ if (terminalInfo.bundleId) {
198
+ return {
199
+ command: "osascript",
200
+ args: ["-e", `tell application id "${terminalInfo.bundleId}" to activate`],
201
+ };
202
+ }
203
+ return null;
204
+ }
205
+
206
+ // Windows 使用 notify-helper.exe 自行处理激活,不需要此函数
207
+
208
+ if (platform === "linux") {
209
+ return {
210
+ command: "xdotool",
211
+ args: ["search", "--pid", String(terminalInfo.pid), "--onlyvisible", "windowactivate"],
212
+ fallbackCommand: "wmctrl",
213
+ fallbackArgs: ["-ia", String(terminalInfo.pid)],
214
+ };
215
+ }
216
+
217
+ return null;
218
+ }
219
+
220
+ module.exports = { detectTerminal, getActivateCommand };
package/src/notify.js CHANGED
@@ -235,15 +235,11 @@ async function sendNotification(options = {}) {
235
235
  return result;
236
236
  }
237
237
  } else if (platform === "win32") {
238
- // VBScript Popup: 零依赖,启动极快(<0.5 秒)
239
- method = "vbscript-popup";
240
- command = "wscript";
241
- const escapedTitle = title.replace(/"/g, '""');
242
- const escapedMessage = message.replace(/"/g, '""');
243
- const vbsScript = `CreateObject("WScript.Shell").Popup "${escapedMessage}", 5, "${escapedTitle}", 64`;
244
- tmpFile = path.join(os.tmpdir(), `claude-notify-${process.pid}.vbs`);
245
- fs.writeFileSync(tmpFile, vbsScript, "utf-8");
246
- args = ["//nologo", tmpFile];
238
+ // SnoreToast: 原生 Windows Toast 通知,UTF-16 支持,不抢焦点
239
+ method = "snoretoast";
240
+ const arch = os.arch() === "x64" ? "x64" : "x86";
241
+ command = path.join(__dirname, "..", "vendor", "snoretoast", `snoretoast-${arch}.exe`);
242
+ args = ["-t", title, "-m", message, "-appID", "Claude.Code"];
247
243
  }
248
244
 
249
245
  const result = { sent: !dryRun, method, command, args };
@@ -0,0 +1,222 @@
1
+ using System;
2
+ using System.Diagnostics;
3
+ using System.Drawing;
4
+ using System.IO;
5
+ using System.Runtime.InteropServices;
6
+ using System.Windows.Forms;
7
+
8
+ [assembly: System.Reflection.AssemblyTitle("Claude Code 完成通知")]
9
+ [assembly: System.Reflection.AssemblyProduct("Claude Code Notify")]
10
+
11
+ class NotifyHelper
12
+ {
13
+ [DllImport("user32.dll")]
14
+ static extern bool SetForegroundWindow(IntPtr hWnd);
15
+
16
+ [DllImport("user32.dll")]
17
+ static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
18
+
19
+ delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
20
+
21
+ [DllImport("user32.dll")]
22
+ static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
23
+
24
+ [DllImport("user32.dll")]
25
+ static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
26
+
27
+ [DllImport("user32.dll")]
28
+ static extern bool IsWindowVisible(IntPtr hWnd);
29
+
30
+ [DllImport("user32.dll", SetLastError = true)]
31
+ static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
32
+
33
+ [DllImport("user32.dll")]
34
+ static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
35
+
36
+ [DllImport("user32.dll")]
37
+ static extern IntPtr GetForegroundWindow();
38
+
39
+ [DllImport("kernel32.dll")]
40
+ static extern uint GetCurrentThreadId();
41
+
42
+ // 已知 GUI 终端的窗口类名(用于 PID 查找失败时的回退)
43
+ static string[] KnownWindowClasses = new string[] {
44
+ "CASCADIA_HOSTING_WINDOW_CLASS", // Windows Terminal
45
+ "mintty", // Git Bash / mintty
46
+ };
47
+
48
+ // Claude sparkle 图标 PNG(内嵌,无需外部文件)
49
+ static string IconBase64 = "iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAAAFVBMVEXZdlXabkzaaEP99vT66uXtw7jimoUHIS5GAAALV0lEQVR4nO1ai3bcuA4TaUv//8nXEkkQlDxNuo+T3XvW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AhU1QqpbQhBERouLlqSWqp/h/2kdfTBBNsnRtCRpYIdJQMIyRh9BE1NmmnqsOpAwYiYq6aPwBQ2w/NKs6/ZcHcRRD9AwWCzs2OYQSFscZNqcMQyZFwUY2zP4WW7EllhKso+Y3ghtieJqAhsFmpSzo8ikG2/KSXJEcqZkFIRJWNibAGOMBtqQYhkNMXEU5stBElDujckSqTeuPGcIZUhFRVXKRZgp5KGkTrzdpLWlEZmaVcm0aBg29jQ6zh7JS3PAWOSAK0w3mgBI4hSWRI2kxDt8C/jpMFVqYod1tbHLCaOXYRZYsrNJCs3rPr4i3akVmW6j5NdVBdks+NzkFoyVS5FLPT2TJvdXbFI4XSbjMaiguLzpJMXVJSoSpKLdjVKv8mH5jRC5t1ox3ZwGygMkWInNSJIPom4FSQZKfcNhqnRNMVBt2ETU6RLhAEXc9GdpeHkuZ5D87ETwkfNCsPM6UqPT5Th+Xis8R74i5P5XTpB6im8tj5EGgWhjU+j8NIyIbPE2A27mk0Lf8VryABzB2qlOzYtQn3jkuEASwOHrDyGkfn27YFOHL3xTRbX29XrVNiscYMgX5djGyTl42fM/Wl1f1muarlQAAAAASUVORK5CYII=";
50
+
51
+ static IntPtr targetHandle = IntPtr.Zero;
52
+ static NotifyIcon notify;
53
+
54
+ static void Main(string[] args)
55
+ {
56
+ string title = args.Length > 0 ? args[0] : "Claude Code";
57
+ string message = args.Length > 1 ? args[1] : "任务完成";
58
+ string targetName = args.Length > 2 ? args[2] : "";
59
+ int targetPid = 0;
60
+ if (args.Length > 3) int.TryParse(args[3], out targetPid);
61
+
62
+ // 策略 1:通过 PID 精确定位终端窗口(多实例场景)
63
+ if (targetPid > 0)
64
+ {
65
+ try
66
+ {
67
+ using (var proc = Process.GetProcessById(targetPid))
68
+ {
69
+ if (proc.MainWindowHandle != IntPtr.Zero)
70
+ {
71
+ targetHandle = proc.MainWindowHandle;
72
+ }
73
+ else
74
+ {
75
+ // MainWindowHandle 可能为零(如 Windows Terminal),用 EnumWindows 查找
76
+ targetHandle = FindWindowByPid(targetPid);
77
+ }
78
+ }
79
+ }
80
+ catch { /* 进程可能已退出,回退到名称查找 */ }
81
+ }
82
+
83
+ // 策略 2:通过进程名查找(回退方案)
84
+ if (targetHandle == IntPtr.Zero && !string.IsNullOrEmpty(targetName))
85
+ {
86
+ foreach (var name in targetName.Split(','))
87
+ {
88
+ try
89
+ {
90
+ var procs = Process.GetProcessesByName(name.Trim());
91
+ try
92
+ {
93
+ foreach (var p in procs)
94
+ {
95
+ if (p.MainWindowHandle != IntPtr.Zero)
96
+ {
97
+ targetHandle = p.MainWindowHandle;
98
+ break;
99
+ }
100
+ // MainWindowHandle 为零时尝试 EnumWindows 查找
101
+ IntPtr hwnd = FindWindowByPid(p.Id);
102
+ if (hwnd != IntPtr.Zero)
103
+ {
104
+ targetHandle = hwnd;
105
+ break;
106
+ }
107
+ }
108
+ }
109
+ finally
110
+ {
111
+ foreach (var p in procs) p.Dispose();
112
+ }
113
+ if (targetHandle != IntPtr.Zero) break;
114
+ }
115
+ catch { }
116
+ }
117
+ }
118
+
119
+ // 策略 3:通过已知窗口类名查找(兜底方案)
120
+ if (targetHandle == IntPtr.Zero)
121
+ {
122
+ foreach (var cls in KnownWindowClasses)
123
+ {
124
+ IntPtr hwnd = FindWindow(cls, null);
125
+ if (hwnd != IntPtr.Zero)
126
+ {
127
+ targetHandle = hwnd;
128
+ break;
129
+ }
130
+ }
131
+ }
132
+
133
+ notify = new NotifyIcon();
134
+ notify.Icon = LoadEmbeddedIcon();
135
+ notify.Visible = true;
136
+ notify.BalloonTipTitle = title;
137
+ notify.BalloonTipText = message;
138
+ notify.BalloonTipIcon = ToolTipIcon.None;
139
+
140
+ notify.BalloonTipClicked += delegate { ActivateAndExit(); };
141
+ notify.Click += delegate { ActivateAndExit(); };
142
+ notify.BalloonTipClosed += delegate { Cleanup(); };
143
+
144
+ notify.ShowBalloonTip(15000);
145
+
146
+ var timer = new Timer();
147
+ timer.Interval = 60000;
148
+ timer.Tick += delegate { Cleanup(); };
149
+ timer.Start();
150
+
151
+ Application.Run();
152
+ }
153
+
154
+ static IntPtr FindWindowByPid(int pid)
155
+ {
156
+ IntPtr found = IntPtr.Zero;
157
+ EnumWindows((hWnd, lParam) =>
158
+ {
159
+ uint procId;
160
+ GetWindowThreadProcessId(hWnd, out procId);
161
+ if ((int)procId == pid && IsWindowVisible(hWnd))
162
+ {
163
+ found = hWnd;
164
+ return false;
165
+ }
166
+ return true;
167
+ }, IntPtr.Zero);
168
+ return found;
169
+ }
170
+
171
+ static Icon LoadEmbeddedIcon()
172
+ {
173
+ try
174
+ {
175
+ var bytes = Convert.FromBase64String(IconBase64);
176
+ using (var ms = new MemoryStream(bytes))
177
+ {
178
+ var bmp = new Bitmap(ms);
179
+ var resized = new Bitmap(bmp, 32, 32);
180
+ return Icon.FromHandle(resized.GetHicon());
181
+ }
182
+ }
183
+ catch
184
+ {
185
+ return SystemIcons.Information;
186
+ }
187
+ }
188
+
189
+ static void ActivateAndExit()
190
+ {
191
+ if (targetHandle != IntPtr.Zero)
192
+ {
193
+ ShowWindow(targetHandle, 9); // SW_RESTORE
194
+ // 使用 AttachThreadInput 确保 SetForegroundWindow 成功
195
+ // (Windows 限制后台进程直接抢占前台焦点)
196
+ uint unusedPid;
197
+ uint targetThread = GetWindowThreadProcessId(targetHandle, out unusedPid);
198
+ uint curThread = GetCurrentThreadId();
199
+ if (curThread != targetThread)
200
+ {
201
+ AttachThreadInput(curThread, targetThread, true);
202
+ SetForegroundWindow(targetHandle);
203
+ AttachThreadInput(curThread, targetThread, false);
204
+ }
205
+ else
206
+ {
207
+ SetForegroundWindow(targetHandle);
208
+ }
209
+ }
210
+ Cleanup();
211
+ }
212
+
213
+ static void Cleanup()
214
+ {
215
+ if (notify != null)
216
+ {
217
+ notify.Visible = false;
218
+ notify.Dispose();
219
+ }
220
+ Application.ExitThread();
221
+ }
222
+ }
@@ -0,0 +1,166 @@
1
+ // Retrieved from https://github.com/KDE/snoretoast/blob/master/COPYING.LGPL-3 version 0.7.0
2
+ GNU LESSER GENERAL PUBLIC LICENSE
3
+ Version 3, 29 June 2007
4
+
5
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
6
+ Everyone is permitted to copy and distribute verbatim copies
7
+ of this license document, but changing it is not allowed.
8
+
9
+
10
+ This version of the GNU Lesser General Public License incorporates
11
+ the terms and conditions of version 3 of the GNU General Public
12
+ License, supplemented by the additional permissions listed below.
13
+
14
+ 0. Additional Definitions.
15
+
16
+ As used herein, "this License" refers to version 3 of the GNU Lesser
17
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
18
+ General Public License.
19
+
20
+ "The Library" refers to a covered work governed by this License,
21
+ other than an Application or a Combined Work as defined below.
22
+
23
+ An "Application" is any work that makes use of an interface provided
24
+ by the Library, but which is not otherwise based on the Library.
25
+ Defining a subclass of a class defined by the Library is deemed a mode
26
+ of using an interface provided by the Library.
27
+
28
+ A "Combined Work" is a work produced by combining or linking an
29
+ Application with the Library. The particular version of the Library
30
+ with which the Combined Work was made is also called the "Linked
31
+ Version".
32
+
33
+ The "Minimal Corresponding Source" for a Combined Work means the
34
+ Corresponding Source for the Combined Work, excluding any source code
35
+ for portions of the Combined Work that, considered in isolation, are
36
+ based on the Application, and not on the Linked Version.
37
+
38
+ The "Corresponding Application Code" for a Combined Work means the
39
+ object code and/or source code for the Application, including any data
40
+ and utility programs needed for reproducing the Combined Work from the
41
+ Application, but excluding the System Libraries of the Combined Work.
42
+
43
+ 1. Exception to Section 3 of the GNU GPL.
44
+
45
+ You may convey a covered work under sections 3 and 4 of this License
46
+ without being bound by section 3 of the GNU GPL.
47
+
48
+ 2. Conveying Modified Versions.
49
+
50
+ If you modify a copy of the Library, and, in your modifications, a
51
+ facility refers to a function or data to be supplied by an Application
52
+ that uses the facility (other than as an argument passed when the
53
+ facility is invoked), then you may convey a copy of the modified
54
+ version:
55
+
56
+ a) under this License, provided that you make a good faith effort to
57
+ ensure that, in the event an Application does not supply the
58
+ function or data, the facility still operates, and performs
59
+ whatever part of its purpose remains meaningful, or
60
+
61
+ b) under the GNU GPL, with none of the additional permissions of
62
+ this License applicable to that copy.
63
+
64
+ 3. Object Code Incorporating Material from Library Header Files.
65
+
66
+ The object code form of an Application may incorporate material from
67
+ a header file that is part of the Library. You may convey such object
68
+ code under terms of your choice, provided that, if the incorporated
69
+ material is not limited to numerical parameters, data structure
70
+ layouts and accessors, or small macros, inline functions and templates
71
+ (ten or fewer lines in length), you do both of the following:
72
+
73
+ a) Give prominent notice with each copy of the object code that the
74
+ Library is used in it and that the Library and its use are
75
+ covered by this License.
76
+
77
+ b) Accompany the object code with a copy of the GNU GPL and this license
78
+ document.
79
+
80
+ 4. Combined Works.
81
+
82
+ You may convey a Combined Work under terms of your choice that,
83
+ taken together, effectively do not restrict modification of the
84
+ portions of the Library contained in the Combined Work and reverse
85
+ engineering for debugging such modifications, if you also do each of
86
+ the following:
87
+
88
+ a) Give prominent notice with each copy of the Combined Work that
89
+ the Library is used in it and that the Library and its use are
90
+ covered by this License.
91
+
92
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
93
+ document.
94
+
95
+ c) For a Combined Work that displays copyright notices during
96
+ execution, include the copyright notice for the Library among
97
+ these notices, as well as a reference directing the user to the
98
+ copies of the GNU GPL and this license document.
99
+
100
+ d) Do one of the following:
101
+
102
+ 0) Convey the Minimal Corresponding Source under the terms of this
103
+ License, and the Corresponding Application Code in a form
104
+ suitable for, and under terms that permit, the user to
105
+ recombine or relink the Application with a modified version of
106
+ the Linked Version to produce a modified Combined Work, in the
107
+ manner specified by section 6 of the GNU GPL for conveying
108
+ Corresponding Source.
109
+
110
+ 1) Use a suitable shared library mechanism for linking with the
111
+ Library. A suitable mechanism is one that (a) uses at run time
112
+ a copy of the Library already present on the user's computer
113
+ system, and (b) will operate properly with a modified version
114
+ of the Library that is interface-compatible with the Linked
115
+ Version.
116
+
117
+ e) Provide Installation Information, but only if you would otherwise
118
+ be required to provide such information under section 6 of the
119
+ GNU GPL, and only to the extent that such information is
120
+ necessary to install and execute a modified version of the
121
+ Combined Work produced by recombining or relinking the
122
+ Application with a modified version of the Linked Version. (If
123
+ you use option 4d0, the Installation Information must accompany
124
+ the Minimal Corresponding Source and Corresponding Application
125
+ Code. If you use option 4d1, you must provide the Installation
126
+ Information in the manner specified by section 6 of the GNU GPL
127
+ for conveying Corresponding Source.)
128
+
129
+ 5. Combined Libraries.
130
+
131
+ You may place library facilities that are a work based on the
132
+ Library side by side in a single library together with other library
133
+ facilities that are not Applications and are not covered by this
134
+ License, and convey such a combined library under terms of your
135
+ choice, if you do both of the following:
136
+
137
+ a) Accompany the combined library with a copy of the same work based
138
+ on the Library, uncombined with any other library facilities,
139
+ conveyed under the terms of this License.
140
+
141
+ b) Give prominent notice with the combined library that part of it
142
+ is a work based on the Library, and explaining where to find the
143
+ accompanying uncombined form of the same work.
144
+
145
+ 6. Revised Versions of the GNU Lesser General Public License.
146
+
147
+ The Free Software Foundation may publish revised and/or new versions
148
+ of the GNU Lesser General Public License from time to time. Such new
149
+ versions will be similar in spirit to the present version, but may
150
+ differ in detail to address new problems or concerns.
151
+
152
+ Each version is given a distinguishing version number. If the
153
+ Library as you received it specifies that a certain numbered version
154
+ of the GNU Lesser General Public License "or any later version"
155
+ applies to it, you have the option of following the terms and
156
+ conditions either of that published version or of any later version
157
+ published by the Free Software Foundation. If the Library as you
158
+ received it does not specify a version number of the GNU Lesser
159
+ General Public License, you may choose any version of the GNU Lesser
160
+ General Public License ever published by the Free Software Foundation.
161
+
162
+ If the Library as you received it specifies that a proxy can decide
163
+ whether future versions of the GNU Lesser General Public License shall
164
+ apply, that proxy's public statement of acceptance of any version is
165
+ permanent authorization for you to choose that version for the
166
+ Library.