Skip to content

Commit

Permalink
conditionally link glog.
Browse files Browse the repository at this point in the history
Previously, glog is a required to compile and link Wmderland.

Now CMake can detect if glog is installed in your system and decide
whether to link glog or not.
  • Loading branch information
aesophor committed Feb 11, 2019
1 parent eb3c640 commit 83690cb
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 35 deletions.
30 changes: 22 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,41 @@ project(Wmderland)
# Request C++11 standard, using new CMake variables.
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto -Wall")

# There are two CMake scripts in ./cmake
# 1. BuildType.cmake - determine build type on demand
# 2. Findglog.cmake - used by `find_package(glog REQUIRED)`
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")

# Load and run CMake code from a file or module.
# Determine BuildType on demand.
include(BuildType)

# Add some extra flags to g++
# -flto: runs the standard link-time optimizer
# -Wall: "warn all" turns on almost all warning
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto -Wall")

# Find the required libraries.
find_package(X11 REQUIRED)
find_package(glog REQUIRED)

# Automatic macro construction.
set(VERSION_MAJOR 0)

This comment has been minimized.

Copy link
@elfring

elfring Feb 11, 2019

I guess that you do need to define these two variables on your own when you can reuse others which would be automatically constructed by a command like “project.

set(VERSION_MINOR 9)

# CMake will generate config.hpp from config.hpp.in
# and store it under src/
configure_file(
"${PROJECT_SOURCE_DIR}/src/config.hpp.in"
"${PROJECT_SOURCE_DIR}/src/config.hpp"

This comment has been minimized.

Copy link
@elfring

elfring Feb 11, 2019

I find it more appropriate to store such a generated source file in the software build directory.
Please omit the variable for the specification of relative paths here.

)


