Jackzz
Jackzz

Reputation: 1467

Get screenshot of linux machine using C++

I have a C++ program that takes the screenshot of my Ubuntu 14.04 machine. The program uses the X11 library. I need to execute this application from a daemon at boot time. But it returns XOpenDisplay failed. I think it is because the X11 server is not loaded. Is there any programmatic way by which I can get the screenshot soon after the X11 server is loaded?

EDIT:

Missed to tell something.. The daemon gets started at boot time and it fails to take screenshot. So after starting I stopped the daemon and started it again from terminal. The same error(XOpenDisplay failed) is shown then.. Is it a problem with screenshot and daemon??

Upvotes: 1

Views: 1974

Answers (2)

eerorika
eerorika

Reputation: 238351

Add a script in /etc/X11/Xsession.d/. The script will be run when X11 is running.

If you really need to start the daemon at boot, then what you could do, is send a signal from the script to the daemon process. You'll need to store the pid somewhere when you start the daemon.

kill -SIGUSR1 $PID

Then, your daemon should at the start register a signal handler that will be called when the signal is received.

Taking a screen shot probably requires dynamic memory allocation though, and that's one of the things that you cannot do in a signal handler. So, what you will have to do, is have the daemon wait on a condition variable and have the signal handler set it and notify the waiter.

It would be much simpler to start the daemon after X11 starts, rather than at boot.

Upvotes: 2

michalsrb
michalsrb

Reputation: 4891

It's not just a matter of waiting until the X server starts. To successfully connect to it:

  • The X server must be running.
  • The program must know its address to connect to it, which is typically stored in DISPLAY environmental variable.
  • The program must authenticate to the X server, which is typically by giving X server cookie that was read from a file that is readable only to the user who is currently logged in.

All those conditions are fulfilled if you simply start the program as part of the graphical session, instead of starting it independently after boot.

There are several ways to achieve start of program inside the graphical session. One is by putting something.desktop file into /etc/xdg/autostart directory. You can check other files from there for syntax.

Upvotes: 4

Related Questions