Commit 518c4b9e authored by epsi sayidina's avatar epsi sayidina

herbstluftwm idle event

parent 03ce009e
#!/usr/bin/env bash
# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----
# pipe
function handle_command_event() {
local monitor=$1
shift
local event=$@
# find out event origin
IFS=$'\t' column=($event);
origin=${column[0]}
# find out event origin
case $origin in
reload)
pkill dzen2
;;
quit_panel)
exit
;;
tag*)
# http://www.tldp.org/LDP/abs/html/x17837.html#HERESTRINGSREF
# echo "resetting tags" >&2
set_tag_value $monitor
;;
focus_changed|window_title_changed)
set_windowtitle "${column[2]}"
;;
esac
}
function init_content() {
monitor=$1
# initialize statusbar before loop
set_tag_value $monitor
set_windowtitle ''
get_statusbar_text $monitor
echo $buffer
}
function walk_content() {
monitor=$1
# start a pipe
command_in='herbstclient --idle'
# wait for each event
$command_in | while read event; do
handle_command_event $monitor "$event"
get_statusbar_text $monitor
echo $buffer
done
}
function run_dzen2() {
monitor=$1
shift
parameters=$@
command_out="dzen2 $parameters"
{
init_content $monitor
walk_content $monitor # loop for each event
} | $command_out
}
function detach_dzen2() {
monitor=$1
shift
parameters=$@
run_dzen2 $monitor $parameters &
}
function detach_transset() {
{
sleep 1
exec `(transset .8 -n dzentop >/dev/null)`
} &
}
module MyPipeHandler
( detachDzen2
, detachTransset
) where
import System.Process
import System.Posix.Types
import System.Exit
import GHC.IO.Handle
import System.IO
import System.Posix.Process
import Control.Concurrent
import Control.Monad
-- cabal install split
import Data.List.Split
import MyOutput
-- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
-- helper
wSleep :: Int -> IO ()
wSleep mySecond = threadDelay (1000000 * mySecond)
-- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
-- pipe
handleCommandEvent :: Int -> String -> IO ()
handleCommandEvent monitor event
| origin == "reload" = do system("pkill dzen2"); return ()
| origin == "quit_panel" = do exitSuccess; return ()
| elem origin tagCmds = do setTagValue monitor
| elem origin titleCmds = do setWindowtitle (column !! 2)
where
tagCmds = ["tag_changed", "tag_flags", "tag_added", "tag_removed"]
titleCmds = ["window_title_changed", "focus_changed"]
-- find out event origin
column = splitOn "\t" event
origin = column !! 0
initContent :: Int -> Handle -> IO ()
initContent monitor pipe_in = do
-- initialize statusbar before loop
setTagValue monitor
setWindowtitle ""
text <- getStatusbarText monitor
hPutStrLn pipe_in text
hFlush pipe_in
walkContent :: Int -> Handle -> IO ()
walkContent monitor pipe_in = do
let command_in = "herbstclient"
(_, Just pipe_out, _, ph) <-
createProcess (proc command_in ["--idle"])
{ std_out = CreatePipe }
forever $ do
-- wait for next event
event <- hGetLine pipe_out
handleCommandEvent monitor event
text <- getStatusbarText monitor
hPutStrLn pipe_in text
hFlush pipe_in
hClose pipe_out
runDzen2 :: Int -> [String] -> IO ()
runDzen2 monitor parameters = do
let command_out = "dzen2"
(Just pipe_in, _, _, ph) <-
createProcess (proc command_out parameters)
{ std_in = CreatePipe }
initContent monitor pipe_in
walkContent monitor pipe_in -- loop for each event
hClose pipe_in
detachDzen2 :: Int -> [String] -> IO ProcessID
detachDzen2 monitor parameters = forkProcess
$ runDzen2 monitor parameters
detachTransset :: IO ProcessID
detachTransset = forkProcess $ do
wSleep 1
system "transset .8 -n dzentop >/dev/null"
return ()
......@@ -13,7 +13,7 @@ local _M = {}
function _M.handle_command_event(monitor, event)
-- find out event origin
column = common.split(event, "\t")
origin = column[0]
origin = column[1] -- non zero based
tag_cmds = {'tag_changed', 'tag_flags', 'tag_added', 'tag_removed'}
title_cmds = {'window_title_changed', 'focus_changed'}
......@@ -25,7 +25,7 @@ function _M.handle_command_event(monitor, event)
elseif common.has_value(tag_cmds, origin) then
output.set_tag_value(monitor)
elseif common.has_value(title_cmds, origin) then
output.set_windowtitle(column[2])
output.set_windowtitle(column[3])
end
end
......
-- luaposix available in AUR
local posix = require "posix"
local common = require('.common')
local helper = require('.helper')
local output = require('.output')
local _M = {}
-- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
-- pipe
function _M.handle_command_event(monitor, event)
-- find out event origin
column = common.split(event, "\t")
origin = column[1] -- non zero based
tag_cmds = {'tag_changed', 'tag_flags', 'tag_added', 'tag_removed'}
title_cmds = {'window_title_changed', 'focus_changed'}
if origin == 'reload' then
os.execute('pkill dzen2')
elseif origin == 'quit_panel' then
os.exit()
elseif common.has_value(tag_cmds, origin) then
output.set_tag_value(monitor)
elseif common.has_value(title_cmds, origin) then
output.set_windowtitle(column[3])
end
end
function _M.init_content(monitor, process)
-- initialize statusbar before loop
output.set_tag_value(monitor)
output.set_windowtitle('')
local text = output.get_statusbar_text(monitor)
process:write(text .. "\n")
process:flush()
end
function _M.walk_content(monitor, process)
-- start a pipe
command_in = 'herbstclient --idle'
local pipe_in = assert(io.popen(command_in, 'r'))
local text = ''
-- wait for each event
for event in pipe_in:lines() do
_M.handle_command_event(monitor, event)
text = output.get_statusbar_text(monitor)
process:write(text .. "\n")
process:flush()
end -- for loop
pipein:close()
end
function _M.run_dzen2(monitor, parameters)
local command_out = 'dzen2 ' .. parameters
local pipe_out = assert(io.popen(command_out, 'w'))
_M.init_content(monitor, pipe_out)
_M.walk_content(monitor, pipe_out) -- loop for each event
pipe_out:close()
end
function _M.detach_dzen2(monitor, parameters)
local pid = posix.fork()
if pid == 0 then -- this is the child process
_M.run_dzen2(monitor, parameters)
else -- this is the parent process
-- nothing
end
end
function _M.detach_transset()
local pid = posix.fork()
if pid == 0 then -- this is the child process
common.sleep(1)
os.execute('transset .8 -n dzentop >/dev/null')
else -- this is the parent process
-- nothing
end
end
-- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- -----
-- return
return _M
package pipehandler;
use warnings;
use strict;
use IO::Pipe;
use File::Basename;
use lib dirname(__FILE__);
use output;
# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----
# pipe
sub handle_command_event {
my $monitor = shift;
my $event = shift;
# find out event origin
my @column = split(/\t/, $event);
my $origin = $column[0];
if ($origin eq 'reload') {
system('pkill dzen2');
} elsif ($origin eq 'quit_panel') {
exit;
} elsif ( # avoiding the unstable ~~ smartmatch operator
($origin eq 'tag_changed')
or ($origin eq 'tag_flags')
or ($origin eq 'tag_added')
or ($origin eq 'tag_removed')
) {
output::set_tag_value($monitor);
} elsif ( ($origin eq 'window_title_changed')
or ($origin eq 'focus_changed')
) {
output::set_windowtitle($column[2]);
}
}
sub init_content {
my $monitor = shift;
my $pipe_out = shift;
# initialize statusbar before loop
output::set_tag_value($monitor);
output::set_windowtitle('');
my $text = output::get_statusbar_text($monitor);
print $pipe_out $text."\n";
flush $pipe_out;
}
sub walk_content {
my $monitor = shift;
my $pipe_out = shift;
# start a pipe
my $pipe_in = IO::Pipe->new();
my $command = 'herbstclient --idle';
my $handle = $pipe_in->reader($command);
my $text = '';
my $event = '';
while(<$pipe_in>) {
# wait for next event
$event = $_;
handle_command_event($monitor, $event);
$text = output::get_statusbar_text($monitor);
print $pipe_out $text;
flush $pipe_out;
}
$pipe_in->close();
}
sub run_dzen2 {
my $monitor = shift;
my $parameters = shift;
my $pipe_out = IO::Pipe->new();
my $command = "dzen2 $parameters";
my $handle = $pipe_out->writer($command);
init_content($monitor, $pipe_out);
walk_content($monitor, $pipe_out); # loop for each event
$pipe_out->close();
}
sub detach_dzen2 {
my $monitor = shift;
my $parameters = shift;
my $pid = fork;
return if $pid; # in the parent process
run_dzen2($monitor, $parameters);
exit;
}
sub detach_transset {
my $pid = fork;
return if $pid; # in the parent process
sleep 1;
system('transset .8 -n dzentop >/dev/null');
exit;
}
# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----
# end of perl module
1;
<?php # using PHP7
require_once(__DIR__.'/output.php');
# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----
# pipe
function handle_command_event($monitor, $event)
{
// find out event origin
$column = explode("\t", $event);
$origin = $column[0];
switch($origin) {
case 'reload':
system('pkill dzen2');
break;
case 'quit_panel':
exit(1);
case 'tag_changed':
case 'tag_flags':
case 'tag_added':
case 'tag_removed':
set_tag_value($monitor);
break;
case 'window_title_changed':
case 'focus_changed':
set_windowtitle($column[2]);
break;
}
}
function init_content($monitor, $process)
{
// initialize statusbar before loop
set_tag_value($monitor);
set_windowtitle('');
$text = get_statusbar_text($monitor);
fwrite($process, $text."\n");
flush();
}
function walk_content($monitor, $process)
{
// start a pipe
$command_in = 'herbstclient --idle';
$pipe_in = popen($command_in, 'r'); // handle
while(!feof($pipe_in)) {
# read next event
$event = fgets($pipe_in);
handle_command_event($monitor, $event);
$text = get_statusbar_text($monitor);
fwrite($process, $text);
flush();
}
pclose($pipe_in);
}
function run_dzen2($monitor, $parameters)
{
$command_out = "dzen2 $parameters";
$pipe_out = popen($command_out, 'w');
init_content($monitor, $pipe_out);
walk_content($monitor, $pipe_out); // loop for each event
pclose($pipe_out);
}
function detach_dzen2($monitor, $parameters)
{
$pid = pcntl_fork();
switch($pid) {
case -1 : // fork errror
die('could not fork');
case 0 : // we are the child
run_dzen2($monitor, $parameters);
break;
default : // we are the parent
return $pid;
}
}
function detach_transset()
{
$pid = pcntl_fork();
if ($pid == 0) {
sleep(1);
system('transset .8 -n dzentop >/dev/null');
}
}
import os
import subprocess
import time
import output
# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----
# pipe
def handle_command_event(monitor, event):
# find out event origin
column = event.split("\t")
origin = column[0]
tag_cmds = ['tag_changed', 'tag_flags', 'tag_added', 'tag_removed']
title_cmds = ['window_title_changed', 'focus_changed']
if origin == 'reload':
os.system('pkill dzen2')
elif origin == 'quit_panel':
exit()
elif origin in tag_cmds:
output.set_tag_value(monitor)
elif origin in title_cmds:
output.set_windowtitle(column[2])
def init_content(monitor, pipe_out):
# initialize statusbar before loop
output.set_tag_value(monitor)
output.set_windowtitle('')
text = output.get_statusbar_text(monitor)
pipe_out.stdin.write(text + '\n')
pipe_out.stdin.flush()
def walk_content(monitor, pipe_out):
# start a pipe
command_in = 'herbstclient --idle'
pipe_in = subprocess.Popen(
[command_in],
# stdout = pipe_out.stdin,
stdout = subprocess.PIPE,
stderr = subprocess.STDOUT,
shell = True,
universal_newlines = True
)
# wait for each event
for event in pipe_in.stdout:
handle_command_event(monitor, event)
text = output.get_statusbar_text(monitor)
pipe_out.stdin.write(text)
pipe_out.stdin.flush()
pipe_in.stdout.close()
def run_dzen2(monitor, parameters):
command_out = 'dzen2 ' + parameters
pipe_out = subprocess.Popen(
[command_out],
stdin = subprocess.PIPE,
shell = True,
universal_newlines=True
)
init_content(monitor, pipe_out)
walk_content(monitor, pipe_out) # loop for each event
pipe_out.stdin.close()
def detach_dzen2(monitor, parameters):
pid = os.fork()
if pid == 0:
try:
run_dzen2(monitor, parameters)
os._exit(1)
finally:
import signal
os.kill(pid, signal.SIGTERM)
def detach_transset():
pid = os.fork()
if pid == 0:
try:
time.sleep(1)
os.system('transset .8 -n dzentop >/dev/null')
os._exit(1)
finally:
import signal
os.kill(pid, signal.SIGTERM)
require_relative 'output'
# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----
# pipe
def handle_command_event(monitor, event)
# find out event origin
column = event.split("\t")
origin = column[0]
tag_cmds = ['tag_changed', 'tag_flags', 'tag_added', 'tag_removed']
title_cmds = ['window_title_changed', 'focus_changed']
case origin
when 'reload'
os.system('pkill dzen2')
when 'quit_panel'
exit
when *tag_cmds # splat operator
set_tag_value(monitor)
when *title_cmds # splat operator
set_windowtitle(column[2])
end
end
def init_content(monitor, stdin)
# initialize statusbar before loop
set_tag_value(monitor)
set_windowtitle('')
text = get_statusbar_text(monitor)
stdin.puts(text)
end
def walk_content(monitor, stdin)
# start a pipe
command_in = 'herbstclient --idle'
IO.popen(command_in, "r") do |f|
while f do
# read next event
event = f.gets
handle_command_event(monitor, event)
text = get_statusbar_text(monitor)
stdin.write(text)
end
f.close()
end
end
def run_dzen2(monitor, parameters)
command_out = 'dzen2 ' + parameters
IO.popen(command_out, "w") do |f|
init_content(monitor, f)
walk_content(monitor, f) # loop for each event
f.close()
end
end
def detach_dzen2(monitor, parameters)
# warning: Signal.trap is application wide
Signal.trap("PIPE", "EXIT")
pid = fork { run_dzen2(monitor, parameters) }
Process.detach(pid)
end
def detach_transset()
pid = fork do
sleep(1)
system('transset .8 -n dzentop >/dev/null')
end
Process.detach(pid)
end
......@@ -67,8 +67,7 @@ function run_lemon() {
{
init_content $monitor
walk_content $monitor # loop for each event
} | $command_out | sh
} | $command_out
}
function detach_lemon() {
......
#!/usr/bin/env bash
# ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----
# pipe
function handle_command_event() {
local monitor=$1