#! perl


sub on_start {
   my ($self) = @_;

    #$self->{resource} = [map $self->resource ("+$_"), 0 .. urxvt::NUM_RESOURCES - 1];
    #$self->resource (int_bwidth => 0);
    $self->resource (name => "URxvt.kuake");
    #$self->resource (pty_fd => -1);

    #default window position
    $self->{pos_x} = $self->x_resource("pos_x") || 0;
    $self->{pos_y} = $self->x_resource("pos_y") || 0;


    $self->{state}   = 16; #magic number

    if($self->{argv}[0] || $self->x_resource("key")) {
      my @mods = split('-', $self->{argv}[0] || $self->x_resource("key"));
      for my $mod (@mods) {
         if($mod eq "C") {
            $self->{state} |= urxvt::ControlMask;
         } elsif($mod eq "S") {
            $self->{state} |= urxvt::ShiftMask;
         } elsif($mod eq "M") {
            $self->{state} |= $self->ModMetaMask;
         } elsif($mod =~ /^\w+$/) {
            $self->{key} = $mod;
         } elsif($mod ne "-" && $mod ne " ") {
            warn("$mod is invalid in $self->{name}<$self->{argv}[0]>\n");
         }
      }
    }else{ 
	#default F1
	$self->{key}  = "F1"; 
	warn("Kuake: option URxvt.kuake.key is empty, using F1 as key binding.\n");
	warn("Kuake: possible values 'C'-control 'M'-meta 'S'-shift example: 'URxvt.kuake.key: S-C-M-F1' \n");
	warn("Kuake: also possible setup default window position: URxvt.kuake.pos_x URxvt.kuake.pos_y\n");
    }


   $self->{keysym} = $self->XStringToKeysym ($self->{key})
      or urxvt::fatal "cannot convert requested kuake wake-up key '$self->{key}' to keysym, unable to continue.\n";

   $self->{keycode} = $self->XKeysymToKeycode ($self->{keysym})
      or urxvt::fatal "cannot convert requested kuake wake-up key '$self->{key}' to keycode, unable to continue.\n";

   $self->XGrabKey ($self->{keycode},$self->{state}, $self->DefaultRootWindow);
    #or  urxvt::fatal "cannot XGrabKey '$self->{key}' with modifier '$self->{state}' , unable to continue.\n";
    #warn ("XGrabKey must return error or succsess, otherwise impossible to check is key already binded or not.\n");

   #hide terminal window at startup
   $self->XUnmapWindow ($self->parent);

   $self->{unmap_me} = 1;

    #undecorate window
    my $atom = $self->XInternAtom ("_MOTIF_WM_HINTS");
    my ($type, $format, $items) = $self->XGetWindowProperty ($self->parent, $atom);
    my @hints;
	$hints[0]=2;
	$hints[1]=0;
	$hints[2]=0;
	$hints[3]=0;
	$hints[4]=0;
    $items = pack "l!*", @hints;
    $format=32;
    $type=$atom; #286
    $self->XChangeProperty ($self->parent, $atom, $type, $format, $items);
    #$self->XMoveResizeWindow($self->parent,0,30,1680,512);# (rxvt_term *term, Window window, int x, int y, unsigned int width, unsigned int height)

    #_NET_WM_STATE(ATOM) = _NET_WM_STATE_STICKY, _NET_WM_STATE_SKIP_TASKBAR, _NET_WM_STATE_ABOV
    my $atom = $self->XInternAtom ("_NET_WM_STATE");
    my ($type, $format, $items) = $self->XGetWindowProperty ($self->parent, $atom);
    my @hints;
	$hints[0]=$self->XInternAtom ("_NET_WM_STATE_STICKY");
	$hints[1]=$self->XInternAtom ("_NET_WM_STATE_SKIP_TASKBAR");
	$hints[2]=$self->XInternAtom ("_NET_WM_STATE_ABOVE");
    $items = pack "l!*", @hints;
    $format=32;
    $type=4; #type atom
   $self->XChangeProperty ($self->parent, $atom, $type, $format, $items);

   ()
}

sub on_map_notify {
   my ($self) = @_;

   # suppress initial map event
   $self->XUnmapWindow ($self->parent)
      if delete $self->{unmap_me};
   
   ()
}

sub on_root_event {
   my ($self, $event) = @_;
   my $state = $event->{state}&(urxvt::ShiftMask|urxvt::ControlMask|urxvt::Mod1Mask|urxvt::Mod2Mask|urxvt::Mod3Mask|urxvt::Mod4Mask|urxvt::Mod5Mask);

   #print "$event->{keycode} == $self->{keycode} $state == $self->{state} $self->{pos_x},$self->{pos_y}\n";
   if ($event->{type} == urxvt::KeyPress && $event->{keycode} == $self->{keycode} && $state == $self->{state}){

   $self->mapped
      ? $self->XUnmapWindow ($self->parent)
      : $self->XMapWindow ($self->parent);

    if(!$self->mapped){
	$self->XMoveResizeWindow($self->parent,$self->{pos_x},$self->{pos_y},$self->width(),$self->height());
	}
   #return 0
   }
   ()
}

sub on_destroy {
   my ($self) = @_;

   $self->XUngrabKey ($self->XKeysymToKeycode ($self->{keysym}), $self->{state}, $self->DefaultRootWindow)
      if $self->{keysym};

   ()
}
