changeset 4:c6dce2064bfb

Works so far
author Eris Caffee <discordia@eldalin.com>
date Sun, 15 Apr 2012 01:52:09 -0500
parents acb4b5af3dfa
children 008e28de2870
files CMakeLists.txt include/App.h include/xlib_App.xbm src/App.cpp src/main.cpp
diffstat 5 files changed, 332 insertions(+), 20 deletions(-) [+]
line diff
     1.1 --- a/CMakeLists.txt	Tue Mar 29 04:36:57 2011 -0500
     1.2 +++ b/CMakeLists.txt	Sun Apr 15 01:52:09 2012 -0500
     1.3 @@ -66,7 +66,7 @@
     1.4  #set (CMAKE_VERBOSE_MAKEFILE ON)
     1.5  
     1.6  # Name your program!
     1.7 -set (App_Name "")
     1.8 +set (App_Name "xlib_App")
     1.9  if (App_Name STREQUAL "") 
    1.10    message (FATAL_ERROR "You must set the App_Name variable!")
    1.11  endif ()
    1.12 @@ -167,11 +167,11 @@
    1.13  )
    1.14  
    1.15  
    1.16 -# # An example for a unix library named utils with a test driver program.
    1.17 -
    1.18 -# set (SRCS_utils
    1.19 -#   src/utils.cpp
    1.20 -#   include/utils.h
    1.21 +# Example to build a linux library with a test driver.
    1.22 + 
    1.23 +# set (SRCS_${App_Name}
    1.24 +#   src/.cpp
    1.25 +#   include/.h
    1.26  # )
    1.27  
    1.28  # include_directories (
    1.29 @@ -191,31 +191,37 @@
    1.30  # # clobber each others temp files since they are being built from the same
    1.31  # # sources.
    1.32  
    1.33 -# add_library (utils SHARED
    1.34 -#   ${SRCS_utils}
    1.35 +# add_library (${App_Name} SHARED
    1.36 +#   ${SRCS_${App_Name}}
    1.37  # )
    1.38 -# set_target_properties (utils PROPERTIES OUTPUT_NAME "utils")
    1.39 -# set_target_properties (utils PROPERTIES PREFIX "lib")
    1.40 -# set_target_properties (utils PROPERTIES CLEAN_DIRECT_OUTPUT 1)
    1.41 +# set_target_properties (${App_Name} PROPERTIES OUTPUT_NAME ${App_Name})
    1.42 +# set_target_properties (${App_Name} PROPERTIES PREFIX "lib")
    1.43 +# set_target_properties (${App_Name} PROPERTIES CLEAN_DIRECT_OUTPUT 1)
    1.44  
    1.45 -# add_library (utils-static STATIC
    1.46 -#   ${SRCS_utils}
    1.47 +# add_library (${App_Name}-static STATIC
    1.48 +#   ${SRCS_${App_Name}}
    1.49  # )
    1.50 -# set_target_properties (utils-static PROPERTIES OUTPUT_NAME "utils")
    1.51 -# set_target_properties (utils-static PROPERTIES PREFIX "lib")
    1.52 -# set_target_properties (utils-static PROPERTIES CLEAN_DIRECT_OUTPUT 1)
    1.53 +# set_target_properties (${App_Name}-static PROPERTIES OUTPUT_NAME ${App_Name})
    1.54 +# set_target_properties (${App_Name}-static PROPERTIES PREFIX "lib")
    1.55 +# set_target_properties (${App_Name}-static PROPERTIES CLEAN_DIRECT_OUTPUT 1)
    1.56  
    1.57  # # Build a test application.
    1.58  
    1.59 -# add_executable (test
    1.60 +# link_directories (
    1.61 +#   )
    1.62 +
    1.63 +# include_directories (
    1.64 +#   ${CMAKE_SOURCE_DIR}/include
    1.65 +#   )
    1.66 +
    1.67 +# add_executable (${App_Name}-test
    1.68  #   src/main.cpp
    1.69  # )
    1.70  
    1.71 -# target_link_libraries (test
    1.72 -#   utils
    1.73 +# target_link_libraries (${App_Name}-test
    1.74 +#   ${App_Name}
    1.75  # )
    1.76  
    1.77 -
    1.78  ################################################################################
    1.79  # X11
    1.80  #
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/include/App.h	Sun Apr 15 01:52:09 2012 -0500
     2.3 @@ -0,0 +1,69 @@
     2.4 +#include <map>
     2.5 +#include <string>
     2.6 +
     2.7 +#include <X11/Xlib.h>
     2.8 +
     2.9 +////////////////////////////////////////////////////////////////////////////////
    2.10 +class App
    2.11 +   {
    2.12 +   public:
    2.13 +
    2.14 +   /////////////////////////////////////
    2.15 +   class BitMap
    2.16 +      {
    2.17 +      public:
    2.18 +      BitMap(const std::string & file);
    2.19 +      ~BitMap();
    2.20 +
    2.21 +      void make_pixmap(Display * dpy, Drawable d);
    2.22 +
    2.23 +      // Yeah, it's all public here.  Keeping it simple.
    2.24 +      std::string name;
    2.25 +      unsigned int width;
    2.26 +      unsigned int height;
    2.27 +      unsigned char * data;
    2.28 +      Pixmap pixmap;
    2.29 +
    2.30 +      private:
    2.31 +      BitMap(const App::BitMap & b);
    2.32 +
    2.33 +      int read_from_file(const std::string & file);
    2.34 +      };
    2.35 +
    2.36 +   /////////////////////////////////////
    2.37 +   typedef struct
    2.38 +      {
    2.39 +      public:
    2.40 +      std::string name;
    2.41 +      Window win;
    2.42 +      App::BitMap * icon;
    2.43 +      } WindowData;
    2.44 +
    2.45 +   /////////////////////////////////////
    2.46 +
    2.47 +   typedef std::pair<std::string, App::WindowData *> win_pair;
    2.48 +   typedef std::map<std::string, App::WindowData *> WindowMap;
    2.49 +
    2.50 +   App(const std::string & appname);
    2.51 +   ~App();
    2.52 +
    2.53 +   static int x_error_handler(Display * dpy, XErrorEvent *err);
    2.54 +
    2.55 +   void create_window(const std::string& win_name,
    2.56 +		      int x, int y,
    2.57 +		      unsigned int width, unsigned int height,
    2.58 +		      App::BitMap * icon);
    2.59 +
    2.60 +   inline int display_height(void) const
    2.61 +      { return DisplayHeight(dpy, DefaultScreen(dpy)); };
    2.62 +
    2.63 +   inline int display_width(void) const
    2.64 +      { return DisplayWidth(dpy, DefaultScreen(dpy)); };
    2.65 +
    2.66 +   private:
    2.67 +   App();
    2.68 +   App(const App & a);
    2.69 +
    2.70 +   Display * dpy;
    2.71 +   WindowMap windows;
    2.72 +   };
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/include/xlib_App.xbm	Sun Apr 15 01:52:09 2012 -0500
     3.3 @@ -0,0 +1,46 @@
     3.4 +#define xlib_App_width 64
     3.5 +#define xlib_App_height 64
     3.6 +static unsigned char xlib_App_bits[] = {
     3.7 +   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     3.8 +   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
     3.9 +   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    3.10 +   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    3.11 +   0x00, 0xc0, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x0f, 0x00,
    3.12 +   0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00,
    3.13 +   0x80, 0xff, 0x3f, 0x00, 0x00, 0x00, 0x10, 0x00, 0xc0, 0xff, 0x3f, 0x00,
    3.14 +   0x00, 0x00, 0x3c, 0x00, 0xe0, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x3e, 0x00,
    3.15 +   0xf0, 0xff, 0x7f, 0x00, 0x00, 0x80, 0x1f, 0x00, 0xf0, 0xff, 0x7f, 0x00,
    3.16 +   0x00, 0x80, 0x0f, 0x00, 0xf8, 0xff, 0xff, 0x00, 0x00, 0xc0, 0x07, 0x00,
    3.17 +   0xfc, 0xff, 0xff, 0x01, 0x00, 0xe0, 0x03, 0x00, 0xfc, 0xf8, 0xff, 0x01,
    3.18 +   0x00, 0xf0, 0x01, 0x00, 0x38, 0xf0, 0xff, 0x03, 0x00, 0xf8, 0x01, 0x00,
    3.19 +   0x00, 0xe0, 0xff, 0x03, 0x00, 0x7c, 0x00, 0x00, 0x00, 0xe0, 0xff, 0x07,
    3.20 +   0x00, 0x7c, 0x00, 0x00, 0x00, 0xc0, 0xff, 0x0f, 0x00, 0x3f, 0x00, 0x00,
    3.21 +   0x00, 0x80, 0xff, 0x0f, 0x00, 0x0f, 0x00, 0x00, 0x00, 0x80, 0xff, 0x1f,
    3.22 +   0x80, 0x0f, 0x00, 0x00, 0x00, 0x00, 0xff, 0x1f, 0xc0, 0x03, 0x00, 0x00,
    3.23 +   0x00, 0x00, 0xff, 0x3f, 0xe0, 0x03, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x3f,
    3.24 +   0xf0, 0x01, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x7f, 0xf8, 0x00, 0x00, 0x00,
    3.25 +   0x00, 0x00, 0xfc, 0xff, 0x78, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xff,
    3.26 +   0x3c, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff, 0x3f, 0x00, 0x00, 0x00,
    3.27 +   0x00, 0x00, 0xf0, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0xff,
    3.28 +   0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0xff, 0x07, 0x00, 0x00, 0x00,
    3.29 +   0x00, 0x00, 0xe0, 0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff,
    3.30 +   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff, 0x00, 0x00, 0x00, 0x00,
    3.31 +   0x00, 0x00, 0xe0, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff,
    3.32 +   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xff, 0x01, 0x00, 0x00, 0x00,
    3.33 +   0x00, 0x00, 0xe0, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0xff,
    3.34 +   0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xff, 0x0f, 0x00, 0x00, 0x00,
    3.35 +   0x00, 0x00, 0xfe, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x80, 0xff, 0xff,
    3.36 +   0x1f, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xcf, 0xff, 0x7f, 0x00, 0x00, 0x00,
    3.37 +   0x00, 0xe0, 0x87, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, 0xf0, 0x03, 0xff,
    3.38 +   0xff, 0x07, 0xe0, 0x00, 0x00, 0xfc, 0x00, 0xfe, 0xff, 0x1f, 0xf0, 0x00,
    3.39 +   0x00, 0xfe, 0x00, 0xfc, 0xff, 0xff, 0x7c, 0x00, 0x00, 0x3f, 0x00, 0xf8,
    3.40 +   0xff, 0xff, 0x7f, 0x00, 0x80, 0x1f, 0x00, 0xf8, 0xff, 0xff, 0x3f, 0x00,
    3.41 +   0xc0, 0x0f, 0x00, 0xf0, 0xff, 0xff, 0x1f, 0x00, 0xe0, 0x03, 0x00, 0xe0,
    3.42 +   0xff, 0xff, 0x0f, 0x00, 0xf0, 0x01, 0x00, 0xc0, 0xff, 0xff, 0x07, 0x00,
    3.43 +   0xf0, 0x00, 0x00, 0xc0, 0xff, 0xff, 0x03, 0x00, 0x40, 0x00, 0x00, 0xc0,
    3.44 +   0xff, 0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0x80, 0xff, 0xff, 0x01, 0x00,
    3.45 +   0x00, 0x00, 0x00, 0x00, 0xfe, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    3.46 +   0xf8, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    3.47 +   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    3.48 +   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    3.49 +   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/App.cpp	Sun Apr 15 01:52:09 2012 -0500
     4.3 @@ -0,0 +1,178 @@
     4.4 +#include "App.h"
     4.5 +
     4.6 +#include <cstdlib>
     4.7 +#include <iostream>
     4.8 +#include <typeinfo>
     4.9 +#include <X11/Xutil.h>
    4.10 +
    4.11 +////////////////////////////////////////////////////////////////////////////////
    4.12 +App::App(const std::string & appname)
    4.13 +   {
    4.14 +   XSetErrorHandler(&App::x_error_handler);
    4.15 +
    4.16 +   if ((dpy = XOpenDisplay(NULL)) == NULL) 
    4.17 +      {
    4.18 +      std::cerr << "Unable to connect to display " << XDisplayName(NULL) << std::endl;
    4.19 +      exit(1);
    4.20 +      }
    4.21 +   std::cout << "Display name     : " << XDisplayName(NULL) << std::endl
    4.22 +	     << "Number of screens: " << ScreenCount(dpy) << std::endl
    4.23 +	     << "Default screen   : " << DefaultScreen(dpy) << std::endl
    4.24 +	     << "Protocol Version : " << ProtocolVersion(dpy) << std::endl
    4.25 +	     << "Protocol Revision: " << ProtocolRevision(dpy) << std::endl
    4.26 +	     << "Server Vendor    : " << ServerVendor(dpy) << std::endl
    4.27 +	     << "Vendor Release   : " << VendorRelease(dpy) << std::endl
    4.28 +	     << std::endl
    4.29 +	     << "Display width    : " << this->display_width() << std::endl
    4.30 +	     << "Display height   : " << this->display_height() << std::endl
    4.31 +	 ;
    4.32 +
    4.33 +   App::BitMap * icon = new App::BitMap("xlib_App.xbm");
    4.34 +
    4.35 +   // Create the main window
    4.36 +   create_window("main", 
    4.37 +		 0, 0 , 
    4.38 +		 this->display_width()/3, this->display_height()/4, 
    4.39 +		 icon);
    4.40 +
    4.41 +   // Even though we are not passing any actual size_hints, the Xlib programming
    4.42 +   // manual says to pass the flags below anyway.
    4.43 +   XSizeHints *size_hints;
    4.44 +   if (!(size_hints = XAllocSizeHints())) 
    4.45 +      {
    4.46 +      std::cerr << "XAllocSizeHints: memory allocation failure" << std::endl;
    4.47 +      exit(1);
    4.48 +      }
    4.49 +   size_hints->flags = PPosition | PSize;
    4.50 +
    4.51 +   XTextProperty windowName, iconName;
    4.52 +   const char * ptr = appname.c_str();
    4.53 +   if (XStringListToTextProperty(const_cast<char **>(&ptr), 1, &windowName) == 0) 
    4.54 +      {
    4.55 +      std::cerr << "XStringListToTextProperty: structure allocation for windowName failed." << std::endl;;
    4.56 +      exit(1);
    4.57 +   }
    4.58 +   ptr = icon->name.c_str();
    4.59 +   if (XStringListToTextProperty(const_cast<char **>(&ptr), 1, &iconName) == 0) 
    4.60 +      {
    4.61 +      std::cerr << "XStringListToTextProperty: structure allocation for iconName failed." << std::endl;;
    4.62 +      exit(1);
    4.63 +   }
    4.64 +
    4.65 +   XWMHints *wm_hints;
    4.66 +   if (!(wm_hints = XAllocWMHints()))
    4.67 +      {
    4.68 +      std::cerr << "XAllocWMHints: memory allocation failure" << std::endl;
    4.69 +      exit(0);
    4.70 +      }
    4.71 +   wm_hints->initial_state = NormalState;
    4.72 +   wm_hints->input = True;
    4.73 +   wm_hints->icon_pixmap = icon->pixmap;
    4.74 +   wm_hints->flags = StateHint | IconPixmapHint | InputHint;
    4.75 +
    4.76 +   App::WindowData * wd = windows["main"];
    4.77 +    XSetWMProperties(dpy, wd->win, &windowName, &iconName,
    4.78 +   		    NULL, 0, size_hints, wm_hints,
    4.79 +   		    NULL);
    4.80 +   }
    4.81 +
    4.82 +////////////////////////////////////////////////////////////////////////////////
    4.83 +App::~App()
    4.84 +   {
    4.85 +   for( WindowMap::iterator i = windows.begin(); i != windows.end(); ++i)
    4.86 +      {
    4.87 +      delete (*i).second->icon;
    4.88 +      App::WindowData * wp = (*i).second;
    4.89 +      windows.erase(i);
    4.90 +      delete wp;
    4.91 +      }
    4.92 +
    4.93 +   if (dpy)
    4.94 +      {
    4.95 +      XCloseDisplay(dpy);
    4.96 +      }
    4.97 +   }
    4.98 +
    4.99 +////////////////////////////////////////////////////////////////////////////////
   4.100 +int App::x_error_handler(Display * dpy, XErrorEvent *err)
   4.101 +   {
   4.102 +   char err_msg[1024];
   4.103 +   err_msg[0] = 0;
   4.104 +   XGetErrorText(dpy, err->error_code, err_msg, 1024);
   4.105 +   std::cerr << "X11 Error"<< std::endl
   4.106 +	     << "    display     : " << DisplayString(dpy) << std::endl
   4.107 +	     << "    serial      : " << err->serial << std::endl
   4.108 +	     << "    error_code  : " << (int) err->error_code << std::endl
   4.109 +	     << "    request_code: " << (int) err->request_code << std::endl
   4.110 +	     << "    minor_code  : " << (int) err->minor_code << std::endl
   4.111 +	     << err_msg << std::endl;
   4.112 +   return 0;
   4.113 +   }
   4.114 +
   4.115 +////////////////////////////////////////////////////////////////////////////////
   4.116 +void App::create_window(const std::string & win_name,
   4.117 +			int x, int y,
   4.118 +			unsigned int width, unsigned int height,
   4.119 +			App::BitMap * icon)
   4.120 +   {
   4.121 +   Window win;
   4.122 +   int border_width = 4;           /* Border four pixels wide */
   4.123 +
   4.124 +   int screen_num = DefaultScreen(dpy);
   4.125 +
   4.126 +   /* Create opaque window */
   4.127 +   win = XCreateSimpleWindow(dpy, 
   4.128 +			     RootWindow(dpy, screen_num),
   4.129 +			     x, y, 
   4.130 +			     width, height, 
   4.131 +			     border_width, 
   4.132 +			     BlackPixel(dpy, screen_num), 
   4.133 +			     WhitePixel(dpy, screen_num));
   4.134 +
   4.135 +   icon->make_pixmap(dpy, win);
   4.136 +
   4.137 +   // Add it to the window list
   4.138 +   App::WindowData * win_data = new App::WindowData;
   4.139 +   win_data->name = win_name;
   4.140 +   win_data->win = win;
   4.141 +   win_data->icon = icon;
   4.142 +   windows.insert(win_pair(win_name, win_data));
   4.143 +   }
   4.144 +
   4.145 +////////////////////////////////////////////////////////////////////////////////
   4.146 +// App::BitMap
   4.147 +////////////////////////////////////////////////////////////////////////////////
   4.148 +App::BitMap::BitMap(const std::string & file)
   4.149 +   {
   4.150 +   read_from_file(file);
   4.151 +   }
   4.152 +
   4.153 +////////////////////////////////////////////////////////////////////////////////
   4.154 +App::BitMap::~BitMap() 
   4.155 +   {
   4.156 +   if (data) XFree(data);
   4.157 +   }
   4.158 +
   4.159 +////////////////////////////////////////////////////////////////////////////////
   4.160 +void App::BitMap::make_pixmap(Display * dpy, Drawable d)
   4.161 +   {
   4.162 +   pixmap = XCreateBitmapFromData(dpy, d, (char *) data, width, height);
   4.163 +   }
   4.164 +
   4.165 +////////////////////////////////////////////////////////////////////////////////
   4.166 +int App::BitMap::read_from_file(const std::string & file)
   4.167 +   {
   4.168 +   if (BitmapSuccess != XReadBitmapFileData(file.c_str(), &width, &height, &data, NULL, NULL))
   4.169 +      {
   4.170 +      return 0;
   4.171 +      }
   4.172 +
   4.173 +   name = file;
   4.174 +   size_t p = name.rfind("/");
   4.175 +   if (p != std::string::npos)
   4.176 +      {
   4.177 +      name.replace(0, p+1, "");
   4.178 +      }
   4.179 +   return 1;
   4.180 +   }
   4.181 +
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/main.cpp	Sun Apr 15 01:52:09 2012 -0500
     5.3 @@ -0,0 +1,13 @@
     5.4 +#include <cstdlib>
     5.5 +
     5.6 +#include <unistd.h>
     5.7 +
     5.8 +#include "App.h"
     5.9 +
    5.10 +int main(void)
    5.11 +   {
    5.12 +   App * app = new App("xlib_App");
    5.13 +
    5.14 +   sleep(5);
    5.15 +   exit(0);
    5.16 +   }