From 1123e48fc4cd9a7894e48117981415b76c2ed991 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Mon, 24 Feb 2014 07:11:30 +0100 Subject: prototype list_monitors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- Makefile | 15 ++- src/blueshift_crtc_c.c | 297 ------------------------------------------------- src/blueshift_idcrtc.c | 297 +++++++++++++++++++++++++++++++++++++++++++++++++ src/monitor.py | 38 +++++++ 4 files changed, 349 insertions(+), 298 deletions(-) delete mode 100644 src/blueshift_crtc_c.c create mode 100644 src/blueshift_idcrtc.c diff --git a/Makefile b/Makefile index d1910b2..48ac92d 100644 --- a/Makefile +++ b/Makefile @@ -7,9 +7,11 @@ PREFIX ?= /usr BIN ?= /bin LIB ?= /lib +LIBEXEC ?= /libexec DATA ?= /share BINDIR ?= $(PREFIX)$(BIN) LIBDIR ?= $(PREFIX)$(LIB) +LIBEXECDIR ?= $(PREFIX)$(LIBEXEC) DATADIR ?= $(PREFIX)$(DATA) DOCDIR ?= $(DATADIR)/doc LICENSEDIR ?= $(DATADIR)/licenses @@ -30,6 +32,7 @@ LIBS_vidmode = x11 xxf86vm LIBS = python3 $(foreach B,$(SERVER_BINDINGS),$(LIBS_$(B))) STD = c99 FLAGS = $$($(PKGCONFIG) --cflags --libs $(LIBS)) -std=$(STD) $(WARN) $(OPTIMISE) -fPIC +# TODO only link to used libs for each binary DATAFILES = 2deg 10deg redshift redshift_old PYFILES = __main__.py colour.py curve.py monitor.py solar.py icc.py @@ -59,7 +62,8 @@ dvi: blueshift.dvi ps: blueshift.ps .PHONY: command -command: $(foreach C,$(CBINDINGS),bin/$(C)) bin/blueshift +command: $(foreach C,$(CBINDINGS),bin/$(C)) bin/blueshift_idcrtc bin/blueshift +# TODO make bin/blueshift_idcrtc optional .PHONY: shell shell: bash zsh fish @@ -87,8 +91,13 @@ obj/%.py: src/%.py cp $< $@ sed -i '/^DATADIR *= /s#^.*$$#DATADIR = '\''$(DATADIR)/$(PKGNAME)'\''#' $@ sed -i '/^LIBDIR *= /s#^.*$$#LIBDIR = '\''$(LIBDIR)'\''#' $@ + sed -i '/^LIBEXECDIR *= /s#^.*$$#LIBEXECDIR = '\''$(LIBEXECDIR)'\''#' $@ +bin/blueshift_idcrtc: obj/blueshift_idcrtc.o + @mkdir -p bin + $(CC) $(FLAGS) -o $@ $^ + bin/%.so: obj/%.o obj/%_c.o @mkdir -p bin $(CC) $(FLAGS) -shared -o $@ $^ @@ -155,6 +164,8 @@ install-command: $(foreach C,$(CBINDINGS),bin/$(C)) bin/blueshift $(foreach D,$( install -m755 bin/blueshift -- "$(DESTDIR)$(BINDIR)/$(COMMAND)" install -dm755 -- "$(DESTDIR)$(LIBDIR)" install -m755 $(foreach C,$(CBINDINGS),bin/$(C)) -- "$(DESTDIR)$(LIBDIR)" + install -dm755 -- "$(DESTDIR)$(LIBEXECDIR)" + install -m755 bin/blueshift_idcrtc -- "$(DESTDIR)$(LIBEXECDIR)/blueshift_idcrtc" install -dm755 -- "$(DESTDIR)$(DATADIR)/$(PKGNAME)" install -m644 -- $(foreach D,$(DATAFILES),res/$(D)) "$(DESTDIR)$(DATADIR)/$(PKGNAME)" @@ -214,6 +225,8 @@ install-fish: bin/blueshift.fish uninstall: -rm -- "$(DESTDIR)$(BINDIR)/$(COMMAND)" -rm -- $(foreach C,$(CBINDINGS),"$(DESTDIR)$(LIBDIR)/$(C)") + -rm -- "$(DESTDIR)$(LIBEXECDIR)/blueshift_idcrtc" + -rmdir -- "$(DESTDIR)$(LIBEXECDIR)" -rm -- "$(DESTDIR)$(LICENSEDIR)/$(PKGNAME)/COPYING" -rm -- "$(DESTDIR)$(LICENSEDIR)/$(PKGNAME)/LICENSE" -rmdir -- "$(DESTDIR)$(LICENSEDIR)/$(PKGNAME)" diff --git a/src/blueshift_crtc_c.c b/src/blueshift_crtc_c.c deleted file mode 100644 index d55f6eb..0000000 --- a/src/blueshift_crtc_c.c +++ /dev/null @@ -1,297 +0,0 @@ -/** - * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ -#include -#include -#include -#include -#include - -#include -#include - - - -/** - * The major version of RANDR the program expects - */ -#define RANDR_VERSION_MAJOR 1U - -/** - * The minor version of RANDR the program expects - */ -#define RANDR_VERSION_MINOR 3U - - - -/** - * Connection to the X server - */ -static xcb_connection_t* connection; - -/** - * Used to store errors in - */ -static xcb_generic_error_t* error; - - - -/** - * Main entry point of the program - * - * @param argc Length of `argv` - * @param argv Command line arguments - * @return Zero on success - */ -int main(int argc, char** argv) -{ - xcb_randr_query_version_cookie_t version_cookie; - xcb_randr_query_version_reply_t* randr_version; - const xcb_setup_t* setup; - xcb_screen_iterator_t iter; - int screen_count; - xcb_screen_t* screens; - int screen_i; - - (void) argc; - (void) argv; - - - /* Get X connection */ - - connection = xcb_connect(NULL, NULL); - - - /* Check RANDR protocol version */ - - version_cookie = xcb_randr_query_version(connection, RANDR_VERSION_MAJOR, RANDR_VERSION_MINOR); - randr_version = xcb_randr_query_version_reply(connection, version_cookie, &error); - - if (error || (randr_version == NULL)) - { - fprintf(stderr, "RANDR version query returned %i", error ? error->error_code : -1); - xcb_disconnect(connection); - return 1; - } - - if (randr_version->major_version != RANDR_VERSION_MAJOR || randr_version->minor_version < RANDR_VERSION_MINOR) - { - fprintf(stderr, "Unsupported RANDR version, got %u.%u, expected %u.%u\n", - randr_version->major_version, randr_version->minor_version, - RANDR_VERSION_MAJOR, RANDR_VERSION_MINOR); - free(randr_version); - xcb_disconnect(connection); - return 1; - } - - free(randr_version); - - - /* Get screen information */ - - setup = xcb_get_setup(connection); - iter = xcb_setup_roots_iterator(setup); - screen_count = iter.rem; - screens = iter.data; - - printf("Screen count: %i\n", screen_count); - for (screen_i = 0; screen_i < screen_count; screen_i++) - { - xcb_screen_t* screen = screens + screen_i; - xcb_randr_get_screen_resources_current_cookie_t res_cookie; - xcb_randr_get_screen_resources_current_reply_t* res_reply; - xcb_randr_output_t* outputs; - xcb_randr_crtc_t* crtcs; - int output_i; - - printf("Screen: %i\n", screen_i); - - res_cookie = xcb_randr_get_screen_resources_current(connection, screen->root); - res_reply = xcb_randr_get_screen_resources_current_reply(connection, res_cookie, &error); - - if (error) - { - fprintf(stderr, "RANDR screen resource query returned %i\n", error->error_code); - xcb_disconnect(connection); - return 1; - } - - printf(" CRTC count: %i\n", res_reply->num_crtcs); - printf(" Output count: %i\n", res_reply->num_outputs); - - - /* Get output information */ - - outputs = xcb_randr_get_screen_resources_current_outputs(res_reply); - crtcs = xcb_randr_get_screen_resources_current_crtcs(res_reply); - for (output_i = 0; output_i < res_reply->num_outputs; output_i++) - { - xcb_randr_get_output_info_cookie_t out_cookie; - xcb_randr_get_output_info_reply_t* out_reply; - char* name; - unsigned char* name_; - - out_cookie = xcb_randr_get_output_info(connection, outputs[output_i], res_reply->config_timestamp); - out_reply = xcb_randr_get_output_info_reply(connection, out_cookie, &error); - - if (error) - { - fprintf(stderr, "RANDR output query returned %i\n", error->error_code); - xcb_disconnect(connection); - free(res_reply); - return 1; - } - - printf(" Output: %i\n", output_i); - - name_ = xcb_randr_get_output_info_name(out_reply); - name = alloca((out_reply->name_len + 1) * sizeof(char)); - memcpy(name, name_, out_reply->name_len * sizeof(char)); - *(name + out_reply->name_len) = 0; - printf(" Name: %s\n", name); - - switch (out_reply->connection) - { - case XCB_RANDR_CONNECTION_CONNECTED: - { - xcb_randr_list_output_properties_cookie_t prop_cookie; - xcb_randr_list_output_properties_reply_t* prop_reply; - xcb_atom_t* atoms; - xcb_atom_t* atoms_end; - int crtc_i; - - printf(" Connection: connected\n"); - printf(" Size: %i %i\n", out_reply->mm_width, out_reply->mm_height); - - for (crtc_i = 0; crtc_i < res_reply->num_crtcs; crtc_i++) - if (crtcs[crtc_i] == out_reply->crtc) - printf(" CRTC: %i\n", crtc_i); - - prop_cookie = xcb_randr_list_output_properties(connection, outputs[output_i]); - prop_reply = xcb_randr_list_output_properties_reply(connection, prop_cookie, &error); - - if (error) - { - fprintf(stderr, "RANDR output property query returned %i\n", error->error_code); - free(out_reply); - free(res_reply); - xcb_disconnect(connection); - return 1; - } - - - /* Get output atoms */ - - atoms = xcb_randr_list_output_properties_atoms(prop_reply); - atoms_end = atoms + xcb_randr_list_output_properties_atoms_length(prop_reply); - - for (; atoms != atoms_end; atoms++) - { - xcb_get_atom_name_cookie_t atom_name_cookie; - xcb_get_atom_name_reply_t* atom_name_reply; - char* atom_name; - char* atom_name_; - int atom_name_len; - - atom_name_cookie = xcb_get_atom_name(connection, *atoms); - atom_name_reply = xcb_get_atom_name_reply(connection, atom_name_cookie, &error); - - if (error) - { - fprintf(stderr, "RANDR atom name query returned %i\n", error->error_code); - free(prop_reply); - free(out_reply); - free(res_reply); - xcb_disconnect(connection); - return 1; - } - - atom_name_ = xcb_get_atom_name_name(atom_name_reply); - atom_name_len = xcb_get_atom_name_name_length(atom_name_reply); - - atom_name = alloca((atom_name_len + 1) * sizeof(char)); - memcpy(atom_name, atom_name_, atom_name_len * sizeof(char)); - *(atom_name + atom_name_len) = 0; - - - /* Get output identifier */ - - if (!strcmp(atom_name, "EDID")) - { - xcb_randr_get_output_property_cookie_t atom_cookie; - xcb_randr_get_output_property_reply_t* atom_reply; - int length; - unsigned char* atom_data_; - char* atom_data; - - atom_cookie = xcb_randr_get_output_property(connection, outputs[output_i], *atoms, - XCB_GET_PROPERTY_TYPE_ANY, 0, 0, 0, 0); - - atom_reply = xcb_randr_get_output_property_reply(connection, atom_cookie, &error); - - if (error) - { - fprintf(stderr, "RANDR atom data query returned %i\n", error->error_code); - free(atom_name_reply); - free(prop_reply); - free(out_reply); - free(res_reply); - xcb_disconnect(connection); - return 1; - } - - atom_data_ = xcb_randr_get_output_property_data(atom_reply); - length = xcb_randr_get_output_property_data_length(atom_reply); - - atom_data = alloca((length + 1) * sizeof(char)); - memcpy(atom_data, atom_data_, length * sizeof(char)); - *(atom_data + length) = 0; - - /* printf(" %s: %s\n", atom_name, atom_data); TODO */ - - free(atom_reply); - } - - free(atom_name_reply); - } - - free(prop_reply); - } - break; - - case XCB_RANDR_CONNECTION_DISCONNECTED: - printf(" Connection: disconnected\n"); - break; - - case XCB_RANDR_CONNECTION_UNKNOWN: - default: - printf(" Connection: unknown\n"); - break; - } - - free(out_reply); - } - - free(res_reply); - } - - - /* Free resources **/ - - xcb_disconnect(connection); - return 0; -} - diff --git a/src/blueshift_idcrtc.c b/src/blueshift_idcrtc.c new file mode 100644 index 0000000..d55f6eb --- /dev/null +++ b/src/blueshift_idcrtc.c @@ -0,0 +1,297 @@ +/** + * Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +#include +#include +#include +#include +#include + +#include +#include + + + +/** + * The major version of RANDR the program expects + */ +#define RANDR_VERSION_MAJOR 1U + +/** + * The minor version of RANDR the program expects + */ +#define RANDR_VERSION_MINOR 3U + + + +/** + * Connection to the X server + */ +static xcb_connection_t* connection; + +/** + * Used to store errors in + */ +static xcb_generic_error_t* error; + + + +/** + * Main entry point of the program + * + * @param argc Length of `argv` + * @param argv Command line arguments + * @return Zero on success + */ +int main(int argc, char** argv) +{ + xcb_randr_query_version_cookie_t version_cookie; + xcb_randr_query_version_reply_t* randr_version; + const xcb_setup_t* setup; + xcb_screen_iterator_t iter; + int screen_count; + xcb_screen_t* screens; + int screen_i; + + (void) argc; + (void) argv; + + + /* Get X connection */ + + connection = xcb_connect(NULL, NULL); + + + /* Check RANDR protocol version */ + + version_cookie = xcb_randr_query_version(connection, RANDR_VERSION_MAJOR, RANDR_VERSION_MINOR); + randr_version = xcb_randr_query_version_reply(connection, version_cookie, &error); + + if (error || (randr_version == NULL)) + { + fprintf(stderr, "RANDR version query returned %i", error ? error->error_code : -1); + xcb_disconnect(connection); + return 1; + } + + if (randr_version->major_version != RANDR_VERSION_MAJOR || randr_version->minor_version < RANDR_VERSION_MINOR) + { + fprintf(stderr, "Unsupported RANDR version, got %u.%u, expected %u.%u\n", + randr_version->major_version, randr_version->minor_version, + RANDR_VERSION_MAJOR, RANDR_VERSION_MINOR); + free(randr_version); + xcb_disconnect(connection); + return 1; + } + + free(randr_version); + + + /* Get screen information */ + + setup = xcb_get_setup(connection); + iter = xcb_setup_roots_iterator(setup); + screen_count = iter.rem; + screens = iter.data; + + printf("Screen count: %i\n", screen_count); + for (screen_i = 0; screen_i < screen_count; screen_i++) + { + xcb_screen_t* screen = screens + screen_i; + xcb_randr_get_screen_resources_current_cookie_t res_cookie; + xcb_randr_get_screen_resources_current_reply_t* res_reply; + xcb_randr_output_t* outputs; + xcb_randr_crtc_t* crtcs; + int output_i; + + printf("Screen: %i\n", screen_i); + + res_cookie = xcb_randr_get_screen_resources_current(connection, screen->root); + res_reply = xcb_randr_get_screen_resources_current_reply(connection, res_cookie, &error); + + if (error) + { + fprintf(stderr, "RANDR screen resource query returned %i\n", error->error_code); + xcb_disconnect(connection); + return 1; + } + + printf(" CRTC count: %i\n", res_reply->num_crtcs); + printf(" Output count: %i\n", res_reply->num_outputs); + + + /* Get output information */ + + outputs = xcb_randr_get_screen_resources_current_outputs(res_reply); + crtcs = xcb_randr_get_screen_resources_current_crtcs(res_reply); + for (output_i = 0; output_i < res_reply->num_outputs; output_i++) + { + xcb_randr_get_output_info_cookie_t out_cookie; + xcb_randr_get_output_info_reply_t* out_reply; + char* name; + unsigned char* name_; + + out_cookie = xcb_randr_get_output_info(connection, outputs[output_i], res_reply->config_timestamp); + out_reply = xcb_randr_get_output_info_reply(connection, out_cookie, &error); + + if (error) + { + fprintf(stderr, "RANDR output query returned %i\n", error->error_code); + xcb_disconnect(connection); + free(res_reply); + return 1; + } + + printf(" Output: %i\n", output_i); + + name_ = xcb_randr_get_output_info_name(out_reply); + name = alloca((out_reply->name_len + 1) * sizeof(char)); + memcpy(name, name_, out_reply->name_len * sizeof(char)); + *(name + out_reply->name_len) = 0; + printf(" Name: %s\n", name); + + switch (out_reply->connection) + { + case XCB_RANDR_CONNECTION_CONNECTED: + { + xcb_randr_list_output_properties_cookie_t prop_cookie; + xcb_randr_list_output_properties_reply_t* prop_reply; + xcb_atom_t* atoms; + xcb_atom_t* atoms_end; + int crtc_i; + + printf(" Connection: connected\n"); + printf(" Size: %i %i\n", out_reply->mm_width, out_reply->mm_height); + + for (crtc_i = 0; crtc_i < res_reply->num_crtcs; crtc_i++) + if (crtcs[crtc_i] == out_reply->crtc) + printf(" CRTC: %i\n", crtc_i); + + prop_cookie = xcb_randr_list_output_properties(connection, outputs[output_i]); + prop_reply = xcb_randr_list_output_properties_reply(connection, prop_cookie, &error); + + if (error) + { + fprintf(stderr, "RANDR output property query returned %i\n", error->error_code); + free(out_reply); + free(res_reply); + xcb_disconnect(connection); + return 1; + } + + + /* Get output atoms */ + + atoms = xcb_randr_list_output_properties_atoms(prop_reply); + atoms_end = atoms + xcb_randr_list_output_properties_atoms_length(prop_reply); + + for (; atoms != atoms_end; atoms++) + { + xcb_get_atom_name_cookie_t atom_name_cookie; + xcb_get_atom_name_reply_t* atom_name_reply; + char* atom_name; + char* atom_name_; + int atom_name_len; + + atom_name_cookie = xcb_get_atom_name(connection, *atoms); + atom_name_reply = xcb_get_atom_name_reply(connection, atom_name_cookie, &error); + + if (error) + { + fprintf(stderr, "RANDR atom name query returned %i\n", error->error_code); + free(prop_reply); + free(out_reply); + free(res_reply); + xcb_disconnect(connection); + return 1; + } + + atom_name_ = xcb_get_atom_name_name(atom_name_reply); + atom_name_len = xcb_get_atom_name_name_length(atom_name_reply); + + atom_name = alloca((atom_name_len + 1) * sizeof(char)); + memcpy(atom_name, atom_name_, atom_name_len * sizeof(char)); + *(atom_name + atom_name_len) = 0; + + + /* Get output identifier */ + + if (!strcmp(atom_name, "EDID")) + { + xcb_randr_get_output_property_cookie_t atom_cookie; + xcb_randr_get_output_property_reply_t* atom_reply; + int length; + unsigned char* atom_data_; + char* atom_data; + + atom_cookie = xcb_randr_get_output_property(connection, outputs[output_i], *atoms, + XCB_GET_PROPERTY_TYPE_ANY, 0, 0, 0, 0); + + atom_reply = xcb_randr_get_output_property_reply(connection, atom_cookie, &error); + + if (error) + { + fprintf(stderr, "RANDR atom data query returned %i\n", error->error_code); + free(atom_name_reply); + free(prop_reply); + free(out_reply); + free(res_reply); + xcb_disconnect(connection); + return 1; + } + + atom_data_ = xcb_randr_get_output_property_data(atom_reply); + length = xcb_randr_get_output_property_data_length(atom_reply); + + atom_data = alloca((length + 1) * sizeof(char)); + memcpy(atom_data, atom_data_, length * sizeof(char)); + *(atom_data + length) = 0; + + /* printf(" %s: %s\n", atom_name, atom_data); TODO */ + + free(atom_reply); + } + + free(atom_name_reply); + } + + free(prop_reply); + } + break; + + case XCB_RANDR_CONNECTION_DISCONNECTED: + printf(" Connection: disconnected\n"); + break; + + case XCB_RANDR_CONNECTION_UNKNOWN: + default: + printf(" Connection: unknown\n"); + break; + } + + free(out_reply); + } + + free(res_reply); + } + + + /* Free resources **/ + + xcb_disconnect(connection); + return 0; +} + diff --git a/src/monitor.py b/src/monitor.py index 7572061..11643d1 100644 --- a/src/monitor.py +++ b/src/monitor.py @@ -16,6 +16,7 @@ # along with this program. If not, see . import sys +from subprocess import Popen, PIPE from curve import * @@ -23,6 +24,9 @@ from curve import * LIBDIR = 'bin' sys.path.append(LIBDIR) +# /usr/libexec +LIBEXECDIR = 'bin' + randr_opened = None vidmode_opened = None @@ -186,3 +190,37 @@ def print_curves(*crtcs, screen = 0): print(G_curve) print(B_curve) + + +def list_monitors(): + process = Popen([LIBEXECDIR + "/blueshift_idcrtc"], stdout = PIPE) + lines = process.communicate()[0].decode('utf-8', 'error').split('\n') + lines = [line.strip() for line in lines] + screens, screen, output = None, None, None + for line in lines: + if line.startswith('Screen count: '): + screens = [None] * int(line[len('Screen count: '):]) + elif line.startswith('Screen: '): + screen_i = int(line[len('Screen: '):]) + screen = {} + screens[screen_i] = screen + elif line.startswith('CRTC count: '): + screen['crtc'] = int(line[len('CRTC count: '):]) + elif line.startswith('Output count: '): + screen['output'] = [None] * int(line[len('Output count: '):]) + elif line.startswith('Output: '): + output_i = int(line[len('Output: '):]) + output = {'name' : None, 'connected' : False, 'width' : None, 'height' : None, 'crtc' : None} + screen['output'][output_i] = output + elif line.startswith('Name: '): + output['name'] = line[len('Name: '):] + elif line.startswith('Connection: '): + output['connected'] = line[len('Connection: '):] == 'connected' + elif line.startswith('Size: '): + width, height = [int(x) for x in line[len('Size: '):].split(' ')] + output['width'] = width + output['height'] = height + elif line.startswith('CRTC: '): + output['crtc'] = int(line[len('CRTC: '):]) + return screens + -- cgit v1.2.3-70-g09d2