# Grab all files end in .cpp
FILE(GLOB cpp_sources src/*.cpp)
add_executable(Wmderland ${cpp_sources})
target_link_libraries(Wmderland X11 glog)

set(LIBRARIES "X11")
if(GLOG_FOUND)
set(LIBRARIES "${LIBRARIES} glog")
endif()
target_link_libraries(Wmderland X11 ${LIBRARIES})

# Install rule
install (TARGETS Wmderland DESTINATION bin)
install(TARGETS Wmderland DESTINATION bin)
9 changes: 4 additions & 5 deletions cmake/Findglog.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ endif()
find_package_handle_standard_args(Glog DEFAULT_MSG GLOG_INCLUDE_DIR GLOG_LIBRARY)

if(GLOG_FOUND)
set(GLOG_INCLUDE_DIRS ${GLOG_INCLUDE_DIR})
set(GLOG_LIBRARIES ${GLOG_LIBRARY})
message(STATUS "Found glog (include: ${GLOG_INCLUDE_DIR}, library: ${GLOG_LIBRARY})")
mark_as_advanced(GLOG_ROOT_DIR GLOG_LIBRARY_RELEASE GLOG_LIBRARY_DEBUG
GLOG_LIBRARY GLOG_INCLUDE_DIR)
set(GLOG_INCLUDE_DIRS ${GLOG_INCLUDE_DIR})
set(GLOG_LIBRARIES ${GLOG_LIBRARY})
message(STATUS "Found glog (include: ${GLOG_INCLUDE_DIR}, library: ${GLOG_LIBRARY})")
mark_as_advanced(GLOG_ROOT_DIR GLOG_LIBRARY_RELEASE GLOG_LIBRARY_DEBUG GLOG_LIBRARY GLOG_INCLUDE_DIR)
endif()
8 changes: 6 additions & 2 deletions src/config.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "action.hpp"
#include "config.hpp"
#include "util.hpp"
#include <glog/logging.h>
#include <fstream>
#include <sstream>
#if GLOG_FOUND != FALSE

This comment has been minimized.

Copy link
@elfring

elfring Feb 11, 2019

Would the specification “#ifdef USE_GLOG” be nicer?

#include <glog/logging.h>
#endif

using std::hex;
using std::pair;
Expand Down Expand Up @@ -71,7 +73,9 @@ Config::Config(Display* dpy, Properties* prop, string filename) : dpy_(dpy), pro
autostart_rules_.push_back(cmd);
break; }
default: {
LOG(INFO) << "Ignored unrecognized symbol in config: " << tokens[0];
#if GLOG_FOUND != FALSE
LOG(INFO) << "Ignored unrecognized symbol in config: " << tokens[0];
#endif
break;
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
#define VARIABLE_PREFIX "$"
#define DEFAULT_EXIT_KEY "Mod4+Shift+Escape"

// The following macro will be generated by CMake which determines if Glog
// is installed on the machine used to compile Wmderland.
#define GLOG_FOUND TRUE

This comment has been minimized.

Copy link
@elfring

elfring Feb 11, 2019

How do you think about to use variables which would be constructed by a CMake command like “find_package(glog)” only on demand?


enum class ConfigKeyword {
SET,
ASSIGN,
Expand Down
95 changes: 95 additions & 0 deletions src/config.hpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#ifndef WMDERLAND_CONFIG_HPP_
#define WMDERLAND_CONFIG_HPP_

#include "action.hpp"
#include "util.hpp"
#include <unordered_map>
#include <vector>
#include <string>

#define WIN_MGR_NAME "Wmderland"
#define VERSION "0.9.1 Beta"

This comment has been minimized.

Copy link
@elfring
#define CONFIG_FILE "~/.config/Wmderland/config"
#define COOKIE_FILE "~/.local/share/Wmderland/cookie"

#define WORKSPACE_UNSPECIFIED -1
#define WORKSPACE_COUNT 9

#define MIN_WINDOW_WIDTH 50
#define MIN_WINDOW_HEIGHT 50
#define DEFAULT_FLOATING_WINDOW_WIDTH 800
#define DEFAULT_FLOATING_WINDOW_HEIGHT 600

#define DEFAULT_GAP_WIDTH 15
#define DEFAULT_BORDER_WIDTH 3
#define DEFAULT_FOCUSED_COLOR 0xffffffff
#define DEFAULT_UNFOCUSED_COLOR 0xff41485f

#define VARIABLE_PREFIX "$"
#define DEFAULT_EXIT_KEY "Mod4+Shift+Escape"

// The following macro will be generated by CMake which determines if Glog
// is installed on the machine used to compile Wmderland.
#define GLOG_FOUND @GLOG_FOUND@

enum class ConfigKeyword {
SET,
ASSIGN,
FLOATING,
PROHIBIT,
BINDSYM,
EXEC,
UNDEFINED
};

class Config {
public:
Config(Display* dpy, Properties* prop, std::string filename);
virtual ~Config();

int GetSpawnWorkspaceId(Window w) const;
bool ShouldFloat(Window w) const;
bool ShouldProhibit(Window w) const;
const std::vector<Action>& GetKeybindActions(const std::string& modifier, const std::string& key) const;
void SetKeybindActions(const std::string& modifier_and_key, const std::string& actions);

unsigned short gap_width() const;
unsigned short border_width() const;
unsigned short min_window_width() const;
unsigned short min_window_height() const;
unsigned long focused_color() const;
unsigned long unfocused_color() const;

const std::unordered_map<std::string, short>& spawn_rules() const;
const std::unordered_map<std::string, bool>& float_rules() const;
const std::unordered_map<std::string, bool>& prohibit_rules() const;
const std::unordered_map<std::string, std::vector<Action>>& keybind_rules() const;
const std::vector<std::string>& autostart_rules() const;

private:
static ConfigKeyword StrToConfigKeyword(const std::string& s);
static std::string ExtractWindowIdentifier(const std::string& s);
std::vector<std::string> GeneratePossibleConfigKeys(Window w) const;
const std::string& ReplaceSymbols(std::string& s);
std::unordered_map<std::string, std::string> symtab_;

// Global variables
unsigned short gap_width_;
unsigned short border_width_;
unsigned short min_window_width_;
unsigned short min_window_height_;
unsigned long focused_color_;
unsigned long unfocused_color_;

// Rules
std::unordered_map<std::string, short> spawn_rules_; // spawn certain apps in certain workspaces.
std::unordered_map<std::string, bool> float_rules_; // start certain apps in floating mode.
std::unordered_map<std::string, bool> prohibit_rules_; // apps that should be prohibit from starting.
std::unordered_map<std::string, std::vector<Action>> keybind_rules_; // keybind actions.
std::vector<std::string> autostart_rules_; // launch certain apps when wm starts.

Display* dpy_;
Properties* prop_;
};

#endif
19 changes: 14 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#include "wm.hpp"
#include "config.hpp"
#include <glog/logging.h>
#include <iostream>
#include <memory>
#include <string>
#include <cstring>
#if GLOG_FOUND != FALSE
#include <glog/logging.h>
#endif

using std::string;
using std::unique_ptr;
Expand All @@ -22,20 +25,26 @@ int main(int argc, char* args[]) {
}

try {
// Initialize google's c++ logging library.
google::InitGoogleLogging(args[0]);
#if GLOG_FOUND != FALSE
// Initialize google's c++ logging library.
google::InitGoogleLogging(args[0]);
#endif

// WindowManager is a singleton class. If XOpenDisplay() fails during
// WindowManager::GetInstance(), it will return None (in xlib, None is
// the universal null resource ID or atom.)
unique_ptr<WindowManager> wm = WindowManager::GetInstance();
if (!wm) {
LOG(INFO) << "Failed to open display to X server.";
#if GLOG_FOUND != FALSE
LOG(INFO) << "Failed to open display to X server.";
#endif
return EXIT_FAILURE;
}
wm->Run();
} catch (const std::exception& ex) {
LOG(ERROR) << ex.what();
#if GLOG_FOUND != FALSE
LOG(ERROR) << ex.what();
#endif
return EXIT_FAILURE;
}

Expand Down
35 changes: 20 additions & 15 deletions src/wm.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#include "wm.hpp"
#include "client.hpp"
#include "util.hpp"
#include <memory>
#include <string>
#include <sstream>
#include <cstring>
#include <algorithm>
extern "C" {
#include <X11/cursorfont.h>
#include <X11/Xproto.h>
#include <X11/Xatom.h>
}
#if GLOG_FOUND != FALSE
#include <glog/logging.h>
#include <memory>
#include <string>
#include <sstream>
#include <algorithm>
#endif

using std::hex;
using std::find;
Expand Down Expand Up @@ -407,16 +410,18 @@ void WindowManager::OnClientMessage(const XClientMessageEvent& e) {
}

int WindowManager::OnXError(Display* dpy, XErrorEvent* e) {
const int MAX_ERROR_TEXT_LENGTH = 1024;
char error_text[MAX_ERROR_TEXT_LENGTH];
XGetErrorText(dpy, e->error_code, error_text, sizeof(error_text));
LOG(ERROR) << "Received X error:\n"
<< " Request: " << int(e->request_code)
<< " Error code: " << int(e->error_code)
<< " - " << error_text << "\n"
<< " Resource ID: " << e->resourceid;
// The return value is ignored.
return 0;
#if GLOG_FOUND != FALSE
const int MAX_ERROR_TEXT_LENGTH = 1024;
char error_text[MAX_ERROR_TEXT_LENGTH];
XGetErrorText(dpy, e->error_code, error_text, sizeof(error_text));
LOG(ERROR) << "Received X error:\n"
<< " Request: " << int(e->request_code)
<< " Error code: " << int(e->error_code)
<< " - " << error_text << "\n"
<< " Resource ID: " << e->resourceid;
// The return value is ignored.
return 0;
#endif
}


Expand Down Expand Up @@ -554,7 +559,7 @@ void WindowManager::KillClient(Window w) {
msg.xclient.window = w;
msg.xclient.format = 32;
msg.xclient.data.l[0] = prop_->wm[atom::WM_DELETE];
CHECK(XSendEvent(dpy_, w, false, 0, &msg));
XSendEvent(dpy_, w, false, 0, &msg);
} else {
XKillClient(dpy_, w);
}
Expand Down

0 comments on commit 83690cb

Please sign in to comment.