Skip to content

Commit

Permalink
Merge pull request #12683 from keymanapp/feat/linux/12498_blob
Browse files Browse the repository at this point in the history
feat(linux): use new km_core_keyboard_load_from_blob API method 🎼
  • Loading branch information
ermshiperete authored Nov 25, 2024
2 parents 00ece2e + 97c99cc commit 63b83bd
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions linux/ibus-keyman/src/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*
*/

#include <errno.h>
#include <ibus.h>
#include <string.h>
#include <stdio.h>
Expand Down Expand Up @@ -498,10 +499,10 @@ ibus_keyman_engine_constructor(

g_strfreev(split_name);

g_autofree gchar *kmx_file = g_path_get_basename(abs_kmx_path);
p = rindex(kmx_file, '.'); // get id to use as dbus service name
g_autofree gchar *kmx_filename = g_path_get_basename(abs_kmx_path);
p = rindex(kmx_filename, '.'); // get id to use as dbus service name
if (p) {
keyman->kb_name = g_strndup(kmx_file, p-kmx_file);
keyman->kb_name = g_strndup(kmx_filename, p-kmx_filename);
p = rindex(abs_kmx_path, '.');
if (p)
{
Expand All @@ -516,7 +517,42 @@ ibus_keyman_engine_constructor(

km_core_status status;

status = km_core_keyboard_load(abs_kmx_path, &(keyman->keyboard));
FILE* kmx_file = fopen(abs_kmx_path, "rb");
if (!kmx_file) {
g_warning("%s: problem opening kmx_file %s. (error: %s).", __FUNCTION__, abs_kmx_path, strerror(errno));
return NULL;
}

if (fseek(kmx_file, 0, SEEK_END) < 0) {
g_warning("%s: problem seeking to end of kmx_file %s (error: %s).", __FUNCTION__, abs_kmx_path, strerror(errno));
fclose(kmx_file);
return NULL;
}
long length = ftell(kmx_file);
if (length < 0) {
g_warning("%s: problem determining length of kmx_file %s (error: %s).", __FUNCTION__, abs_kmx_path, strerror(errno));
fclose(kmx_file);
return NULL;
}
rewind(kmx_file);
void* buffer = malloc(length);
if (!buffer) {
g_warning("%s: problem allocating buffer for reading kmx_file %s (error: %s).", __FUNCTION__, abs_kmx_path, strerror(errno));
fclose(kmx_file);
return NULL;
}
if (fread(buffer, 1, length, kmx_file) != length) {
g_warning("%s: problem reading entire kmx_file %s (error: %s).", __FUNCTION__, abs_kmx_path, strerror(errno));
fclose(kmx_file);
free(buffer);
return NULL;
}
fclose(kmx_file);

status = km_core_keyboard_load_from_blob(abs_kmx_path, buffer, length,
&(keyman->keyboard));

free(buffer);

if (status != KM_CORE_STATUS_OK) {
g_warning("%s: problem loading km_core_keyboard %s. Status is %u.", __FUNCTION__, abs_kmx_path, status);
Expand Down

0 comments on commit 63b83bd

Please sign in to comment.