xpra icon
Bug tracker and wiki

Ticket #282: little-x-example.c

File little-x-example.c, 2.4 KB (added by Antoine Martin, 7 years ago)

very simple X11 test application which sets the input=True hint only once

Line 
1#include<stdio.h>
2#include <X11/Xlib.h>
3#include <X11/Xutil.h>
4#include <X11/Xos.h>
5#include <X11/Xatom.h>
6
7int main (int ArgC, char **ArgV)
8{
9    Display *cdisDisplay;
10    int ciScreenNum;
11    Screen *cscrScreenPtr;
12    XWMHints *xwmhHints;
13    XClassHint  *xchClass;
14    XTextProperty xtpWinName, xtpIconName;
15    XSizeHints *xshSize;
16    char *pcIconName = "Icon Name?", *pcWinName = "Window Name?", *pcProgName = "Test App";
17    XEvent xeEvent;
18    Window Handle;
19   
20    cdisDisplay = XOpenDisplay(NULL);
21    if (cdisDisplay == NULL)
22    {//Failed to connect to an X Server
23        printf("\nFailed to connect to the display\n");
24        return 0;
25    }
26    else
27    {
28        ciScreenNum = DefaultScreen(cdisDisplay);
29        cscrScreenPtr = DefaultScreenOfDisplay(cdisDisplay);
30    }
31   
32    Handle = XCreateSimpleWindow(cdisDisplay, RootWindow(cdisDisplay, ciScreenNum), 0, 0, 200, 200, 0, BlackPixel(cdisDisplay, ciScreenNum), WhitePixel(cdisDisplay, ciScreenNum));
33   
34    //Allocate space for the hints
35    xshSize = XAllocSizeHints();
36    xwmhHints = XAllocWMHints();
37    xchClass = XAllocClassHint();
38   
39    xshSize->flags = PPosition | PSize;
40   
41    XStringListToTextProperty(&pcWinName, 1, &xtpWinName);
42    XStringListToTextProperty(&pcIconName, 1, &xtpIconName);
43   
44    xwmhHints->initial_state = NormalState;
45    xwmhHints->input = True;
46    xwmhHints->flags = StateHint | InputHint;
47   
48    xchClass->res_name = pcProgName;
49    xchClass->res_class = "Base Win";
50   
51    XSetWMProperties(cdisDisplay, Handle, &xtpWinName, &xtpIconName, 0, NULL, xshSize, xwmhHints, xchClass);
52
53    XSelectInput(cdisDisplay, Handle, ExposureMask | KeyPressMask | ButtonPressMask | StructureNotifyMask);
54    XMapWindow(cdisDisplay, Handle); //Show the window
55   
56    while (1)
57    {
58        XNextEvent(cdisDisplay, &xeEvent);
59       
60        switch (xeEvent.type)
61        {
62            case  (ConfigureNotify) :
63            {
64                printf("Window moved or resized!\n");
65                break;
66            }
67            case (ButtonPress) :
68            {
69                printf("Mouse button %d pressed!\n", xeEvent.xbutton.button);
70                break;
71            }
72            case (DestroyNotify) :
73            {
74                printf("Window killed!\n");
75            }
76            default :
77            {
78                printf("Unknown event: %d (discarding)\n", xeEvent.type);
79                break;
80            }
81        }
82    }
83}