# HG changeset patch # User Eris Caffee # Date 1334472729 18000 # Node ID c6dce2064bfbc1fa8147bb4c09229b1932f25ad8 # Parent acb4b5af3dfa2e34cfb2f9cc8f475b082f8d63e4 Works so far diff -r acb4b5af3dfa -r c6dce2064bfb CMakeLists.txt --- a/CMakeLists.txt Tue Mar 29 04:36:57 2011 -0500 +++ b/CMakeLists.txt Sun Apr 15 01:52:09 2012 -0500 @@ -66,7 +66,7 @@ #set (CMAKE_VERBOSE_MAKEFILE ON) # Name your program! -set (App_Name "") +set (App_Name "xlib_App") if (App_Name STREQUAL "") message (FATAL_ERROR "You must set the App_Name variable!") endif () @@ -167,11 +167,11 @@ ) -# # An example for a unix library named utils with a test driver program. - -# set (SRCS_utils -# src/utils.cpp -# include/utils.h +# Example to build a linux library with a test driver. + +# set (SRCS_${App_Name} +# src/.cpp +# include/.h # ) # include_directories ( @@ -191,31 +191,37 @@ # # clobber each others temp files since they are being built from the same # # sources. -# add_library (utils SHARED -# ${SRCS_utils} +# add_library (${App_Name} SHARED +# ${SRCS_${App_Name}} # ) -# set_target_properties (utils PROPERTIES OUTPUT_NAME "utils") -# set_target_properties (utils PROPERTIES PREFIX "lib") -# set_target_properties (utils PROPERTIES CLEAN_DIRECT_OUTPUT 1) +# set_target_properties (${App_Name} PROPERTIES OUTPUT_NAME ${App_Name}) +# set_target_properties (${App_Name} PROPERTIES PREFIX "lib") +# set_target_properties (${App_Name} PROPERTIES CLEAN_DIRECT_OUTPUT 1) -# add_library (utils-static STATIC -# ${SRCS_utils} +# add_library (${App_Name}-static STATIC +# ${SRCS_${App_Name}} # ) -# set_target_properties (utils-static PROPERTIES OUTPUT_NAME "utils") -# set_target_properties (utils-static PROPERTIES PREFIX "lib") -# set_target_properties (utils-static PROPERTIES CLEAN_DIRECT_OUTPUT 1) +# set_target_properties (${App_Name}-static PROPERTIES OUTPUT_NAME ${App_Name}) +# set_target_properties (${App_Name}-static PROPERTIES PREFIX "lib") +# set_target_properties (${App_Name}-static PROPERTIES CLEAN_DIRECT_OUTPUT 1) # # Build a test application. -# add_executable (test +# link_directories ( +# ) + +# include_directories ( +# ${CMAKE_SOURCE_DIR}/include +# ) + +# add_executable (${App_Name}-test # src/main.cpp # ) -# target_link_libraries (test -# utils +# target_link_libraries (${App_Name}-test +# ${App_Name} # ) - ################################################################################ # X11 # diff -r acb4b5af3dfa -r c6dce2064bfb include/App.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/App.h Sun Apr 15 01:52:09 2012 -0500 @@ -0,0 +1,69 @@ +#include +#include + +#include + +//////////////////////////////////////////////////////////////////////////////// +class App + { + public: + + ///////////////////////////////////// + class BitMap + { + public: + BitMap(const std::string & file); + ~BitMap(); + + void make_pixmap(Display * dpy, Drawable d); + + // Yeah, it's all public here. Keeping it simple. + std::string name; + unsigned int width; + unsigned int height; + unsigned char * data; + Pixmap pixmap; + + private: + BitMap(const App::BitMap & b); + + int read_from_file(const std::string & file); + }; + + ///////////////////////////////////// + typedef struct + { + public: + std::string name; + Window win; + App::BitMap * icon; + } WindowData; + + ///////////////////////////////////// + + typedef std::pair win_pair; + typedef std::map WindowMap; + + App(const std::string & appname); + ~App(); + + static int x_error_handler(Display * dpy, XErrorEvent *err); + + void create_window(const std::string& win_name, + int x, int y, + unsigned int width, unsigned int height, + App::BitMap * icon); + + inline int display_height(void) const + { return DisplayHeight(dpy, DefaultScreen(dpy)); }; + + inline int display_width(void) const + { return DisplayWidth(dpy, DefaultScreen(dpy)); }; + + private: + App(); + App(const App & a); + + Display * dpy; + WindowMap windows; + }; diff -r acb4b5af3dfa -r c6dce2064bfb include/xlib_App.xbm --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/xlib_App.xbm Sun Apr 15 01:52:09 2012 -0500 @@ -0,0 +1,46 @@ +#define xlib_App_width 64 +#define xlib_App_height 64 +static unsigned char xlib_App_bits[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0xc0, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x0f, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x80, 0xff, 0x3f, 0x00, 0x00, 0x00, 0x10, 0x00, 0xc0, 0xff, 0x3f, 0x00, + 0x00, 0x00, 0x3c, 0x00, 0xe0, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x3e, 0x00, + 0xf0, 0xff, 0x7f, 0x00, 0x00, 0x80, 0x1f, 0x00, 0xf0, 0xff, 0x7f, 0x00, + 0x00, 0x80, 0x0f, 0x00, 0xf8, 0xff, 0xff, 0x00, 0x00, 0xc0, 0x07, 0x00, + 0xfc, 0xff, 0xff, 0x01, 0x00, 0xe0, 0x03, 0x00, 0xfc, 0xf8, 0xff, 0x01, + 0x00, 0xf0, 0x01, 0x00, 0x38, 0xf0, 0xff, 0x03, 0x00, 0xf8, 0x01, 0x00, + 0x00, 0xe0, 0xff, 0x03, 0x00, 0x7c, 0x00, 0x00, 0x00, 0xe0, 0xff, 0x07, + 0x00, 0x7c, 0x00, 0x00, 0x00, 0xc0, 0xff, 0x0f, 0x00, 0x3f, 0x00, 0x00, + 0x00, 0x80, 0xff, 0x0f, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x80, 0xff, 0x1f, + 0x80, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xff, 0x1f, 0xc0, 0x03, 0x00, 0x00, + 0x00, 0x00, 0xff, 0x3f, 0xe0, 0x03, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x3f, + 0xf0, 0x01, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x7f, 0xf8, 0x00, 0x00, 0x00, + 0x00, 0x00, 0xfc, 0xff, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xff, + 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0x3f, 0x00, 0x00, 0x00, + 0x00, 0x00, 0xf0, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0xff, + 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0xff, 0x07, 0x00, 0x00, 0x00, + 0x00, 0x00, 0xe0, 0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0xe0, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff, 0x01, 0x00, 0x00, 0x00, + 0x00, 0x00, 0xe0, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, + 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xff, 0x0f, 0x00, 0x00, 0x00, + 0x00, 0x00, 0xfe, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x80, 0xff, 0xff, + 0x1f, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xcf, 0xff, 0x7f, 0x00, 0x00, 0x00, + 0x00, 0xe0, 0x87, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, 0xf0, 0x03, 0xff, + 0xff, 0x07, 0xe0, 0x00, 0x00, 0xfc, 0x00, 0xfe, 0xff, 0x1f, 0xf0, 0x00, + 0x00, 0xfe, 0x00, 0xfc, 0xff, 0xff, 0x7c, 0x00, 0x00, 0x3f, 0x00, 0xf8, + 0xff, 0xff, 0x7f, 0x00, 0x80, 0x1f, 0x00, 0xf8, 0xff, 0xff, 0x3f, 0x00, + 0xc0, 0x0f, 0x00, 0xf0, 0xff, 0xff, 0x1f, 0x00, 0xe0, 0x03, 0x00, 0xe0, + 0xff, 0xff, 0x0f, 0x00, 0xf0, 0x01, 0x00, 0xc0, 0xff, 0xff, 0x07, 0x00, + 0xf0, 0x00, 0x00, 0xc0, 0xff, 0xff, 0x03, 0x00, 0x40, 0x00, 0x00, 0xc0, + 0xff, 0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0x80, 0xff, 0xff, 0x01, 0x00, + 0x00, 0x00, 0x00, 0x00, 0xfe, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xf8, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; diff -r acb4b5af3dfa -r c6dce2064bfb src/App.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/App.cpp Sun Apr 15 01:52:09 2012 -0500 @@ -0,0 +1,178 @@ +#include "App.h" + +#include +#include +#include +#include + +//////////////////////////////////////////////////////////////////////////////// +App::App(const std::string & appname) + { + XSetErrorHandler(&App::x_error_handler); + + if ((dpy = XOpenDisplay(NULL)) == NULL) + { + std::cerr << "Unable to connect to display " << XDisplayName(NULL) << std::endl; + exit(1); + } + std::cout << "Display name : " << XDisplayName(NULL) << std::endl + << "Number of screens: " << ScreenCount(dpy) << std::endl + << "Default screen : " << DefaultScreen(dpy) << std::endl + << "Protocol Version : " << ProtocolVersion(dpy) << std::endl + << "Protocol Revision: " << ProtocolRevision(dpy) << std::endl + << "Server Vendor : " << ServerVendor(dpy) << std::endl + << "Vendor Release : " << VendorRelease(dpy) << std::endl + << std::endl + << "Display width : " << this->display_width() << std::endl + << "Display height : " << this->display_height() << std::endl + ; + + App::BitMap * icon = new App::BitMap("xlib_App.xbm"); + + // Create the main window + create_window("main", + 0, 0 , + this->display_width()/3, this->display_height()/4, + icon); + + // Even though we are not passing any actual size_hints, the Xlib programming + // manual says to pass the flags below anyway. + XSizeHints *size_hints; + if (!(size_hints = XAllocSizeHints())) + { + std::cerr << "XAllocSizeHints: memory allocation failure" << std::endl; + exit(1); + } + size_hints->flags = PPosition | PSize; + + XTextProperty windowName, iconName; + const char * ptr = appname.c_str(); + if (XStringListToTextProperty(const_cast(&ptr), 1, &windowName) == 0) + { + std::cerr << "XStringListToTextProperty: structure allocation for windowName failed." << std::endl;; + exit(1); + } + ptr = icon->name.c_str(); + if (XStringListToTextProperty(const_cast(&ptr), 1, &iconName) == 0) + { + std::cerr << "XStringListToTextProperty: structure allocation for iconName failed." << std::endl;; + exit(1); + } + + XWMHints *wm_hints; + if (!(wm_hints = XAllocWMHints())) + { + std::cerr << "XAllocWMHints: memory allocation failure" << std::endl; + exit(0); + } + wm_hints->initial_state = NormalState; + wm_hints->input = True; + wm_hints->icon_pixmap = icon->pixmap; + wm_hints->flags = StateHint | IconPixmapHint | InputHint; + + App::WindowData * wd = windows["main"]; + XSetWMProperties(dpy, wd->win, &windowName, &iconName, + NULL, 0, size_hints, wm_hints, + NULL); + } + +//////////////////////////////////////////////////////////////////////////////// +App::~App() + { + for( WindowMap::iterator i = windows.begin(); i != windows.end(); ++i) + { + delete (*i).second->icon; + App::WindowData * wp = (*i).second; + windows.erase(i); + delete wp; + } + + if (dpy) + { + XCloseDisplay(dpy); + } + } + +//////////////////////////////////////////////////////////////////////////////// +int App::x_error_handler(Display * dpy, XErrorEvent *err) + { + char err_msg[1024]; + err_msg[0] = 0; + XGetErrorText(dpy, err->error_code, err_msg, 1024); + std::cerr << "X11 Error"<< std::endl + << " display : " << DisplayString(dpy) << std::endl + << " serial : " << err->serial << std::endl + << " error_code : " << (int) err->error_code << std::endl + << " request_code: " << (int) err->request_code << std::endl + << " minor_code : " << (int) err->minor_code << std::endl + << err_msg << std::endl; + return 0; + } + +//////////////////////////////////////////////////////////////////////////////// +void App::create_window(const std::string & win_name, + int x, int y, + unsigned int width, unsigned int height, + App::BitMap * icon) + { + Window win; + int border_width = 4; /* Border four pixels wide */ + + int screen_num = DefaultScreen(dpy); + + /* Create opaque window */ + win = XCreateSimpleWindow(dpy, + RootWindow(dpy, screen_num), + x, y, + width, height, + border_width, + BlackPixel(dpy, screen_num), + WhitePixel(dpy, screen_num)); + + icon->make_pixmap(dpy, win); + + // Add it to the window list + App::WindowData * win_data = new App::WindowData; + win_data->name = win_name; + win_data->win = win; + win_data->icon = icon; + windows.insert(win_pair(win_name, win_data)); + } + +//////////////////////////////////////////////////////////////////////////////// +// App::BitMap +//////////////////////////////////////////////////////////////////////////////// +App::BitMap::BitMap(const std::string & file) + { + read_from_file(file); + } + +//////////////////////////////////////////////////////////////////////////////// +App::BitMap::~BitMap() + { + if (data) XFree(data); + } + +//////////////////////////////////////////////////////////////////////////////// +void App::BitMap::make_pixmap(Display * dpy, Drawable d) + { + pixmap = XCreateBitmapFromData(dpy, d, (char *) data, width, height); + } + +//////////////////////////////////////////////////////////////////////////////// +int App::BitMap::read_from_file(const std::string & file) + { + if (BitmapSuccess != XReadBitmapFileData(file.c_str(), &width, &height, &data, NULL, NULL)) + { + return 0; + } + + name = file; + size_t p = name.rfind("/"); + if (p != std::string::npos) + { + name.replace(0, p+1, ""); + } + return 1; + } + diff -r acb4b5af3dfa -r c6dce2064bfb src/main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main.cpp Sun Apr 15 01:52:09 2012 -0500 @@ -0,0 +1,13 @@ +#include + +#include + +#include "App.h" + +int main(void) + { + App * app = new App("xlib_App"); + + sleep(5); + exit(0); + }