xcore.h (9690B)
1 // Copyright (c) 2003 - 2009 Anselm R Garbe <anselm@garbe.us> 2 // See LICENSE for license details. 3 4 #ifndef __XCORE_H 5 #define __XCORE_H 6 7 #include "wmi.h" 8 9 extern "C" { 10 #include <X11/Xlib.h> 11 #include <X11/Xutil.h> 12 #ifdef XFT_SUPPORT 13 #include <X11/Xft/Xft.h> 14 #endif // XFT_SUPPORT 15 } 16 17 #include <string> 18 19 #include "singleton.h" 20 21 #define PDELETE 1 22 #define PTAKEFOCUS 2 23 24 /** 25 * Motif bits - see Motif reference manual for further details. 26 */ 27 struct MWMHints { 28 // Think: do we need CARD32 and INT32 here? 29 long flags; 30 long functions; 31 long decorations; 32 int inputMode; 33 long status; 34 }; 35 36 class XCore; 37 class Rectangle; 38 class WFont; 39 40 /** 41 * This class provides an abstraction layer for graphical user 42 * interfaces such as X11 or maybe Y in future. 43 */ 44 class XCore : public Singleton<XCore> 45 { 46 47 public: 48 49 /** Constructs new kernel instance. */ 50 XCore(); 51 52 ~XCore(); 53 54 void setDisplay(Display *display); 55 56 /** Creates new window. */ 57 Window createWindow(Window root, XSetWindowAttributes *attr, 58 int x, int y, unsigned int w, unsigned int h, 59 unsigned long mask); 60 61 /** Selects events specified by mask for window. */ 62 void selectEvents(Window window, long mask); 63 64 /** Input focus setting. */ 65 void setInputFocus(Window window); 66 67 /** Shows the specified window. (map) */ 68 void show(Window window); 69 70 /** Shows the specified window raised. (map raised) */ 71 void showRaised(Window window); 72 73 /** Hides the window. (unmap) */ 74 void hide(Window window); 75 76 /** 77 * Creates new GC for the specified window, with gcv masked values. 78 */ 79 GC createGC(Window window, unsigned long mask, XGCValues *gcv); 80 81 /** Grabs the keyboard for the specific window. */ 82 void grabKeyboard(Window window); 83 84 /** Ungrabs the keyboard. */ 85 void ungrabKeyboard(); 86 87 /** Frees the data by X request. */ 88 void free(void *data); 89 90 /** Syncs with X. */ 91 void sync(bool discard = False); 92 93 /** Sets foreground color. */ 94 void setForeground(GC gc, unsigned long color); 95 96 /** Draws lines performantly. */ 97 void drawLines(Window window, GC gc, XPoint *points, unsigned int num); 98 99 /** Draws a line. */ 100 void drawLine(Window window, GC gc, unsigned int x1, unsigned int y1, 101 unsigned int x2, unsigned int y2); 102 103 /** Draws rectangle. */ 104 void drawRectangle(Window window, GC gc, int x, int y, unsigned int w, 105 unsigned int h); 106 107 /** Draws rectangle. */ 108 void drawRectangle(Window window, GC gc, Rectangle *rect); 109 110 /** Clears the specific area within specified window. */ 111 void clearArea(Window window, Rectangle *rect); 112 113 /** Clears the specific area within specified window. */ 114 void clearArea(Window window, int x, int y, unsigned int w, unsigned int h); 115 116 /** Clears the specified window. */ 117 void clearWindow(Window window); 118 119 /** Fills the specific window with rectangle. */ 120 void fillRectangle(Window window, GC gc, Rectangle *rect); 121 122 /** Fills the specific window with rectangle. */ 123 void fillRectangle(Window window, GC gc, int x, int y, 124 unsigned int w, unsigned int h); 125 126 /** Loads the font specified by name. */ 127 XFontStruct *loadQueryFont(string name); 128 129 /** Draws a string at (x, y) to the specific window. */ 130 void drawText(Window window, GC gc, int x, int y, 131 string text); 132 133 /** Returns text width for the given values. */ 134 int textWidth(XFontStruct *font, string text); 135 136 #ifdef XFT_SUPPORT 137 /** Opens a XftFont. */ 138 XftFont *xftOpenFontName(unsigned int screen, string name); 139 140 /** Closes the specific XftFont. */ 141 void xftCloseFont(XftFont *font); 142 143 /** Draws a string at (x, y) to the specific window. */ 144 void drawText(XftFont *font, Window window, GC gc, 145 unsigned int screen, int x, int y, 146 string text); 147 148 /** Returns text width for the given values. */ 149 int textWidth(XftFont *font, string text); 150 #endif 151 152 /** Returns class name for the specific window. */ 153 string className(Window window); 154 155 /** Returns instance name for the specific window. */ 156 string instanceName(Window window); 157 158 /** Returns state for the specific window. */ 159 int state(Window window); 160 161 /** Sets the state for the specific window. */ 162 void setState(Window window, int state); 163 164 /** Returns value of the specific atom as string. */ 165 string atomValue(Window window, Atom atom); 166 167 /** Updates size infos for the specific window. */ 168 void updateSize(Window window, 169 unsigned int *minW, unsigned int *minH, 170 unsigned int *maxW, unsigned int *maxH, 171 bool *isCentered); 172 173 /** Returns true if the specific window has MWM decoration. */ 174 bool hasDecoration(Window window); 175 176 /** Returns transient for the specific window. */ 177 Window transient(Window window); 178 179 /** Returns property for specific values. */ 180 int property(Window window, Atom atom, Atom type, 181 long length, unsigned char **prop); 182 183 /** Sends configuration to the specific window. */ 184 void configure(Window window, XConfigureEvent *event); 185 186 /** Returns protocols for the specific window. */ 187 int protocols(Window window); 188 189 /** Sends a message to the specific window. */ 190 void sendMessage(Window window, Atom atom, long value); 191 192 /** Kills the client with the specific window. */ 193 void kill(Window window, int protos); 194 195 /** Resizes the specific window. */ 196 void moveResize(Window window, Rectangle *rect); 197 198 /** Destroys the window. */ 199 void destroy(Window window); 200 201 /** Raises the specific window. */ 202 void raise(Window window); 203 204 /** Lowers the specific window. */ 205 void lower(Window window); 206 207 /** Reparents the specific window. */ 208 void reparent(Window child, Window parent, int x, int y); 209 210 /** Grabs the key for the specified window. */ 211 void grabKey(Window window, unsigned int modMask, KeyCode keycode); 212 213 /** Ungrabs key. */ 214 void ungrabKey(Window window, unsigned int modMask, KeyCode keycode); 215 216 /** Grabs button. */ 217 void grabButton(Window window, unsigned int modMask, unsigned int button); 218 219 /** Ungrabs button. */ 220 void ungrabButton(Window window, unsigned int modMask, unsigned int button); 221 222 /** Grabs the server. */ 223 void grabServer(); 224 225 /** Ungrabs the server. */ 226 void ungrabServer(); 227 228 /** Grabs the pointer. */ 229 void grabPointer(Window window, unsigned int mask, Time time, 230 Cursor cursor = None); 231 232 /** Ungrabs pointer. */ 233 void ungrabPointer(Time time); 234 235 /** Returns specified atom. */ 236 Atom internAtom(string atom); 237 238 /** Returns new font cursor specified by cursor. */ 239 Cursor createFontCursor(unsigned int cursor); 240 241 /** Set window attributes. */ 242 void setWindowAttributes(Window window, unsigned int mask, 243 XSetWindowAttributes *attr); 244 245 /** Writes window attributes to attr. */ 246 void windowAttributes(Window window, XWindowAttributes *attr); 247 248 /** Returns KeyCode for string. */ 249 KeyCode stringToKeyCode(string key); 250 251 /** Returns string for KeyCode. */ 252 string keyCodeToString(KeyCode keyCode); 253 254 /** Returns KeySym for KeyCode. */ 255 KeySym keyCodeToKeySym(KeyCode keyCode); 256 257 /** Returns name of atom. */ 258 string atomName(Atom atom); 259 260 /** Writes next event back to event. */ 261 void nextEvent(XEvent *event); 262 263 /** Checks for events specified by mask, returns true if matched. */ 264 bool checkMaskEvent(long mask, XEvent *event); 265 266 /** 267 * Translates Coordinates given by srcX and srcY absoluted to child 268 * window into destX and destY coordinates absoluted to parent 269 * to window. 270 */ 271 void translateCoordinates(Window child, Window parent, 272 int srcX, int srcY, int *destX, int *destY); 273 274 /** Returns true if color could be allocated, otherwise false. */ 275 bool allocNamedColor(Colormap colormap, string name, XColor *color); 276 277 /** Installs colormap. */ 278 void installColormap(Colormap colormap); 279 280 Colormap defaultColormap(unsigned int id); 281 282 /** Queries a window tree. */ 283 void queryTree(Window root, Window **windows, unsigned int *num); 284 285 /** Returns KeySym for next char by this XKeyEvent. */ 286 KeySym lookupNextKeySym(XKeyEvent *event, int *count, char *buffer); 287 288 /** Writes back text property value for window and specified atom. */ 289 bool textProperty(Window window, Atom atom, string *value); 290 291 /** Converts string to XTextProperty. */ 292 void stringListToTextProperty(string text, XTextProperty *textProperty); 293 void setTextProperty(Display *display, Window window, 294 XTextProperty *textProperty, Atom atom); 295 296 /** Default error handler. */ 297 static int handleErrors(Display *display, XErrorEvent *e); 298 299 void configureWindow(Window window, unsigned int mask, XWindowChanges *wc); 300 301 /** NumLock/ScrollLock modifier detection. */ 302 XModifierKeymap *modifierMapping(); 303 void freeModifierMapping(XModifierKeymap *modmap); 304 305 void addToSaveSet(Window window); 306 void removeFromSaveSet(Window window); 307 308 /** Fetches size hints. */ 309 void sizeHints(Window window, XSizeHints *sizeHints); 310 311 void warpPointer(Window window, int x, int y); 312 313 void freeCursor(Cursor cursor); 314 315 void maskEvent(unsigned long mask, XEvent *event); 316 317 void sendEvent(Window window, unsigned long mask, XEvent *event); 318 319 XWMHints *getWMHints(Window window); 320 321 private: 322 323 Display *display_; 324 325 }; 326 327 #endif // __XCORE_H