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 Feb 22, 2024
1 parent f9b6ce2 commit a91b9ae
Show file tree
Hide file tree
Showing 25 changed files with 433 additions and 121 deletions.
164 changes: 164 additions & 0 deletions lib/Drawing/Canvas.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
* 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;

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, scale_factor);

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 ();
}
}


public virtual signal void draw (Cairo.Context cr, int width, int height, float scale_factor);
}
#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 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, canvas.get_scale_factor ());
return true;
}

public virtual signal void draw (Cairo.Context cr, int width, int height, float scale_factor);
}
#endif
4 changes: 4 additions & 0 deletions lib/Utils.vala
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,11 @@ namespace Gala {
}
}

#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) => {
Expand Down
1 change: 1 addition & 0 deletions lib/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,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
10 changes: 3 additions & 7 deletions plugins/pip/SelectionArea.vala
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ public class Gala.Plugins.PIP.SelectionArea : Clutter.Actor {
width = screen_width;
height = screen_height;

var canvas = new Clutter.Canvas ();
var canvas = new Gala.Drawing.Canvas ();
canvas.set_size (screen_width, screen_height);
canvas.draw.connect (draw_area);
set_content (canvas);

canvas.invalidate ();
}

#if HAS_MUTTER45
Expand Down Expand Up @@ -159,7 +157,7 @@ public class Gala.Plugins.PIP.SelectionArea : Clutter.Actor {
height = (start_point.y - end_point.y).abs ();
}

private bool draw_area (Cairo.Context ctx) {
private void draw_area (Cairo.Context ctx, int width, int height, float scale_factor) {
ctx.save ();

ctx.set_operator (Cairo.Operator.CLEAR);
Expand All @@ -168,7 +166,7 @@ public class Gala.Plugins.PIP.SelectionArea : Clutter.Actor {
ctx.restore ();

if (!dragging) {
return true;
return;
}

int x, y, w, h;
Expand All @@ -182,7 +180,5 @@ public class Gala.Plugins.PIP.SelectionArea : Clutter.Actor {
ctx.set_source_rgb (0.7, 0.7, 0.7);
ctx.set_line_width (1.0);
ctx.stroke ();

return true;
}
}
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
7 changes: 6 additions & 1 deletion src/KeyboardManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class Gala.KeyboardManager : Object {

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

var sources = settings.get_value ("sources");
if (!sources.is_of_type (sources_variant_type)) {
Expand Down Expand Up @@ -116,7 +116,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
Loading

0 comments on commit a91b9ae

Please sign in to comment.