stevee

My wayland statusbar
git clone git://gtms.dev/stevee
Log | Files | Refs | README | LICENSE

Input.zig (2617B)


      1 const wl = @import("wayland").client.wl;
      2 
      3 const Bar = @import("Bar.zig");
      4 const Input = @This();
      5 
      6 const state = &@import("root").state;
      7 
      8 seat: *wl.Seat,
      9 globalName: u32,
     10 
     11 pointer: struct {
     12     pointer: ?*wl.Pointer,
     13     x: i32,
     14     y: i32,
     15     bar: ?*Bar,
     16     surface: ?*wl.Surface,
     17 },
     18 
     19 pub fn create(registry: *wl.Registry, name: u32) !*Input {
     20     const self = try state.gpa.create(Input);
     21     self.seat = try registry.bind(name, wl.Seat, 7);
     22     self.globalName = name;
     23 
     24     self.pointer.pointer = null;
     25     self.pointer.bar = null;
     26     self.pointer.surface = null;
     27 
     28     self.seat.setListener(*Input, listener, self);
     29     return self;
     30 }
     31 
     32 pub fn destroy(self: *Input) void {
     33     if (self.pointer.pointer) |pointer| {
     34         pointer.release();
     35     }
     36     self.seat.release();
     37     state.gpa.destroy(self);
     38 }
     39 
     40 fn listener(seat: *wl.Seat, event: wl.Seat.Event, input: *Input) void {
     41     switch (event) {
     42         .capabilities => |data| {
     43             if (input.pointer.pointer) |pointer| {
     44                 pointer.release();
     45                 input.pointer.pointer = null;
     46             }
     47             if (data.capabilities.pointer) {
     48                 input.pointer.pointer = seat.getPointer() catch return;
     49                 input.pointer.pointer.?.setListener(
     50                     *Input,
     51                     pointerListener,
     52                     input,
     53                 );
     54             }
     55         },
     56         .name => {},
     57     }
     58 }
     59 
     60 fn pointerListener(
     61     _: *wl.Pointer,
     62     event: wl.Pointer.Event,
     63     input: *Input,
     64 ) void {
     65     switch (event) {
     66         .enter => |data| {
     67             input.pointer.x = data.surface_x.toInt();
     68             input.pointer.y = data.surface_y.toInt();
     69             const bar = state.wayland.findBar(data.surface);
     70             input.pointer.bar = bar;
     71             input.pointer.surface = data.surface;
     72         },
     73         .leave => |_| {
     74             input.pointer.bar = null;
     75             input.pointer.surface = null;
     76         },
     77         .motion => |data| {
     78             input.pointer.x = data.surface_x.toInt();
     79             input.pointer.y = data.surface_y.toInt();
     80         },
     81         .button => |data| {
     82             if (data.state != .pressed) return;
     83             if (input.pointer.bar) |bar| {
     84                 if (!bar.configured) return;
     85 
     86                 const tagsSurface = bar.tags.surface;
     87                 if (input.pointer.surface != tagsSurface) return;
     88 
     89                 const x = input.pointer.x;
     90                 if (x < bar.height * 9) {
     91                     bar.monitor.tags.handleClick(@intCast(x), input) catch return;
     92                 }
     93             }
     94         },
     95         else => {},
     96     }
     97 }