/** unregister setup */ static void _unregister() { if(!_setup) NFT_LOG_NULL(); /* free all hardware nodes */ LedHardware *h; for(h = led_setup_get_hardware(_setup); h; h = led_hardware_list_get_next(h)) { /* unregister all tiles of hardware */ LedTile *t; for(t = led_hardware_get_tile(h); t; t = led_tile_list_get_next(t)) { tile_unregister_from_gui(led_tile_get_privdata(t)); } /* unregister chain of hardware */ chain_unregister_from_gui(led_chain_get_privdata (led_hardware_get_chain(h))); /* unregister hardware */ hardware_unregister_from_gui(led_hardware_get_privdata(h)); } led_setup_destroy(_setup); _setup = NULL; free(_current_filename); _current_filename = NULL; }
/** * NftArray setter */ void nft_array_set_type(NftArray * a, int type) { if(!a) NFT_LOG_NULL(); a->type = type; }
/** * NftArray getter * * @param a NftArray descriptor * @result type of NftArray or -1 upon failure */ int nft_array_get_type(NftArray * a) { if(!a) NFT_LOG_NULL(-1); return a->type; }
/** * NftArray setter * * @param a NftArray descriptor * @param name printable namestring with max. NFT_ARRAY_NAME_MAXLEN */ void nft_array_set_name(NftArray * a, const char *name) { if(!a || !name) NFT_LOG_NULL(); strncpy(a->name, name, sizeof(a->name)); }
/** * NftArray getter * * @param a NftArray descriptor * @result printable name of array or NULL upon error */ const char *nft_array_get_name(NftArray * a) { if(!a) NFT_LOG_NULL(NULL); return a->name[0] ? a->name : ("[unnamed]"); }
/** getter for position inside chain */ LedCount led_get_chainpos(NiftyconfLed * l) { if(!l) NFT_LOG_NULL(0); return l->pos; }
/** getter for boolean value whether element is currently highlighted */ gboolean led_get_highlighted(NiftyconfLed * l) { if(!l) NFT_LOG_NULL(false); return l->highlight; }
/** * get name of this NftPrefsNode * * @param n NftPrefsNode to get name from * @result classname of this NftPrefsNode or NULL */ const char *nft_prefs_node_get_name(NftPrefsNode * n) { if(!n) NFT_LOG_NULL(NULL); return (const char *) n->name; }
/** * get first child of a node * * @param n parent node * @result child node or NULL */ NftPrefsNode *nft_prefs_node_get_first_child(NftPrefsNode * n) { if(!n) NFT_LOG_NULL(NULL); return xmlFirstElementChild(n); }
/** * generate LedPrefsNode from LedSetup object * * @param p LedPrefs context * @param s LedSetup object * @result newly created LedPrefsNode */ LedPrefsNode *led_prefs_setup_to_node(LedPrefs * p, LedSetup * s) { if(!p || !s) NFT_LOG_NULL(NULL); return nft_prefs_obj_to_node(p, LED_SETUP_NAME, s, NULL); }
/** * Object-to-Config function. * Creates a config-node (and subnodes) from a LedSetup model * @param c the current preferences context * @param n a freshly created prefs node that this function should fill with properties of obj * @param obj object of this class where preferences should be generated from * @result NFT_SUCCESS if everything went fine, NFT_FAILURE otherwise * @note you shouldn't call this function directly * It's used by nft_prefs_obj_to_node() etc. */ static NftResult _prefs_from_setup(NftPrefs * p, NftPrefsNode * n, void *obj, void *userptr) { if(!p || !n || !obj) NFT_LOG_NULL(NFT_FAILURE); /* "Setup" object */ LedSetup *s = obj; /* process all "hardware" objects */ LedHardware *h; for(h = led_setup_get_hardware(s); h; h = led_hardware_list_get_next(h)) { /* generate prefs for each hardware node */ NftPrefsNode *node; if(! (node = nft_prefs_obj_to_node(p, LED_HARDWARE_NAME, h, NULL))) return NFT_FAILURE; /* add hardware to this setup */ if(!(nft_prefs_node_add_child(n, node))) return NFT_FAILURE; } return NFT_SUCCESS; }
/** * capture image */ static NftResult _capture(LedFrame *frame, LedFrameCord x, LedFrameCord y) { if(!frame) NFT_LOG_NULL(NFT_FAILURE); /* get screen-portion from X server */ XImage *image = NULL; if(!(image = XGetImage(_c.display, RootWindow(_c.display, _c.screen), x, y, led_frame_get_width(frame), led_frame_get_height(frame), AllPlanes, ZPixmap))) { NFT_LOG(L_ERROR, "XGetImage() failed"); return NFT_FAILURE; } /* copy framebuffer */ memcpy(led_frame_get_buffer(frame), image->data, led_frame_get_buffersize(frame)); /* destroy images */ XDestroyImage(image); return NFT_SUCCESS; }
/** * allocate a new custom NftPrefsNode * * @param name the name of the node * @result freshly created NftPrefsNode * @note use nft_prefs_node_free() if node is not used anymore */ NftPrefsNode *nft_prefs_node_alloc(const char *name) { if(!name) NFT_LOG_NULL(NULL); return xmlNewNode(NULL, BAD_CAST name); }
/** * add current node as child of parent node * * @param parent parent node * @param cur child node * @result NFT_SUCCESS or NFT_FAILURE */ NftResult nft_prefs_node_add_child(NftPrefsNode * parent, NftPrefsNode * cur) { if(!parent) NFT_LOG_NULL(NFT_FAILURE); return xmlAddChild(parent, cur) ? NFT_SUCCESS : NFT_FAILURE; }
/** * NftArray getter * * @param a NftArray descriptor * @result amount of elements currently in array or -1 upon error */ ssize_t nft_array_get_elementcount(NftArray * a) { if(!a) NFT_LOG_NULL(-1); return a->elementcount; }
/** * generate LedPrefsNode from LedChain object * * @param p LedPrefs context * @param c LedChain object * @result newly created LedPrefsNode */ LedPrefsNode *led_prefs_chain_to_node(LedPrefs * p, LedChain * c) { if(!p || !c) NFT_LOG_NULL(NULL); return nft_prefs_obj_to_node(p, LED_CHAIN_NAME, c, NULL); }
/** * find the slot of a certain element by sequentially * walking array and comparing criteria using a finder function * * @param a NftArray descriptor * @param s pointer where slot of found element will be written to * @param finder Function to check if an element matches the criterion * and then returns TRUE and FALSE otherwise * @param criterion the criterion that's passed to the finder function * @param userptr arbitrary user pointer * @result NFT_SUCCESS if element was found an slot has been written into *s, * NFT_FAILURE upon error */ NftResult nft_array_find_slot(NftArray * a, NftArraySlot * s, bool(*finder) (void *element, void *criterion, void *userptr), void *criterion, void *userptr) { if(!a || !s) NFT_LOG_NULL(NFT_FAILURE); NftArraySlot r; for(r = 0; r < a->arraysize; r++) { /* skip empty element */ if(!a->elements[r].occupied) continue; /* check if element matches */ if(finder(&a->buffer[a->elementsize * r], criterion, userptr)) { *s = r; return NFT_SUCCESS; } } return NFT_FAILURE; }
/** getter for parent chain */ NiftyconfChain *led_get_chain(NiftyconfLed * l) { if(!l) NFT_LOG_NULL(NULL); return l->chain; }
/** getter for renderer */ NiftyconfRenderer *led_get_renderer(NiftyconfLed * l) { if(!l) NFT_LOG_NULL(NULL); return l->renderer; }
/** * check type of an NftArray * * @param a NftArray descriptor * @param type the type to compare a's type with * @result true if "type" and type of NftArray match, false otherwise */ bool nft_array_is_type(NftArray * a, int type) { if(!a) NFT_LOG_NULL(FALSE); return (a->type == type); }
/** register new setup */ NftResult setup_register_to_gui(LedSetup * s) { if(!s) NFT_LOG_NULL(NFT_FAILURE); /* previous setup? */ if(_setup) _unregister(); /* initialize our element descriptor and set as privdata in niftyled * model */ LedHardware *h; for(h = led_setup_get_hardware(s); h; h = led_hardware_list_get_next(h)) { /* create new hardware element */ if(!hardware_register_to_gui(h)) { g_warning("failed to allocate new hardware element"); return false; } /* create chain of this hardware */ if(!chain_register_to_gui(led_hardware_get_chain(h))) { g_warning("failed to allocate new chain element"); return false; } /* walk all tiles belonging to this hardware & initialize */ LedTile *t; for(t = led_hardware_get_tile(h); t; t = led_tile_list_get_next(t)) { if(!tile_register_to_gui(t)) { g_warning ("failed to allocate new tile element"); return false; } } } /* save new setup */ _setup = s; /* allocate renderer */ if(!(_renderer = renderer_setup_new())) { g_error("Failed to allocate renderer for Setup"); _unregister(); return NFT_FAILURE; } /* initially draw setup */ renderer_setup_damage(); return NFT_SUCCESS; }
/** * register "chain" prefs class (called once for initialization) */ NftResult _prefs_chain_class_register(NftPrefs * p) { if(!p) NFT_LOG_NULL(NFT_FAILURE); return nft_prefs_class_register(p, LED_CHAIN_NAME, &_prefs_to_chain, &_prefs_from_chain); }
/** * register "hardware" prefs class (called once for initialization) */ NftResult prefs_hardware_class_register(NftPrefs * p) { if(!p) NFT_LOG_NULL(NFT_FAILURE); return nft_prefs_class_register(p, LED_HARDWARE_NAME, &_prefs_to_hardware, &_prefs_from_hardware); }
/** * trigger hardware to show data */ NftResult _show(void *privdata) { Niftylino *n = privdata; if(!n) NFT_LOG_NULL(NFT_FAILURE); /* latch hardware */ return _adapter_usb_send(n, NIFTY_LATCH, NULL, 0); }
/** * show yes/no dialog and wat for answer */ gboolean ui_log_dialog_yesno( char *title, char *message, ...) { if(!message) NFT_LOG_NULL(false); /* allocate mem to build message */ char *tmp; if(!(tmp = alloca(MAX_MSG_SIZE))) { NFT_LOG_PERROR("alloca"); return false; } /* build message */ va_list ap; va_start(ap, message); /* print log-string */ if(vsnprintf((char *) tmp, MAX_MSG_SIZE, message, ap) < 0) { NFT_LOG_PERROR("vsnprintf"); return false; } va_end(ap); /* Create the widgets */ GtkWidget *dialog, *label, *content_area; dialog = gtk_dialog_new_with_buttons(title, NULL, 0, GTK_STOCK_NO, GTK_RESPONSE_REJECT, GTK_STOCK_YES, GTK_RESPONSE_ACCEPT, NULL); content_area = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); label = gtk_label_new(tmp); /* Add the label, and show everything we've added to the dialog. */ gtk_container_add(GTK_CONTAINER(content_area), label); gtk_widget_show_all(dialog); /* wait for answer */ gboolean result = false; if(gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) result = true; gtk_widget_destroy(dialog); return result; }
/** finder for nft_array_find_slot() */ static bool _class_find_by_name(void *element, void *criterion, void *userptr) { if(!element || !criterion) NFT_LOG_NULL(FALSE); NftPrefsClass *c = element; const char *name = criterion; return (strcmp(c->name, name) == 0); }
/** * initialize an array descriptor * * @param a pointer to space that should be initialized to be used as NftArray * @param elementSize size of one array element in bytes * @result NFT_SUCCESS or NFT_FAILURE */ NftResult nft_array_init(NftArray * a, size_t elementSize) { if(!a) NFT_LOG_NULL(NFT_FAILURE); memset(a, 0, sizeof(NftArray)); a->elementsize = elementSize; return NFT_SUCCESS; }
/** send message + payload to adapter */ static NftResult _adapter_usb_send(Niftylino * n, uint message, char *payload, size_t payload_size) { if(!n->usb_handle) NFT_LOG_NULL(NFT_FAILURE); if(usb_control_msg(n->usb_handle, RECV_EP, message, 0, 0, payload, payload_size, n->usb_timeout) < 0) return NFT_FAILURE; return NFT_SUCCESS; }
/** * register object class * * @param p NftPrefs context where new class should be registered to * @param className unique name of new class * @param toObj pointer to NftPrefsToObjFunc used by the new class * @param fromObj pointer to NftPrefsFromObjFunc used by the new class * @result NFT_SUCCESS or NFT_FAILURE */ NftResult nft_prefs_class_register(NftPrefs * p, const char *className, NftPrefsToObjFunc * toObj, NftPrefsFromObjFunc * fromObj) { if(!p || !className) NFT_LOG_NULL(NFT_FAILURE); if(strlen(className) == 0) { NFT_LOG(L_ERROR, "class name may not be empty"); return NFT_FAILURE; } /** check if class is already registered */ NFT_LOG(L_DEBUG, "Checking if another class \"%s\" is already registered...", className); if(prefs_class_find_by_name(prefs_classes(p), className)) { NFT_LOG(L_ERROR, "class named \"%s\" already registered", className); return NFT_FAILURE; } /** allocate new slot in class array */ NftArraySlot s; if(!(nft_array_slot_alloc(prefs_classes(p), &s))) { NFT_LOG(L_ERROR, "Failed to allocate new slot"); return NFT_FAILURE; } /* get empty array element */ NftPrefsClass *n; if(!(n = nft_array_get_element(prefs_classes(p), s))) { NFT_LOG(L_ERROR, "Failed to get element from array"); goto _pcr_error; } /* register new class */ strncpy(n->name, className, NFT_PREFS_MAX_CLASSNAME); n->toObj = toObj; n->fromObj = fromObj; n->slot = s; return NFT_SUCCESS; _pcr_error: nft_array_slot_free(prefs_classes(p), s); return NFT_FAILURE; }
/* setter for boolean value whether element is currently highlighted */ void led_set_highlighted(NiftyconfLed * l, gboolean is_highlighted) { if(!l) NFT_LOG_NULL(); l->highlight = is_highlighted; /* highlight real hardware */ if(is_highlighted) { live_preview_highlight_led(l); } }