diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a6a237..2761830 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,10 @@ set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake") include(GNUInstallDirs) find_package(PkgConfig) +# Add path for custom CMake modules. +list(APPEND CMAKE_MODULE_PATH + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules") + execute_process( COMMAND git log -1 --format=%h WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} @@ -50,7 +54,7 @@ list(APPEND COMBINED_LDFLAGS ${LOGIN_LIBS_LDFLAGS}) # Optional dependencies # Needed to eventually build wayland protocols -add_subdirectory(protocol) +include(WaylandProtocols) # Helper macro for dealing correctly with optional pkg-config dependencies. # There are a number of issues when using pkg-config with cmake (as compared to @@ -98,9 +102,9 @@ macro(optional_dep name modules description) endif() endmacro() -optional_dep(GAMMA "x11;xrandr;libdrm;wayland-client" "Gamma correction" src/modules/gamma_plugins protocol/wlr-gamma-control-unstable-v1.xml) -optional_dep(DPMS "x11;xext;libdrm;wayland-client" "DPMS" src/modules/dpms_plugins protocol/org_kde_kwin_dpms.xml;protocol/wlr-output-power-management-unstable-v1.xml) -optional_dep(SCREEN "x11" "screen emitted brightness" src/modules/screen_plugins protocol/wlr-screencopy-unstable-v1.xml) +optional_dep(GAMMA "x11;xrandr;libdrm;wayland-client" "Gamma correction" src/modules/gamma_plugins https://gitlab.freedesktop.org/wlroots/wlr-protocols/-/raw/master/unstable/wlr-gamma-control-unstable-v1.xml;https://raw.githubusercontent.com/KDE/plasma-wayland-protocols/master/src/protocols/outputdevice.xml;https://raw.githubusercontent.com/KDE/plasma-wayland-protocols/master/src/protocols/output-management.xml) +optional_dep(DPMS "x11;xext;libdrm;wayland-client" "DPMS" src/modules/dpms_plugins https://raw.githubusercontent.com/KDE/plasma-wayland-protocols/master/src/protocols/dpms.xml;https://gitlab.freedesktop.org/wlroots/wlr-protocols/-/raw/master/unstable/wlr-output-power-management-unstable-v1.xml) +optional_dep(SCREEN "x11" "screen emitted brightness" src/modules/screen_plugins https://gitlab.freedesktop.org/wlroots/wlr-protocols/-/raw/master/unstable/wlr-screencopy-unstable-v1.xml) optional_dep(DDC "ddcutil>=0.9.5" "external monitor backlight") optional_dep(YOCTOLIGHT "libusb-1.0" "Yoctolight usb als devices support") optional_dep(PIPEWIRE "libpipewire-0.3" "Enable pipewire camera sensor support") diff --git a/TODO.md b/TODO.md index 5395408..08eee8b 100644 --- a/TODO.md +++ b/TODO.md @@ -35,6 +35,7 @@ ### Generic - [ ] Drop is_smooth options for gamma - [ ] avoid returning boolean where it does not make sense +- [ ] Rename Wl to Wlr (wlroots) in interfaces ## Ideas - [ ] follow ddcci kernel driver and in case, drop ddcutil and add the kernel driver as clightd opt-dep diff --git a/protocol/CMakeLists.txt b/cmake/modules/WaylandProtocols.cmake similarity index 53% rename from protocol/CMakeLists.txt rename to cmake/modules/WaylandProtocols.cmake index 7b6a2d1..96d4545 100644 --- a/protocol/CMakeLists.txt +++ b/cmake/modules/WaylandProtocols.cmake @@ -1,13 +1,19 @@ -# Stolen from: https://github.com/giucam/orbital/blob/512c1b3b20a32cf67ba326e8acce3c1c32e11fa2/cmake/Wayland.cmake, thanks! +include(FetchContent) + function(WAYLAND_ADD_PROTOCOL_CLIENT protocol) find_program(WAYLAND_SCANNER_EXECUTABLE NAMES wayland-scanner REQUIRED) get_filename_component(proto_name ${protocol} NAME_WLE) - get_filename_component(proto_fullpath ${protocol} ABSOLUTE) + FetchContent_Declare(${proto_name} URL ${protocol} DOWNLOAD_NO_EXTRACT TRUE) + FetchContent_Populate(${proto_name}) + message(STATUS "Enabled '${proto_name}' wayland client protocol") - set(_client_header "${CMAKE_CURRENT_BINARY_DIR}/${proto_name}-client-protocol.h") - set(_code "${CMAKE_CURRENT_BINARY_DIR}/${proto_name}-protocol.c") + set(_client_header "${${proto_name}_BINARY_DIR}/${proto_name}-client-protocol.h") + set(_code "${${proto_name}_BINARY_DIR}/${proto_name}-protocol.c") + # set(_client_header "${CMAKE_CURRENT_BINARY_DIR}/${proto_name}-client-protocol.h") + # set(_code "${CMAKE_CURRENT_BINARY_DIR}/${proto_name}-protocol.c") + set(proto_fullpath "${${proto_name}_SOURCE_DIR}/${proto_name}.xml") add_custom_command( OUTPUT "${_client_header}" "${_code}" @@ -17,5 +23,6 @@ function(WAYLAND_ADD_PROTOCOL_CLIENT protocol) ) target_sources(${PROJECT_NAME} PRIVATE ${_code}) - target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) + target_include_directories(${PROJECT_NAME} PRIVATE ${${proto_name}_BINARY_DIR}) endfunction() + diff --git a/protocol/org_kde_kwin_dpms.xml b/protocol/org_kde_kwin_dpms.xml deleted file mode 100644 index bb4f740..0000000 --- a/protocol/org_kde_kwin_dpms.xml +++ /dev/null @@ -1,81 +0,0 @@ - - - - - - The Dpms manager allows to get a org_kde_kwin_dpms for a given wl_output. - The org_kde_kwin_dpms provides the currently used VESA Display Power Management - Signaling state (see https://en.wikipedia.org/wiki/VESA_Display_Power_Management_Signaling ). - In addition it allows to request a state change. A compositor is not obliged to honor it - and will normally automatically switch back to on state. - - - - Factory request to get the org_kde_kwin_dpms for a given wl_output. - - - - - - - - This interface provides information about the VESA DPMS state for a wl_output. - It gets created through the request get on the org_kde_kwin_dpms_manager interface. - - On creating the resource the server will push whether DPSM is supported for the output, - the currently used DPMS state and notifies the client through the done event once all - states are pushed. Whenever a state changes the set of changes is committed with the - done event. - - - - This event gets pushed on binding the resource and indicates whether the wl_output - supports DPMS. There are operation modes of a Wayland server where DPMS might not - make sense (e.g. nested compositors). - - - - - - - - - - - - This mode gets pushed on binding the resource and provides the currently used - DPMS mode. It also gets pushed if DPMS is not supported for the wl_output, in that - case the value will be On. - - The event is also pushed whenever the state changes. - - - - - - This event gets pushed on binding the resource once all other states are pushed. - - In addition it gets pushed whenever a state changes to tell the client that all - state changes have been pushed. - - - - - Requests that the compositor puts the wl_output into the passed mode. The compositor - is not obliged to change the state. In addition the compositor might leave the mode - whenever it seems suitable. E.g. the compositor might return to On state on user input. - - The client should not assume that the mode changed after requesting a new mode. - Instead the client should listen for the mode event. - - - - - - - - diff --git a/protocol/wlr-gamma-control-unstable-v1.xml b/protocol/wlr-gamma-control-unstable-v1.xml deleted file mode 100644 index a9db762..0000000 --- a/protocol/wlr-gamma-control-unstable-v1.xml +++ /dev/null @@ -1,126 +0,0 @@ - - - - Copyright © 2015 Giulio camuffo - Copyright © 2018 Simon Ser - - Permission to use, copy, modify, distribute, and sell this - software and its documentation for any purpose is hereby granted - without fee, provided that the above copyright notice appear in - all copies and that both that copyright notice and this permission - notice appear in supporting documentation, and that the name of - the copyright holders not be used in advertising or publicity - pertaining to distribution of the software without specific, - written prior permission. The copyright holders make no - representations about the suitability of this software for any - purpose. It is provided "as is" without express or implied - warranty. - - THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS - SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND - FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY - SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN - AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, - ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF - THIS SOFTWARE. - - - - This protocol allows a privileged client to set the gamma tables for - outputs. - - Warning! The protocol described in this file is experimental and - backward incompatible changes may be made. Backward compatible changes - may be added together with the corresponding interface version bump. - Backward incompatible changes are done by bumping the version number in - the protocol and interface names and resetting the interface version. - Once the protocol is to be declared stable, the 'z' prefix and the - version number in the protocol and interface names are removed and the - interface version number is reset. - - - - - This interface is a manager that allows creating per-output gamma - controls. - - - - - Create a gamma control that can be used to adjust gamma tables for the - provided output. - - - - - - - - All objects created by the manager will still remain valid, until their - appropriate destroy request has been called. - - - - - - - This interface allows a client to adjust gamma tables for a particular - output. - - The client will receive the gamma size, and will then be able to set gamma - tables. At any time the compositor can send a failed event indicating that - this object is no longer valid. - - There must always be at most one gamma control object per output, which - has exclusive access to this particular output. When the gamma control - object is destroyed, the gamma table is restored to its original value. - - - - - Advertise the size of each gamma ramp. - - This event is sent immediately when the gamma control object is created. - - - - - - - - - - - Set the gamma table. The file descriptor can be memory-mapped to provide - the raw gamma table, which contains successive gamma ramps for the red, - green and blue channels. Each gamma ramp is an array of 16-byte unsigned - integers which has the same length as the gamma size. - - The file descriptor data must have the same length as three times the - gamma size. - - - - - - - This event indicates that the gamma control is no longer valid. This - can happen for a number of reasons, including: - - The output doesn't support gamma tables - - Setting the gamma tables failed - - Another client already has exclusive gamma control for this output - - The compositor has transferred gamma control to another client - - Upon receiving this event, the client should destroy this object. - - - - - - Destroys the gamma control object. If the object is still valid, this - restores the original gamma tables. - - - - diff --git a/protocol/wlr-output-power-management-unstable-v1.xml b/protocol/wlr-output-power-management-unstable-v1.xml deleted file mode 100644 index 4ca1b38..0000000 --- a/protocol/wlr-output-power-management-unstable-v1.xml +++ /dev/null @@ -1,129 +0,0 @@ - - - - Copyright © 2019 Purism SPC - - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal in the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the - Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice (including the next - paragraph) shall be included in all copies or substantial portions of the - Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - DEALINGS IN THE SOFTWARE. - - - - This protocol allows clients to control power management modes - of outputs that are currently part of the compositor space. The - intent is to allow special clients like desktop shells to power - down outputs when the system is idle. - - To modify outputs not currently part of the compositor space see - wlr-output-management. - - Warning! The protocol described in this file is experimental and - backward incompatible changes may be made. Backward compatible changes - may be added together with the corresponding interface version bump. - Backward incompatible changes are done by bumping the version number in - the protocol and interface names and resetting the interface version. - Once the protocol is to be declared stable, the 'z' prefix and the - version number in the protocol and interface names are removed and the - interface version number is reset. - - - - - This interface is a manager that allows creating per-output power - management mode controls. - - - - - Create an output power management mode control that can be used to - adjust the power management mode for a given output. - - - - - - - - All objects created by the manager will still remain valid, until their - appropriate destroy request has been called. - - - - - - - This object offers requests to set the power management mode of - an output. - - - - - - - - - - - - - - Set an output's power save mode to the given mode. The mode change - is effective immediately. If the output does not support the given - mode a failed event is sent. - - - - - - - Report the power management mode change of an output. - - The mode event is sent after an output changed its power - management mode. The reason can be a client using set_mode or the - compositor deciding to change an output's mode. - This event is also sent immediately when the object is created - so the client is informed about the current power management mode. - - - - - - - This event indicates that the output power management mode control - is no longer valid. This can happen for a number of reasons, - including: - - The output doesn't support power management - - Another client already has exclusive power management mode control - for this output - - The output disappeared - - Upon receiving this event, the client should destroy this object. - - - - - - Destroys the output power management mode control object. - - - - - diff --git a/protocol/wlr-screencopy-unstable-v1.xml b/protocol/wlr-screencopy-unstable-v1.xml deleted file mode 100644 index 50b1b7d..0000000 --- a/protocol/wlr-screencopy-unstable-v1.xml +++ /dev/null @@ -1,232 +0,0 @@ - - - - Copyright © 2018 Simon Ser - Copyright © 2019 Andri Yngvason - - Permission is hereby granted, free of charge, to any person obtaining a - copy of this software and associated documentation files (the "Software"), - to deal in the Software without restriction, including without limitation - the rights to use, copy, modify, merge, publish, distribute, sublicense, - and/or sell copies of the Software, and to permit persons to whom the - Software is furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice (including the next - paragraph) shall be included in all copies or substantial portions of the - Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - DEALINGS IN THE SOFTWARE. - - - - This protocol allows clients to ask the compositor to copy part of the - screen content to a client buffer. - - Warning! The protocol described in this file is experimental and - backward incompatible changes may be made. Backward compatible changes - may be added together with the corresponding interface version bump. - Backward incompatible changes are done by bumping the version number in - the protocol and interface names and resetting the interface version. - Once the protocol is to be declared stable, the 'z' prefix and the - version number in the protocol and interface names are removed and the - interface version number is reset. - - - - - This object is a manager which offers requests to start capturing from a - source. - - - - - Capture the next frame of an entire output. - - - - - - - - - Capture the next frame of an output's region. - - The region is given in output logical coordinates, see - xdg_output.logical_size. The region will be clipped to the output's - extents. - - - - - - - - - - - - - All objects created by the manager will still remain valid, until their - appropriate destroy request has been called. - - - - - - - This object represents a single frame. - - When created, a series of buffer events will be sent, each representing a - supported buffer type. The "buffer_done" event is sent afterwards to - indicate that all supported buffer types have been enumerated. The client - will then be able to send a "copy" request. If the capture is successful, - the compositor will send a "flags" followed by a "ready" event. - - For objects version 2 or lower, wl_shm buffers are always supported, ie. - the "buffer" event is guaranteed to be sent. - - If the capture failed, the "failed" event is sent. This can happen anytime - before the "ready" event. - - Once either a "ready" or a "failed" event is received, the client should - destroy the frame. - - - - - Provides information about wl_shm buffer parameters that need to be - used for this frame. This event is sent once after the frame is created - if wl_shm buffers are supported. - - - - - - - - - - Copy the frame to the supplied buffer. The buffer must have a the - correct size, see zwlr_screencopy_frame_v1.buffer and - zwlr_screencopy_frame_v1.linux_dmabuf. The buffer needs to have a - supported format. - - If the frame is successfully copied, a "flags" and a "ready" events are - sent. Otherwise, a "failed" event is sent. - - - - - - - - - - - - - - - - Provides flags about the frame. This event is sent once before the - "ready" event. - - - - - - - Called as soon as the frame is copied, indicating it is available - for reading. This event includes the time at which presentation happened - at. - - The timestamp is expressed as tv_sec_hi, tv_sec_lo, tv_nsec triples, - each component being an unsigned 32-bit value. Whole seconds are in - tv_sec which is a 64-bit value combined from tv_sec_hi and tv_sec_lo, - and the additional fractional part in tv_nsec as nanoseconds. Hence, - for valid timestamps tv_nsec must be in [0, 999999999]. The seconds part - may have an arbitrary offset at start. - - After receiving this event, the client should destroy the object. - - - - - - - - - This event indicates that the attempted frame copy has failed. - - After receiving this event, the client should destroy the object. - - - - - - Destroys the frame. This request can be sent at any time by the client. - - - - - - - Same as copy, except it waits until there is damage to copy. - - - - - - - This event is sent right before the ready event when copy_with_damage is - requested. It may be generated multiple times for each copy_with_damage - request. - - The arguments describe a box around an area that has changed since the - last copy request that was derived from the current screencopy manager - instance. - - The union of all regions received between the call to copy_with_damage - and a ready event is the total damage since the prior ready event. - - - - - - - - - - - Provides information about linux-dmabuf buffer parameters that need to - be used for this frame. This event is sent once after the frame is - created if linux-dmabuf buffers are supported. - - - - - - - - - This event is sent once after all buffer events have been sent. - - The client should proceed to create a buffer of one of the supported - types, and send a "copy" request. - - - - diff --git a/src/modules/dpms.c b/src/modules/dpms.c index bc592e9..2dc9ecb 100644 --- a/src/modules/dpms.c +++ b/src/modules/dpms.c @@ -116,7 +116,7 @@ static int method_getdpms(sd_bus_message *m, void *userdata, sd_bus_error *ret_e if (dpms_state < 0) { switch (dpms_state) { case COMPOSITOR_NO_PROTOCOL: - sd_bus_error_set_const(ret_error, SD_BUS_ERROR_FAILED, "Compositor does not support wayland protocol."); + sd_bus_error_set_const(ret_error, SD_BUS_ERROR_FAILED, "Compositor does not support required wayland protocols."); break; case WRONG_PLUGIN: sd_bus_error_set_const(ret_error, SD_BUS_ERROR_FAILED, "No plugin available for your configuration."); @@ -171,7 +171,7 @@ static int method_setdpms(sd_bus_message *m, void *userdata, sd_bus_error *ret_e if (err) { switch (err) { case COMPOSITOR_NO_PROTOCOL: - sd_bus_error_set_const(ret_error, SD_BUS_ERROR_FAILED, "Compositor does not support neither 'wlr-output-power-management-unstable-v1' nor 'org_kde_kwin_dpms' protocols."); + sd_bus_error_set_const(ret_error, SD_BUS_ERROR_FAILED, "Compositor does not support required wayland protocols."); break; case WRONG_PLUGIN: sd_bus_error_set_const(ret_error, SD_BUS_ERROR_FAILED, "No plugin available for your configuration."); diff --git a/src/modules/dpms_plugins/kwin_wl.c b/src/modules/dpms_plugins/kwin_wl.c index 8df118b..a26a172 100644 --- a/src/modules/dpms_plugins/kwin_wl.c +++ b/src/modules/dpms_plugins/kwin_wl.c @@ -1,4 +1,4 @@ -#include "org_kde_kwin_dpms-client-protocol.h" +#include "dpms-client-protocol.h" #include "wl_utils.h" #include "dpms.h" @@ -85,7 +85,7 @@ static int wl_init(const char *display, const char *env) { wl_display_roundtrip(dpy); if (dpms_control_manager == NULL) { - fprintf(stderr, "compositor doesn't support org_kde_kwin_dpms\n"); + fprintf(stderr, "compositor doesn't support '%s'\n", org_kde_kwin_dpms_manager_interface.name); ret = COMPOSITOR_NO_PROTOCOL; goto err; } diff --git a/src/modules/gamma.c b/src/modules/gamma.c index f75ab09..6bca9de 100644 --- a/src/modules/gamma.c +++ b/src/modules/gamma.c @@ -332,7 +332,7 @@ static int method_setgamma(sd_bus_message *m, void *userdata, sd_bus_error *ret_ sd_bus_error_set_const(ret_error, SD_BUS_ERROR_FAILED, "Temperature value should be between 1000 and 10000."); break; case COMPOSITOR_NO_PROTOCOL: - sd_bus_error_set_const(ret_error, SD_BUS_ERROR_FAILED, "Compositor does not support wayland protocol."); + sd_bus_error_set_const(ret_error, SD_BUS_ERROR_FAILED, "Compositor does not support required wayland protocols."); break; case WRONG_PLUGIN: sd_bus_error_set_const(ret_error, SD_BUS_ERROR_FAILED, "No plugin available for your configuration."); @@ -375,7 +375,7 @@ static int method_getgamma(sd_bus_message *m, void *userdata, sd_bus_error *ret_ if (error || temp == -1) { switch (error) { case COMPOSITOR_NO_PROTOCOL: - sd_bus_error_set_const(ret_error, SD_BUS_ERROR_FAILED, "Compositor does not support 'wlr-gamma-control-unstable-v1' protocol."); + sd_bus_error_set_const(ret_error, SD_BUS_ERROR_FAILED, "Compositor does not support required wayland protocols."); break; case WRONG_PLUGIN: sd_bus_error_set_const(ret_error, SD_BUS_ERROR_FAILED, "No plugin available for your configuration."); diff --git a/src/modules/gamma.h b/src/modules/gamma.h index 504da91..b48cd1a 100644 --- a/src/modules/gamma.h +++ b/src/modules/gamma.h @@ -7,7 +7,8 @@ struct _gamma_cl; #define _GAMMA_PLUGINS \ X(XORG, 0) \ X(WL, 1) \ - X(DRM, 2) + X(KWIN_WL, 2) \ + X(DRM, 3) enum gamma_plugins { #define X(name, val) name = val, diff --git a/src/modules/gamma_plugins/kwin_wl.c b/src/modules/gamma_plugins/kwin_wl.c new file mode 100644 index 0000000..806eddb --- /dev/null +++ b/src/modules/gamma_plugins/kwin_wl.c @@ -0,0 +1,203 @@ +#include "output-management-client-protocol.h" +#include "outputdevice-client-protocol.h" +#include "wl_utils.h" +#include "gamma.h" +#include + +#define UNIMPLEMENTED(registry_listener) registry_listener {} + +static void registry_handle_global(void *data, struct wl_registry *registry, + uint32_t name, const char *interface, uint32_t version); +UNIMPLEMENTED(static void registry_handle_global_remove(void *data, + struct wl_registry *registry, uint32_t name)) +static void gamma_applied(void *data, struct org_kde_kwin_outputconfiguration *org_kde_kwin_outputconfiguration); +static void gamma_failed(void *data, struct org_kde_kwin_outputconfiguration *org_kde_kwin_outputconfiguration); + +static void device_colorcurves(void *data, struct org_kde_kwin_outputdevice *org_kde_kwin_outputdevice, + struct wl_array *red, + struct wl_array *green, + struct wl_array *blue); +static void device_uuid(void *data, struct org_kde_kwin_outputdevice *org_kde_kwin_outputdevice, const char *uuid); + +typedef struct { + struct org_kde_kwin_outputdevice *od; + struct wl_array red; + struct wl_array green; + struct wl_array blue; + char *name; +} kwin_color_dev; + +typedef struct { + struct wl_display *dpy; + queue_t *devices; + struct org_kde_kwin_outputconfiguration *output_config; + struct org_kde_kwin_outputmanagement *output_control_manager; + struct wl_registry *gamma_registry; + bool applied; + bool failed; +} kwin_gamma_priv; + +static const struct wl_registry_listener registry_listener = { + .global = registry_handle_global, + .global_remove = registry_handle_global_remove, +}; + +static const struct org_kde_kwin_outputconfiguration_listener gamma_listener = { + .applied = gamma_applied, + .failed = gamma_failed, +}; + +static const struct org_kde_kwin_outputdevice_listener device_listener = { + .colorcurves = device_colorcurves, + .uuid = device_uuid, +}; + +GAMMA("KWin_wl"); + +static void registry_handle_global(void *data, struct wl_registry *registry, + uint32_t name, const char *interface, uint32_t version) { + kwin_gamma_priv *priv = (kwin_gamma_priv *)data; + if (strcmp(interface, org_kde_kwin_outputdevice_interface.name) == 0) { + struct org_kde_kwin_outputdevice *od = wl_registry_bind(registry, name, &org_kde_kwin_outputdevice_interface, 1); + kwin_color_dev *dev = calloc(1, sizeof(kwin_color_dev)); + dev->od = od; + queue_enqueue(priv->devices, dev); + } else if (strcmp(interface, org_kde_kwin_outputmanagement_interface.name) == 0) { + priv->output_control_manager = wl_registry_bind(registry, name, &org_kde_kwin_outputmanagement_interface, 1); + } +} + +static void gamma_applied(void *data, struct org_kde_kwin_outputconfiguration *org_kde_kwin_outputconfiguration) { + kwin_gamma_priv *priv = (kwin_gamma_priv *)data; + priv->applied = true; +} + +static void gamma_failed(void *data, struct org_kde_kwin_outputconfiguration *org_kde_kwin_outputconfiguration) { + kwin_gamma_priv *priv = (kwin_gamma_priv *)data; + priv->failed = true; +} + +static void device_colorcurves(void *data, struct org_kde_kwin_outputdevice *org_kde_kwin_outputdevice, + struct wl_array *red, + struct wl_array *green, + struct wl_array *blue) +{ + kwin_color_dev *priv = (kwin_color_dev *)data; + wl_array_init(&priv->red); + wl_array_copy(&priv->red, red); + + wl_array_init(&priv->green); + wl_array_copy(&priv->green, green); + + wl_array_init(&priv->blue); + wl_array_copy(&priv->blue, blue); +} + +static void device_uuid(void *data, struct org_kde_kwin_outputdevice *org_kde_kwin_outputdevice, const char *uuid) { + kwin_color_dev *priv = (kwin_color_dev *)data; + priv->name = strdup(uuid); +} + +static void device_dtor(void *dev) { + kwin_color_dev *priv = (kwin_color_dev *)dev; + org_kde_kwin_outputdevice_destroy(priv->od); + wl_array_release(&priv->red); + wl_array_release(&priv->green); + wl_array_release(&priv->blue); + free(priv->name); +} + +static int validate(const char **id, const char *env, void **priv_data) { + int ret = 0; + struct wl_display *dpy = fetch_wl_display(*id, env); + if (dpy == NULL) { + ret = WRONG_PLUGIN; + return ret; + } + + *priv_data = calloc(1, sizeof(kwin_gamma_priv)); + kwin_gamma_priv *priv = (kwin_gamma_priv *)*priv_data; + if (!priv) { + return -ENOMEM; + } + + priv->dpy = dpy; + priv->devices = queue_new(device_dtor); + priv->gamma_registry = wl_display_get_registry(priv->dpy); + wl_registry_add_listener(priv->gamma_registry, ®istry_listener, priv); + wl_display_roundtrip(priv->dpy); + if (priv->output_control_manager == NULL) { + fprintf(stderr, "compositor doesn't support '%s'\n", org_kde_kwin_outputmanagement_interface.name); + ret = COMPOSITOR_NO_PROTOCOL; + goto err; + } + + priv->output_config = org_kde_kwin_outputmanagement_create_configuration(priv->output_control_manager); + if (priv->output_config) { + org_kde_kwin_outputconfiguration_add_listener(priv->output_config, &gamma_listener, priv); + } else { + fprintf(stderr, "failed to receive KWin output management configuration\n"); + ret = -errno; + goto err; + } + wl_display_roundtrip(priv->dpy); + + for (queue_itr_t *itr = queue_itr_new(priv->devices); itr; itr = queue_itr_next(itr)) { + kwin_color_dev *dev = queue_itr_get_data(itr); + org_kde_kwin_outputdevice_add_listener(dev->od, &device_listener, dev); + } + wl_display_roundtrip(priv->dpy); + + /* No supported output found */ + if (queue_length(priv->devices) == 0) { + ret = UNSUPPORTED; + } + +err: + if (ret != 0) { + dtor(priv); + } + return ret; +} + +static int set(void *priv_data, const int temp) { + kwin_gamma_priv *priv = (kwin_gamma_priv *)priv_data; + for (queue_itr_t *itr = queue_itr_new(priv->devices); itr; itr = queue_itr_next(itr)) { + kwin_color_dev *dev = queue_itr_get_data(itr); + const double br = get_gamma_brightness(dev->name); + uint16_t *r = (uint16_t *)dev->red.data; + uint16_t *g = (uint16_t *)dev->green.data; + uint16_t *b = (uint16_t *)dev->blue.data; + fill_gamma_table(r, g, b, br, dev->red.size, temp); + org_kde_kwin_outputconfiguration_colorcurves(priv->output_config, dev->od, &dev->red, &dev->green, &dev->blue); + } + return 0; +} + +static int get(void *priv_data) { + kwin_gamma_priv *priv = (kwin_gamma_priv *)priv_data; + kwin_color_dev *dev = queue_peek(priv->devices); + const double br = get_gamma_brightness(dev->name); + uint16_t *r = (uint16_t *)dev->red.data; + uint16_t *b = (uint16_t *)dev->blue.data; + return get_temp(clamp(r[1] / br, 0, 255), clamp(b[1] / br, 0, 255));; +} + +static void dtor(void *priv_data) { + kwin_gamma_priv *priv = (kwin_gamma_priv *)priv_data; + queue_free(priv->devices); + if (priv->gamma_registry) { + wl_registry_destroy(priv->gamma_registry); + } + if (priv->output_config) { + org_kde_kwin_outputconfiguration_destroy(priv->output_config); + } + if (priv->output_control_manager) { + org_kde_kwin_outputmanagement_destroy(priv->output_control_manager); + } + free(priv_data); + // NOTE: dpy is disconnected on program exit to workaround + // gamma protocol limitation that resets gamma as soon as display is disconnected. + // See wl_utils.c +} + diff --git a/src/modules/gamma_plugins/wl.c b/src/modules/gamma_plugins/wl.c index 4525802..513cf1a 100644 --- a/src/modules/gamma_plugins/wl.c +++ b/src/modules/gamma_plugins/wl.c @@ -149,7 +149,7 @@ static int validate(const char **id, const char *env, void **priv_data) { wl_display_roundtrip(display); if (priv->gamma_control_manager == NULL) { - fprintf(stderr, "compositor doesn't support wlr-gamma-control-unstable-v1\n"); + fprintf(stderr, "compositor doesn't support '%s'\n", zwlr_gamma_control_manager_v1_interface.name); ret = COMPOSITOR_NO_PROTOCOL; goto err; }