view src/App.cpp @ 4:c6dce2064bfb

Works so far
author Eris Caffee <discordia@eldalin.com>
date Sun, 15 Apr 2012 01:52:09 -0500
parents
children 008e28de2870
line source
1 #include "App.h"
3 #include <cstdlib>
4 #include <iostream>
5 #include <typeinfo>
6 #include <X11/Xutil.h>
8 ////////////////////////////////////////////////////////////////////////////////
9 App::App(const std::string & appname)
10 {
11 XSetErrorHandler(&App::x_error_handler);
13 if ((dpy = XOpenDisplay(NULL)) == NULL)
14 {
15 std::cerr << "Unable to connect to display " << XDisplayName(NULL) << std::endl;
16 exit(1);
17 }
18 std::cout << "Display name : " << XDisplayName(NULL) << std::endl
19 << "Number of screens: " << ScreenCount(dpy) << std::endl
20 << "Default screen : " << DefaultScreen(dpy) << std::endl
21 << "Protocol Version : " << ProtocolVersion(dpy) << std::endl
22 << "Protocol Revision: " << ProtocolRevision(dpy) << std::endl
23 << "Server Vendor : " << ServerVendor(dpy) << std::endl
24 << "Vendor Release : " << VendorRelease(dpy) << std::endl
25 << std::endl
26 << "Display width : " << this->display_width() << std::endl
27 << "Display height : " << this->display_height() << std::endl
28 ;
30 App::BitMap * icon = new App::BitMap("xlib_App.xbm");
32 // Create the main window
33 create_window("main",
34 0, 0 ,
35 this->display_width()/3, this->display_height()/4,
36 icon);
38 // Even though we are not passing any actual size_hints, the Xlib programming
39 // manual says to pass the flags below anyway.
40 XSizeHints *size_hints;
41 if (!(size_hints = XAllocSizeHints()))
42 {
43 std::cerr << "XAllocSizeHints: memory allocation failure" << std::endl;
44 exit(1);
45 }
46 size_hints->flags = PPosition | PSize;
48 XTextProperty windowName, iconName;
49 const char * ptr = appname.c_str();
50 if (XStringListToTextProperty(const_cast<char **>(&ptr), 1, &windowName) == 0)
51 {
52 std::cerr << "XStringListToTextProperty: structure allocation for windowName failed." << std::endl;;
53 exit(1);
54 }
55 ptr = icon->name.c_str();
56 if (XStringListToTextProperty(const_cast<char **>(&ptr), 1, &iconName) == 0)
57 {
58 std::cerr << "XStringListToTextProperty: structure allocation for iconName failed." << std::endl;;
59 exit(1);
60 }
62 XWMHints *wm_hints;
63 if (!(wm_hints = XAllocWMHints()))
64 {
65 std::cerr << "XAllocWMHints: memory allocation failure" << std::endl;
66 exit(0);
67 }
68 wm_hints->initial_state = NormalState;
69 wm_hints->input = True;
70 wm_hints->icon_pixmap = icon->pixmap;
71 wm_hints->flags = StateHint | IconPixmapHint | InputHint;
73 App::WindowData * wd = windows["main"];
74 XSetWMProperties(dpy, wd->win, &windowName, &iconName,
75 NULL, 0, size_hints, wm_hints,
76 NULL);
77 }
79 ////////////////////////////////////////////////////////////////////////////////
80 App::~App()
81 {
82 for( WindowMap::iterator i = windows.begin(); i != windows.end(); ++i)
83 {
84 delete (*i).second->icon;
85 App::WindowData * wp = (*i).second;
86 windows.erase(i);
87 delete wp;
88 }
90 if (dpy)
91 {
92 XCloseDisplay(dpy);
93 }
94 }
96 ////////////////////////////////////////////////////////////////////////////////
97 int App::x_error_handler(Display * dpy, XErrorEvent *err)
98 {
99 char err_msg[1024];
100 err_msg[0] = 0;
101 XGetErrorText(dpy, err->error_code, err_msg, 1024);
102 std::cerr << "X11 Error"<< std::endl
103 << " display : " << DisplayString(dpy) << std::endl
104 << " serial : " << err->serial << std::endl
105 << " error_code : " << (int) err->error_code << std::endl
106 << " request_code: " << (int) err->request_code << std::endl
107 << " minor_code : " << (int) err->minor_code << std::endl
108 << err_msg << std::endl;
109 return 0;
110 }
112 ////////////////////////////////////////////////////////////////////////////////
113 void App::create_window(const std::string & win_name,
114 int x, int y,
115 unsigned int width, unsigned int height,
116 App::BitMap * icon)
117 {
118 Window win;
119 int border_width = 4; /* Border four pixels wide */
121 int screen_num = DefaultScreen(dpy);
123 /* Create opaque window */
124 win = XCreateSimpleWindow(dpy,
125 RootWindow(dpy, screen_num),
126 x, y,
127 width, height,
128 border_width,
129 BlackPixel(dpy, screen_num),
130 WhitePixel(dpy, screen_num));
132 icon->make_pixmap(dpy, win);
134 // Add it to the window list
135 App::WindowData * win_data = new App::WindowData;
136 win_data->name = win_name;
137 win_data->win = win;
138 win_data->icon = icon;
139 windows.insert(win_pair(win_name, win_data));
140 }
142 ////////////////////////////////////////////////////////////////////////////////
143 // App::BitMap
144 ////////////////////////////////////////////////////////////////////////////////
145 App::BitMap::BitMap(const std::string & file)
146 {
147 read_from_file(file);
148 }
150 ////////////////////////////////////////////////////////////////////////////////
151 App::BitMap::~BitMap()
152 {
153 if (data) XFree(data);
154 }
156 ////////////////////////////////////////////////////////////////////////////////
157 void App::BitMap::make_pixmap(Display * dpy, Drawable d)
158 {
159 pixmap = XCreateBitmapFromData(dpy, d, (char *) data, width, height);
160 }
162 ////////////////////////////////////////////////////////////////////////////////
163 int App::BitMap::read_from_file(const std::string & file)
164 {
165 if (BitmapSuccess != XReadBitmapFileData(file.c_str(), &width, &height, &data, NULL, NULL))
166 {
167 return 0;
168 }
170 name = file;
171 size_t p = name.rfind("/");
172 if (p != std::string::npos)
173 {
174 name.replace(0, p+1, "");
175 }
176 return 1;
177 }