Skip to content

Commit

Permalink
Update Mutter 46 support
Browse files Browse the repository at this point in the history
Update to the latest version on time.
  • Loading branch information
tintou committed Mar 6, 2024
1 parent 331e059 commit 5644c66
Show file tree
Hide file tree
Showing 24 changed files with 769 additions and 204 deletions.
5 changes: 2 additions & 3 deletions lib/CanvasActor.vala
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
*/

public class Gala.CanvasActor : Clutter.Actor {
private Clutter.Canvas canvas;
private Gala.Drawing.Canvas canvas;

construct {
canvas = new Clutter.Canvas ();
canvas = new Gala.Drawing.Canvas ();
content = canvas;
canvas.draw.connect ((ctx, width, height) => {
draw (ctx, width, height);
return true;
});
}

Expand Down
163 changes: 163 additions & 0 deletions lib/Drawing/Canvas.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/*
* Copyright 2024 elementary, Inc. (https://elementary.io)
* SPDX-License-Identifier: LGPL-2.0-or-later
*/

#if HAS_MUTTER46
public class Gala.Drawing.Canvas : GLib.Object, Clutter.Content {
private int width = -1;
private int height = -1;
private float scale_factor = 1.0f;

private Cogl.Texture? texture = null;
private Cogl.Bitmap? bitmap = null;

private bool dirty = false;

public signal void draw (Cairo.Context cr, int width, int height);

private void emit_draw () requires (width > 0 && height > 0) {
dirty = true;
int real_width = (int) Math.ceilf (width * scale_factor);
int real_height = (int) Math.ceilf (height * scale_factor);
if (bitmap == null) {
unowned Cogl.Context ctx = Clutter.get_default_backend ().get_cogl_context ();
bitmap = new Cogl.Bitmap.with_size (ctx, real_width, real_height, Cogl.PixelFormat.CAIRO_ARGB32_COMPAT);
}

unowned Cogl.Buffer? buffer = bitmap.get_buffer ();
if (buffer == null) {
return;
}

buffer.set_update_hint (Cogl.BufferUpdateHint.DYNAMIC);
void* data = buffer.map (Cogl.BufferAccess.READ_WRITE, Cogl.BufferMapHint.DISCARD);
Cairo.ImageSurface surface;
bool mapped_buffer;
if (data != null) {
var bitmap_stride = bitmap.get_rowstride ();
surface = new Cairo.ImageSurface.for_data ((uchar[]) data, Cairo.Format.ARGB32, real_width, real_height, bitmap_stride);
mapped_buffer = true;
} else {
surface = new Cairo.ImageSurface (Cairo.Format.ARGB32, real_width, real_height);
mapped_buffer = false;
}

surface.set_device_scale (scale_factor, scale_factor);
var cr = new Cairo.Context (surface);
draw ((owned) cr, width, height);

if (mapped_buffer) {
buffer.unmap ();
} else {
int size = surface.get_stride () * height;
buffer.set_data (0, surface.get_data (), size);
}
}

public bool get_preferred_size (out float out_width, out float out_height) {
if (width < 0 || width < 0) {
out_width = 0;
out_height = 0;
return false;
}

out_width = Math.ceilf (width * scale_factor);
out_height = Math.ceilf (height * scale_factor);

return true;
}

public void invalidate () {
if (width < 0 || height < 0) {
return;
}

emit_draw ();
}

public void invalidate_size () { }

public void paint_content (Clutter.Actor actor, Clutter.PaintNode root, Clutter.PaintContext paint_context) {
if (bitmap == null) {
return;
}

if (dirty) {
texture = null;
}

if (texture == null) {
texture = new Cogl.Texture2D.from_bitmap (bitmap);
}

if (texture == null) {
return;
}

var node = actor.create_texture_paint_node (texture);
root.add_child (node);

dirty = false;
}

public void set_size (int new_width, int new_height) requires (new_width >= -1 && new_height >= -1) {
if (new_width == width && new_height == height) {
return;
}

width = new_width;
height = new_height;

invalidate ();
}

public void set_scale_factor (float new_scale_factor) requires (new_scale_factor > 0.0f) {
if (new_scale_factor != scale_factor) {
scale_factor = new_scale_factor;

invalidate ();
}
}
}
#else
public class Gala.Drawing.Canvas : GLib.Object, Clutter.Content {
public Clutter.Canvas canvas;

construct {
canvas = new Clutter.Canvas ();
canvas.draw.connect (on_draw);
}

public signal void draw (Cairo.Context cr, int width, int height);

public bool get_preferred_size (out float out_width, out float out_height) {
return canvas.get_preferred_size (out out_width, out out_height);
}

public void invalidate () {
canvas.invalidate ();
}

public void invalidate_size () {
canvas.invalidate_size ();
}

public void paint_content (Clutter.Actor actor, Clutter.PaintNode root, Clutter.PaintContext paint_context) {
canvas.paint_content (actor, root, paint_context);
}

public void set_size (int new_width, int new_height) requires (new_width >= -1 && new_height >= -1) {
canvas.set_size (new_width, new_height);
}

public void set_scale_factor (float new_scale_factor) requires (new_scale_factor > 0.0f) {
canvas.set_scale_factor (new_scale_factor);
}

private bool on_draw (Cairo.Context cr, int width, int height) {
draw (cr, width, height);
return true;
}
}
#endif
4 changes: 2 additions & 2 deletions lib/ShadowEffect.vala
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ public class Gala.ShadowEffect : Clutter.Effect {
pipeline.set_layer_texture (0, shadow);
}

var opacity = actor.get_paint_opacity () * shadow_opacity / 255;
var alpha = Cogl.Color.from_4ub (255, 255, 255, opacity);
var opacity = actor.get_paint_opacity () * shadow_opacity / 255.0f;
var alpha = Cogl.Color.from_4f (1.0f, 1.0f, 1.0f, opacity / 255.0f);
alpha.premultiply ();

pipeline.set_color (alpha);
Expand Down
42 changes: 24 additions & 18 deletions lib/Utils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,30 @@ namespace Gala {
}
}

