[dundee] X11 Programming in C
Nicholas Walker
tel0seh at googlemail.com
Fri Mar 25 01:50:48 UTC 2011
Hey guys,
this is somewhat complicated (and very much above my paygrade) but out of
interest im trying out some X11 programming in C.
I've managed to attach to the running X11 Display, and am able to probe it
for information using the following code:
#include<X11/Xlib.h>
#include<X11/Xutil.h>
#include<stdio.h>
int main(){
Display *display_name /* returns a pointer containing info about our
display */;
int depth,screen,connection;
/*Opening display and setting defaults*/
display_name = XOpenDisplay(NULL); /*opens current display */
screen = DefaultScreen(display_name); /* returns default screen for
display_name */
depth = DefaultDepth(display_name,screen);
connection = ConnectionNumber(display_name);
Window root_window; /* variable for storing a window handle and/or ID */
/*Displaying the info gathered*/
printf("The display is::%s\n",XDisplayName((char*)display_name));
printf("Width::%d\tHeight::%d\n",
DisplayWidth(display_name,screen),
DisplayHeight(display_name,screen));
printf("Connection number is %d\n",connection);
if(depth == 1)
printf("You live in prehistoric times\n");
else
printf("You've got a coloured monitor with depth of %d\n",
depth);
root_window = RootWindow(display_name,screen); /* determine root window of
screen */
printf("Root window of ID: %d found.\n",root_window);
/*Closing the display*/
XCloseDisplay(display_name);
}
I can also create a simple window that the window manager handles, and I can
also draw simple shapes and write text to the window I've created. I'd like
to be able to draw to the root window, (IE I want to draw a rectangle on top
of all other windows, using the entire display as my canvas, rather than a
window.) the code for this is as follows:
#include<X11/Xutil.h>
#include <X11/Xos.h>
#include<stdio.h>
int main(){
Display *display_name /* returns a pointer containing info about our
display */;
int depth,screen,connection;
char string[] = "TESTING!";
GC gc; /* contains Graphical context handle */
/* next two lines create variables for defining the GC setup */
XGCValues values;
values.cap_style = CapButt;
values.join_style = JoinBevel;
unsigned long valuemask = GCCapStyle | GCJoinStyle;
/*Opening display and setting defaults*/
display_name = XOpenDisplay(NULL); /*opens current display */
screen = DefaultScreen(display_name); /* returns default screen for
display_name */
depth = DefaultDepth(display_name,screen);
connection = ConnectionNumber(display_name);
Window root_window; /* variable for storing a window handle and/or ID */
/*Displaying the info gathered*/
printf("The display is::%s\n",XDisplayName((char*)display_name));
printf("Width::%d\tHeight::%d\n",
DisplayWidth(display_name,screen),
DisplayHeight(display_name,screen));
printf("Connection number is %d\n",connection);
if(depth == 1)
printf("You live in prehistoric times\n");
else
printf("You've got a coloured monitor with depth of %d\n",
depth);
root_window = RootWindow(display_name,screen); /* determine root window of
screen */
printf("Root window of ID: %d found.\n",root_window);
/*Closing the display*/
gc = XCreateGC(display_name,root_window,valuemask, &values); /* create a
Graphical Context, I think this a\
cts like a canvas? :3 */
if (gc < 0) {
fprintf(stderr,"XCreateGC: \n");
}
/* use the canvas to write text */
XDrawString(display_name,root_window,gc,800,600, string, strlen(string));
XCloseDisplay(display_name);
}
and it compiles cleanly, however upon executing, i never see string[] drawn
on the screen.
anyone have previous experience, or programming insight that can help me?
--
Nick Walker
President : The Linux Society
UAD Ethical Hacker
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.lug.org.uk/pipermail/dundee/attachments/20110325/e0883818/attachment.htm>
More information about the dundee
mailing list