From 130d729af89ff402d6ed1d2ae998b946bcf38740 Mon Sep 17 00:00:00 2001 From: ZiuChen <457353192@qq.com> Date: Tue, 20 Sep 2022 00:00:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0clipboard-event?= =?UTF-8?q?=E4=B8=8D=E5=90=8C=E5=B9=B3=E5=8F=B0=E6=BA=90=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../platform/clipboard-event-handler-linux.c | 38 ++++++++++++++++ .../clipboard-event-handler-mac.swift | 24 ++++++++++ .../platform/clipboard-event-handler-win32.cs | 44 +++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 public/node_modules/clipboard-event/platform/clipboard-event-handler-linux.c create mode 100644 public/node_modules/clipboard-event/platform/clipboard-event-handler-mac.swift create mode 100644 public/node_modules/clipboard-event/platform/clipboard-event-handler-win32.cs diff --git a/public/node_modules/clipboard-event/platform/clipboard-event-handler-linux.c b/public/node_modules/clipboard-event/platform/clipboard-event-handler-linux.c new file mode 100644 index 0000000..5ef0b66 --- /dev/null +++ b/public/node_modules/clipboard-event/platform/clipboard-event-handler-linux.c @@ -0,0 +1,38 @@ +// https://stackoverflow.com/a/44992967 +// gcc -o xclipwatch xclipwatch.c -lX11 -lXfixes + +#include +#include +#include + +void WatchSelection(Display *display, Window window, const char *bufname) +{ + int event_base, error_base; + XEvent event; + Atom bufid = XInternAtom(display, bufname, False); + + assert( XFixesQueryExtension(display, &event_base, &error_base) ); + XFixesSelectSelectionInput(display, DefaultRootWindow(display), bufid, XFixesSetSelectionOwnerNotifyMask); + + while (True) + { + XNextEvent(display, &event); + + if (event.type == event_base + XFixesSelectionNotify && + ((XFixesSelectionNotifyEvent*)&event)->selection == bufid) + { + printf("CLIPBOARD_CHANGE\n"); + fflush(stdout); + } + } +} + +int main(){ + Display *display = XOpenDisplay(NULL); + Window window = DefaultRootWindow(display); + // WatchSelection(display,window,"PRIMARY"); + WatchSelection(display,window,"CLIPBOARD"); + XDestroyWindow(display, window); + XCloseDisplay(display); + return 0; +} diff --git a/public/node_modules/clipboard-event/platform/clipboard-event-handler-mac.swift b/public/node_modules/clipboard-event/platform/clipboard-event-handler-mac.swift new file mode 100644 index 0000000..e9a8a35 --- /dev/null +++ b/public/node_modules/clipboard-event/platform/clipboard-event-handler-mac.swift @@ -0,0 +1,24 @@ +// +// main.swift +// clipboard-event +// +// Created by Joonhee Lee on 2021/01/30. +// + +import Foundation +import Cocoa + +let pasteboard: NSPasteboard = NSPasteboard.general +var count: Int = pasteboard.changeCount +var saved: String = pasteboard.string(forType: .string) ?? "null" + +repeat{ + usleep(500000) + if(count < pasteboard.changeCount && saved != pasteboard.string(forType: .string)){ + usleep(100000) + count = pasteboard.changeCount + saved = pasteboard.string(forType: .string) ?? "null" + print("CLIPBOARD_CHANGE") + fflush(stdout) + } +}while true diff --git a/public/node_modules/clipboard-event/platform/clipboard-event-handler-win32.cs b/public/node_modules/clipboard-event/platform/clipboard-event-handler-win32.cs new file mode 100644 index 0000000..fa00f86 --- /dev/null +++ b/public/node_modules/clipboard-event/platform/clipboard-event-handler-win32.cs @@ -0,0 +1,44 @@ +using System; +using System.Windows.Forms; +using System.Runtime.InteropServices; + +namespace clipboard_event_handler_win32 { + internal static class NativeMethods { + //Reference https://docs.microsoft.com/en-us/windows/desktop/dataxchg/wm-clipboardupdate + public const int WM_CLIPBOARDUPDATE = 0x031D; + //Reference https://www.pinvoke.net/default.aspx/Constants.HWND + public static IntPtr HWND_MESSAGE = new IntPtr(-3); + //Reference https://www.pinvoke.net/default.aspx/user32/AddClipboardFormatListener.html + [DllImport("user32.dll", SetLastError = true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool AddClipboardFormatListener(IntPtr hwnd); + //Reference https://www.pinvoke.net/default.aspx/user32.setparent + [DllImport("user32.dll", SetLastError = true)] + public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); + } + + public sealed class ClipboardNotification { + private class NotificationForm : Form { + public NotificationForm() { + //Turn the child window into a message-only window (refer to Microsoft docs) + NativeMethods.SetParent(Handle, NativeMethods.HWND_MESSAGE); + //Place window in the system-maintained clipboard format listener list + NativeMethods.AddClipboardFormatListener(Handle); + } + + protected override void WndProc(ref Message m) { + //Listen for operating system messages + if (m.Msg == NativeMethods.WM_CLIPBOARDUPDATE) + { + Console.WriteLine("CLIPBOARD_CHANGE"); + } + //Called for any unhandled messages + base.WndProc(ref m); + } + } + + private static void Main(string[] args) { + Application.Run(new NotificationForm()); + } + } +} \ No newline at end of file