LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-09-2010, 08:25 AM   #1
El Mago
LQ Newbie
 
Registered: Mar 2004
Posts: 26

Rep: Reputation: 15
Grabbing window content


I'd like to write a C/C++ program, that would grab content of window (in form of pixmap), which belongs to another application and do something with it. However, I ran into problems already at the beginning. In all toolkits I have searched through, I only found one function that claims to do what I want, namely XGetImage from Xlib. However, among other parameters like Display, coordinates, etc., it requires a Drawable. How do I specify a Drawable, that would correspond to the window I want to grab content of?
Or even better question: is there any easier way to achieve this?
 
Old 04-10-2010, 08:10 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Suggestion - don't reinvent the wheel. Just use one of the standard screenshot programs available to you (all of which can easily be called from C/C++):

http://tips.webdesign10.com/how-to-t...n-ubuntu-linux
http://www.sb.fsu.edu/~xray/Manuals/ScreenCapture.html

Otherwise, download the source for one of the above programs (the one you like the best) and simply look at how they do it.

'Hope that helps .. PSM

Last edited by paulsm4; 04-10-2010 at 11:01 PM.
 
Old 04-15-2010, 10:26 AM   #3
El Mago
LQ Newbie
 
Registered: Mar 2004
Posts: 26

Original Poster
Rep: Reputation: 15
Thanks for the links, they were useful. I managed to came up with the following solution, it waits for 2 seconds in order to get focus into window of interest and it updates the window content each second:

Code:
#include <Xlib.h>
#include <stdio.h>


int main(int argc, char **argv)
{ Display *dp;
  XImage *im;
  Window win, fw, root;
  GC gc;
  int ok, x, y;
  unsigned int w, h, bw, d;

  dp = XOpenDisplay(0);
  
  win = XCreateSimpleWindow(dp, RootWindow(dp, 0), 0, 0, 900, 700, 2, BlackPixel(dp, 0), WhitePixel(dp, 0));
  
  gc = XCreateGC(dp, win, 0, 0);
  
  XSelectInput(dp, win, StructureNotifyMask);

  XMapWindow(dp, win);
  
  for(;;) {
	    XEvent e;
	    XNextEvent(dp, &e);
	    if (e.type == MapNotify)
		  break;
      }

  sleep(2);
  XGetInputFocus(dp, &fw, &ok);
  
  XGetGeometry(dp, fw, &root, &x, &y, &w, &h, &bw, &d);
  
  while (1)
  { im = XGetImage(dp, fw, 0, 0, w, h, AllPlanes, ZPixmap);
    
    XPutImage(dp, win, gc, im, 0, 0, 0, 0, w, h);
    
    XFlush(dp);
    
    
    sleep(1);
    }
  
  XDestroyWindow(dp, w);
  XCloseDisplay(dp);
  }
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Resizing window content when resizing window? (Debian squeeze) mike11 Linux - Newbie 2 03-04-2010 02:14 AM
[SOLVED] grabbing mysql content from a remote server qwertyjjj Linux - Server 9 08-16-2009 11:19 PM
Grabbing text from a text box in a window running in wine. thefalling881 Programming 6 07-21-2008 08:38 PM
Can I change title bar content in an application window? dhave Linux - Desktop 4 09-19-2007 01:27 PM
Prevent gaim grabbing window focus chrism01 Linux - Software 1 01-31-2006 07:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration