/* ***************************************************************************** * manage/handle.c -- create, free a handle. * Copyright (C) 2017 Thomas "Cakeisalie5" Touhey * * This file is part of libg1m. * libg1m is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 3.0 of the License, * or (at your option) any later version. * * libg1m is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with libg1m; if not, see . * ************************************************************************** */ #include #include #define mkhandle() \ if (!(*(h) = malloc(sizeof(g1m_handle_t)))) return (g1m_error_alloc); \ g1m_handle_t *handle = *h; memset(handle, 0, sizeof(g1m_handle_t)) /* ************************************************************************** */ /* Make a handle */ /* ************************************************************************** */ /** * g1m_make_picture: * Make a picture handle. * * @arg handle the handle to make. * @arg width the picture width. * @arg height the picture height. * @return the error code (0 if ok). */ int g1m_make_picture(g1m_handle_t **h, unsigned int width, unsigned int height) { /* make the handle */ mkhandle(); handle->g1m_handle_type = g1m_type_picture; handle->g1m_handle_width = width; handle->g1m_handle_height = height; handle->g1m_handle_pixels = alloc_pixels(width, height); if (!handle->g1m_handle_pixels) { free(*h); *h = NULL; return (g1m_error_alloc); } prepare_pixels(handle->g1m_handle_pixels, width, height) /* everything went well! */ return (0); } /** * g1m_make_mcs: * Make an MCS file. * * @arg h pointer to the handle to create. * @arg count the number of slots in the index. * @return the error code (0 if ok). */ int g1m_make_mcs(g1m_handle_t **h, int count) { /* make the handle */ mkhandle(); handle->g1m_handle_type = g1m_type_mcs; handle->g1m_handle_platform = g1m_platform_fx; handle->g1m_handle_count = 0; handle->g1m_handle__size = 0; handle->g1m_handle_files = NULL; /* allocate space */ if (count) { handle->g1m_handle_files = malloc(sizeof(g1m_mcsfile_t*) * count); if (!handle->g1m_handle_files) goto fail; memset(handle->g1m_handle_files, 0, sizeof(g1m_mcsfile_t*) * count); handle->g1m_handle__size = count; } /* no error */ return (0); fail: g1m_free(*h); *h = NULL; return (g1m_error_alloc); } /** * g1m_make_fkey: * Make a Function Keys handle. * * @arg h pointer to the handle to create. * @arg pf the platform. * @arg count the number of slots in the index. * @return the error code (0 if ok). */ int g1m_make_fkey(g1m_handle_t **h, g1m_platform_t pf, int count) { /* make the handle */ mkhandle(); handle->g1m_handle_type = g1m_type_fkey; handle->g1m_handle_platform = pf; handle->g1m_handle_count = 0; handle->g1m_handle__size = 0; handle->g1m_handle_fkeys = NULL; /* allocate index */ if (count) { handle->g1m_handle_fkeys = malloc(sizeof(uint32_t**) * count); if (!handle->g1m_handle_fkeys) goto fail; memset(handle->g1m_handle_fkeys, 0, sizeof(uint32_t**) * count); handle->g1m_handle__size = count; } /* no error */ return (0); fail: g1m_free(*h); *h = NULL; return (g1m_error_alloc); } /** * g1m_make_lang: * Make a language handle. * * @arg h pointer to the handle to create. * @arg platform the platform. * @arg count the number of slots in the index. * @return the error code (0 if ok). */ int g1m_make_lang(g1m_handle_t **h, g1m_platform_t platform, int count) { /* make the handle */ mkhandle(); handle->g1m_handle_type = g1m_type_lang; handle->g1m_handle_platform = platform; handle->g1m_handle_count = 0; handle->g1m_handle__size = 0; handle->g1m_handle_messages = NULL; /* allocate index */ if (count) { handle->g1m_handle_messages = malloc(sizeof(char*) * count); if (!handle->g1m_handle_messages) goto fail; memset(handle->g1m_handle_messages, 0, sizeof(char*) * count); handle->g1m_handle__size = count; } /* no error */ return (0); fail: g1m_free(*h); *h = NULL; return (g1m_error_alloc); } /** * g1m_make_addin: * Make an add-in handle. * * @arg h pointer to the handle to create. * @arg platform the platform for which to make the add-in. * @arg size the code size. * @arg name the name. * @arg internal the internal name. * @arg version the version of the add-in. * @arg created the creation date of the add-in. * @return the error code (0 if ok). */ int g1m_make_addin(g1m_handle_t **h, g1m_platform_t platform, size_t size, const char *name, const char *internal, const g1m_version_t *version, const time_t *created) { *h = NULL; /* make checks */ if (platform != g1m_platform_fx && platform != g1m_platform_cg && platform != g1m_platform_cp) return (g1m_error_op); if (platform == g1m_platform_fx && size > 512 * 1024) return (g1m_error_op); if (!isupper(name[0]) || internal[0] != '@' || !isupper(internal[1])) return (g1m_error_op); /* make the handle */ mkhandle(); handle->g1m_handle_type = g1m_type_addin; handle->g1m_handle_platform = platform; handle->g1m_handle_version = *version; handle->g1m_handle_creation_date = *created; handle->g1m_handle_size = size; /* allocate the content */ handle->g1m_handle_content = malloc(size); if (!handle->g1m_handle_content) goto fail; /* check the platform */ unsigned int width, height; int titlesize = 8, intsize = 8; switch (platform) { case g1m_platform_fx: width = G1A_ICON_WIDTH; height = G1A_ICON_HEIGHT; break; case g1m_platform_cp: width = C1A_ICON_WIDTH; height = C1A_ICON_HEIGHT; titlesize = 16; break; default: /* case g1m_platform_cg: */ width = G3A_ICON_WIDTH; height = G3A_ICON_HEIGHT; titlesize = 16; break; } /* copy the addin title */ int i; for (i = 0; (isupper(name[i]) || isdigit(name[i])) && i < titlesize; i++) handle->g1m_handle_title[i] = name[i]; handle->g1m_handle_title[i] = 0; if (!handle->g1m_handle_title[0]) strcpy(handle->g1m_handle_title, "@ADDIN"); /* copy the internal name */ handle->g1m_handle_intname[0] = '@'; for (i = 1; (isupper(internal[i]) || isdigit(internal[i])) && i < intsize; i++) handle->g1m_handle_intname[i] = internal[i]; handle->g1m_handle_intname[i] = 0; /* allocate pictures */ handle->g1m_handle_icon_unsel = alloc_pixels(width, height); if (!handle->g1m_handle_icon_unsel) goto fail; handle->g1m_handle_icon_sel = alloc_pixels(width, height); if (!handle->g1m_handle_icon_sel) goto fail; /* prepare pictures */ handle->g1m_handle_width = width; handle->g1m_handle_height = height; prepare_pixels(handle->g1m_handle_icon_unsel, width, height) prepare_pixels(handle->g1m_handle_icon_sel, width, height) /* end my suffering */ return (0); fail: g1m_free(*h); *h = NULL; return (g1m_error_alloc); } /* ************************************************************************** */ /* Free a handle */ /* ************************************************************************** */ /** * g1m_free: * Free a handle and its data. * * @arg handle the handle. */ void g1m_free(g1m_handle_t *handle) { int i; /* check if there is something to free */ if (!handle) return ; /* addin time! */ if (handle->g1m_handle_type & g1m_type_addin) { free(handle->g1m_handle_content); free(handle->g1m_handle_icon_unsel); free(handle->g1m_handle_icon_sel); } /* mcs time! */ if (handle->g1m_handle_type & g1m_type_mcs) { /* check if mcs */ if (!handle->g1m_handle_files) return ; /* foreach file in mcs */ g1m_mcsfile_t **files = handle->g1m_handle_files; int file_count = handle->g1m_handle_count; for (i = 0; i < file_count; i++) { /* free the file if exists */ if (files[i]) g1m_free_mcsfile(files[i]); } free(handle->g1m_handle_files); handle->g1m_handle_files = NULL; } /* messages time! */ if (handle->g1m_handle_type & g1m_type_lang && handle->g1m_handle_messages) { for (i = 0; i < handle->g1m_handle_count; i++) free(handle->g1m_handle_messages[i]); free(handle->g1m_handle_messages); } /* function keys time! */ if (handle->g1m_handle_type & g1m_type_fkey && handle->g1m_handle_fkeys) { for (i = 0; i < handle->g1m_handle_count; i++) free(handle->g1m_handle_fkeys[i]); free(handle->g1m_handle_fkeys); } /* picture time! */ if (handle->g1m_handle_type & g1m_type_picture) free(handle->g1m_handle_pixels); /* e-activities time! */ if (handle->g1m_handle_type & g1m_type_eact) g1m_free_line_content(handle->g1m_handle_line); /* free the handle itself! */ free(handle); }