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