Skip to content

Commit

Permalink
Gtk4 (#24)
Browse files Browse the repository at this point in the history
* Initial port to GTK4

* Fix range slider but sliding on it is still broken

* Fix header bar and most of the ui except the menu popup and about window

* Fix about dialog and menu popup except popup autohide and About button text centered issue

* Fix all issues and deprecation except About button text centered issue and styling deprecations, unref, unparent warnings

* Bump version and update debian and rpm dependencies
  • Loading branch information
sidevesh authored Jun 25, 2024
1 parent a32df9b commit 25471b7
Show file tree
Hide file tree
Showing 16 changed files with 141 additions and 190 deletions.
9 changes: 6 additions & 3 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/gtk-3.0",
"/usr/include/gtk-4.0",
"/usr/include/libadwaita-1",
"/usr/include/glib-2.0",
"/usr/lib/glib-2.0/include",
"/usr/include/pango-1.0",
"/usr/include/harfbuzz",
"/usr/include/cairo",
"/usr/include/gdk-pixbuf-2.0",
"/usr/include/atk-1.0"
"/usr/include/atk-1.0",
"/usr/include/graphene-1.0",
"/usr/lib/graphene-1.0/include/"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
Expand All @@ -21,4 +24,4 @@
}
],
"version": 4
}
}
2 changes: 1 addition & 1 deletion build-debug.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
gcc -g -pthread `pkg-config --cflags gtk+-3.0` -o build/app.debug src/main.c `pkg-config --libs gtk+-3.0` -l ddcutil
gcc -g -pthread `pkg-config --cflags gtk4 libadwaita-1` -o build/app.debug src/main.c `pkg-config --libs gtk4 libadwaita-1` -l ddcutil
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#!/bin/bash
gcc -pthread `pkg-config --cflags gtk+-3.0` -o build/app src/main.c `pkg-config --libs gtk+-3.0` -l ddcutil
gcc -pthread `pkg-config --cflags gtk4 libadwaita-1` -o build/app src/main.c `pkg-config --libs gtk4 libadwaita-1` -l ddcutil
4 changes: 2 additions & 2 deletions debian/control
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: luminance
Version: 1.0.3
Version: 1.1.0
Architecture: amd64
Maintainer: Swapnil Devesh <[email protected]>
Homepage: https://github.com/sidevesh/Luminance
Description: A simple GTK application to control brightness of displays including external displays supporting DDC/CI
License: GPL-3.0
Depends: libgtk-3-0, ddcutil, libddcutil4
Depends: libgtk-4-1, libadwaita-1-0, ddcutil, libddcutil4
2 changes: 1 addition & 1 deletion info.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define APP_INFO_VERSION_NUMBER "1.0.3"
#define APP_INFO_VERSION_NUMBER "1.1.0"
#define APP_INFO_PACKAGE_NAME "com.sidevesh.Luminance"
#define APP_INFO_DISPLAY_NAME "Luminance"
#define APP_INFO_ICON_NAME "com.sidevesh.Luminance"
Expand Down
7 changes: 4 additions & 3 deletions rpm/com.sidevesh.Luminance.spec
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Name: com.sidevesh.Luminance
Version: 1.0.3
Version: 1.1.0
Release: 1%{?dist}
Summary: A simple GTK application to control brightness of displays including external displays supporting DDC/CI

Expand All @@ -9,9 +9,10 @@ BuildArch: x86_64

# Define the BuildRequires and Requires packages
BuildRequires: pkgconfig(glib-2.0)
BuildRequires: pkgconfig(gtk+-3.0)
BuildRequires: pkgconfig(gtk4)

Requires: gtk3
Requires: gtk4
Requires: libadwaita
Requires: ddcutil

%description
Expand Down
22 changes: 8 additions & 14 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <string.h>
#include <getopt.h>
#include <gtk/gtk.h>

#include <adwaita.h>
#include "./constants/main.c"
#include "./osd/main.c"
#include "./states/displays.c"
Expand Down Expand Up @@ -40,7 +40,7 @@ void update_window_contents_in_ui() {
}

static void activate_gtk_ui(GtkApplication *app) {
initialize_application_window(app);
initialize_application_window(GTK_APPLICATION(app));
update_window_contents_in_ui();
load_displays(update_window_contents_in_ui, update_window_contents_in_ui);
}
Expand All @@ -54,7 +54,6 @@ int ensure_displays_are_present_in_cli() {
}
}

