0

I am using ubuntu. The command to get window id of a window by its name in terminal is

xdotool search --name "name of the window"

But i want to get the same using c++ program.

Bavani
  • 329
  • 1
  • 3
  • 12
  • 1
    Take a look at this: https://github.com/jordansissel/xdotool/blob/master/cmd_search.c –  Nov 16 '13 at 06:16

3 Answers3

1

References:

Sending Keystrokes to a X Window

Github

I cannot compile myself and check, but I think this is the minimal code to search for a window by name.

#include <xdo.h>

Window *list;
xdo_search_t search;
unsigned int nwindows;
memset(&search, 0, sizeof(xdo_search_t));
search.max_depth = -1;
search.require = xdo_search::SEARCH_ANY;

search.searchmask |= SEARCH_NAME;
search.winname = "enter name here";

// the context
xdo_t* p_xdo = xdo_new(NULL);
int id = xdo_window_search(p_xdo, &search, &list, &nwindows);
Community
  • 1
  • 1
  • I am getting two errors. error: 'SEARCH_ANY' was not declared in this scope error: 'xdo_search_windows' was not declared in this scope – Bavani Nov 16 '13 at 06:58
  • sorry again getting two errors.error: undefined reference to `xdo_new' , error: undefined reference to `xdo_window_search'. – Bavani Nov 18 '13 at 05:09
  • @Bavani Undefined reference sounds like a linker error. Make sure you're specifying the right library with `-l`. –  Nov 18 '13 at 05:11
  • I linked the library libxdo.so with program. but still error: undefined reference to `xdo_window_search(xdo const*, xdo_search const*, unsigned long**, int*)' . Do you know the reason? – Bavani Nov 18 '13 at 07:05
  • Several things to try: Specifiy `-lxdo`, not `-llibxdo.so`. Specify the location of the library with `-L` (example: `-L/xdo-install/lib`). Make sure that `-lxdo` comes at the end of your gcc invocation, like: `g++ -L/xdo-install/lib -o main main.cpp -lxdo`. –  Nov 18 '13 at 07:09
  • this can be cause by c++ name mangling. Try to put the "#include " inside a extern "C" {} block. – João Monteiro Aug 08 '19 at 20:52
1

I've had the same proble for xdo_window_search. Check the function's name in xdo.h, because of changing names.

Panos Kalatzantonakis
  • 12,525
  • 8
  • 64
  • 85
ewen
  • 11
  • 1
1

I've had the same problem with (xdo_window_search changed to xdo_search_windows as ewen said)

undefined reference to xdo_new'
undefined reference to xdo_search_windows'

it was fixed using in terminal

sudo apt-get install libxdo-dev

in addition to, in cpp file,

#include <xdotool-master/xdo.h>

in my particular case, was used for a qt application. For it, I've had to add the line below in .pro file too

LIBS += -lxdo

but the last variable 'id' always returns zero (0).

To get the window ID definitively, I used code in link below, using xdotool command too. In this case, the use should be carefully, because a program can generate more than one ID until fully open and the code runs faster than the ID is available to read. So a timer or a another strategy is necessary.

How do I execute a command and get the output of the command within C++ using POSIX?

zepolik
  • 47
  • 4
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/29982584) – Alex Guteniev Oct 03 '21 at 15:11