/* Hey EMACS -*- linux-c -*- */ /* libcalcprotocols - link protocol library, a part of the CalcForge project * Copyright (C) 1999-2005 Romain Lievin * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifdef HAVE_CONFIG_H # include #endif #include #include #include #include #if defined(__WIN32__) #include #endif #include "gettext.h" #include "calcprotocols.h" #include "logging.h" #include "error.h" #include "calc_xx.h" /*****************/ /* Internal data */ /*****************/ extern const CalcUpdate default_update; static CalcFncts const *const calcs[] = { &calc_00, #ifndef NO_TI73 &calc_73, #endif #ifndef NO_TI82 &calc_82, #endif #ifndef NO_TI82S &calc_83/*&calc_82s*/, #endif #ifndef NO_TI83 &calc_83, #endif #ifndef NO_TI83P &calc_83p, #endif #ifndef NO_TI84P &calc_84p, #endif #ifndef NO_TI85 &calc_85, #endif #ifndef NO_TI86 &calc_86, #endif #ifndef NO_TI89 &calc_89, #endif #ifndef NO_TI92 &calc_92, #endif #ifndef NO_TI92P &calc_92p, #endif #ifndef NO_V200 &calc_v2, #endif #ifndef NO_TI89T &calc_89t, #endif #ifndef NO_TI84P_USB &calc_84p_usb, #endif #ifndef NO_TI89T_USB &calc_89t_usb, #endif #ifndef NO_SPIRE &calc_nsp, #endif NULL }; /****************/ /* Entry points */ /****************/ // not static, must be shared between instances int calcprotocols_instance = 0; // counts # of instances /** * calcprotocols_library_init: * * This function must be the first one to call. It inits library internals. * * Return value: the instance count. **/ CALCPROTOCOLS_EXPORT int CALCFORGE_CALL calcprotocols_library_init(void) { char locale_dir[65536]; #ifdef __WIN32__ HANDLE hDll; int i; hDll = GetModuleHandle("libcalcprotocols-0.dll"); GetModuleFileName(hDll, locale_dir, 65535); for (i = strlen(locale_dir); i >= 0; i--) { if (locale_dir[i] == '\\') break; } locale_dir[i] = '\0'; #ifdef __MINGW32__ strcat(locale_dir, "\\..\\share\\locale"); #else strcat(locale_dir, "\\locale"); #endif #else strcpy(locale_dir, LOCALEDIR); #endif if (calcprotocols_instance) return (++calcprotocols_instance); calcprotocols_info(_("libcalcprotocols version %s"), VERSION); errno = 0; #if defined(ENABLE_NLS) calcprotocols_info("setlocale: %s", setlocale(LC_ALL, "")); calcprotocols_info("bindtextdomain: %s", bindtextdomain(PACKAGE, locale_dir)); //bind_textdomain_codeset(PACKAGE, "UTF-8"/*"ISO-8859-15"*/); calcprotocols_info("textdomain: %s", textdomain(PACKAGE)); #endif return (++calcprotocols_instance); } /** * calcprotocols_library_exit: * * This function must be the last one to call. Used to release internal resources. * * Return value: the instance count. **/ CALCPROTOCOLS_EXPORT int CALCFORGE_CALL calcprotocols_library_exit(void) { return (--calcprotocols_instance); } /***********/ /* Methods */ /***********/ /** * calcprotocols_version_get: * * This function returns the library version like "X.Y.Z". * * Return value: a string. **/ CALCPROTOCOLS_EXPORT const char *CALCFORGE_CALL calcprotocols_version_get(void) { return VERSION; } /** * calcprotocols_handle_new: * @model: a hand-held model * * Create a new handle associated with the given cable on the given port. * Must be freed with calccables_handle_del when no longer needed. * Note: the handle is a pointer on an opaque structure and should not be modified. * * Return value: NULL if error, an handle otherwise. **/ CALCPROTOCOLS_EXPORT CalcHandle* CALCFORGE_CALL calcprotocols_handle_new(CalcModel model) { CalcHandle *handle = (CalcHandle *)g_malloc0(sizeof(CalcHandle)); int i; handle->model = model; for(i = 0; calcs[i]; i++) if(calcs[i]->model == model) { handle->calc = (CalcFncts *)calcs[i]; break; } if(handle->calc == NULL) return NULL; handle->updat = (CalcUpdate *)&default_update; handle->priv2 = (uint8_t *)g_malloc(65536 + 6); if(handle->priv2 == NULL) return NULL; return handle; } /** * calcprotocols_handle_del: * @handle: the handle * * Release the cable and free the associated resources. * If cable has not been detached with #calcprotocols_cable_detach, * it will be detached. * * Return value: always 0. **/ CALCPROTOCOLS_EXPORT int CALCFORGE_CALL calcprotocols_handle_del(CalcHandle* handle) { if(handle->attached) calcprotocols_cable_detach(handle); if(handle->priv2) g_free(handle->priv2); if(handle) g_free(handle); handle = NULL; return 0; } /** * calcprotocols_handle_show: * @handle: the handle * * Show informations stored in the handle. * * Return value: always 0. **/ CALCPROTOCOLS_EXPORT int CALCFORGE_CALL calcprotocols_handle_show(CalcHandle* handle) { calcprotocols_info(_("Link calc handle details:")); calcprotocols_info(_(" model : %s"), calcprotocols_model_to_string(handle->model)); return 0; } /** * calcprotocols_cable_attach: * @handle: the handle * @cable: a cable to use * * Attach and open the given cable for use with the hand-held. * * Return value: 0 if successful, an error code otherwise. **/ CALCPROTOCOLS_EXPORT int CALCFORGE_CALL calcprotocols_cable_attach(CalcHandle* handle, CableHandle* cable) { handle->cable = cable; handle->attached = !0; TRYC(calccables_cable_open(cable)); handle->open = !0; return 0; } /** * calcprotocols_cable_detach: * @handle: the handle * @cable: a cable to use * * Close and detach the cable associated with the hand-held. * * Return value: 0 if successful, an error code otherwise. **/ CALCPROTOCOLS_EXPORT int CALCFORGE_CALL calcprotocols_cable_detach(CalcHandle* handle) { TRYC(calccables_cable_close(handle->cable)); handle->open = 0; handle->attached = 0; handle->cable = NULL; handle = NULL; return 0; } /** * calcprotocols_update_set: * @handle: the handle * @update: the callbacks to use * * Set the callbacks to use for the given handle. * * Return value: always 0. **/ CALCPROTOCOLS_EXPORT int CALCFORGE_CALL calcprotocols_update_set(CalcHandle* handle, CalcUpdate* upd) { if(handle) handle->updat = upd; return 0; }