diff --git a/x/1-3-WindowingSystem/hello.cpp b/x/1-3-WindowingSystem/hello.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0a676d72d19dc83beb3a43b64480109569c0e4c6 --- /dev/null +++ b/x/1-3-WindowingSystem/hello.cpp @@ -0,0 +1,59 @@ +/* +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); + +} diff --git a/x/1-3-WindowingSystem/makefile b/x/1-3-WindowingSystem/makefile new file mode 100644 index 0000000000000000000000000000000000000000..76deb31f2b1b4a882b1cc37890a9bb46d7eb915c --- /dev/null +++ b/x/1-3-WindowingSystem/makefile @@ -0,0 +1,19 @@ +# 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 diff --git a/x/1-3-WindowingSystem/null.min.cpp b/x/1-3-WindowingSystem/null.min.cpp new file mode 100644 index 0000000000000000000000000000000000000000..45e036c8009f293ff130a1b815e4cb21dd6216bf --- /dev/null +++ b/x/1-3-WindowingSystem/null.min.cpp @@ -0,0 +1,28 @@ +/* +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 + } +} diff --git a/x/1-3-WindowingSystem/openwindow.cpp b/x/1-3-WindowingSystem/openwindow.cpp new file mode 100644 index 0000000000000000000000000000000000000000..96e102a7d2f2a4be9703de9726032adddd5af91e --- /dev/null +++ b/x/1-3-WindowingSystem/openwindow.cpp @@ -0,0 +1,116 @@ +/* +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) { + XSizeHints hints; + + hints.x = 100; + hints.y = 100; + hints.width = 400; + hints.height = 400; + hints.flags = PPosition | PSize; + + /* + * 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 + hints.x, hints.y, // upper left corner location + hints.width, hints.height, // size of the window + 10, // 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 + &hints); // 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); +} diff --git a/x/1-3-WindowingSystem/openwindow.min.cpp b/x/1-3-WindowingSystem/openwindow.min.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7596f366a735b7676383f561e237219c71142a49 --- /dev/null +++ b/x/1-3-WindowingSystem/openwindow.min.cpp @@ -0,0 +1,35 @@ +/* +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); +} diff --git a/x/readme.md b/x/readme.md new file mode 100644 index 0000000000000000000000000000000000000000..bb8c5c649a42a38c691bd3a7f15d275965d696b8 --- /dev/null +++ b/x/readme.md @@ -0,0 +1,22 @@ +# CS 349 X Windows and XLib Example Code + + +To make ("compile and link") an example, use the included makefile with +the name of cpp file passed as a variable. For example, to make hello.cpp: + +```bash + make NAME="hello" +``` + +Then, to run: + +```bash + ./hello +``` + +Or you can even compile, link, and run in one step: + +```bash + make run NAME="hello" +``` +