Write For Us
Submit Tips
Subscribe to Print Edition
Contact Us
Search
HOME
REVIEWS
HOW-TOS
CODING
INTERVIEWS
FEATURES
OVERVIEW
BLOGS
SERIES
IT ADMIN
Graphics Using Xlib, Part 2: Writing Programs
By Amol Takkalki on July 1, 2011 in Coding, Developers 3 Comments
Search for:
Search
This article applies the concepts explained in Part l of this series with a sample program.
In case you have forgotten the concepts, a quick recap might be helpful. A client (user program) and the server communicate with each other through Xlib, which is running on either the local or a remote system. The X protocol defines each packet of information transferred in both directions, in the form of requests, replies, events and errors. I have assumed a basic knowledge of C on your part, as a requisite to learning to use Xlib. Before we commence with programming, you should be aware of certain important conventions and concepts defined to be standard. Also remember that X is not a part of the OS, but is an application. Windows: The window system in Xlib follows a hierarchical architecture, which makes it easy to identify the parent and child, and also control them. This hierarchy could be deep, to handle more complexities. Each window has its own coordinate system, which has the X axis (horizontal) and the Y axis (vertical) with the origin [0, 0] at the upper-left corner of the window. Coordinates are integral, in terms of pixels, and coincide with pixel centres. For a window, the origin is inside the border at the inside, upper-left corner. The X window system can support one or more screens on the monitor. A collection of screens that belongs to a user, along with the keyboard and mouse, is called a display. X provides graphics, text, and raster operations for the windows.
Get Connected RSS Feed Twitter
Function naming conventions:
1. All function names and user-visible data structures start with X . 2. Macro names are entirely in upper-case letters. 3. The names of all elements in a data structure are lower-case. 4. Compound words are separated by an underscore (_ ). Errors: If the function fails, it returns a zero. When Xlib detects an error, it calls an error handler, which your program can provide. If you do not provide an error handler, the error is printed, and your program terminates.
Writing programs in X
If you have read my previous article, the main emphasis was on understanding the working of X. The outline of an X client program could be as follows: 1. Establish a connection with the server. 2. Open a window(s). 3. Declare variables/allocate memory.
4. Send requests and get replies from the server. 5. Handle all the errors appropriately. 6. Free all the variables. 7. Close the window(s). 8. Close the connection with the server. Lets begin programming by following these steps, in order to avoid complexities and confusion. We will write a very simple program, with the least necessary operations, to understand the steps listed above. Almost all programs that you write using Xlib will have pretty much the same basic steps, with additional code providing application-specific functionality.
Find us on Facebook
LINUX For You on
Follow
+2,530
Open Source For You
Like 256,395 people like Open Source For You.
The program
Our simple program opens a window with a black background, and closes it after a short delay. The code is as follows:
1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8
# i n c l u d e< X 1 1 / X l i b . h >/ * X l i bh e a d e rf i l e * / # i n c l u d e< s t d i o . h > i n tm a i n ( v o i d ) { D i s p l a yd i s ;/ * V a r i a b l ef o rh a n d l i n gd i s p l a y * / W i n d o ww i n ;/ * V a r i a b l ef o rh a n d l i n gw i n d o w * / F acebook social plugin d i s=X o p e n D i s p l a y ( N U L L ) ; / * W eh a v ej u s to p e n e dac o n n e c t i o nw i t ht h eXs e r v e r . I ft h i sf a i l s ,t h ef u n c t i o nw i l lr e t u r na' 0 '* / w i n=X C r e a t e S i m p l e W i n d o w ( d i s ,R o o t W i n d o w ( d i s ,0 ) ,1 ,1 ,5 0 0 ,5 0 0 ,0 ,B l a c k P i x e l( d i s ,0 ) ,B l a c k P i x e l( d i s ,0 ) ) ; / * T h i sf u n c t i o nw i l lc r e a t eas i m p l ew i n d o wo f5 0 0 x 5 0 0w i t hb l a c kb a c k g r o u n d * / X M a p W i n d o w ( d i s ,w i n ) ; Popular Comments Tag cloud / * T h eX M a p W i n d o wf u n c t i o nm a p st h ew i n d o wa n da l lo fi t ss u b w i n d o w st h a th a v eh a dm a pr e q u e s t s .* / X f l u s h ( d i s ) ;/ * T h eX F l u s hf u n c t i o nf l u s h e st h eo u t p u tb u f f e r . * / August 13, 2013 46 Comments Diksha P Gupta s l e e p ( 5 ) ;/ * F r e e z ef o r5s e c o n d sb e f o r ec l o s i n g . * / India has immense under-utilised talent in the cloud X c l o s e D i s p l a y ( d i s ) ;/ * C l o s et h ec o n n e c t i o nw i t ht h eXs e r v e r* / r e t u r n ( 0 ) ; security space }
June 20, 2013 5 Comments sophie-samuel
New and amazing features of Linux
Compile the program with: g c ci p . coo pl X 1 1 ; and run it with: . / o p
June 20, 2013 3 Comments Priyanka Sarkar
Conclusion
These are the steps necessary to write a program with Xlib. C programmers ought not to face any difficulty in understanding the program, since most parts are self-explanatory. We might do a program to plot graph points in a future article, but I would like you to explore some part of Xlib on your own; I suggest you take a look at the graphics functions, like drawing a rectangle. Obtaining some insight before the next article would definitely help you understand the scope of Xlib.
Feature image courtesy: Nate Steiner. Reused under the terms of CC-BY 2.0 License.
What it Takes to be an Open Source Expert
August 24, 2013 3 Comments Priyanka Sarkar
Secure Your Career with Ethical Hacking!
August 24, 2013 0 Comments Shashwat Pant
Get Fit With Android
Related Posts:
Graphics Using Xlib, Part 1 Graphics Programming in Linux Joy of Programming: Scope, Lifetime and Visibility in C Function Pointers and Callbacks in C An Odyssey Developing Apps on Qt, Part 1
Tags: error handler, Exception handling, LFY July 2011, naming conventions, Nate Steiner, window coordinates, X Protocol, X server, X Window System, Xlib
Article written by:
Amol Takkalki
The author has been using Linux for the past three years, and is a big fan of it. His hobbies are photography and programming. Connect with him: Website
Previous Post
Next Post
Making Your Web Apps Social with Facebook
Device Drivers, Part 8: Accessing x86-Specific I/O-Mapped Hardware
3 comments Leave a message...
Newest Community s rivat s a
2 months ago
Share
The corrections compiled successfuly
Reply
Share
arc t ic -fox
2 months ago
There seems to be a few little typos mostly case. 1 Display dis; should be Display *dis; 2 dis = XopenDislpay(NULL); should be XOpenDisplay(NULL): //capital O 3 Xflush(dis); should be XFlush(dis); //capital F 4 XcloseDisplay(dis); should be XCloseDisplay(dis); //capital C
1
Reply
Share
t ed
2 months ago
$ gcc ip.c -o op -lX11 ip.c: In function main: ip.[Link] error: storage size of dis isnt known
1
Reply
Share
C o m m e n t fe e d
Su b s cri b e vi a e m a i l
Reviews
How-Tos
Coding
Interviews
Features
Overview
Blogs
Search
Popular tags
Linux , ubuntu, Java, MySQL, Google, python, Fedora, Android, PHP, C, html, w eb applications , India, Microsoft, unix , Window s , Red Hat, Oracle, Security , Apache, xml, LFY April 2012, FOSS, GNOME, http, JavaScript, LFY June 2011, open source, RAM, operating systems
For You & Me Developers Sysadmins Open Gurus CXOs Columns
All published articles are released under Creative Commons Attribution-NonCommercial 3.0 Unported License, unless otherw ise noted. LINUX For You is pow ered by WordPress, w hich gladly sits on top of a CentOS-based LEMP stack.