/* ***************************************************************************** * 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 /* ************************************************************************** */ /* 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_t **h, unsigned int width, unsigned int height) { /* allocate the handle */ *h = malloc(sizeof(g1m_t)); if (!*h) return (g1m_error_alloc); g1m_t *handle = *h; memset(handle, 0, sizeof(g1m_t)); /* allocate the pixels */ handle->width = width; handle->height = height; handle->pixels = alloc_pixels(width, height); if (!handle->pixels) { free(*h); *h = NULL; return (g1m_error_alloc); } prepare_pixels(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_t **h, int count) { /* allocate the handle */ *h = malloc(sizeof(g1m_t)); if (!*h) return (g1m_error_alloc); g1m_t *handle = *h; memset(handle, 0, sizeof(g1m_t)); /* initialize it */ handle->type = g1m_type_mcs; handle->platform = g1m_platform_fx; handle->count = 0; handle->_size = 0; handle->files = NULL; /* allocate space */ if (count) { handle->files = malloc(sizeof(g1m_mcsfile_t*) * count); if (!handle->files) goto fail; memset(handle->files, 0, sizeof(g1m_mcsfile_t*) * count); 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_t **h, g1m_platform_t pf, int count) { /* allocate the handle */ *h = malloc(sizeof(g1m_t)); if (!*h) return (g1m_error_alloc); g1m_t *handle = *h; memset(handle, 0, sizeof(g1m_t)); /* initialize it */ handle->type = g1m_type_fkey; handle->platform = pf; handle->count = 0; handle->_size = 0; handle->fkeys = NULL; /* allocate index */ if (count) { handle->fkeys = malloc(sizeof(uint32_t**) * count); if (!handle->fkeys) goto fail; memset(handle->fkeys, 0, sizeof(uint32_t**) * count); 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_t **h, g1m_platform_t platform, int count) { /* allocate the handle */ *h = malloc(sizeof(g1m_t)); if (!*h) return (g1m_error_alloc); g1m_t *handle = *h; memset(handle, 0, sizeof(g1m_t)); /* initialize it */ handle->type = g1m_type_lang; handle->platform = platform; handle->count = 0; handle->_size = 0; handle->messages = NULL; /* allocate index */ if (count) { handle->messages = malloc(sizeof(char*) * count); if (!handle->messages) goto fail; memset(handle->messages, 0, sizeof(char*) * count); 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 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_t **h, g1m_platform_t platform, const g1m_version_t *version, const time_t *created) { *h = NULL; /* check the platform */ if (platform != g1m_platform_fx && platform != g1m_platform_cg && platform != g1m_platform_cp) return (g1m_error_op); /* allocate the handle */ *h = malloc(sizeof(g1m_t)); if (!*h) return (g1m_error_alloc); g1m_t *handle = *h; memset(handle, 0, sizeof(g1m_t)); /* set basic options */ handle->type = g1m_type_addin; handle->platform = platform; handle->version = *version; handle->creation_date = *created; /* check the platform */ unsigned int width, height; 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; break; default: /* case g1m_platform_cg: */ width = G3A_ICON_WIDTH; height = G3A_ICON_HEIGHT; } /* allocate pictures */ handle->icon_unsel = alloc_pixels(width, height); if (!handle->icon_unsel) goto fail; handle->icon_sel = alloc_pixels(width, height); if (!handle->icon_sel) goto fail; /* prepare pictures */ prepare_pixels(handle->icon_unsel, width, height) prepare_pixels(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_t *handle) { /* check if there is something to free */ if (!handle) return ; /* addin time! */ if (handle->type & g1m_type_addin) { free(handle->icon_unsel); free(handle->icon_sel); } /* mcs time! */ if (handle->type & g1m_type_mcs) { /* check if mcs */ if (!handle->files) return ; /* foreach file in mcs */ g1m_mcsfile_t **files = handle->files; int file_count = handle->count; for (int i = 0; i < file_count; i++) { /* free the file if exists */ if (files[i]) g1m_free_mcsfile(files[i]); } free(handle->files); handle->files = NULL; } /* messages time! */ if (handle->type & g1m_type_lang && handle->messages) { for (int i = 0; i < handle->count; i++) free(handle->messages[i]); free(handle->messages); } /* function keys time! */ if (handle->type & g1m_type_fkey && handle->fkeys) { for (int i = 0; i < handle->count; i++) free(handle->fkeys[i]); free(handle->fkeys); } /* picture time! */ if (handle->type & g1m_type_picture) free(handle->pixels); /* e-activities time! */ if (handle->type & g1m_type_eact) g1m_free_line_content(handle->line); /* free the handle itself! */ free(handle); }