123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. };
  31. class Rectangle {
  32. public:
  33. Rectangle( int x, int y, int width, int height, int border, int padding, float r, float g, float b );
  34. ~Rectangle();
  35. void setPos( int x, int y );
  36. void setDim( int w, int h );
  37. Window m_window;
  38. int m_x;
  39. int m_y;
  40. int m_xoffset;
  41. int m_yoffset;
  42. int m_width;
  43. int m_height;
  44. int m_border;
  45. int m_padding;
  46. XColor m_color;
  47. bool m_flippedx;
  48. bool m_flippedy;
  49. private:
  50. int convertColor( float r, float g, float b );
  51. void constrain( int w, int h );
  52. };
  53. class XEngine {
  54. public:
  55. XEngine();
  56. ~XEngine();
  57. int init( std::string display );
  58. void tick();
  59. int grabCursor( slop::CursorType type );
  60. int grabKeyboard();
  61. bool anyKeyPressed();
  62. int releaseCursor();
  63. int releaseKeyboard();
  64. void setCursor( slop::CursorType type );
  65. void drawRect( int x, int y, unsigned int w, unsigned int h );
  66. void addRect( Rectangle* rect );
  67. void removeRect( Rectangle* rect );
  68. Display* m_display;
  69. Visual* m_visual;
  70. Screen* m_screen;
  71. Colormap m_colormap;
  72. Window m_root;
  73. int m_mousex;
  74. int m_mousey;
  75. std::vector<bool> m_mouse;
  76. bool mouseDown( unsigned int button );
  77. WindowRectangle m_hoverWindow;
  78. Window m_hoverXWindow;
  79. bool m_keypressed;
  80. private:
  81. void updateHoverWindow();
  82. void updateHoverWindow( Window child );
  83. bool m_good;
  84. std::vector<Cursor> m_cursors;
  85. std::vector<Rectangle*> m_rects;
  86. Cursor getCursor( slop::CursorType type );
  87. };
  88. int XEngineErrorHandler( Display* dpy, XErrorEvent* event );
  89. }
  90. extern slop::XEngine* xengine;
  91. #endif // IS_X_H_