add linux watch

This commit is contained in:
Emil Lerch 2021-11-08 20:42:46 -08:00
parent 121cfb6864
commit 9249ad092e
Signed by: lobo
GPG Key ID: A7B62D657EF764F8
2 changed files with 39 additions and 0 deletions

View File

@ -18,6 +18,7 @@ pub fn build(b: *std.build.Builder) void {
exe.linkLibC();
exe.addIncludeDir("/usr/include/");
exe.linkSystemLibrary("X11");
exe.linkSystemLibrary("Xfixes");
}
if (std.builtin.os.tag == .windows) {
// exe.linkLibC();

View File

@ -2,6 +2,7 @@ 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 {
@ -45,6 +46,31 @@ fn printSelection(display: *c.Display, window: c.Window, bufname: [*c]const u8,
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 main() !u8 {
const stdout = std.io.getStdOut().writer();
var display: *c.Display = c.XOpenDisplay(null).?;
@ -53,6 +79,18 @@ pub fn main() !u8 {
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)