Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <linux/input.h>
- #include <unistd.h>
- #include <sys/time.h>
- #include <climits>
- #include <chrono>
- #include <X11/Xos.h>
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
- #include <X11/Xatom.h>
- #include <X11/extensions/shape.h>
- #include <X11/extensions/Xcomposite.h>
- #include <X11/extensions/Xfixes.h>
- #include <math.h>
- #include <sstream>
- #include <string>
- using namespace std;
- #define ALIGN_LEFT 0
- #define ALIGN_CENTER 1
- #define ALIGN_RIGHT 2
- // Events for normal windows
- #define BASIC_EVENT_MASK (StructureNotifyMask|ExposureMask|PropertyChangeMask|EnterWindowMask|LeaveWindowMask|KeyPressMask|KeyReleaseMask|KeymapStateMask)
- #define NOT_PROPAGATE_MASK (KeyPressMask|KeyReleaseMask|ButtonPressMask|ButtonReleaseMask|PointerMotionMask|ButtonMotionMask)
- #define FPSMETERSAMPLE 100
- #define CLEARSCREEN_ITERATIONS 2000
- class Draw {
- public:
- Draw();
- void drawstring(const char * text, int x, int y, XColor fgcolor, XColor bgcolor, int align);
- XColor createXColorFromRGBA(short red, short green, short blue, short alpha);
- XColor createXColorFromRGB(short red, short green, short blue);
- void createShapedWindow();
- void openDisplay();
- void allow_input_passthrough (Window w);
- void list_fonts();
- void init();
- void halt();
- void toggleoverlay(bool state);
- void toggleoverlay();
- void clearscreen();
- void startdraw();
- void enddraw();
- void cleararea(int x, int y, int width, int height);
- Display *g_display;
- int g_screen;
- Window g_win;
- int g_disp_width;
- int g_disp_height;
- Colormap g_colormap;
- bool overlayenabled = false;
- bool overlayavailable = false;
- GC gc;
- XGCValues gcv;
- XFontStruct * font;
- XColor red, black, blacka, blackla, white, transparent, ltblue, blue, yellow, grey, ltgrey;
- const char * font_name = "9x15bold";
- const int font_width = 9;
- const int font_height = 15;
- // The window size
- int WIDTH = 1920;
- int HEIGHT = 1080;
- private:
- long event_mask = (StructureNotifyMask|ExposureMask|PropertyChangeMask|EnterWindowMask|LeaveWindowMask|KeyRelease | ButtonPress|ButtonRelease|KeymapStateMask);
- int shape_event_base;
- int shape_error_base;
- std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
- std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
- int fpsmeterc = 0;
- uint64_t duration = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
- string fpsstring = "";
- int renderi = 0;
- };
- // __________________________________________________________________________
- #include "draw.hpp"
- Draw::Draw() {
- }
- void Draw::allow_input_passthrough (Window w) {
- XserverRegion region = XFixesCreateRegion (g_display, NULL, 0);
- //XFixesSetWindowShapeRegion (g_display, w, ShapeBounding, 0, 0, 0);
- XFixesSetWindowShapeRegion (g_display, w, ShapeInput, 0, 0, region);
- XFixesDestroyRegion (g_display, region);
- }
- void Draw::list_fonts() {
- char **fontlist;
- int num_fonts;
- fontlist = XListFonts (g_display, "*", 1000, &num_fonts);
- for (int i = 0; i < num_fonts; ++i) {
- fprintf(stderr, "> %s\n", fontlist[i]);
- }
- }
- XColor Draw::createXColorFromRGB(short red, short green, short blue) {
- XColor color;
- // m_color.red = red * 65535 / 255;
- color.red = (red * 0xFFFF) / 0xFF;
- color.green = (green * 0xFFFF) / 0xFF;
- color.blue = (blue * 0xFFFF) / 0xFF;
- color.flags = DoRed | DoGreen | DoBlue;
- if (!XAllocColor(g_display, DefaultColormap(g_display, g_screen), &color)) {
- std::cerr << "createXColorFromRGB: Cannot create color" << endl;
- exit(-1);
- }
- return color;
- }
- XColor Draw::createXColorFromRGBA(short red, short green, short blue, short alpha) {
- XColor color;
- // m_color.red = red * 65535 / 255;
- color.red = (red * 0xFFFF) / 0xFF;
- color.green = (green * 0xFFFF) / 0xFF;
- color.blue = (blue * 0xFFFF) / 0xFF;
- color.flags = DoRed | DoGreen | DoBlue;
- if (!XAllocColor(g_display, DefaultColormap(g_display, g_screen), &color)) {
- std::cerr << "createXColorFromRGBA: Cannot create color" << endl;
- exit(-1);
- }
- *(&color.pixel) = ((*(&color.pixel)) & 0x00ffffff) | (alpha << 24);
- return color;
- }
- void Draw::openDisplay() {
- g_display = XOpenDisplay(0);
- if (!g_display) {
- cerr << "Failed to open X display!" << endl;
- return;
- }
- g_screen = DefaultScreen(g_display);
- g_disp_width = DisplayWidth(g_display, g_screen);
- g_disp_height = DisplayHeight(g_display, g_screen);
- if (!XShapeQueryExtension (g_display, &shape_event_base, &shape_error_base)) {
- cerr << "XShape extension is not available in your system!" << endl;
- return;
- }
- overlayavailable = true;
- }
- // Create a window
- void Draw::createShapedWindow() {
- XSetWindowAttributes wattr;
- XColor bgcolor = createXColorFromRGBA(0, 0, 0, 0);
- Window root = DefaultRootWindow(g_display);
- Visual *visual = DefaultVisual(g_display, g_screen);
- XVisualInfo vinfo;
- XMatchVisualInfo(g_display, DefaultScreen(g_display), 32, TrueColor, &vinfo);
- g_colormap = XCreateColormap(g_display, DefaultRootWindow(g_display), vinfo.visual, AllocNone);
- XSetWindowAttributes attr;
- attr.background_pixmap = None;
- attr.background_pixel = bgcolor.pixel;
- attr.border_pixel=0;
- attr.win_gravity=NorthWestGravity;
- attr.bit_gravity=ForgetGravity;
- attr.save_under=1;
- attr.event_mask=BASIC_EVENT_MASK;
- attr.do_not_propagate_mask=NOT_PROPAGATE_MASK;
- attr.override_redirect=1; // OpenGL > 0
- attr.colormap = g_colormap;
- //unsigned long mask = CWBackPixel|CWBorderPixel|CWWinGravity|CWBitGravity|CWSaveUnder|CWEventMask|CWDontPropagate|CWOverrideRedirect;
- unsigned long mask = CWColormap | CWBorderPixel | CWBackPixel | CWEventMask | CWWinGravity|CWBitGravity | CWSaveUnder | CWDontPropagate | CWOverrideRedirect;
- g_win = XCreateWindow(g_display, root, 0, 0, WIDTH, HEIGHT, 0, vinfo.depth, InputOutput, vinfo.visual, mask, &attr);
- //XShapeCombineMask(g_display, g_win, ShapeBounding, 900, 500, g_bitmap, ShapeSet);
- XShapeCombineMask(g_display, g_win, ShapeInput, 0, 0, None, ShapeSet );
- // We want shape-changed event too
- #define SHAPE_MASK ShapeNotifyMask
- XShapeSelectInput (g_display, g_win, SHAPE_MASK );
- // Tell the Window Manager not to draw window borders (frame) or title.
- wattr.override_redirect = 1;
- XChangeWindowAttributes(g_display, g_win, CWOverrideRedirect, &wattr);
- allow_input_passthrough(g_win);
- // Show the window
- XMapWindow(g_display, g_win);
- }
- void Draw::startdraw() {
- if(!overlayenabled) return;
- fpsmeterc++;
- if(fpsmeterc == FPSMETERSAMPLE) {
- fpsmeterc = 0;
- t1 = t2;
- t2 = std::chrono::high_resolution_clock::now();
- duration = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
- fpsstring = /*to_string(duration) + " / " +*/ to_string(duration/FPSMETERSAMPLE);
- }
- /*// Line width and type
- gcv.line_width = 1;
- gcv.line_style = LineSolid;
- gcv.foreground = red.pixel;
- unsigned long mask = GCLineWidth | GCLineStyle | GCForeground;
- gc = XCreateGC(g_display, g_win, mask, &gcv);*/
- XSetBackground (g_display, gc, white.pixel);
- XSetForeground (g_display, gc, red.pixel);
- font = XLoadQueryFont (g_display, font_name);
- XSetFont (g_display, gc, font->fid);
- }
- void Draw::drawstring(const char * text, int x, int y, XColor fgcolor, XColor bgcolor, int align) {
- if(!overlayenabled) return;
- int tlen = strlen(text);
- XSetForeground (g_display, gc, bgcolor.pixel);
- XFillRectangle(g_display, g_win, gc, x - (align == ALIGN_CENTER ? tlen*font_width/2 : (align == ALIGN_RIGHT ? tlen*font_width : 0)) - 4, y, tlen*font_width+8, font_height+4);
- XSetForeground (g_display, gc, fgcolor.pixel);
- XDrawString(g_display, g_win, gc, x - (align == ALIGN_CENTER ? tlen*font_width/2 : (align == ALIGN_RIGHT ? tlen*font_width : 0)), y+font_height, text, tlen);
- }
- void Draw::enddraw() {
- if(!overlayenabled) return;
- if(duration > 0.0f) drawstring(fpsstring.c_str(), WIDTH/2, 44, red, blacka, ALIGN_CENTER);
- renderi++;
- if(renderi == CLEARSCREEN_ITERATIONS) {
- clearscreen();
- renderi = 0;
- }
- XFreeFont(g_display, font);
- //XFreeGC(g_display, gc);
- }
- void Draw::clearscreen() {
- if(!overlayenabled) return;
- XClearArea(g_display, g_win, 0, 0, WIDTH, HEIGHT, false);
- }
- void Draw::cleararea(int x, int y, int width, int height) {
- if(!overlayenabled) return;
- XClearArea(g_display, g_win, x, y, width, height, false);
- }
- void Draw::init() {
- openDisplay();
- if(!overlayavailable) {
- fprintf (stderr, "Overlay is not availalbe!\n");
- return;
- }
- createShapedWindow();
- gc = XCreateGC (g_display, g_win, 0, 0);
- font = XLoadQueryFont (g_display, font_name);
- red = createXColorFromRGBA(255, 0, 0, 255);
- ltblue = createXColorFromRGBA(0, 255, 255, 255);
- blue = createXColorFromRGBA(0, 0, 255, 255);
- black = createXColorFromRGBA(0, 0, 0, 255);
- blacka = createXColorFromRGBA(0, 0, 0, 150);
- blackla = createXColorFromRGBA(0, 0, 0, 80);
- grey = createXColorFromRGBA(110, 110, 110, 255);
- ltgrey = createXColorFromRGBA(170, 170, 170, 255);
- white = createXColorFromRGBA(255, 255, 255, 255);
- yellow = createXColorFromRGBA(255, 255, 0, 255);
- transparent = createXColorFromRGBA(255, 255, 255, 0);
- // const char * fontname = "-misc-fixed-bold-r-normal--18-120-100-100-c-90-iso8859-2";
- // const char * fontname = "rk24"; // ~ chinese shit
- // list_fonts();
- font = XLoadQueryFont (g_display, font_name);
- /* If the font could not be loaded, revert to the "fixed" font. */
- if (!font) {
- fprintf (stderr, "Unable to load font %s > using default (fixed)\n", font_name);
- font = XLoadQueryFont (g_display, "fixed");
- }
- }
- void Draw::toggleoverlay(bool state) { if(overlayavailable) overlayenabled = state; }
- void Draw::toggleoverlay() { if(overlayavailable) overlayenabled = !overlayenabled; }
- void Draw::halt() { if(!overlayavailable) XCloseDisplay(g_display); }
Advertisement
Add Comment
Please, Sign In to add comment