Skip to content

Instantly share code, notes, and snippets.

@thundergnat
Created December 16, 2018 14:43
Show Gist options
  • Save thundergnat/9aec3d1a6cc2be0f37899c5fe0c33e77 to your computer and use it in GitHub Desktop.
Save thundergnat/9aec3d1a6cc2be0f37899c5fe0c33e77 to your computer and use it in GitHub Desktop.
Having trouble figuring out calling conventions
use NativeCall;
class Xdo is export {
has Pointer $.id is rw;
submethod BUILD {
sub xdo_new(Str) returns Pointer is native('xdo') { * }
self.id = xdo_new('');
}
method version() {
sub xdo_version() returns Str is encoded('utf8') is native('xdo') { * }
xdo_version()
}
#`[
Search for windows.
search: the search query.
* @param windowlist_ret the list of matching windows to return
* @param nwindows_ret the number of windows (length of windowlist_ret)
* @see xdo_search_t
see https://github.com/jordansissel/xdotool/blob/master/xdo.h#L168
for details of xdo_search_t structure
*/
int xdo_search_windows(const xdo_t *xdo, const xdo_search_t *search,
Window **windowlist_ret, unsigned int *nwindows_ret);
]
# search bitmask bits
#define SEARCH_TITLE (1UL << 0) DEPRECATED - Use SEARCH_NAME
#define SEARCH_CLASS (1UL << 1)
#define SEARCH_NAME (1UL << 2)
#define SEARCH_PID (1UL << 3)
#define SEARCH_ONLYVISIBLE (1UL << 4)
#define SEARCH_SCREEN (1UL << 5)
#define SEARCH_CLASSNAME (1UL << 6)
#define SEARCH_DESKTOP (1UL << 7)
class Query is repr('CStruct') {
has str $.title; # regex pattern to test against a window title
has str $.winclass; # regex pattern to test against a window class
has str $.winclassname; # regex pattern to test against a window class
has str $.winname; # regex pattern to test against a window name
has int $.pid; # window pid (From window atom _NET_WM_PID)
has long $.max_depth = 1; # depth of search. 1 means only toplevel windows
has int $.only_visible = 1; # boolean; set true to search only visible windows
has int $.screen; # what screen to search, if any. If none given, search all
# Should the tests be 'and' (0) or 'or' (1)? If 'and', any failure will skip the
# window. If 'or', any success will keep the window in search results.
has int $.require = 0;
# bitmask of things you are searching for, such as SEARCH_NAME, etc.
# @see SEARCH_NAME, SEARCH_CLASS, SEARCH_PID, SEARCH_CLASSNAME, etc
has long $.searchmask = 2;
# What desktop to search, if any. If none given, search all screens. */
has long $.desktop;
# How many results to return? If 0, return all. */
has uint32 $.limit = 0;
}
# Hard code a bunch of stuff for right now until I figure out the
# correct calling conventions.
method search-windows (str $string) {
my $query = Query.new(:winname($string), :searchmask(2));
#dd $query;
my Pointer $ids;
sub xdo_search_windows(Pointer, Query, Pointer, int32 ) returns int32 is native('xdo') { * };
my int32 $n;
say xdo_search_windows(self.id, $query, $ids, $n);
$ids
}
}
my $xdo = Xdo.new;
say 'libxdo version: ', $xdo.version, ' - loaded ok.';
say $xdo.search-windows('Firefox');
@thundergnat
Copy link
Author

I'm trying to get the same results as the command line tool: E.G.

xdotool search --onlyvisible --maxdepth 1 --name "Firefox"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment