vinput: refactors and set window name

This commit is contained in:
LordMZTE 2023-03-16 08:18:45 +01:00
parent 1a367a4ee3
commit f54b38311a
Signed by: LordMZTE
GPG Key ID: B64802DC33A64FF6
3 changed files with 37 additions and 24 deletions

View File

@ -28,6 +28,7 @@ pub fn init() !ClipboardConnection {
screen.*.black_pixel,
screen.*.white_pixel,
);
_ = c.XStoreName(dpy, win, "vinput");
return .{
.dpy = dpy,
@ -121,29 +122,8 @@ pub fn provide(self: ClipboardConnection, data: []const u8) !void {
_ = c.XSendEvent(self.dpy, ev.requestor, 0, 0, @ptrCast(*c.XEvent, &ev));
if (sent_data) {
var real: c.Atom = undefined;
var format: c_int = 0;
var n: c_ulong = 0;
var extra: c_ulong = 0;
var name_cstr: [*c]u8 = undefined;
_ = c.XGetWindowProperty(
self.dpy,
xsr.requestor,
c.XA_WM_NAME,
0,
~@as(c_int, 0),
0,
c.AnyPropertyType,
&real,
&format,
&n,
&extra,
&name_cstr,
);
if (name_cstr != null) {
defer _ = c.XFree(name_cstr);
const name = std.mem.span(name_cstr);
if (ffi.xGetWindowName(self.dpy, xsr.requestor)) |name| {
defer _ = c.XFree(name.ptr);
log.info("sent clipboard to {s}", .{name});
} else {
@ -163,6 +143,7 @@ pub fn provide(self: ClipboardConnection, data: []const u8) !void {
/// Get the current text in the clipboard. Must be freed using XFree.
pub fn getText(self: ClipboardConnection) !?[]u8 {
log.info("reading clipboard", .{});
const utf8 = c.XInternAtom(self.dpy, "UTF8_STRING", 0);
if (try self.getContentForType(utf8)) |data| return data;

View File

@ -20,3 +20,31 @@ pub fn checkXError(dpy: *c.Display, code: c_int) !void {
log.err("X: {s}", .{buf});
return error.XError;
}
/// Result must be freed using XFree
pub fn xGetWindowName(dpy: *c.Display, win: c.Window) ?[]u8 {
var real: c.Atom = undefined;
var format: c_int = 0;
var n: c_ulong = 0;
var extra: c_ulong = 0;
var name_cstr: [*c]u8 = undefined;
_ = c.XGetWindowProperty(
dpy,
win,
c.XA_WM_NAME,
0,
~@as(c_int, 0),
0,
c.AnyPropertyType,
&real,
&format,
&n,
&extra,
&name_cstr,
);
if (name_cstr == null)
return null;
return name_cstr[0..@intCast(usize, n)];
}

View File

@ -2,7 +2,9 @@ const std = @import("std");
const c = @import("ffi.zig").c;
const ClipboardConnection = @import("ClipboardConnection.zig");
pub const log_level = .debug;
pub const std_options = struct {
pub const log_level = .debug;
};
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
@ -39,6 +41,8 @@ pub fn main() !void {
if (cp_data) |data| {
try file.writeAll(data);
} else {
std.log.info("clipboard empty", .{});
}
}