98 lines
4.2 KiB
Zig
98 lines
4.2 KiB
Zig
|
const std = @import("std");
|
||
|
const c = @cImport({
|
||
|
@cInclude("limits.h");
|
||
|
@cInclude("X11/Xlib.h");
|
||
|
@cInclude("X11/extensions/Xfixes.h");
|
||
|
});
|
||
|
|
||
|
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;
|
||
|
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);
|
||
|
try writer.print("{s}", .{result});
|
||
|
}
|
||
|
}
|
||
|
return true;
|
||
|
} else // request failed, e.g. owner can't convert to the target format
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
fn watch(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);
|
||
|
|
||
|
// _ = try writer.print("{d} == {d} + {d}. {d} == {d}", .{ event.type, event_base, c.XFixesSelectionNotify, event.xselection.selection, bufid });
|
||
|
// _ = std.debug.print("{}", .{event.xselection});
|
||
|
|
||
|
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);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn clipboardAction() !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);
|
||
|
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")) {
|
||
|
try watch(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;
|
||
|
}
|