Skip to content

Commit

Permalink
Merge pull request #3 from JJoeDev/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
JJoeDev authored Aug 7, 2024
2 parents 1a4ef1d + 4af9b14 commit d119523
Show file tree
Hide file tree
Showing 12 changed files with 936 additions and 160 deletions.
11 changes: 10 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,13 @@ find_package(Lua REQUIRED)
include_directories(${LUA_INCLUDE_DIRS})

add_executable(${PROJECT_NAME} ${SRC})
target_link_libraries(${PROJECT_NAME} xcb xcb-util xcb-icccm xcb-randr ${LUA_LIBRARIES})
target_link_libraries(${PROJECT_NAME}
xcb
xcb-util
xcb-icccm
xcb-randr
xcb-image
xcb-shm
xcb-shape
${LUA_LIBRARIES}
)
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

148 changes: 53 additions & 95 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,131 +1,89 @@
# MiBar

![Header](./github_assets/github-header-image.png)

(image generated with [github-profile-header-generator](https://github.com/leviarista/github-profile-header-generator?tab=readme-ov-file))

> [!WARNING]
>
> MiBar is under heavy development and should not be used yet.
> MiBar is at a usable
## Table of content
[Intro](#intro)
![Header_image](./github_assets/github-header-image.png)
(image generated with [github-profile-header-generator](https://github.com/leviarista/github-profile-header-generator?tab=readme-ov-file))

[Current state](#current-state)
MiBar is a simple Linux X11 status bar with Lua support

[Current and future features](#features--planned)
## Description

[How to build, run & configure](#building-and-running)
MiBar is a simple Linux X11 status bar built with the C++ programming language. The application uses 2 external libraries, XCB to interact with the X server, and sol2 to run user made lua scripts.

- [Building](#building)
MiBar is supposed to be customizable and easy to configure. This is the reason for using the Lua language for plugins as it makes it easy to develop for MiBar.

- [Configuring](#configuration)
## Getting Started

- [Custom modules](#creating-custom-modules)
Currently the only way to use MiBar is by building from source.

[Dependencies](#dependencies)
### Dependencies

[Images](#images)
* git
* cmake
* make
* All xcb libraries
* sol2

[Getting involved](#getting-involved)
### Installing

## Intro
MiBar is a C++17 app designed to be a user-friendly and configurable status bar for the X11 window system on linux. The goal is to create a minimal and light weight status bar with easy customization and extendability so users can create their own bar components.
MiBar can only be run by building from source until a future release.

## Current state
MiBar is in the early stages of development so components like configuration are not 100% in place and will have to take place in a C header file for now, this has the consequence of requireing the application to be recompiled every time the configuration file is modifed.
But until then this is how you can use MiBar

Custom components are also not implemented yet and neither is components like: time, volume, and so on.
```
$ git clone https://github.com/JJoeDev/MiBar.git
$ cd MiBar
$ cmake -DCMAKE_BUILD_TYPE=Release .
$ make
```

## Features (+ planned)
### Executing program

*More features might be added in the future*
Once MiBar has bin built from source the executable can be found in the bin directory
```
$ ./bin/MiBar
```

* (Basic Implementation) **Modules:** MiBar would like for users to be able to create their own modules for the bar to allow for even more customizability
* (planned) **Configuration:** At some point in the future MiBar should move away from the C header file to a toml or json approach for configuration
* (planned) **Fonts** Currently MiBar only supports X11 fonts (use xlsfonts to see all fonts) in the future we should switch to cairo
## Configuring

## Building and running
The current version includes a config file in the form of a C header file located in ``miBar/src/general.config.h``. Each time this config file is modified a recompilation is required.
Currently the bar uses a C header file for its configuration, when changing this file you will need to recompile the application again.

### Building
The configuration file can be found in [src/general.config.h](./src/general.config.h)

Before you can build you need to make sure libxcb is installes and sol2 should also be installed
## Images

*On Arch linux*
![Image_1](./github_assets/DemoBar.png)

```
$ sudo pacman -S libxcb
$ yay -S sol2
```
![Image_2](./github_assets/DemoBar2.png)

*Building*
![Image_3](./github_assets/DemoBarWLua.png)

```
$ cd /Path/To/MiBar/CMakeLists.txt
$ cmake . -DCMAKE_BUILD_TYPE=Release
$ cmake --build .
$ ./bin/MiBar
```
## Plugin Development

### Configuration
The current method to configure the bar is to take a look in [general.config.h](./src/general.config.h) for configuration directly to the bar

To get modules on the bar such as text or a clock you will have to find a lua script for MiBar and move it to ``$HOME/.config/MiBar/plugins``

[general.config.h](./src/general.config.h)
```C
#ifndef MIBAR_GENERAL_CONFIG_H
#define MIBAR_GENERAL_CONFIG_H

// Colors to use
#define BACKGROUND 0x0A3632
#define FOREGROUND 0xC6EFEB
#define COLOR1 0x115852
#define COLOR2 0x237E76
#define COLOR3 0x3CA99F

// Monitor for MiBar to find
#define TARGET_MONITOR "HDMI-0"

// Bar configuration
#define BAR_WIDTH 0
#define BAR_HEIGHT 40
#define BAR_X 0
#define BAR_Y 0

// Underline configuration
#define ENABLE_UNDERLINE 1
#define UNDERLINE_HEIGHT 3
#define UNDERLINE_X_OFFSET -1
#define UNDERLINE_Y_OFFSET -10

// Currently only PADDING_TOP & PADDING_LEFT is in use
#define PADDING_TOP 10
#define PADDING_RIGHT 0
#define PADDING_BOTTOM 0
#define PADDING_LEFT 5

#endif
```
MiBar allowes for users to create their own bar by creating plugins that do what they want them to do. Lua is a simple and easy scripting language with tons of documentation online.

## Creating custom modules
At this point in time the bar only supports drawing text to the bar, all in the same spot. MiBar has exposed a function to lua called ``Draw()`` that only accepts a string as its argument.
### Creating a Plugin

```cpp
void Draw(const std::string& str);
```
* **Director:** Plugins should be located in ```~/.config/MiBar/plugins```
* **Available Functions** MiBar currently only exposes one function to the plugins, Here is an example of how to use it
* * ```DrawString(text, alignment, x_position)```: This function draws a string to the status bar
* * - ```text```: This is the string that will get displayed on the status bar
* * - ```alignment```: This is an alignment option (``LEFT``, ``CENTER``, or ``RIGHT``)
* * - ```x_position```: This is an added position on top of the alignment option

## Dependencies
[xcb](https://xcb.freedesktop.org/) is used to interact with the X11 server
**Time.lua**: Time.lua is a simple plugin that simply displays the current time on the center of the bar

[sol2](https://github.com/ThePhD/sol2) Is used to interact with lua
```lua
local time = os.date("%a %d / %H:%M")

## Images
![DemoImage](./github_assets/DemoBar.png)
DrawString(time, Alignment.CENTER, 0)
```

![DemoImage2](./github_assets/DemoBar2.png)
For a visual example of this script take a look at the third [image](#images)

## Getting involved
## License

Currently, the project is not actively seeking contributions. However, if you have any suggestions or find potential improvements, feel free to reach out. Well-explained pull requests with clear descriptions and code comments will be considered for future versions.
This project is licensed under the GNU General Public License v3.0 License - see the [LICENSE](./LICENSE) file for details
Binary file added github_assets/DemoBarWLua.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 43 additions & 14 deletions src/bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
#include "randr.h"
#include "pluginManager.h"

mibar::mibar(){
m_logger.Log(__FILE_NAME__, __LINE__, "DEBUG BUILD", LogLvl::DBUG);
#if AUTOMATIC_UPDATES_ENABLE
#include <thread>
#endif

mibar::mibar(){
m_conn = xcb_connect(nullptr, nullptr);
if(xcb_connection_has_error(m_conn)){
m_logger.Log(__FILE_NAME__, __LINE__, "Could not connect to X server!", LogLvl::ERROR);
Expand Down Expand Up @@ -61,23 +63,50 @@ mibar::~mibar(){

}

void mibar::EventLoop(){
void mibar::EventLoop() {
Renderer r(m_screen, m_conn, m_window);

PluginManager pmgr;
pmgr.ExposeFuncToLua("Draw", [&r](const std::string& str, const int x){r.DrawStr(str.c_str(), str.length(), x);});
pmgr.ExposeFuncToLua("DrawString", [&r](const std::string& str, ALIGNMENT align, const int x){r.DrawStr(str, align, x);});
pmgr.ExposeFuncToLua("DrawRect", [&r](int x, int y, int w, int h, int idx){r.DrawRect(x, y, w, h, idx);});

#if AUTOMATIC_UPDATES_ENABLE
using namespace std::chrono;

auto last_update = steady_clock::now();
const auto update_interval = seconds(UPDATE_TIME);
#endif

xcb_generic_event_t* e;
while((e = xcb_wait_for_event(m_conn))){
switch(e->response_type & 0x7F){
case XCB_EXPOSE:
r.Clear(0, 0, m_w, m_h);
pmgr.RunScripts();

xcb_flush(m_conn);
break;
while (true) {
if ((e = xcb_poll_for_event(m_conn))) {
if (e) {
switch (e->response_type & 0x7F) {
case XCB_EXPOSE:
r.Clear(0, 0, m_w, m_h);
pmgr.RunScripts();
xcb_flush(m_conn);
break;
default:
// Handle other events if needed
break;
}
free(e);
}
}

free(e);
#if AUTOMATIC_UPDATES_ENABLE
else {
auto now = steady_clock::now();
if (now - last_update >= update_interval) {
// Force update
r.Clear(0, 0, m_w, m_h);
pmgr.RunScripts();
xcb_flush(m_conn);
last_update = now;
}
// Yield CPU time to other threads
std::this_thread::yield();
}
#endif
}
}
4 changes: 2 additions & 2 deletions src/bar.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ class mibar{
private:
// Basic connections
xcb_connection_t* m_conn = nullptr;
xcb_screen_t* m_screen;
xcb_screen_t* m_screen = nullptr;
xcb_window_t m_window = 0;

int m_x, m_y, m_w, m_h;

uint32_t m_winMask;
uint32_t m_winMask = 0;
uint32_t m_winValues[2];

Logger m_logger;
Expand Down
44 changes: 27 additions & 17 deletions src/general.config.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,41 @@
#define MIBAR_GENERAL_CONFIG_H

// Colors to use
#define BACKGROUND 0x0A3632
#define FOREGROUND 0xC6EFEB
#define COLOR1 0x115852
#define COLOR2 0x237E76
#define COLOR3 0x3CA99F
#define BACKGROUND 0x111111
#define FOREGROUND 0x999999
#define COLOR1 0x777777
#define COLOR2 0x555555
#define COLOR3 0x333333

// Monitor for MiBar to find
#define TARGET_MONITOR "HDMI-0"

// Target font to use (To find available fonts use the xlsfonts command in a terminal)
#define FONT "-bitstream-charter-medium-i-normal--0-0-100-100-p-0-iso8859-1"
//#define FONT "lucidasans-10"
#define FONT_FALLBACK "fixed"

// Bar configuration
#define BAR_WIDTH 0
#define BAR_HEIGHT 40
#define BAR_X 0
#define BAR_Y 0
#define BAR_WIDTH -100
#define BAR_HEIGHT 32
#define BAR_X 50
#define BAR_Y 3

// If Automatic Updates is enabled the bar will call all lua plugins every set amount of time + when an event is recieved
#define AUTOMATIC_UPDATES_ENABLE 0
// The amount of time between updates in seconds
#define UPDATE_TIME 30

// Underline configuration
#define ENABLE_UNDERLINE 1
#define UNDERLINE_HEIGHT 3
#define UNDERLINE_X_OFFSET -1
#define UNDERLINE_Y_OFFSET -10
#define UNDERLINE_X_OFFSET 0
#define UNDERLINE_Y_OFFSET 0

// Currently only PADDING_TOP & PADDING_LEFT is in use
#define PADDING_TOP 10
#define PADDING_RIGHT 0
#define PADDING_BOTTOM 0
#define PADDING_LEFT 5
// Currently padding has not been implemented to the bar as plugins define their own positions
//#define PADDING_TOP 10
//#define PADDING_RIGHT 0
//#define PADDING_BOTTOM 0
//#define PADDING_LEFT 5

#endif
#endif
20 changes: 16 additions & 4 deletions src/pluginManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,35 @@ PluginManager::PluginManager(){
m_logger.Log("", 0, "Plugin directory found at: " + m_pluginDir);
}

lua.open_libraries(sol::lib::base, sol::lib::math, sol::lib::os, sol::lib::string);
lua.open_libraries(sol::lib::base, sol::lib::math, sol::lib::os, sol::lib::string, sol::lib::io);

for(auto&& entry : std::filesystem::directory_iterator(m_pluginDir)){
if(entry.path().extension() == ".lua"){
m_plugins.emplace_back(entry.path());
m_logger.Log(__FILE_NAME__, 0, "Found plugin: " + entry.path().filename().string());
}
}

lua["Alignment"] = lua.create_table_with(
"LEFT", ALIGNMENT::LEFT,
"CENTER", ALIGNMENT::CENTER,
"RIGHT", ALIGNMENT::RIGHT
);
}

PluginManager::~PluginManager(){

}

void PluginManager::ExposeFuncToLua(const std::string& funcName, std::function<void(const std::string&, const int x)> func){
lua[funcName] = [func](const std::string& arg, const int x){
func(arg, x);
void PluginManager::ExposeFuncToLua(const std::string& funcName, std::function<void(const std::string&, ALIGNMENT, const int)> func){
lua[funcName] = [func](const std::string& arg, ALIGNMENT align, const int x){
func(arg, align, x);
};
}

void PluginManager::ExposeFuncToLua(const std::string& funcName, std::function<void(int x, int y, int w, int h, int idx)> func){
lua[funcName] = [func](int x, int y, int w, int h, int idx){
func(x, y, w, h, idx);
};
}

Expand Down
Loading

0 comments on commit d119523

Please sign in to comment.