Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colors #45

Merged
merged 10 commits into from
May 16, 2022
10 changes: 9 additions & 1 deletion ARGS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

Activate linux takes command line arguments for custom text and scaling.

There are 4 different options for command line arguments: a custom main message, scale percent, custom main and secondary message, or all 3 options.
There are 5 different options for command line arguments: a custom main message, scale percent, custom main and secondary message, all 3 options, or all 3 options and custom color.

Scaling is used to display the message correctly on screens of different resolutions. 100% is based on 1080p. It also affects the offset from the corner of the screen
so is not reccomended to be changed unless you are not using a 1080p screen.

Color is formatted in "r-g-b-a", with each number being a decimal from 0 to 1. Default is a light grey color.

### Custom Main Message

```
Expand All @@ -31,4 +33,10 @@ note that scale is a percent represented as a floating point number, 1 being 100

```
./bin/activate_linux (main) (secondary) (scale)
```

### All 3 options and custom color

```
./bin/activate_linux (main) (secondary) (scale) (color)
```
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ make
### Arch Linux
This project is in the AUR under [activate-linux-git](https://aur.archlinux.org/packages/activate-linux-git).

Install it using your favorite AUR helper.
Install/uninstall it using your favorite AUR helper.

### OpenSUSE (Open Build Service)
This project is in the OBS under [activate-linux](https://software.opensuse.org//download.html?project=home%3AWoMspace&package=activate-linux).

### Other
You can use `make install` to install to /usr/bin, and `make uninstall` to remove it

# MacOS (Horrific)
## Dependencies
Expand All @@ -53,9 +55,6 @@ make

> Note that the executable is located in `bin/`

### Installing
You can use `make install` to install to /usr/bin, and `make uninstall` to remove it

## Wayland
This works out of the box through XWayland, or alternatively you can use [this port of the project by Kljunas2](https://github.com/Kljunas2/activate-linux).

Expand Down
59 changes: 54 additions & 5 deletions src/activate_linux.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <X11/Xlib.h>
#include <X11/X.h>
Expand All @@ -10,10 +11,20 @@
#include <cairo.h>
#include <cairo-xlib.h>

//struct to hold rgb color
struct RGBAColor
{
//rgba color values from 0 to 1
float r;
float g;
float b;
float a;
};

// draw text
void draw(cairo_t *cr, char *title, char *subtitle, float scale) {
void draw(cairo_t *cr, char *title, char *subtitle, float scale, struct RGBAColor color) {
//set color
cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 0.35);
cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);

// set font size, and scale up or down
cairo_set_font_size(cr, 24 * scale);
Expand All @@ -25,6 +36,32 @@ void draw(cairo_t *cr, char *title, char *subtitle, float scale) {
cairo_show_text(cr, subtitle);
}

//fill RGBAColor struct values from a string formatted in "r-g-b-a" from 0.0 to 1.0
void RGBAColor_from_string(struct RGBAColor* color, char* text)
{
//split text into 4 parts along "-". If the input is not valid, use default setting
char* red = strtok(text, "-");
if (red != NULL)
{
color->r = atof(red);
}
char* green = strtok(NULL, "-");
if (green != NULL)
{
color->g = atof(green);
}
char* blue = strtok(NULL, "-");
if (green != NULL)
{
color->b = atof(blue);
}
char* alpha = strtok(NULL, "-");
if (alpha != NULL)
{
color->a = atof(alpha);
}
}

int main(int argc, char *argv[]) {
Display *d = XOpenDisplay(NULL);
Window root = DefaultRootWindow(d);
Expand All @@ -42,11 +79,15 @@ int main(int argc, char *argv[]) {
return 1;
}

//title and subtitle text
char *title, *subtitle;

int overlay_width = 340;
int overlay_height = 120;

//color of text - set default as light grey
struct RGBAColor text_color = {.r= 1.0, .g= 1.0, .b= 1.0, .a= 0.35};

// default scale
float scale = 1.0f;

Expand Down Expand Up @@ -95,9 +136,17 @@ int main(int argc, char *argv[]) {
scale = atof(argv[3]);
break;

//4 arguments
case (5):
title = argv[1];
subtitle = argv[2];
scale = atof(argv[3]);
RGBAColor_from_string(&text_color, argv[4]);
break;

// if there are more than 3 arguments, print usage
default:
printf("More than needed arguments have been passed. This program only supports at most 3 arguments.\n");
printf("More than needed arguments have been passed. This program only supports at most 4 arguments.\n");
return 1;
}

Expand Down Expand Up @@ -163,7 +212,7 @@ int main(int argc, char *argv[]) {
// cairo context
surface[i] = cairo_xlib_surface_create(d, overlay[i], vinfo.visual, overlay_width, overlay_height);
cairo_ctx[i] = cairo_create(surface[i]);
draw(cairo_ctx[i], title, subtitle, scale);
draw(cairo_ctx[i], title, subtitle, scale, text_color);
}

// wait for X events forever
Expand All @@ -182,4 +231,4 @@ int main(int argc, char *argv[]) {
XFree(si);
XCloseDisplay(d);
return 0;
}
}