Browse Source

Get the current background on GTK 2 Gnome Linux

master
Macoy Madson 2 months ago
parent
commit
55efce6bcc
  1. 3
      .gitmodules
  2. 2
      Dependencies/gamelib
  3. 1
      Dependencies/stb
  4. 70
      src/AutoColor.cake
  5. 29
      src/AutoColorCLI.cake

3
.gitmodules

@ -4,3 +4,6 @@
[submodule "Dependencies/gamelib"]
path = Dependencies/gamelib
url = https://macoy.me/code/macoy/gamelib.git
[submodule "Dependencies/stb"]
path = Dependencies/stb
url = https://github.com/nothings/stb

2
Dependencies/gamelib

@ -1 +1 @@
Subproject commit f3537168a01904f6049a615d8a890e7d02d42e91
Subproject commit d75b3f7aa95cd86b0b07ed26da3e3a17299b5a24

1
Dependencies/stb

@ -0,0 +1 @@
Subproject commit af1a5bc352164740c1cc1354942b1c6b72eacb8a

70
src/AutoColor.cake

@ -1 +1,69 @@
(defun auto-color-pick-from-current-background ())
(import "Image.cake"
&comptime-only "CHelpers.cake")
(c-import "stdio.h") ;; fprintf
(comptime-cond
('Unix
;; Gnome GTK
(add-c-search-directory-module "/usr/include/glib-2.0")
(add-c-search-directory-module "/usr/include/glib-2.0")
(add-c-search-directory-module "/usr/lib/x86_64-linux-gnu/glib-2.0/include")
(c-import "gio/gio.h")
(add-library-dependency "glib-2.0" "gio-2.0")
(defun-local auto-color-get-current-background-filename (error-string (* (* (const char)))
&return (* (const char)))
(var g-settings (* GSettings) (g_settings_new "org.gnome.desktop.background"))
(unless g-settings
(set (deref error-string) "Unable to get GTK settings for org.gnome.desktop.background")
(return null))
(var background (* gchar) (g_settings_get_string g-settings "picture-uri"))
(unless background
(set (deref error-string) "Unable to get picture-uri from org.gnome.desktop.background")
(return null))
(var file-uri (* (const char)) "file://")
(var file-uri-prefix-length size_t (strlen file-uri))
(unless (= 0 (strncmp file-uri background file-uri-prefix-length))
(set (deref error-string) "Unable to process picture-uri: uri type not supported")
(return null))
(set background (+ background file-uri-prefix-length))
(return background))))
(defun auto-color-pick-from-file (image-to-load (* (const char)) &return bool)
(var width int 0)
(var height int 0)
(var num-pixel-components int 0)
(var num-desired-channels int 3)
(var pixel-data (* (unsigned char))
(stbi_load image-to-load (addr width) (addr height) (addr num-pixel-components)
num-desired-channels))
(unless pixel-data
(fprintf stderr "error: failed to load %s with message: %s\n" image-to-load
(stbi_failure_reason))
(return false))
(fprintf stderr "size of %s: %dx%d\n" image-to-load width height)
(fprintf stderr "num components in %s: %d\n" image-to-load num-pixel-components)
(fprintf stderr "first three pixels:\n")
(each-in-range 3 i
(var rgb-components (* (unsigned char)) (addr (at (* i 3) pixel-data)))
(fprintf stderr "[%d] %d %d %d\n"
i
(at 0 rgb-components)
(at 1 rgb-components)
(at 2 rgb-components)))
(return true))
(defun auto-color-pick-from-current-background (&return bool)
(var error-string (* (const char)) null)
(var background-filename (* (const char))
(auto-color-get-current-background-filename (addr error-string)))
(unless background-filename
(fprintf stderr "error: %s" error-string)
(return false))
(fprintf stderr "\nPicking colors from '%s'\n" background-filename)
(return (auto-color-pick-from-file background-filename)))

29
src/AutoColorCLI.cake

@ -1,7 +1,34 @@
(import "AutoColor.cake")
(c-import "<stdio.h>")
(var g-copyright-string (* (const char))
#"#Auto Color
Created by Macoy Madson <macoy@macoy.me>.
https://macoy.me/code/macoy/file-helper
Copyright (c) 2021 Macoy Madson.
Auto Color is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Auto Color 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with Auto Color. If not, see <https://www.gnu.org/licenses/>.#"#)
;; (defun-local print-help ()
;; (fprintf stderr "Expected filename\n"))
(defun main (num-arguments int arguments ([] (* char)) &return int)
(fprintf stderr "Hello, world!\n")
;; (unless (> 1 num-arguments)
;; (print-help)
;; (return 1))
(fprintf stderr "%s\n" g-copyright-string)
(auto-color-pick-from-current-background)
(return 0))
(set-cakelisp-option executable-output "auto-color")

Loading…
Cancel
Save