openwindow.cpp 2.44 KB
Newer Older
Daniel Vogel's avatar
Daniel Vogel committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
CS 349 Code Examples: X Windows and XLib

    openwindow     Opens a single blank window

- - - - - - - - - - - - - - - - - - - - - -

See associated makefile for compiling instructions

*/

#include <cstdlib>
#include <iostream>
#include <list>

/*
 * Header files for X functions
 */
#include <X11/Xlib.h>
#include <X11/Xutil.h>

using namespace std;


/*
 * Information to draw on the window.
 */
struct XInfo {
    Display* 	display;
    int		 screen;
    Window	 window;
};


/*
 * Function to put out a message on error exits.
 */
void error( string str ) {
    cerr << str << endl;
    exit(0);
}


/*
 * Create a window
 */
void initX(int argc, char* argv[], XInfo& xinfo) {


    /*
    * Display opening uses the DISPLAY	environment variable.
    * It can go wrong if DISPLAY isn't set, or you don't have permission.
    */
    xinfo.display = XOpenDisplay( "" );
    if ( !xinfo.display )	{
        error( "Can't open display." );
    }

    /*
    * Find out some things about the display you're using.
    */
    // DefaultScreen is as macro to get default screen index
    xinfo.screen = DefaultScreen( xinfo.display ); 

    unsigned long white, black;
    white = XWhitePixel( xinfo.display, xinfo.screen ); 
    black = XBlackPixel( xinfo.display, xinfo.screen );

    xinfo.window = XCreateSimpleWindow(
       xinfo.display,				// display where window appears
       DefaultRootWindow( xinfo.display ), // window's parent in window tree
       10, 10,			            // upper left corner location
       400, 300,	                // size of the window
       5,						    // width of window's border
       black,						// window border colour
       white );					    // window background colour

    // extra window properties like a window title
    XSetStandardProperties(
        xinfo.display,		// display containing the window
        xinfo.window,		// window whose properties are set
        "x1_openWindow",	// window's title
        "OW",				// icon's title
        None,				// pixmap for the icon
        argv, argc,			// applications command line args
        None );			    // size hints for the window

    /*
     * Put the window on the screen.
     */
    XMapRaised( xinfo.display, xinfo.window );

    XFlush(xinfo.display);
}


int main ( int argc, char* argv[] ) {

    XInfo xinfo;

    initX(argc, argv, xinfo);

    // wait for user input to quit (a concole event for now)
    cout << "press ENTER to exit";
    cin.get();

    XCloseDisplay(xinfo.display);
}