first commit

This commit is contained in:
Alexander Zhirov 2022-05-20 16:28:04 +03:00
commit 61df35ee17
8 changed files with 2352 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.o
mswitch

40
README.md Normal file
View File

@ -0,0 +1,40 @@
# Monitor switch (based on XrandR)
Switching occurs only if 2 monitors are connected to the computer!
## dub
- [Introduction](https://dub.pm/package-format-json)
- [General usage](https://dub.pm/commandline.html)
- [Configuring default settings](https://dub.pm/settings)
## How to build
Install `gcc` and `X11` development libraries. Also install `dmd` or `ldc` compiler. Run:
```sh
git clone https://git.zhirov.website/alexander/mswitch.git
cd mswitch
dub
```
or
```sh
dub -- ~/mswitch.log
```
Something similar will be output to the log file `mswitch.log`, and the monitors will be changed:
```sh
[Monitor("eDP-1", true, 1366, 768), Monitor("HDMI-1", false, 1920, 1080)]
-- Switch monitors --
[Monitor("HDMI-1", true, 1920, 1080), Monitor("eDP-1", false, 1366, 768)]
```
where first (or second) line:
- `eDP-1` - output
- `true` - is primary
- `1366` - width
- `768` - height

27
dub.json Normal file
View File

@ -0,0 +1,27 @@
{
"name": "mswitch",
"authors": [
"Alexander Zhirov"
],
"homepage": "https://git.zhirov.website/alexander/mswitcher",
"license": "GPL-2.0",
"copyright": "Copyright © 2022, Alexander Zhirov",
"description": "Monitor switcher, based on XrandR",
"dflags": [
"-i"
],
"libs": [
"X11",
"Xrandr",
"m"
],
"preBuildCommands": [
"gcc -Os -c source/clang/monitors.c -o obj/monitors.o"
],
"sourceFiles": [
"obj/*.o"
],
"targetType": "executable",
"targetPath": "bin",
"targetName": "mswitch"
}

5
dub.selections.json Normal file
View File

@ -0,0 +1,5 @@
{
"fileVersion": 1,
"versions": {
}
}

4
dub.settings.json Normal file
View File

@ -0,0 +1,4 @@
{
"defaultArchitecture": "x86_64",
"defaultCompiler": "ldc2"
}

28
source/app.d Normal file
View File

@ -0,0 +1,28 @@
import std.stdio;
import modules.monitors;
/**
* Switching occurs only if 2 monitors are connected to the computer!
*/
int main(string[] args)
{
string path = "mswitch.log";
if (args.length > 1)
{
path = args[1];
}
auto file = File(path, "w");
auto monitors = getMonitorsInfo();
file.writeln(monitors);
setPrimaryMonitor(monitors[1].name);
file.writeln("-- Switch monitors --");
swapMonitors(monitors[0].name, monitors[1].name, Relation.right_of);
monitors = getMonitorsInfo();
file.writeln(monitors);
file.close();
return 0;
}

2150
source/clang/monitors.c Normal file

File diff suppressed because it is too large Load Diff

96
source/modules/monitors.d Normal file
View File

@ -0,0 +1,96 @@
module modules.monitors;
import std.conv;
import std.string;
extern (C)
{
enum Relation
{
left_of,
right_of,
above,
below,
same_as,
}
struct x_change
{
immutable(char)* first_monitor;
immutable(char)* second_monitor;
int is_position;
Relation position;
int primary;
}
struct x_monitor
{
char[10] name = "\0";
int width;
int height;
int primary;
}
struct x_info
{
int count;
x_monitor[5] monitor;
}
int XInfo(x_info* info);
int XChange(x_change* settings);
}
struct Monitor
{
string name;
bool primary;
int width;
int height;
}
Monitor[] getMonitorsInfo()
{
Monitor[] m;
x_info monitors;
if (!XInfo(&monitors))
{
foreach (i; 0 .. monitors.count)
{
Monitor current;
current.name = fromStringz(monitors.monitor[i].name.idup);
current.primary = to!bool(monitors.monitor[i].primary);
current.width = monitors.monitor[i].width;
current.height = monitors.monitor[i].height;
m ~= current;
}
}
return m;
}
bool setPrimaryMonitor(string monitor)
{
x_change settings;
settings.first_monitor = toStringz(monitor);
settings.primary = 1;
settings.is_position = 0;
return to!bool(XChange(&settings));
}
bool swapMonitors(string first, string second, Relation position)
{
x_change settings;
settings.first_monitor = toStringz(first);
settings.second_monitor = toStringz(second);
settings.is_position = 1;
settings.position = position;
settings.primary = 0;
return to!bool(XChange(&settings));
}