drop GTK < 4.14 ifdefs

This commit is contained in:
Julian Winkler 2024-12-22 10:37:47 +01:00
parent 260821d68c
commit 0dfcb9324f
6 changed files with 0 additions and 229 deletions

View file

@ -38,14 +38,6 @@ marshal_files = gnome.genmarshal('marshal',
internal: true,
)
wl_mod = import('unstable-wayland')
xml = wl_mod.find_protocol('linux-dmabuf', state: 'unstable', version: 1)
linux_dmabuf = wl_mod.scan_xml(xml)
xml = wl_mod.find_protocol('viewporter')
viewporter = wl_mod.scan_xml(xml)
mpris = gnome.gdbus_codegen('mpris-dbus',
'src/api-impl-jni/media/org.mpris.MediaPlayer2.xml',
interface_prefix: 'org.mpris')
@ -140,8 +132,6 @@ libtranslationlayer_so = shared_library('translation_layer_main', [
'src/api-impl-jni/widgets/android_widget_Spinner.c',
'src/api-impl-jni/widgets/android_widget_SeekBar.c',
'src/api-impl-jni/widgets/android_widget_TextView.c',
linux_dmabuf,
viewporter,
mpris,
portal_openuri,
] + marshal_files,

View file

@ -207,7 +207,6 @@ struct filechooser_callback_data { jobject activity; jint request_code; jint act
#define RESULT_OK -1
#define RESULT_CANCELED 0
#if GTK_CHECK_VERSION(4, 10, 0)
static void file_dialog_callback(GObject* source_object, GAsyncResult* res, gpointer data) {
struct filechooser_callback_data *d = data;
GtkFileDialog *dialog = GTK_FILE_DIALOG(source_object);
@ -235,63 +234,24 @@ static void file_dialog_callback(GObject* source_object, GAsyncResult* res, gpoi
}
free(d);
}
#else
static void on_filechooser_response(GtkNativeDialog *native, int response, struct filechooser_callback_data *data)
{
JNIEnv *env = get_jni_env();
jmethodID fileChooserResultCallback = _METHOD(handle_cache.activity.class, "fileChooserResultCallback", "(IIILjava/lang/String;)V");
GtkFileChooser *chooser = GTK_FILE_CHOOSER(native);
GtkFileChooserAction action = gtk_file_chooser_get_action(chooser);
if (response == GTK_RESPONSE_ACCEPT) {
GFile *file = gtk_file_chooser_get_file(chooser);
char *uri = g_file_get_uri(file);
(*env)->CallVoidMethod(env, data->activity, fileChooserResultCallback, data->request_code, RESULT_OK, action, _JSTRING(uri));
if ((*env)->ExceptionCheck(env))
(*env)->ExceptionDescribe(env);
g_free(uri);
g_object_unref(file);
} else {
(*env)->CallVoidMethod(env, data->activity, fileChooserResultCallback, data->request_code, RESULT_CANCELED, action, NULL);
}
g_object_unref(native);
_UNREF(data->activity);
free(data);
}
#endif
JNIEXPORT void JNICALL Java_android_app_Activity_nativeFileChooser(JNIEnv *env, jobject this, jint action, jstring type_jstring, jstring filename_jstring, jint request_code)
{
const char *chooser_title = ((char *[]){"Open File", "Save File", "Select Folder"})[action];
#if GTK_CHECK_VERSION(4, 10, 0)
GtkFileDialog *dialog = gtk_file_dialog_new();
gtk_file_dialog_set_title(GTK_FILE_DIALOG(dialog), chooser_title);
#else
GtkFileChooserNative *native = gtk_file_chooser_native_new(chooser_title, window, action, NULL, NULL);
#endif
const char *type = type_jstring ? (*env)->GetStringUTFChars(env, type_jstring, NULL) : NULL;
if (type && !strchr(type, '*')) {
GtkFileFilter *filter = gtk_file_filter_new();
gtk_file_filter_add_mime_type(filter, type);
gtk_file_filter_set_name(filter, type);
#if GTK_CHECK_VERSION(4, 10, 0)
gtk_file_dialog_set_default_filter(GTK_FILE_DIALOG(dialog), filter);
#else
gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(native), filter);
#endif
(*env)->ReleaseStringUTFChars(env, type_jstring, type);
}
const char *filename = filename_jstring ? (*env)->GetStringUTFChars(env, filename_jstring, NULL) : NULL;
if (filename) {
#if GTK_CHECK_VERSION(4, 10, 0)
gtk_file_dialog_set_initial_name(GTK_FILE_DIALOG(dialog), filename);
#else
gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(native), filename);
#endif
(*env)->ReleaseStringUTFChars(env, filename_jstring, filename);
}
@ -299,17 +259,12 @@ JNIEXPORT void JNICALL Java_android_app_Activity_nativeFileChooser(JNIEnv *env,
callback_data->activity = _REF(this);
callback_data->request_code = request_code;
callback_data->action = action;
#if GTK_CHECK_VERSION(4, 10, 0)
void (* const file_dialog_functions[])(GtkFileDialog *, GtkWindow *, GCancellable *, GAsyncReadyCallback, gpointer) = {
gtk_file_dialog_open,
gtk_file_dialog_save,
gtk_file_dialog_select_folder,
};
file_dialog_functions[action](dialog, window, NULL, file_dialog_callback, callback_data);
#else
g_signal_connect (native, "response", G_CALLBACK(on_filechooser_response), callback_data);
gtk_native_dialog_show (GTK_NATIVE_DIALOG (native));
#endif
}
JNIEXPORT jboolean JNICALL Java_android_app_Activity_isInMultiWindowMode(JNIEnv *env, jobject this)

View file

@ -8,11 +8,9 @@
#include <libavutil/hwcontext.h>
#include <libavutil/pixfmt.h>
#include <stdio.h>
#include <EGL/eglplatform.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <gdk/wayland/gdkwayland.h>
#include <libavcodec/avcodec.h>
#include <libavutil/hwcontext_drm.h>
@ -22,11 +20,6 @@
#include <libswscale/swscale.h>
#include <stdlib.h>
#if !GTK_CHECK_VERSION(4, 14, 0)
#include <wayland-client.h>
#include "linux-dmabuf-unstable-v1-client-protocol.h"
#include "viewporter-client-protocol.h"
#endif
#include "jni.h"
#include "../generated_headers/android_media_MediaCodec.h"
@ -44,14 +37,6 @@ struct ATL_codec_context {
struct {
struct SwsContext *sws; // for software decoding
SurfaceViewWidget *surface_view_widget;
#if !GTK_CHECK_VERSION(4, 14, 0)
struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf_v1;
struct wp_viewporter *wp_viewporter;
struct wp_viewport *viewport;
struct ANativeWindow *native_window;
int surface_width;
int surface_height;
#endif
} video;
};
};
@ -149,13 +134,8 @@ struct render_frame_data {
AVFrame *frame;
GdkTexture *texture; // for software decoding
SurfaceViewWidget *surface_view_widget;
#if !GTK_CHECK_VERSION(4, 14, 0)
struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf_v1;
struct ANativeWindow *native_window;
#endif
};
#if GTK_CHECK_VERSION(4, 14, 0)
static void handle_dmabuftexture_destroy(void *data)
{
AVFrame *drm_frame = data;
@ -203,89 +183,6 @@ static GdkTexture *import_drm_frame_desc_as_texture(const AVDRMFrameDescriptor *
return texture;
}
#else // GTK_CHECK_VERSION(4, 14, 0)
static void handle_global(void *data, struct wl_registry *registry,
uint32_t name, const char *interface, uint32_t version)
{
struct ATL_codec_context *ctx = data;
if (strcmp(interface, zwp_linux_dmabuf_v1_interface.name) == 0) {
ctx->video.zwp_linux_dmabuf_v1 =
wl_registry_bind(registry, name, &zwp_linux_dmabuf_v1_interface, 2);
} else if (strcmp(interface, wp_viewporter_interface.name) == 0) {
ctx->video.wp_viewporter =
wl_registry_bind(registry, name, &wp_viewporter_interface, 1);
}
}
static void handle_global_remove(void *data, struct wl_registry *registry,
uint32_t name)
{
// This space is intentionally left blank
}
static const struct wl_registry_listener registry_listener = {
.global = handle_global,
.global_remove = handle_global_remove,
};
static struct wl_buffer *import_drm_frame_desc(struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf_v1,
const AVDRMFrameDescriptor *drm_frame_desc, int width, int height)
{
// VA-API drivers may use separate layers with one plane each, or a single
// layer with multiple planes. We need to handle both.
uint32_t drm_format = get_drm_frame_format(drm_frame_desc);
if (drm_format == DRM_FORMAT_INVALID) {
fprintf(stderr, "Failed to get DRM frame format\n");
return NULL;
}
// fprintf(stderr, "DRM format: 0x%X\n", drm_format);
struct zwp_linux_buffer_params_v1 *dmabuf_params =
zwp_linux_dmabuf_v1_create_params(zwp_linux_dmabuf_v1);
int k = 0;
for (int i = 0; i < drm_frame_desc->nb_layers; i++) {
const AVDRMLayerDescriptor *drm_layer = &drm_frame_desc->layers[i];
for (int j = 0; j < drm_layer->nb_planes; j++) {
const AVDRMPlaneDescriptor *drm_plane = &drm_layer->planes[j];
const AVDRMObjectDescriptor *drm_object =
&drm_frame_desc->objects[drm_plane->object_index];
uint32_t modifier_hi = drm_object->format_modifier >> 32;
uint32_t modifier_lo = drm_object->format_modifier & 0xFFFFFFFF;
zwp_linux_buffer_params_v1_add(dmabuf_params, drm_object->fd, k,
drm_plane->offset, drm_plane->pitch, modifier_hi, modifier_lo);
k++;
}
}
return zwp_linux_buffer_params_v1_create_immed(dmabuf_params,
width, height, drm_format, 0);
}
static void handle_buffer_release(void *data, struct wl_buffer *buffer)
{
AVFrame *frame = data;
av_frame_free(&frame);
wl_buffer_destroy(buffer);
}
static const struct wl_buffer_listener buffer_listener = {
.release = handle_buffer_release,
};
static void on_resize(GtkWidget* widget, gint width, gint height, struct ATL_codec_context *ctx)
{
ctx->video.surface_width = gtk_widget_get_width(widget);
ctx->video.surface_height = gtk_widget_get_height(widget);
wp_viewport_set_destination(ctx->video.viewport, ctx->video.surface_width, ctx->video.surface_height);
}
#endif // GTK_CHECK_VERSION(4, 14, 0)
JNIEXPORT void JNICALL Java_android_media_MediaCodec_native_1configure_1video(JNIEnv *env, jobject this, jlong codec, jobject csd0, jobject csd1, jobject surface_obj)
{
struct ATL_codec_context *ctx = _PTR(codec);
@ -354,12 +251,6 @@ JNIEXPORT void JNICALL Java_android_media_MediaCodec_native_1configure_1video(JN
SurfaceViewWidget *surface_view_widget = SURFACE_VIEW_WIDGET(gtk_widget_get_first_child(_PTR(_GET_LONG_FIELD(surface_obj, "widget"))));
ctx->video.surface_view_widget = surface_view_widget;
#if !GTK_CHECK_VERSION(4, 14, 0)
struct ANativeWindow *native_window = ANativeWindow_fromSurface(env, surface_obj);
ctx->video.native_window = native_window;
ctx->video.surface_width = gtk_widget_get_width(native_window->surface_view_widget);
ctx->video.surface_height = gtk_widget_get_height(native_window->surface_view_widget);
#endif
}
JNIEXPORT void JNICALL Java_android_media_MediaCodec_native_1start(JNIEnv *env, jobject this, jlong codec)
@ -389,24 +280,6 @@ JNIEXPORT void JNICALL Java_android_media_MediaCodec_native_1start(JNIEnv *env,
fprintf(stderr, "FFmpegDecoder error: Swresampler alloc fail\n");
}
swr_init(ctx->audio.swr);
} else if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) {
#if !GTK_CHECK_VERSION(4, 14, 0)
struct ANativeWindow *native_window = ctx->video.native_window;
struct wl_registry *registry = wl_display_get_registry(native_window->wayland_display);
wl_registry_add_listener(registry, &registry_listener, ctx);
wl_display_roundtrip(native_window->wayland_display);
wl_registry_destroy(registry);
if (ctx->video.zwp_linux_dmabuf_v1 == NULL || ctx->video.wp_viewporter == NULL) {
fprintf(stderr, "Missing zwp_linux_dmabuf_v1 or wp_viewporter support\n");
exit(1);
}
ctx->video.viewport = wp_viewporter_get_viewport(ctx->video.wp_viewporter, native_window->wayland_surface);
wp_viewport_set_destination(ctx->video.viewport, ctx->video.surface_width, ctx->video.surface_height);
g_signal_connect(native_window->surface_view_widget, "resize", G_CALLBACK(on_resize), ctx);
#endif
}
}
@ -515,24 +388,8 @@ static gboolean render_frame(void *data)
AVDRMFrameDescriptor *drm_frame_desc = (void *)drm_frame->data[0];
#if GTK_CHECK_VERSION(4, 14, 0)
GdkTexture *texture = import_drm_frame_desc_as_texture(drm_frame_desc, drm_frame->width, drm_frame->height, drm_frame);
surface_view_widget_set_texture(d->surface_view_widget, texture);
#else
struct wl_buffer *wl_buffer = import_drm_frame_desc(d->zwp_linux_dmabuf_v1,
drm_frame_desc, drm_frame->width, drm_frame->height);
if (!wl_buffer) {
exit(1);
}
wl_buffer_add_listener(wl_buffer, &buffer_listener, drm_frame);
struct ANativeWindow *native_window = d->native_window;
wl_surface_damage(native_window->wayland_surface, 0, 0, INT32_MAX, INT32_MAX);
wl_surface_attach(native_window->wayland_surface, wl_buffer, 0, 0);
wl_surface_commit(native_window->wayland_surface);
#endif
free(d);
return G_SOURCE_REMOVE;
@ -604,12 +461,7 @@ JNIEXPORT void JNICALL Java_android_media_MediaCodec_native_1releaseOutputBuffer
struct render_frame_data *data = malloc(sizeof(struct render_frame_data));
data->frame = frame;
#if GTK_CHECK_VERSION(4, 14, 0)
data->surface_view_widget = ctx->video.surface_view_widget;
#else
data->native_window = ctx->video.native_window;
data->zwp_linux_dmabuf_v1 = ctx->video.zwp_linux_dmabuf_v1;
#endif
g_idle_add(render_frame, data);
}
}
@ -617,12 +469,6 @@ JNIEXPORT void JNICALL Java_android_media_MediaCodec_native_1releaseOutputBuffer
JNIEXPORT void JNICALL Java_android_media_MediaCodec_native_1release(JNIEnv *env, jobject this, jlong codec)
{
struct ATL_codec_context *ctx = _PTR(codec);
#if !GTK_CHECK_VERSION(4, 14, 0)
if (ctx->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
struct ANativeWindow *native_window = ctx->video.native_window;
g_signal_handlers_disconnect_by_data(native_window->surface_view_widget, ctx);
}
#endif
if (ctx->codec->codec_type == AVMEDIA_TYPE_VIDEO && ctx->video.sws) {
sws_freeContext(ctx->video.sws);
}

View file

@ -332,11 +332,7 @@ JNIEXPORT void JNICALL Java_android_view_View_native_1setPadding(JNIEnv *env, jo
GtkCssProvider *css_provider = gtk_css_provider_new();
char *css_string = g_markup_printf_escaped("*{ padding-left: %dpx; padding-top: %dpx; padding-right: %dpx; padding-bottom: %dpx; }", left, top, right, bottom);
#if GTK_CHECK_VERSION(4, 12, 0)
gtk_css_provider_load_from_string(css_provider, css_string);
#else
gtk_css_provider_load_from_data(css_provider, css_string, strlen(css_string));
#endif
g_free(css_string);
gtk_style_context_add_provider(style_context, GTK_STYLE_PROVIDER(css_provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
@ -499,11 +495,7 @@ JNIEXPORT void JNICALL Java_android_view_View_setBackgroundColor(JNIEnv *env, jo
GtkCssProvider *css_provider = gtk_css_provider_new();
char *css_string = g_markup_printf_escaped("* { background-image: none; background-color: #%06x%02x; }", color & 0xFFFFFF, (color >> 24) & 0xFF);
#if GTK_CHECK_VERSION(4, 12, 0)
gtk_css_provider_load_from_string(css_provider, css_string);
#else
gtk_css_provider_load_from_data(css_provider, css_string, strlen(css_string));
#endif
g_free(css_string);
gtk_style_context_add_provider(style_context, GTK_STYLE_PROVIDER(css_provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

View file

@ -117,15 +117,7 @@ JNIEXPORT jlong JNICALL Java_android_view_SurfaceView_native_1constructor(JNIEnv
GtkWidget *wrapper = g_object_ref(wrapper_widget_new());
GtkWidget *dummy = surface_view_widget_new();
gtk_widget_set_name(dummy, "dummy widget for SurfaceView");
#if GTK_CHECK_VERSION(4, 14, 0)
GtkWidget *graphics_offload = gtk_graphics_offload_new(dummy);
#else
// use a dummy GtkBox, so that the SurfaceViewWidget also becomes a grand child of the WrapperWidget
GtkWidget *graphics_offload = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_widget_set_vexpand(dummy, TRUE);
gtk_widget_set_hexpand(dummy, TRUE);
gtk_widget_insert_before(dummy, graphics_offload, NULL);
#endif
wrapper_widget_set_child(WRAPPER_WIDGET(wrapper), graphics_offload);
wrapper_widget_set_jobject(WRAPPER_WIDGET(wrapper), env, this);
// TODO: is this correct for all usecases? how do we know when it's not?

View file

@ -64,11 +64,7 @@ JNIEXPORT void JNICALL Java_android_widget_TextView_native_1setTextColor(JNIEnv
GtkCssProvider *css_provider = gtk_css_provider_new();
char *css_string = g_markup_printf_escaped("* { color: #%06x%02x; }", color & 0xFFFFFF, (color >> 24) & 0xFF);
#if GTK_CHECK_VERSION(4, 12, 0)
gtk_css_provider_load_from_string(css_provider, css_string);
#else
gtk_css_provider_load_from_data(css_provider, css_string, strlen(css_string));
#endif
g_free(css_string);
gtk_style_context_add_provider(style_context, GTK_STYLE_PROVIDER(css_provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);