AnyEvent-I3X-Workspace-OnDemand

 view release on metacpan or  search on metacpan

lib/AnyEvent/I3X/Workspace/OnDemand.pm  view on Meta::CPAN


  $self->log($msg);

  return unless $log_all_events;

  open my $fh, '>>', $log_all_events;
  print $fh join($/, $msg, Dumper $event, "");
  close($fh);
}

method x11() {

  unless ($x11) {
    $x11 = X11::Protocol->new();
    $xroot = $x11->root;
    return $x11;
  }

  my $fh = $x11->connection->fh;
  my $sel = IO::Select->new($fh);

  if ($sel->can_read(0)) {
    eval { $x11->handle_input() };
    $x11 = X11::Protocol->new() if $@;
  }
  $xroot = $x11->root;
  return $x11;

}

method _get_string_property_from_root_window($key, $atom_type) {
  my $prop      = $self->x11->atom($key);
  my $prop_type = $self->x11->atom($atom_type);

  my ($value, $type, $format, $bytes_after)
      = $x11->GetProperty($xroot, $prop, $prop_type, 0, 1024);

  return $value if $value;
  return;
}

method _get_property_from_root_window($key) {
  my $value = $self->_get_string_property_from_root_window($key, 'UTF8_STRING');
  return $value if defined $value;
  # Allow xprop values to be set but also allow
  $value = $self->_get_string_property_from_root_window($key, 'STRING');
  return unless defined $value;
  $self->_set_property_on_root_window($key, $value);
  return $value;
}

method get_group_from_root_window() {
  my $group = $self->_get_property_from_root_window('_I3_WOD_GROUP');
  return $group if $group;
  $self->set_group_on_root_window($groups[0]);
  return $groups[0];
}

method _set_property_on_root_window($key, $value) {
  my $prop = $self->x11->atom($key);
  my $utf8 = $self->x11->atom('UTF8_STRING');

  $x11->ChangeProperty($xroot, $prop, $utf8, 8, 'Replace', $value);
  $self->restart_i3_status;
  $x11->flush;
}

method set_group_on_root_window($name) {
  $self->_set_property_on_root_window('_I3_WOD_GROUP', $name);
}

method restart_i3_status() {
  return unless $i3status;
  $i3status = $self->parse_path($i3status);
  my $pt = Proc::ProcessTable->new(enable_ttys => 0);
  my @pids = grep { ($_->exec //'') eq $i3status } @{ $pt->table };
  kill('USR1', $_->pid) foreach @pids;
}

method set_workspace_on_root_window($name) {
  $self->_set_property_on_root_window('_I3_WOD_WORKSPACE', $name);
}

method get_workspace_from_root_window() {
  my $ws = $self->_get_property_from_root_window('_I3_WOD_WORKSPACE');
  return $ws if $ws;
  return;
}



ADJUST {

  $i3 = $socket ? i3($socket) : i3();
  $i3->connect->recv or die "Error connecting to i3";

  $c = Data::Compare->new();

  $current_group = $self->get_group_from_root_window();
  $current_workspace = $self->get_workspace_from_root_window();
  $self->workspace($starting_workspace) unless $current_workspace;

  my $name;

  $self->subscribe(
    workspace => sub {

      my $event = shift;
      my $type  = $event->{change};

      $current_workspace = $event->{current}{name};
      $name              = $current_workspace;

      $self->set_workspace_on_root_window($name) unless $type eq 'empty';

      $self->log_event('workspace', $event);

      # It doesn't have anything, skip skip next;
      return if $type eq 'reload';

      return unless %workspace;
      # Don't allow access to workspace which aren't part of the current group
      if (exists $workspace{$name}{group}



( run in 2.703 seconds using v1.01-cache-2.11-cpan-600a1bdf6e4 )