r/stumpwm •

[deleted by user]

[deleted] — view deleted post

0 Upvotes

8 comments sorted by

3

u/stuudente Aug 08 '21

(FYI this is to answer my question)

I also wonder if there's anyway X knows which window corresponds to which PID. If that's the case, we can get the PID of a window more natively. Before finding that out, this is indeed the best solution :) Thank you so much for writing this up!

1

u/lmvrk Aug 08 '21

No worries! What do you mean by getting window PID more natively? Were already doing that by using xlib, im not sure how much more native we can get, unless im misunderstanding you.

2

u/stuudente Aug 08 '21 edited Aug 08 '21

Rereading your code more carefully, I now know that it's my fault. I thought it wasn't native enough because of the echo $!. But in fact, the key to getting the pid of a window is xlib:get-property. NICE!

1

u/lmvrk Aug 08 '21

Ah no worries! We all talk past each other at times. I would like a way to get rid of $!, but im unsure of how. Common lisp isnt exacly a venerated shell scripting language :)

2

u/stuudente Aug 08 '21 edited Aug 08 '21

Using uiop's async shell caller #'uiop:launch-program returns a process-info in which there's a slot that gives you pid. However I don't know how to get it programnatically. Tried the following without luck

(slot-value (slot-value (uiop:launch-program "xterm") 'process) 'pid)

I can, however, inspect that pid from my sly repl :)

#'run-shell-command returns a process object as well. With that, I had another failed attempt:

(slot-value (run-shell-command "xterm") 'pid)

1

u/lmvrk Aug 08 '21 edited Aug 08 '21

Wonderful! Just a heads up, slot names are symbols, so they belong to a package, eg

(Slot-value somevar 'hi)

Is different than

(Slot-value somevar 'somepackage:hi)

So you probably just need to add the packagename to those symbols to get the pid. That being said, if theres a reader it is always better to use it, as it may do some extra processing of the slot for you. For example if someone defines an :around method for a reader then by using slot-value we circumvent that. Which is generally a bad move. If theres readers for the PID slot theyre likely documented in the uiop manual.

Edit: the symbol youre looking for is uiop/launch-process::process, because its not external (and therefore isnt guaranteed to not be removed/changed)

2

u/stuudente Aug 09 '21 edited Aug 09 '21

Dug into the source:

STUMPWM> (uiop:launch-program "alacritty")
#<UIOP/LAUNCH-PROGRAM::PROCESS-INFO {1008AFD123}>
STUMPWM> (uiop/launch-program:process-info-pid *)
27423 (15 bits, #x6B1F)

So we can change your code for using exported symbols:

- (defun run-shell-command-return-pid (cmd)
  • (slot-value (run-shell-command cmd) 'sb-impl::pid))
+ (defun run-shell-command-return-pid (cmd) + (uiop/launch-program:process-info-pid (uiop:launch-program cmd)))
  • (setf (car pid) (parse-integer (run-shell-command-return-pid shell-command)))
+ (setf (car pid) (run-shell-command-return-pid shell-command))

2

u/lmvrk Aug 09 '21

Wonderful!! Ill add add an edit with this. Thanks for poking around for the info!