const std = @import("std"); const c = @cImport({ @cInclude("limits.h"); @cInclude("X11/Xlib.h"); @cInclude("X11/extensions/Xfixes.h"); }); pub fn main() !u8 { var watch = false; var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); const allocator = &gpa.allocator; var args = std.process.args(); defer args.deinit(); while (args.next(allocator)) |arg_or_err| { const arg = try arg_or_err; defer allocator.free(arg); if (std.mem.eql(u8, arg, "-w")) { watch = true; break; } } clipboardAction(watch) catch return 1; return 0; } pub fn clipboardAction(watch: bool) !void { const stdout = std.io.getStdOut().writer(); var display: *c.Display = c.XOpenDisplay(null).?; defer _ = c.XCloseDisplay(display); const default_screen = c.XDefaultScreen(display); var color = c.XBlackPixel(display, default_screen); var window = c.XCreateSimpleWindow(display, c.XDefaultRootWindow(display), 0, 0, 1, 1, 0, color, color); defer _ = c.XDestroyWindow(display, window); // Watch will not return if (watch) try watchClip(display, window, "CLIPBOARD", stdout); var result = (try printSelection(display, window, "CLIPBOARD", "UTF8_STRING", stdout)) or (try printSelection(display, window, "CLIPBOARD", "STRING", stdout)); if (!result) return error.ClipboardActionFailed; } fn watchClip(display: *c.Display, window: c.Window, bufname: [*c]const u8, writer: anytype) !void { var event_base: c_int = undefined; var error_base: c_int = undefined; var event: c.XEvent = undefined; var bufid = c.XInternAtom(display, bufname, c.False); _ = c.XFixesQueryExtension(display, &event_base, &error_base); _ = c.XFixesSelectSelectionInput(display, c.XDefaultRootWindow(display), bufid, c.XFixesSetSelectionOwnerNotifyMask); while (true) { _ = c.XNextEvent(display, &event); if (event.type == event_base + c.XFixesSelectionNotify and event.xselection.property == bufid) { _ = try writer.print("y", .{}); if (!try printSelection(display, window, bufname, "UTF8_STRING", writer)) _ = try printSelection(display, window, bufname, "STRING", writer); } } } fn printSelection(display: *c.Display, window: c.Window, bufname: [*c]const u8, fmtname: [*c]const u8, writer: anytype) !bool { var ressize: c_ulong = undefined; var restail: c_ulong = undefined; var resbits: c_int = undefined; var bufid = c.XInternAtom(display, bufname, c.False); var fmtid = c.XInternAtom(display, fmtname, c.False); var propid = c.XInternAtom(display, "XSEL_DATA", c.False); var incrid = c.XInternAtom(display, "INCR", c.False); var event: c.XEvent = undefined; _ = c.XSelectInput(display, window, c.PropertyChangeMask); _ = c.XConvertSelection(display, bufid, fmtid, propid, window, c.CurrentTime); _ = c.XNextEvent(display, &event); while (event.type != c.SelectionNotify or event.xselection.selection != bufid) _ = c.XNextEvent(display, &event); if (event.xselection.property > 0) { var result: [*c]u8 = undefined; _ = c.XGetWindowProperty(display, window, propid, 0, c.LONG_MAX / 4, c.True, c.AnyPropertyType, &fmtid, &resbits, &ressize, &restail, &result); defer _ = c.XFree(result); if (fmtid != incrid) try writer.print("{s}", .{result}); if (fmtid == incrid) { ressize = 1; var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); var arena_allocator = std.heap.ArenaAllocator.init(&gpa.allocator); defer arena_allocator.deinit(); var buffer = std.ArrayList(u8).init(&arena_allocator.allocator); defer buffer.deinit(); // try writer.print("BIG:", .{}); while (ressize > 0) { _ = c.XNextEvent(display, &event); while (event.type != c.PropertyNotify or event.xproperty.atom != propid or event.xproperty.state != c.PropertyNewValue) { _ = c.XNextEvent(display, &event); } _ = c.XGetWindowProperty(display, window, propid, 0, c.LONG_MAX / 4, c.True, c.AnyPropertyType, &fmtid, &resbits, &ressize, &restail, &result); //defer _ = c.XFree(result); // Creates double free error, but not sure why try buffer.appendSlice(try std.fmt.allocPrint(&arena_allocator.allocator, "{s}", .{result})); } try writer.print("{s}", .{buffer.items}); } return true; } else // request failed, e.g. owner can't convert to the target format return false; }