vinput: refactors and set window name
This commit is contained in:
parent
1a367a4ee3
commit
f54b38311a
|
@ -28,6 +28,7 @@ pub fn init() !ClipboardConnection {
|
||||||
screen.*.black_pixel,
|
screen.*.black_pixel,
|
||||||
screen.*.white_pixel,
|
screen.*.white_pixel,
|
||||||
);
|
);
|
||||||
|
_ = c.XStoreName(dpy, win, "vinput");
|
||||||
|
|
||||||
return .{
|
return .{
|
||||||
.dpy = dpy,
|
.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));
|
_ = c.XSendEvent(self.dpy, ev.requestor, 0, 0, @ptrCast(*c.XEvent, &ev));
|
||||||
if (sent_data) {
|
if (sent_data) {
|
||||||
var real: c.Atom = undefined;
|
if (ffi.xGetWindowName(self.dpy, xsr.requestor)) |name| {
|
||||||
var format: c_int = 0;
|
defer _ = c.XFree(name.ptr);
|
||||||
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);
|
|
||||||
|
|
||||||
log.info("sent clipboard to {s}", .{name});
|
log.info("sent clipboard to {s}", .{name});
|
||||||
} else {
|
} 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.
|
/// Get the current text in the clipboard. Must be freed using XFree.
|
||||||
pub fn getText(self: ClipboardConnection) !?[]u8 {
|
pub fn getText(self: ClipboardConnection) !?[]u8 {
|
||||||
|
log.info("reading clipboard", .{});
|
||||||
const utf8 = c.XInternAtom(self.dpy, "UTF8_STRING", 0);
|
const utf8 = c.XInternAtom(self.dpy, "UTF8_STRING", 0);
|
||||||
if (try self.getContentForType(utf8)) |data| return data;
|
if (try self.getContentForType(utf8)) |data| return data;
|
||||||
|
|
||||||
|
|
|
@ -20,3 +20,31 @@ pub fn checkXError(dpy: *c.Display, code: c_int) !void {
|
||||||
log.err("X: {s}", .{buf});
|
log.err("X: {s}", .{buf});
|
||||||
return error.XError;
|
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)];
|
||||||
|
}
|
||||||
|
|
|
@ -2,7 +2,9 @@ const std = @import("std");
|
||||||
const c = @import("ffi.zig").c;
|
const c = @import("ffi.zig").c;
|
||||||
const ClipboardConnection = @import("ClipboardConnection.zig");
|
const ClipboardConnection = @import("ClipboardConnection.zig");
|
||||||
|
|
||||||
pub const log_level = .debug;
|
pub const std_options = struct {
|
||||||
|
pub const log_level = .debug;
|
||||||
|
};
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
|
@ -39,6 +41,8 @@ pub fn main() !void {
|
||||||
|
|
||||||
if (cp_data) |data| {
|
if (cp_data) |data| {
|
||||||
try file.writeAll(data);
|
try file.writeAll(data);
|
||||||
|
} else {
|
||||||
|
std.log.info("clipboard empty", .{});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue