Commit b6bc6614 authored by Daniel Vogel's avatar Daniel Vogel
Browse files

added window system code

parents
/*
CS 349 Hello X Windows
- - - - - - - - - - - - - - - - - - - - - -
Commands to compile and run:
g++ -o drawing.min drawing.min.cpp -L/usr/X11R6/lib -lX11 -lstdc++
./drawing.min
Note: the -L option and -lstdc++ may not be needed on some machines.
*/
#include <unistd.h>
#include <cstdlib>
#include <iostream>
#include <X11/Xlib.h>
Display* display;
Window window;
int main( int argc, char* argv[] ) {
// open display
display = XOpenDisplay("");
if (!display) exit (-1);
int screen = DefaultScreen(display);
int w = 200;
int h = 100;
window = XCreateSimpleWindow(display, DefaultRootWindow(display),
10, 10, w, h, 2,
BlackPixel(display, screen), WhitePixel(display, screen));
XMapRaised(display, window);
XFlush(display);
// give server 10ms to get set up before sending drawing commands
usleep(10 * 1000);
// drawing demo with graphics context here ...
GC gc = XCreateGC(display, window, 0, 0); // create a graphics context
XSetForeground(display, gc, BlackPixel(display, screen));
XSetBackground(display, gc, WhitePixel(display, screen));
//load a larger font
XFontStruct * font;
font = XLoadQueryFont (display, "12x24");
XSetFont (display, gc, font->fid);
// draw text
std::string text("hello X Windows");
XDrawImageString( display, window, gc,
10, h / 2, text.c_str(), text.length());
XFlush(display);
std::cout << "ENTER2exit"; std::cin.get(); // wait for input
XCloseDisplay(display);
}
# super simple makefile
# call it using 'make NAME=name_of_code_file_without_extension'
# (assumes a .cpp extension)
NAME = "null.min"
# Add $(MAC_OPT) to the compile line for macOS
# (should be ignored by Linux, set to nothing if causing problems)
MAC_OPT = -I/opt/X11/include
all:
@echo "Compiling..."
g++ -o $(NAME) $(NAME).cpp -L/opt/X11/lib -lX11 -lstdc++ $(MAC_OPT)
run: all
@echo "Running..."
./$(NAME)
clean:
-rm *o
/*
CS 349 Code Examples: X Windows and XLib
null Creates and destroys a display (a good first test to see
if X Windows is working).
- - - - - - - - - - - - - - - - - - - - - -
See associated makefile for compiling instructions
*/
#include <cstdlib>
#include <iostream>
#include <X11/Xlib.h> // main Xlib header
Display* display;
int main() {
display = XOpenDisplay(""); // open display (using DISPLAY env var)
if (display == NULL) {
std::cout << "error\n";
exit (-1);
} else {
std::cout << "success!: ";
XCloseDisplay(display); // close display
}
}
/*
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);
}
/*
CS 349 Code Examples: X Windows and XLib
openwindow.min Opens a single blank window.
- - - - - - - - - - - - - - - - - - - - - -
See associated makefile for compiling instructions
*/
#include <cstdlib>
#include <iostream>
#include <X11/Xlib.h>
Display* display;
Window window; // save the window id
int main( int argc, char* argv[] ) {
display = XOpenDisplay(""); // open display using DISPLAY env var
if (!display) exit (-1); // couldn't open, so bail
int screen = DefaultScreen(display);// info about the display
window = XCreateSimpleWindow(
display, // display where window appears
DefaultRootWindow(display), // window's parent
10, 10, // location: x,y
400, 300, // size: width, height
2, // width of border
BlackPixel(display, screen), // foreground colour
WhitePixel(display, screen)); // background colour
XMapRaised(display, window); // put window on screen
XFlush(display); // flush the output buffer
std::cout << "ENTER2exit"; std::cin.get(); // wait for input
XCloseDisplay(display);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment