Monday, May 6, 2013

Drawing With Mouse - An Example for Event Handling in X Window System (X11) Programming

What is X Window System?

The X Window System is client/server system for managing a windowed graphical user interface in a distributed network. In general, such systems are known as windowing system.

Event handling in X Window System Programming:

Most applications simply are event loops. They wait for an event, decide what to do with it, execute some amount of code that results in changes to the display, and then wait for the next event. X window system also provides large amount of support for event handling. Here I am going to show you how to draw with mouse as shown in this video, using X window System or X11 events. Watch Video Here:
  Screen shot:

// File Name: XEventHandling.c
#include <stdio.h>
#include <X11/Xutil.h>
#include <X11/cursorfont.h>
#include <X11/extensions/XTest.h>
#include <X11/extensions/XInput.h>
struct Point{
              int x;
              int y;
void main()
 Display *dpy = XOpenDisplay(0);
 char *window_name = (char*)"Drawing window";
 int whiteColor = WhitePixel(dpy, DefaultScreen(dpy));
 Window parent = XCreateSimpleWindow(dpy, RootWindow(dpy, DefaultScreen(dpy)), 0, 0,
                     230, 150, 0, whiteColor, whiteColor);
 XStoreName(dpy, parent, window_name);
 XMapWindow(dpy, parent);
 XSelectInput(dpy, parent, ButtonPressMask| PointerMotionMask| LeaveWindowMask| EnterWindowMask| ButtonReleaseMask );
 Drawable d = parent;
 XGCValues values;
 values.line_width = 4;
 values.line_style = LineSolid;
 GC gc = XCreateGC(dpy, d, GCLineWidth, &values);
 int flag = 0;
 for(;;) {
   XEvent e;
   XNextEvent(dpy, &e);
   if(e.xany.window == parent)
         case EnterNotify: printf("\n POINTER Entered in Window");
         case LeaveNotify: printf("\n POINTER Left Window");
         case ButtonPress:if(flag == 0 && e.xbutton.button==1)
                          printf("\n Button %d Pressed at (%d, %d)", e.xbutton.button, e.xbutton.x, e.xbutton.y);
                          p1.x = e.xbutton.x;
                          p1.y = e.xbutton.y;
                          flag =1;
         case ButtonRelease:if(flag == 1 && e.xbutton.button == 3)
                           printf("\n Button %d Pressed at (%d, %d)", e.xbutton.button, e.xbutton.x, e.xbutton.y);
                           flag =0;
                           XUnmapWindow(dpy, parent);
                           XMapRaised(dpy, parent);
                           XMoveWindow(dpy, parent, 0, 100);
        case MotionNotify: if(flag ==1)
                           printf("\n Motion Notified at (%d, %d)", e.xbutton.button, e.xbutton.x, e.xbutton.y);
                           p2.x = e.xbutton.x;
                           p2.y = e.xbutton.y;
                           XDrawLine(dpy, parent, gc, p1.x, p1.y, p2.x, p2.y);
                           p1.x = p2.x;
                           p1.y = p2.y;

To Run: gcc -lX11 XEventHandling.c 

Out Put:  To Start drawing Click & release mouse button-1 in Drawing window
                 Move mouse pointer
                 To end drawing Click & release mouse button-3 in Drawing window

1 comment: