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

Better multi-monitor support #41

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PROGRAM = xob
MANPAGE = doc/xob.1
SYSCONF = styles.cfg
LIBS = x11 libconfig
LIBS = x11 libconfig xrandr
SOURCES = src/conf.c src/display.c src/main.c

# Feature: alpha channel (transparency)
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ When starting, xob looks for the configuration file in the following order:
Consult the man page for detailed information about the configuration file and the available options. The following `styles.cfg` defines a single style called "default" that showcases all the possible options set to the default values. The configuration file may contain additional styles to choose among using the **-s** argument.

default = {
monitor = "auto";
x = {relative = 1; offset = -48;};
y = {relative = 0.5; offset = 0;};
length = {relative = 0.3; offset = 0;};
Expand Down Expand Up @@ -315,9 +316,10 @@ There is no support for panel integration. You can however use absolute position

> "How about multiple monitors?"

xob works well under multihead setups. The `x`, `y`, and `length` style options refer to the combined screen surface. By default the bar is vertical near the right edge of the rightmost monitor. In vertical layouts, you may prefer to switch the bar to horizontal mode as follows to avoid splits.
xob works well under multihead setups. The `monitor`, `x`, `y`, and `length` style options refer to the combined screen surface. By default the bar is vertical near the right edge of the rightmost monitor. In vertical layouts, you may prefer to switch the bar to horizontal mode as follows to avoid splits. By default options `x` and `y` refer to the combined screen surface, but you can specify a monitor for the bar, for example you can set option `monitor = "HDMI-1"` to show the bar only on `HDMI-1` (use `xrandr` command to get monitors names).
juster-0 marked this conversation as resolved.
Show resolved Hide resolved

horizontal = {
monitor = "auto";
x = {relative = 0.5; offset = 0;};
y = {relative = 1; offset = -48;};
orientation = "horizontal";
Expand Down
27 changes: 16 additions & 11 deletions doc/xob.1
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ In the following, a dot \[lq].\[rq] means \[lq]suboption\[rq].
For instance \[lq]color.normal.fg\[rq] means \[lq]The suboption fg of
the suboption normal of option color\[rq].
.TP
\f[B]monitor\f[R] \f[I]\[lq]output_name\[rq]\f[R] (default: auto)
Output monitor for the bar, use \f[I]xrandr\f[R] command to get monitors names.
The option is case-sensitive.
.TP
\f[B]orientation\f[R] \f[I]\[lq]horizontal\[rq] | \[lq]vertical\[rq]\f[R] (default: vertical)
Orientation of the bar which either fills up from left to right
(\[lq]horizontal\[rq]) or bottom to top (\[lq]vertical\[rq]).
Expand Down Expand Up @@ -301,6 +305,7 @@ backlight = {
.nf
\f[C]
default = {
monitor = \[dq]auto\[dq];
x = {relative = 1; offset = -48;};
y = {relative = 0.5; offset = 0;};
length = {relative = 0.3; offset = 0;};
Expand Down Expand Up @@ -367,17 +372,17 @@ situations.
\[lq]How to set up xob with multiple monitors?\[rq]
.RE
.PP
xob works well under multihead setups but there is no easy way to
configure the position of the bar for now.
For example, in a dual monitor setup with the default configuration, the
horizontal centering is not local to one of the two monitors.
It is global.
The bar might be split in two: one part on each screen.
Stick to a corner or use absolute positioning.
If you want an xob instance to be centered (horizontally) on the
far-right monitor, set \f[I]x.relative\f[R] to 1.0 (anchored on the far
right) and the \f[I]x.offset\f[R] to minus half the width of that
screen.
xob works well under multihead setups, use option \f[I]monitor\f[R] to
specify one. By default xob use \f[I]auto\f[R] for the option. It means
that in a dual monitor setup with the default configuration,
the horizontal centering is not local to one of the two monitors.
It is global. The bar might be split in two: one part on each screen.
If you want an xob instance to be centered (horizontally) on the specific
monitor, set \f[I]monitor\f[R] option to your monitor output,
for example \f[I]monitor = \[lq]HDMI-1\[rq]\f[R] and set options
\f[I]x.relative\f[R] and \f[I]y.relative\f[R] relative to the monitor.
To get monitors output name you can use \f[I]xrandr --listmonitors\f[R]
command in your terminal.
.SH CONTRIBUTIONS
.PP
Feedback and contributions are welcome.
Expand Down
6 changes: 5 additions & 1 deletion doc/xob.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ Options can be grouped together inside curly brackets. Some options expect a gro

In the following, a dot "." means "suboption". For instance "color.normal.fg" means "The suboption fg of the suboption normal of option color".

**monitor** "output_name" (default: auto)
: Output monitor for the bar, use `xrandr` command to get monitors names. The option is case-sensitive.

**orientation** *"horizontal" | "vertical"* (default: vertical)
: Orientation of the bar which either fills up from left to right ("horizontal") or bottom to top ("vertical").

Expand Down Expand Up @@ -194,6 +197,7 @@ This example configuration file provides two styles "volume" and "backlight". In
## DEFAULT CONFIGURATION FILE

default = {
monitor = "auto";
x = {relative = 1; offset = -48;};
y = {relative = 0.5; offset = 0;};
length = {relative = 0.3; offset = 0;};
Expand Down Expand Up @@ -242,7 +246,7 @@ There is no support for panel integration. You can however use absolute position

> "How to set up xob with multiple monitors?"

xob works well under multihead setups but there is no easy way to configure the position of the bar for now. For example, in a dual monitor setup with the default configuration, the horizontal centering is not local to one of the two monitors. It is global. The bar might be split in two: one part on each screen. Stick to a corner or use absolute positioning. If you want an xob instance to be centered (horizontally) on the far-right monitor, set *x.relative* to 1.0 (anchored on the far right) and the *x.offset* to minus half the width of that screen.
xob works well under multihead setups, use option `monitor` to specify one. By default xob use `auto` for the option. It means that in a dual monitor setup with the default configuration, the horizontal centering is not local to one of the two monitors. It is global. The bar might be split in two: one part on each screen. If you want an xob instance to be centered (horizontally) on the specific monitor, set `monitor` option to your monitor output, for example `monitor = "HDMI-1"` and set options `x.relative` and `y.relative` relative to the monitor. To get monitors output name you can use `xrandr --listmonitors` command in your terminal.

# CONTRIBUTIONS

Expand Down
18 changes: 18 additions & 0 deletions src/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,22 @@ static int config_setting_lookup_orientation(const config_setting_t *setting,
return success_status;
}

static int config_setting_lookup_monitor(const config_setting_t *setting,
const char *name, char *monitorvalue)
{
const char *stringvalue;

if (config_setting_lookup_string(setting, name, &stringvalue))
{
strncpy(monitorvalue, stringvalue, LNAME_MONITOR);
}
else
{
fprintf(stderr, "Error: No style %s.\n", name);
}
return CONFIG_TRUE;
}

Style parse_style_config(FILE *file, const char *stylename, Style default_style)
{
config_t config;
Expand All @@ -243,6 +259,8 @@ Style parse_style_config(FILE *file, const char *stylename, Style default_style)
xob_config = config_lookup(&config, stylename);
if (xob_config != NULL)
{
config_setting_lookup_monitor(xob_config, "monitor",
style.monitor);
config_setting_lookup_int(xob_config, "thickness",
&style.thickness);
config_setting_lookup_int(xob_config, "border", &style.border);
Expand Down
5 changes: 5 additions & 0 deletions src/conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

#include <stdio.h>

#define MONITOR_AUTO "auto"
#define LNAME_MONITOR 12

typedef struct
{
unsigned char red;
Expand Down Expand Up @@ -63,6 +66,7 @@ typedef enum

typedef struct
{
char monitor[LNAME_MONITOR];
Dim x;
Dim y;
Dim length;
Expand All @@ -77,6 +81,7 @@ typedef struct

/* clang-format off */
#define DEFAULT_CONFIGURATION (Style) {\
.monitor = MONITOR_AUTO,\
.x =\
{\
.rel = 1.0,\
Expand Down
63 changes: 55 additions & 8 deletions src/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/Xrandr.h>
#include <stdlib.h>
#include <string.h>

Expand Down Expand Up @@ -107,26 +108,26 @@ void compute_geometry(Style conf, Display_context *dc, int *topleft_x,

/* Orientation-related dimensions */
*available_length = dc->geometry.orientation == HORIZONTAL
? WidthOfScreen(dc->x.screen)
: HeightOfScreen(dc->x.screen);
? dc->x.monitor_info.width
: dc->x.monitor_info.height;

dc->geometry.length =
fit_in(*available_length * conf.length.rel + conf.length.abs, 0,
*available_length - 2 * *fat_layer);

/* Compute position of the top-left corner */
*topleft_x = fit_in(WidthOfScreen(dc->x.screen) * conf.x.rel -
*topleft_x = fit_in(dc->x.monitor_info.width * conf.x.rel -
(size_x(dc->geometry) + 2 * *fat_layer) / 2,
0,
WidthOfScreen(dc->x.screen) -
dc->x.monitor_info.width -
(size_x(dc->geometry) + 2 * *fat_layer)) +
conf.x.abs;
*topleft_y = fit_in(HeightOfScreen(dc->x.screen) * conf.y.rel -
conf.x.abs + dc->x.monitor_info.x;
*topleft_y = fit_in(dc->x.monitor_info.height * conf.y.rel -
(size_y(dc->geometry) + 2 * *fat_layer) / 2,
0,
HeightOfScreen(dc->x.screen) -
dc->x.monitor_info.width -
(size_y(dc->geometry) + 2 * *fat_layer)) +
conf.y.abs;
conf.y.abs + dc->x.monitor_info.y;
}

/* PUBLIC Returns a new display context from a given configuration. If the
Expand Down Expand Up @@ -159,6 +160,52 @@ Display_context init(Style conf)
window_attributes.border_pixel = 0;
window_attributes.override_redirect = True;

if (strcmp(conf.monitor, MONITOR_AUTO) != 0)
{
/* Get monitors info */
int num_monitors;
char *monitor_name;
XRRMonitorInfo *monitor_sizes = XRRGetMonitors(
dc.x.display, root, 0, &num_monitors);
int i;
for (i = 0; i < num_monitors; i++)
{
monitor_name = XGetAtomName(dc.x.display,
monitor_sizes[i].name);
if (strcmp(conf.monitor, monitor_name) == 0)
break;
}
if (i == num_monitors) // Monitor name is not found
{
/* Use auto for monitor option if no monitors with
* provided name found*/
fprintf(stderr, "Monitor %s is not found. Use FULL mode\n",
monitor_name);
dc.x.monitor_info.x = 0;
dc.x.monitor_info.y = 0;
dc.x.monitor_info.width = WidthOfScreen(dc.x.screen);
dc.x.monitor_info.height = HeightOfScreen(dc.x.screen);
strcpy(dc.x.monitor_info.name, MONITOR_AUTO);
}
else
{
dc.x.monitor_info.x = monitor_sizes[i].x;
dc.x.monitor_info.y = monitor_sizes[i].y;
dc.x.monitor_info.width = monitor_sizes[i].width;
dc.x.monitor_info.height = monitor_sizes[i].height;
strcpy(dc.x.monitor_info.name, monitor_name);
}
XRRFreeMonitors(monitor_sizes);
}
else
{
dc.x.monitor_info.x = 0;
dc.x.monitor_info.y = 0;
dc.x.monitor_info.width = WidthOfScreen(dc.x.screen);
dc.x.monitor_info.height = HeightOfScreen(dc.x.screen);
strcpy(dc.x.monitor_info.name, MONITOR_AUTO);
}

compute_geometry(conf, &dc, &topleft_x, &topleft_y, &fat_layer,
&available_length);

Expand Down
10 changes: 10 additions & 0 deletions src/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,23 @@ typedef enum
ALTERNATIVE
} Show_mode;

typedef struct
{
char name[10];
int x;
int y;
int width;
int height;
} MonitorInfo;

typedef struct
{
Display *display;
int screen_number;
Screen *screen;
Window window;
Bool mapped;
MonitorInfo monitor_info;
} X_context;

typedef struct
Expand Down
1 change: 1 addition & 0 deletions styles.cfg
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
default = {
monitor = "auto";
x = {relative = 1; offset = -48;};
y = {relative = 0.5; offset = 0;};
length = {relative = 0.3; offset = 0;};
Expand Down