x.hpp 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // x.hpp: handles starting and managing X
  2. #ifndef IS_X_H_
  3. #define IS_X_H_
  4. #include <unistd.h>
  5. #include <X11/Xlib.h>
  6. #include <X11/cursorfont.h>
  7. #include <X11/extensions/shape.h>
  8. #include <cstdlib>
  9. #include <cmath>
  10. #include <cstdio>
  11. #include <string>
  12. #include <vector>
  13. namespace slop {
  14. enum CursorType {
  15. Left,
  16. Crosshair,
  17. Cross,
  18. UpperLeftCorner,
  19. UpperRightCorner,
  20. LowerRightCorner,
  21. LowerLeftCorner
  22. };
  23. class WindowRectangle {
  24. public:
  25. int m_x;
  26. int m_y;
  27. unsigned int m_width;
  28. unsigned int m_height;
  29. unsigned int m_border;
  30. bool m_decorations;
  31. void setGeometry( Window win, bool decorations );
  32. };
  33. class Rectangle {
  34. public:
  35. Rectangle( int x, int y, int width, int height, int border, int padding, float r, float g, float b );
  36. ~Rectangle();
  37. void setPos( int x, int y );
  38. void setDim( int w, int h );
  39. Window m_window;
  40. int m_x;
  41. int m_y;
  42. int m_xoffset;
  43. int m_yoffset;
  44. int m_width;
  45. int m_height;
  46. int m_border;
  47. int m_padding;
  48. XColor m_color;
  49. bool m_flippedx;
  50. bool m_flippedy;
  51. private:
  52. int convertColor( float r, float g, float b );
  53. void constrain( int w, int h );
  54. };
  55. class XEngine {
  56. public:
  57. XEngine();
  58. ~XEngine();
  59. int init( std::string display );
  60. void tick();
  61. int grabCursor( slop::CursorType type );
  62. int grabKeyboard();
  63. bool anyKeyPressed();
  64. int releaseCursor();
  65. int releaseKeyboard();
  66. void setCursor( slop::CursorType type );
  67. void drawRect( int x, int y, unsigned int w, unsigned int h );
  68. void addRect( Rectangle* rect );
  69. void removeRect( Rectangle* rect );
  70. Display* m_display;
  71. Visual* m_visual;
  72. Screen* m_screen;
  73. Colormap m_colormap;
  74. Window m_root;
  75. int m_mousex;
  76. int m_mousey;
  77. std::vector<bool> m_mouse;
  78. bool mouseDown( unsigned int button );
  79. Window m_hoverWindow;
  80. bool m_keypressed;
  81. private:
  82. void updateHoverWindow();
  83. void updateHoverWindow( Window child );
  84. bool m_good;
  85. std::vector<Cursor> m_cursors;
  86. std::vector<Rectangle*> m_rects;
  87. Cursor getCursor( slop::CursorType type );
  88. };
  89. int XEngineErrorHandler( Display* dpy, XErrorEvent* event );
  90. }
  91. extern slop::XEngine* xengine;
  92. #endif // IS_X_H_