// Function to list all displays and their brightness
int list_displays_in_cli() {
for (guint index = 0; index < displays_count(); index++) {
gchar *label = get_display_name(index);
Expand All @@ -65,7 +64,6 @@ int list_displays_in_cli() {
return 0;
}

// Function to get the brightness percentage of a specified display
int get_display_brightness_in_cli(guint display_number) {
for (guint index = 0; index < displays_count(); index++) {
if ((index + 1) == display_number) {
Expand All @@ -90,14 +88,13 @@ void set_brightness_percentage_in_cli(guint display_index, double brightness_per
fprintf(stderr, "Invalid display number: %d\n", display_index + 1);
}

// Function to set the brightness of a specified display
int set_display_brightness_if_needed_in_cli(guint display_number, guint brightness_percentage, gchar option, gchar show_osd) {
load_displays(NULL, NULL);
ensure_displays_are_present_in_cli();

gdouble linked_all_displays_brightness_percentage = -1;
gdouble non_linked_all_displays_brightness_percentages_average = -1;

load_displays(NULL, NULL);
ensure_displays_are_present_in_cli();

for (guint index = 0; index < displays_count(); index++) {
if (display_number == 0 || (index + 1) == display_number) {
gdouble current_brightness_percentage = get_display_brightness_percentage(index);
Expand Down Expand Up @@ -158,7 +155,6 @@ int set_display_brightness_if_needed_in_cli(guint display_number, guint brightne
return 0;
}

// Function to display command-line arguments and help information
int display_help_in_cli() {
printf("Usage: %s [OPTIONS]\n", APP_INFO_PACKAGE_NAME);
printf("An application to control brightness of displays including external displays supporting DDC/CI\n");
Expand All @@ -180,7 +176,6 @@ int display_help_in_cli() {
return 0;
}

// CLI argument parsing
int parse_cli_arguments(int argc, char **argv) {
struct option long_options[] = {
{"list-displays", no_argument, NULL, 'l'},
Expand Down Expand Up @@ -303,11 +298,9 @@ int parse_cli_arguments(int argc, char **argv) {
if (status == 0 && show_osd != 0) {
show_osd_after_brightness_change(show_osd);
}

return status;
}

// Entry point of the program
int main(int argc, char **argv) {
// Check if the lock file exists
if (g_file_test(LOCK_FILE_PATH, G_FILE_TEST_EXISTS)) {
Expand Down Expand Up @@ -336,8 +329,9 @@ int main(int argc, char **argv) {
#else
flags = G_APPLICATION_FLAGS_NONE;
#endif
GtkApplication *app;
app = gtk_application_new(APP_INFO_PACKAGE_NAME, flags);

AdwApplication *app;
app = adw_application_new(APP_INFO_PACKAGE_NAME, flags);
g_signal_connect(app, "activate", G_CALLBACK(activate_gtk_ui), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
Expand Down
13 changes: 10 additions & 3 deletions src/ui/components/display_brightness_scale.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
#include <gtk/gtk.h>
#include "../constants/main.c"


static char* format_brightness_value(GtkScale *scale, gdouble value, gpointer data) {
return g_strdup_printf(" %d%%", (int)value);
}

GtkWidget* get_display_brightness_scale(gdouble last_value, gdouble max_value) {
GtkWidget *scale = gtk_scale_new_with_range(GTK_ORIENTATION_HORIZONTAL, 0, max_value, 1);
gtk_range_set_value(GTK_RANGE(scale), last_value);

gtk_scale_set_draw_value(GTK_SCALE(scale), TRUE);
gtk_scale_set_value_pos(GTK_SCALE(scale), GTK_POS_RIGHT);
gtk_scale_set_format_value_func(GTK_SCALE(scale), format_brightness_value, NULL, NULL);
gtk_widget_set_halign(scale, GTK_ALIGN_FILL);
gtk_widget_set_hexpand(scale, TRUE);
gtk_widget_set_valign(scale, GTK_ALIGN_START);
gtk_widget_set_halign(scale, 0.0);
gtk_widget_set_margin_start(scale, MARGIN_UNIT / 4);
gtk_widget_set_margin_end(scale, MARGIN_UNIT / 2);
gtk_widget_set_margin_bottom(scale, MARGIN_UNIT);
gtk_range_set_value(GTK_RANGE(scale), last_value);

return scale;
}
9 changes: 5 additions & 4 deletions src/ui/components/display_icon.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
#include "../constants/main.c"

GtkWidget* get_display_icon() {
GtkWidget *icon = gtk_image_new_from_icon_name("video-display-symbolic", GTK_ICON_SIZE_DIALOG);
gtk_widget_set_valign(icon, GTK_ALIGN_CENTER);
gtk_widget_set_margin_start(icon, MARGIN_UNIT);
GtkWidget *icon = gtk_image_new_from_icon_name("video-display-symbolic");

return icon;
gtk_image_set_pixel_size(GTK_IMAGE(icon), 56);
gtk_widget_set_margin_start(icon, MARGIN_UNIT);

return icon;
}
3 changes: 1 addition & 2 deletions src/ui/components/display_label.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@

GtkWidget* get_display_label(gchar *text) {
GtkWidget *label = gtk_label_new(text);

gtk_label_set_xalign(GTK_LABEL(label), 0.0);
gtk_widget_set_hexpand(label, FALSE);
gtk_widget_set_valign(label, GTK_ALIGN_START);
gtk_widget_set_halign(label, 0.0);
gtk_widget_set_margin_start(label, MARGIN_UNIT);
gtk_widget_set_margin_end(label, MARGIN_UNIT);
gtk_widget_set_margin_top(label, MARGIN_UNIT);
Expand Down
4 changes: 3 additions & 1 deletion src/ui/components/link_brightness_check_button.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

GtkWidget* get_link_brightness_checkbox(gboolean initial_value) {
GtkWidget *link_brightness_checkbox = gtk_check_button_new_with_label("Sync brightness of all displays");
gtk_check_button_set_active(GTK_CHECK_BUTTON(link_brightness_checkbox), initial_value);

gtk_widget_set_margin_start(link_brightness_checkbox, MARGIN_UNIT);
gtk_widget_set_margin_end(link_brightness_checkbox, MARGIN_UNIT);
gtk_widget_set_margin_top(link_brightness_checkbox, MARGIN_UNIT);
gtk_widget_set_margin_bottom(link_brightness_checkbox, MARGIN_UNIT);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(link_brightness_checkbox), initial_value);
gtk_widget_set_halign(link_brightness_checkbox, GTK_ALIGN_END);

return link_brightness_checkbox;
}
3 changes: 2 additions & 1 deletion src/ui/components/separator.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

GtkWidget* get_separator() {
GtkWidget *separator = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
gtk_widget_set_halign(separator, 0);

gtk_widget_set_halign(separator, GTK_ALIGN_FILL);
gtk_widget_set_margin_start(separator, 0);

return separator;
Expand Down
37 changes: 12 additions & 25 deletions src/ui/screens/no_displays.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,51 +10,38 @@ void _on_no_displays_screen_refresh_button_clicked(GtkWidget *widget, gpointer d

GtkWidget* get_no_displays_screen() {
GtkWidget *box, *image, *title, *subtitle, *button;
GtkStyleContext *image_style_context, *button_style_context;
GtkCssProvider *button_css_provider;

box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
gtk_box_set_spacing(GTK_BOX(box), 0);
gtk_box_set_homogeneous(GTK_BOX(box), FALSE);
gtk_widget_set_halign(box, GTK_ALIGN_CENTER);
gtk_widget_set_valign(box, GTK_ALIGN_CENTER);
gtk_widget_set_margin_top(box, MARGIN_UNIT * 2.4);
gtk_widget_set_margin_start(box, MARGIN_UNIT);
gtk_widget_set_margin_end(box, MARGIN_UNIT);
gtk_widget_set_margin_bottom(box, MARGIN_UNIT);

image = gtk_image_new_from_icon_name("video-display-symbolic", GTK_ICON_SIZE_DIALOG);
image_style_context = gtk_widget_get_style_context(image);
gtk_style_context_add_class(image_style_context, "dim-label");
image = gtk_image_new_from_icon_name("video-display-symbolic");
gtk_widget_add_css_class(image, "dim-label");
gtk_image_set_pixel_size(GTK_IMAGE(image), 128);
gtk_widget_set_halign(image, GTK_ALIGN_CENTER);
gtk_widget_set_valign(image, GTK_ALIGN_CENTER);
gtk_widget_set_size_request(image, 128, 128);
gtk_widget_set_margin_bottom(image, MARGIN_UNIT * 2);
gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 0);
gtk_box_append(GTK_BOX(box), image);

title = gtk_label_new(NULL);
gtk_label_set_markup (GTK_LABEL(title), "<span size='xx-large' weight='heavy'>No compatible displays found</span>");
gtk_widget_set_halign(title, GTK_ALIGN_CENTER);
gtk_widget_set_valign(title, GTK_ALIGN_CENTER);
gtk_box_pack_start(GTK_BOX(box), title, FALSE, FALSE, 5);
gtk_label_set_markup(GTK_LABEL(title), "<span size='xx-large' weight='heavy'>No compatible displays found</span>");
gtk_box_append(GTK_BOX(box), title);

subtitle = gtk_label_new("Try refreshing if you have just connected the displays");
gtk_widget_set_halign(subtitle, GTK_ALIGN_CENTER);
gtk_widget_set_valign(subtitle, GTK_ALIGN_CENTER);
gtk_widget_set_margin_bottom(subtitle, MARGIN_UNIT);
gtk_box_pack_start(GTK_BOX(box), subtitle, FALSE, FALSE, 5);
gtk_box_append(GTK_BOX(box), subtitle);

button = gtk_button_new_with_label("Refresh");
button_style_context = gtk_widget_get_style_context(button);
gtk_style_context_add_class(button_style_context, "circular");
gtk_widget_add_css_class(button, "circular");
button_css_provider = gtk_css_provider_new();
gtk_css_provider_load_from_data(button_css_provider, "button {padding: 10px;}", -1, NULL);
gtk_style_context_add_provider(button_style_context, GTK_STYLE_PROVIDER(button_css_provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
gtk_css_provider_load_from_string(button_css_provider, "button {padding-left: 12px;padding-right: 12px;padding-top: 4px;padding-bottom: 4px;}");
gtk_style_context_add_provider(gtk_widget_get_style_context(button), GTK_STYLE_PROVIDER(button_css_provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
gtk_widget_set_halign(button, GTK_ALIGN_CENTER);
gtk_widget_set_valign(button, GTK_ALIGN_CENTER);
gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 5);

g_signal_connect(button, "clicked", G_CALLBACK(_on_no_displays_screen_refresh_button_clicked), NULL);
g_signal_connect(button, "clicked", G_CALLBACK(_on_no_displays_screen_refresh_button_clicked), NULL);
gtk_box_append(GTK_BOX(box), button);

return box;
}
19 changes: 5 additions & 14 deletions src/ui/screens/refreshing_displays.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

GtkWidget* get_refreshing_displays_screen() {
GtkWidget *box, *image, *title, *spinner;
GtkStyleContext *image_style_context;

box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
gtk_box_set_spacing(GTK_BOX(box), 0);
Expand All @@ -15,29 +14,21 @@ GtkWidget* get_refreshing_displays_screen() {
gtk_widget_set_margin_end(box, MARGIN_UNIT);
gtk_widget_set_margin_bottom(box, MARGIN_UNIT);

image = gtk_image_new_from_icon_name("system-search-symbolic", GTK_ICON_SIZE_DIALOG);

image_style_context = gtk_widget_get_style_context(image);
gtk_style_context_add_class(image_style_context, "dim-label");
image = gtk_image_new_from_icon_name("system-search-symbolic");
gtk_widget_add_css_class(image, "dim-label");
gtk_image_set_pixel_size(GTK_IMAGE(image), 128);
gtk_widget_set_halign(image, GTK_ALIGN_CENTER);
gtk_widget_set_valign(image, GTK_ALIGN_CENTER);
gtk_widget_set_margin_bottom(image, MARGIN_UNIT * 2);
gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 0);
gtk_box_append(GTK_BOX(box), image);

title = gtk_label_new(NULL);
gtk_label_set_markup (GTK_LABEL(title), "<span size='xx-large' weight='heavy'>Refreshing displays...</span>");
gtk_widget_set_halign(title, GTK_ALIGN_CENTER);
gtk_widget_set_valign(title, GTK_ALIGN_CENTER);
gtk_widget_set_margin_bottom(title, MARGIN_UNIT);
gtk_box_pack_start(GTK_BOX(box), title, FALSE, FALSE, 5);
gtk_box_append(GTK_BOX(box), title);

spinner = gtk_spinner_new();
gtk_widget_set_size_request(spinner, 32, 32);
gtk_widget_set_halign(spinner, GTK_ALIGN_CENTER);
gtk_widget_set_valign(spinner, GTK_ALIGN_CENTER);
gtk_widget_set_margin_bottom(spinner, MARGIN_UNIT * 2.7);
gtk_box_pack_start(GTK_BOX(box), spinner, FALSE, FALSE, 5);
gtk_box_append(GTK_BOX(box), spinner);
gtk_spinner_start(GTK_SPINNER(spinner));

return box;
Expand Down
Loading

0 comments on commit 25471b7

Please sign in to comment.