/** * Object-to-Config function. * Creates a config-node (and subnodes) from a LedHardware 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_chain(NftPrefs * p, NftPrefsNode * n, void *obj, void *userptr) { if(!p || !n || !obj) NFT_LOG_NULL(NFT_FAILURE); /* chain to generate preferences from */ LedChain *c = obj; /* amount of LEDs in this chain - ledcount */ if(!nft_prefs_node_prop_int_set(n, LED_CHAIN_PROP_LEDCOUNT, led_chain_get_ledcount(c))) return NFT_FAILURE; /* pixel-format of this chain */ if(!nft_prefs_node_prop_string_set(n, LED_CHAIN_PROP_FORMAT, (char *) led_pixel_format_to_string (led_chain_get_format(c)))) return NFT_FAILURE; /* add all LEDs in this chain */ LedCount i; for(i = 0; i < led_chain_get_ledcount(c); i++) { /* generate prefs node from LED */ NftPrefsNode *node; if(! (node = led_prefs_led_to_node(p, led_chain_get_nth(c, i)))) return NFT_FAILURE; /* add node as child of this node */ if(!nft_prefs_node_add_child(n, node)) return NFT_FAILURE; } return NFT_SUCCESS; }
/** build list of Leds */ static void _build( NiftyconfChain * c) { /* rebuild */ LedCount i; for(i = 0; i < led_chain_get_ledcount(chain_niftyled(c)); i++) { Led *led = led_chain_get_nth(chain_niftyled(c), i); GtkTreeIter iter; gtk_list_store_append(GTK_LIST_STORE(UI("liststore")), &iter); gtk_list_store_set(GTK_LIST_STORE(UI("liststore")), &iter, C_CHAIN_LED, i, C_CHAIN_ELEMENT, led_get_privdata(led), -1); led_set_highlighted(led_get_privdata(led), false); renderer_led_damage(led_get_privdata(led)); } gtk_widget_show(GTK_WIDGET(UI("treeview"))); }
/** * Config-to-Object function. * Creates a LedHardware model from a prefs node * @note you shouldn't call this function directly * It's used by nft_prefs_obj_from_node() etc. */ static NftResult _prefs_to_chain(LedPrefs * p, void **newObj, NftPrefsNode * n, void *userptr) { if(!p || !newObj) NFT_LOG_NULL(NFT_FAILURE); /* LedCount of chain */ int ledcount = 0; if(!nft_prefs_node_prop_int_get (n, LED_CHAIN_PROP_LEDCOUNT, &ledcount)) { NFT_LOG(L_WARNING, "chain has no \"%s\" property. Using %d as default.", LED_CHAIN_PROP_LEDCOUNT, ledcount); } /* new chain */ LedChain *c; /* get format of this chain */ char *format; if(!(format = nft_prefs_node_prop_string_get(n, LED_CHAIN_PROP_FORMAT))) { #define LED_CHAIN_DEFAULT_FORMAT "RGB u8" NFT_LOG(L_WARNING, "chain has no \"%s\" property. Using \"%s\" as default.", LED_CHAIN_PROP_FORMAT, LED_CHAIN_DEFAULT_FORMAT); /* new chain (with default format) */ if(!(c = led_chain_new((LedCount) ledcount, LED_CHAIN_DEFAULT_FORMAT))) { NFT_LOG(L_ERROR, "Failed to create new LedSetup object"); return NFT_FAILURE; } /* free string */ nft_prefs_free(format); } /* create new chain with given format */ else { /* new chain */ if(!(c = led_chain_new(ledcount, format))) { NFT_LOG(L_ERROR, "Failed to create new LedSetup object"); return NFT_FAILURE; } } /* free string */ nft_prefs_free(format); /* process child nodes (LEDs) */ NftPrefsNode *child; LedCount i = 0; for(child = nft_prefs_node_get_first_child(n); child; child = nft_prefs_node_get_next(child)) { /* check if node describes a Led object */ if(!led_prefs_is_led_node(child)) { NFT_LOG(L_ERROR, "\"chain\" may only contain \"%s\" children. Skipping \"%s\".", LED_LED_NAME, nft_prefs_node_get_name(child)); continue; } /* create Led from node */ Led *l = led_chain_get_nth(c, i++); if(!led_prefs_led_from_node(p, child, l)) goto _ptc_error; } /* save new chain-object to "newObj" pointer */ *newObj = c; return NFT_SUCCESS; _ptc_error: led_chain_destroy(c); return NFT_FAILURE; }
/** renderer for chains */ static NftResult _render_chain( cairo_surface_t ** s, gpointer element) { if(!s || !*s || !element) NFT_LOG_NULL(NFT_FAILURE); /* get this chain */ NiftyconfChain *chain = (NiftyconfChain *) element; LedChain *c = chain_niftyled(chain); /* if dimensions changed, we need to allocate a new surface */ int width = (led_chain_get_max_x(c) + 1) * renderer_scale_factor(); int height = (led_chain_get_max_y(c) + 1) * renderer_scale_factor(); NiftyconfRenderer *r = chain_get_renderer(chain); if(!renderer_resize(r, width, height)) { g_error("Failed to resize renderer to %dx%d", width, height); return NFT_FAILURE; } /* create context for drawing */ cairo_t *cr = cairo_create(*s); /* disable antialiasing */ cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE); /* clear surface */ cairo_set_source_rgba(cr, 0, 0, 0, 1); cairo_rectangle(cr, 0, 0, (double) cairo_image_surface_get_width(*s), (double) cairo_image_surface_get_height(*s)); cairo_fill(cr); /* set line-width */ cairo_set_line_width(cr, 1); /* walk all LEDs */ LedCount i; for(i = 0; i < led_chain_get_ledcount(c); i++) { Led *l = led_chain_get_nth(c, i); NiftyconfLed *led = led_get_privdata(l); NiftyconfRenderer *lr = led_get_renderer(led); cairo_set_source_surface(cr, renderer_get_surface(lr), (double) led_get_x(l) * renderer_scale_factor(), (double) led_get_y(l) * renderer_scale_factor()); cairo_paint(cr); } cairo_destroy(cr); return NFT_SUCCESS; }