unowned Meta.Group group = window.get_group ();
if (group != null) {
var group_windows = group.list_windows ();
group_windows.foreach ((window) => {
if (window.get_window_type () != Meta.WindowType.NORMAL) {
return;
}

if (window_to_desktop_cache[window] != null) {
desktop_app = window_to_desktop_cache[window];
}
});

if (desktop_app != null) {
var icon = get_icon_for_desktop_app_info (desktop_app, icon_size, scale);
if (icon != null) {
window_to_desktop_cache[window] = desktop_app;
return icon;
if (window.get_client_type () == Meta.WindowClientType.X11) {
#if HAS_MUTTER46
unowned Meta.Group group = window.x11_get_group ();
#else
unowned Meta.Group group = window.get_group ();
#endif
if (group != null) {
var group_windows = group.list_windows ();
group_windows.foreach ((window) => {
if (window.get_window_type () != Meta.WindowType.NORMAL) {
return;
}

if (window_to_desktop_cache[window] != null) {
desktop_app = window_to_desktop_cache[window];
}
});

if (desktop_app != null) {
var icon = get_icon_for_desktop_app_info (desktop_app, icon_size, scale);
if (icon != null) {
window_to_desktop_cache[window] = desktop_app;
return icon;
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ gala_lib_sources = files(
'Constants.vala',
'DragDropAction.vala',
'Drawing/BufferSurface.vala',
'Drawing/Canvas.vala',
'Drawing/Color.vala',
'Drawing/Utilities.vala',
'Plugin.vala',
Expand Down
8 changes: 7 additions & 1 deletion src/Dialogs.vala
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,14 @@ namespace Gala {

if (parent != null) {
if (parent.get_client_type () == Meta.WindowClientType.X11) {
//TODO: wayland support
#if HAS_MUTTER46
unowned Meta.Display display = parent.get_display ();
unowned Meta.X11Display x11display = display.get_x11_display ();
parent_handler = "x11:%x".printf ((uint) x11display.lookup_xwindow (parent));
#else
parent_handler = "x11:%x".printf ((uint) parent.get_xwindow ());
#endif
//TODO: wayland support
}

app_id = parent.get_sandboxed_app_id () ?? "";
Expand Down
4 changes: 4 additions & 0 deletions src/HotCorners/HotCorner.vala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ private class Gala.Barrier : Meta.Barrier {
public double pressure_y { get; set; default = 0; }

public Barrier (Meta.Display display, int x1, int y1, int x2, int y2, Meta.BarrierDirection directions) {
#if HAS_MUTTER46
Object (x1: x1, y1: y1, x2: x2, y2: y2, directions: directions);
#else
Object (display: display, x1: x1, y1: y1, x2: x2, y2: y2, directions: directions);
#endif
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/KeyboardManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ public class Gala.KeyboardManager : Object {

[CCode (instance_pos = -1)]
public static bool handle_modifiers_accelerator_activated (Meta.Display display, bool backward) {
#if HAS_MUTTER46
display.get_compositor ().backend.ungrab_keyboard (display.get_current_time ());
#else
display.ungrab_keyboard (display.get_current_time ());
#endif

var sources = settings.get_value ("sources");
if (!sources.is_of_type (sources_variant_type)) {
Expand Down Expand Up @@ -116,7 +120,12 @@ public class Gala.KeyboardManager : Object {
var variant = string.joinv (",", variants);
var options = string.joinv (",", xkb_options);

#if HAS_MUTTER46
//TODO: add model support
display.get_context ().get_backend ().set_keymap (layout, variant, options, "");
#else
display.get_context ().get_backend ().set_keymap (layout, variant, options);
#endif
} else if (key == "current") {
display.get_context ().get_backend ().lock_layout_group (settings.get_uint ("current"));
}
Expand Down
4 changes: 4 additions & 0 deletions src/ScreenshotManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ namespace Gala {
var flash_actor = new Clutter.Actor ();
flash_actor.set_size (width, height);
flash_actor.set_position (x, y);
#if HAS_MUTTER46
flash_actor.set_background_color (Clutter.Color.from_pixel (0xffffffffu));
#else
flash_actor.set_background_color (Clutter.Color.get_static (Clutter.StaticColor.WHITE));
#endif
flash_actor.set_opacity (0);
flash_actor.transitions_completed.connect ((actor) => {
wm.ui_group.remove_child (actor);
Expand Down
7 changes: 2 additions & 5 deletions src/Widgets/IconGroup.vala
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ namespace Gala {
add_child (icon_container);

resize ();

#if HAS_MUTTER46
icon_container.child_removed.connect_after (redraw);
#else
Expand Down Expand Up @@ -228,8 +227,8 @@ namespace Gala {
cr,
0,
0,
(int) width,
(int) height,
width,
height,
InternalUtils.scale_to_int (5, scale_factor)
);

Expand Down Expand Up @@ -382,8 +381,6 @@ namespace Gala {
y += InternalUtils.scale_to_int (size, scale_factor) + spacing;
}
}

return;
}

private Clutter.Actor? drag_begin (float click_x, float click_y) {
Expand Down
4 changes: 4 additions & 0 deletions src/Widgets/WindowSwitcher/WindowSwitcher.vala
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ public class Gala.WindowSwitcher : CanvasActor {

container = new Clutter.Actor () {
reactive = true,
#if HAS_MUTTER46
layout_manager = new Clutter.FlowLayout (Clutter.Orientation.HORIZONTAL)
#else
layout_manager = new Clutter.FlowLayout (Clutter.FlowOrientation.HORIZONTAL)
#endif
};

caption = new Clutter.Text () {
Expand Down
2 changes: 1 addition & 1 deletion src/Widgets/WorkspaceClone.vala
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ namespace Gala {
debug (e.message);
}

var color = Cogl.Color.from_4ub (255, 255, 255, 25);
var color = Cogl.Color.from_4f (1.0f, 1.0f, 1.0f, 25.0f / 255.0f);
color.premultiply ();

pipeline.set_color (color);
Expand Down
Loading

0 comments on commit 5644c66

Please sign in to comment.