Created
February 16, 2015 23:18
-
-
Save Camusensei/ac3040285801c39245af to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
g++ src/* -o manager -lX11 -lglog -w |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "WindowManager.hpp" | |
int main() | |
{ | |
WindowManager manager; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "WindowManager.hpp" | |
#include <X11/Xlib.h> | |
#include <X11/Xutil.h> | |
#include <cstring> | |
#include <algorithm> | |
#include <iostream> | |
#include <stdio.h> | |
#include <glog/logging.h> | |
WindowManager::WindowManager() | |
{ | |
display = XOpenDisplay(NULL); | |
if (display == NULL) | |
{ | |
printf("Cannot connect to X server!\n"); | |
return; | |
} | |
root = XDefaultRootWindow(display); | |
XGrabPointer(display, root, false, ButtonPressMask, | |
GrabModeAsync, GrabModeAsync, None, None, CurrentTime); | |
XSelectInput(display, root, ButtonPressMask); | |
// --- handle already created windows | |
Window returned_root, returned_parent; | |
Window* top_level_windows; | |
unsigned int num_top_level_windows; | |
CHECK(XQueryTree(display, root, &returned_root, &returned_parent, | |
&top_level_windows, &num_top_level_windows)); | |
CHECK_EQ(returned_root, root); | |
for(unsigned int i = 0; i < num_top_level_windows; ++i) | |
{ | |
Frame(top_level_windows[i]); | |
} | |
XFree(top_level_windows); | |
while(1) | |
{ | |
XNextEvent(display, &event); | |
switch(event.type) | |
{ | |
case ButtonPress: | |
switch(event.xbutton.button) | |
{ | |
case Button1: | |
mouseX = event.xbutton.x; | |
mouseY = event.xbutton.y; | |
mouseButton = Button1; | |
break; | |
case Button3: | |
mouseX = event.xbutton.x; | |
mouseY = event.xbutton.y; | |
mouseButton = Button3; | |
break; | |
default: | |
break; | |
} | |
printf("Mouse press"); | |
break; | |
case CreateNotify: | |
OnCreateNotify(event.xcreatewindow); | |
break; | |
case DestroyNotify: | |
OnDestroyNotify(event.xdestroywindow); | |
break; | |
default: | |
break; | |
} | |
} | |
} | |
WindowManager::~WindowManager() | |
{ | |
XCloseDisplay(display); | |
} | |
void WindowManager::Frame(Window window) | |
{ | |
const unsigned int BORDER_WIDTH = 3; | |
const unsigned int BORDER_COLOR = 0xFF4242; | |
const unsigned int BG_COLOR = 0xc0c0c0; | |
XWindowAttributes x_window_attrs; | |
CHECK(XGetWindowAttributes(display, window, &x_window_attrs)); | |
const Window window_border = XCreateSimpleWindow( | |
display, root, x_window_attrs.x, x_window_attrs.y, | |
x_window_attrs.width, x_window_attrs.height, BORDER_WIDTH, | |
BORDER_COLOR, BG_COLOR); | |
const Window window_titlebar = XCreateSimpleWindow( | |
display, window_border, 0, 0, x_window_attrs.width, 20, BORDER_WIDTH, | |
BORDER_COLOR, BG_COLOR); | |
XSelectInput(display, window_border, | |
SubstructureRedirectMask | SubstructureNotifyMask); | |
// XSelectInput(display, window_titlebar, | |
// ButtonPressMask); | |
XAddToSaveSet(display, window); | |
XReparentWindow(display, window, window_border, 0, 20); | |
XMapWindow(display, window_border); | |
XMapWindow(display, window_titlebar); | |
//TODO(noah): Remove this and replace with better mouse listener | |
XGrabPointer(display, window_titlebar, 1, | |
ButtonPressMask | ButtonReleaseMask | PointerMotionMask, | |
GrabModeAsync, GrabModeAsync, None, None, CurrentTime); | |
std::cout << "Framed window " << window << " [" << window_border | |
<< "]" << std::endl; | |
} | |
void WindowManager::Unframe(Window window) | |
{ | |
} | |
void WindowManager::OnCreateNotify(const XCreateWindowEvent& e) | |
{ | |
} | |
void WindowManager::OnDestroyNotify(const XDestroyWindowEvent& e) | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _WINDOWMANAGER_HPP_ | |
#define _WINDOWMANAGER_HPP_ | |
#include <X11/Xlib.h> | |
#include <string> | |
class WindowManager | |
{ | |
public: | |
WindowManager(); | |
~WindowManager(); | |
void Frame(Window window); | |
void Unframe(Window window); | |
void OnCreateNotify(const XCreateWindowEvent& e); | |
void OnDestroyNotify(const XDestroyWindowEvent& e); | |
private: | |
Display *display; | |
Window root; | |
int mouseX = -1; | |
int mouseY = -1; | |
int mouseButton; | |
XEvent event; | |
}; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment