Esempio n. 1
0
/** 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;
}
Esempio n. 2
0
/** 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;
}
Esempio n. 3
0
/**
 * 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_hardware(NftPrefs * p, NftPrefsNode * n,
                                      void *obj, void *userptr)
{
        if(!p || !n || !obj)
                NFT_LOG_NULL(NFT_FAILURE);

        /* hardware "object" */
        LedHardware *h = obj;



        /* name of hardware */
        if(!nft_prefs_node_prop_string_set(n, LED_HARDWARE_PROP_NAME,
                                           (char *) led_hardware_get_name(h)))
                return NFT_FAILURE;


        /* plugin family of hardware */
        if(!nft_prefs_node_prop_string_set(n, LED_HARDWARE_PROP_PLUGIN,
                                           (char
                                            *) led_hardware_plugin_get_family
                                           (h)))
                return NFT_FAILURE;


        /* id of hardware */
        if(!nft_prefs_node_prop_string_set(n, LED_HARDWARE_PROP_ID,
                                           (char *) led_hardware_get_id(h)))
                return NFT_FAILURE;


        /* LED stride */
        if(!nft_prefs_node_prop_int_set(n, LED_HARDWARE_PROP_STRIDE,
                                        led_hardware_get_stride(h)))
                return NFT_FAILURE;

        /* handle custom plugin properties */
        int i, a = led_hardware_plugin_prop_get_count(h);
        for(i = 0; i < a; i++)
        {
                LedPluginCustomProp *prop;
                if(!(prop = led_hardware_plugin_prop_get_nth(h, i)))
                {
                        NFT_LOG(L_ERROR,
                                "Could not get property %d (but %d registered). This is a bug!",
                                i, a);
                        break;
                }

                /* create new node for property */
                NftPrefsNode *pnode;
                if(!
                   (pnode = nft_prefs_node_alloc(LED_HARDWARE_PROPERTY_NAME)))
                {
                        NFT_LOG(L_ERROR, "Failed to create new node.");
                        return NFT_FAILURE;
                }

                /* name of property */
                if(!nft_prefs_node_prop_string_set
                   (pnode, LED_HARDWARE_PROPERTY_PROP_NAME,
                    (char *) led_hardware_plugin_prop_get_name(prop)))
                        return NFT_FAILURE;

                /* handle various types of properties */
                switch (led_hardware_plugin_prop_get_type(prop))
                {
                        case LED_HW_CUSTOM_PROP_STRING:
                        {
                                /* save type */
                                if(!nft_prefs_node_prop_string_set
                                   (pnode, LED_HARDWARE_PROPERTY_PROP_TYPE,
                                    "string"))
                                        return NFT_FAILURE;

                                /* get string */
                                char *string;
                                if(!led_hardware_plugin_prop_get_string(h,
                                                                        led_hardware_plugin_prop_get_name
                                                                        (prop),
                                                                        &string))
                                        return NFT_FAILURE;

                                /* save value */
                                if(!nft_prefs_node_prop_string_set
                                   (pnode, LED_HARDWARE_PROPERTY_PROP_VALUE,
                                    string))
                                        return NFT_FAILURE;

                                break;
                        }

                        case LED_HW_CUSTOM_PROP_INT:
                        {
                                /* save type */
                                if(!nft_prefs_node_prop_string_set
                                   (pnode, LED_HARDWARE_PROPERTY_PROP_TYPE,
                                    "int"))
                                        return NFT_FAILURE;

                                /* get integer */
                                int integer;
                                if(!led_hardware_plugin_prop_get_int(h,
                                                                     led_hardware_plugin_prop_get_name
                                                                     (prop),
                                                                     &integer))
                                        return NFT_FAILURE;

                                /* convert to string */
                                char *string;
                                if(!(string = alloca(64)))
                                {
                                        NFT_LOG_PERROR("alloca");
                                        return NFT_FAILURE;
                                }
                                snprintf(string, 64, "%d", integer);

                                /* save value */
                                if(!nft_prefs_node_prop_string_set
                                   (pnode, LED_HARDWARE_PROPERTY_PROP_VALUE,
                                    string))
                                        return NFT_FAILURE;
                                break;
                        }

                        case LED_HW_CUSTOM_PROP_FLOAT:
                        {
                                /* save type */
                                if(!nft_prefs_node_prop_string_set
                                   (pnode, LED_HARDWARE_PROPERTY_PROP_TYPE,
                                    "float"))
                                        return NFT_FAILURE;

                                /* get float */
                                float fp;
                                if(!led_hardware_plugin_prop_get_float(h,
                                                                       led_hardware_plugin_prop_get_name
                                                                       (prop),
                                                                       &fp))
                                        return NFT_FAILURE;

                                /* convert to string */
                                char *string;
                                if(!(string = alloca(64)))
                                {
                                        NFT_LOG_PERROR("alloca");
                                        return NFT_FAILURE;
                                }
                                snprintf(string, 64, "%f", fp);

                                /* save value */
                                if(!nft_prefs_node_prop_string_set
                                   (pnode, LED_HARDWARE_PROPERTY_PROP_VALUE,
                                    string))
                                        return NFT_FAILURE;

                                break;
                        }

                                /* unsupported type */
                        default:
                        {
                                NFT_LOG(L_WARNING,
                                        "Property \"%s\" is of unsupported type. Ignoring",
                                        led_hardware_plugin_prop_get_name
                                        (prop));
                                continue;
                        }
                }

                /* add node as child of this node */
                nft_prefs_node_add_child(n, pnode);

        }

        /* chain of this hardware */
        LedChain *c;
        if((c = led_hardware_get_chain(h)))
        {
                /* generate prefs node from chain */
                NftPrefsNode *node;
                if(!(node = led_prefs_chain_to_node(p, c)))
                        return NFT_FAILURE;

                /* add node as child of this node */
                nft_prefs_node_add_child(n, node);
        }

        /* tiles of this hardware */
        LedTile *t;
        for(t = led_hardware_get_tile(h); t; t = led_tile_list_get_next(t))
        {
                NftPrefsNode *node;
                if(!(node = led_prefs_tile_to_node(p, t)))
                        return NFT_FAILURE;

                /* add node as child of this node */
                nft_prefs_node_add_child(n, node);
        }


        /* all OK */
        return NFT_SUCCESS;
}
Esempio n. 4
0
/** renderer for setups */
static NftResult _render_setup(cairo_surface_t ** surface, gpointer element)
{
        if(!surface || !element)
                NFT_LOG_NULL(NFT_FAILURE);

        /* setup to render */
        LedSetup *s = (LedSetup *) element;

        /* get dimensions of setup */
        LedFrameCord w, h;
        led_setup_get_dim(s, &w, &h);

        /* calculate rendered dimensions of this tile */
        double width = (double) w * ui_renderer_scale_factor();
        double height = (double) h * ui_renderer_scale_factor();

        /* if dimensions changed, we need to allocate a new surface */
        if(!renderer_resize(setup_get_renderer(), width, height))
        {
                g_error("Failed to resize renderer to %dx%d", w, h);
                return NFT_FAILURE;
        }

        /* create context for drawing */
        cairo_t *cr = cairo_create(*surface);

        /* clear surface */
#ifdef DEBUG
        cairo_set_source_rgba(cr, 0.1, 0.1, 0.1, 1);
#else
        cairo_set_source_rgba(cr, 0, 0, 0, 1);
#endif
        cairo_rectangle(cr,
                        0, 0,
                        (double) cairo_image_surface_get_width(*surface),
                        (double) cairo_image_surface_get_height(*surface));
        cairo_fill(cr);



        /* walk through all hardware LED adapters */
        LedHardware *hw;
        for(hw = led_setup_get_hardware(s);
            hw; hw = led_hardware_list_get_next(hw))
        {

                /* calculate offset of complete setup */
                LedTile *t;
                double xOff = 0, yOff = 0;
                for(t = led_hardware_get_tile(hw);
                    t; t = led_tile_list_get_next(t))
                {
                        double xOffT, yOffT;
                        tile_calc_render_offset(led_tile_get_privdata(t),
                                                (double) w, (double) h,
                                                &xOffT, &yOffT);
                        xOff = MIN(xOff, xOffT);
                        yOff = MIN(yOff, yOffT);
                }

                renderer_set_offset(setup_get_renderer(),
                                    xOff * ui_renderer_scale_factor(),
                                    yOff * ui_renderer_scale_factor());

                /* Walk all tiles of this hardware & draw their surface */
                for(t = led_hardware_get_tile(hw);
                    t; t = led_tile_list_get_next(t))
                {
                        /** @todo check for visibility? */

                        /* get surface from tile */
                        NiftyconfTile *tile =
                                (NiftyconfTile *) led_tile_get_privdata(t);

                        /* get surface */
                        cairo_surface_t *tsurface = renderer_get_surface
                                (tile_get_renderer(tile));

                        /* compensate child tiles' offset */
                        double xOffT, yOffT;
                        renderer_get_offset(tile_get_renderer(tile), &xOffT,
                                            &yOffT);
                        cairo_translate(cr, xOffT, yOffT);

                        /* compensate setup offset */
                        cairo_translate(cr,
                                        -xOff * ui_renderer_scale_factor(),
                                        -yOff * ui_renderer_scale_factor());

                        /* move to x/y */
                        LedFrameCord x, y;
                        led_tile_get_pos(t, &x, &y);
                        cairo_translate(cr,
                                        ((double) x) *
                                        ui_renderer_scale_factor(),
                                        ((double) y) *
                                        ui_renderer_scale_factor());

                        /* rotate around pivot */
                        double pX, pY;
                        led_tile_get_pivot(t, &pX, &pY);
                        cairo_translate(cr,
                                        pX * ui_renderer_scale_factor(),
                                        pY * ui_renderer_scale_factor());
                        cairo_rotate(cr, led_tile_get_rotation(t));
                        cairo_translate(cr,
                                        -pX * ui_renderer_scale_factor(),
                                        -pY * ui_renderer_scale_factor());

                        /* draw surface */
                        cairo_set_source_surface(cr, tsurface, 0, 0);

                        /* disable filtering */
                        cairo_pattern_set_filter(cairo_get_source(cr),
                                                 ui_renderer_filter());
                        /* disable antialiasing */
                        cairo_set_antialias(cr, ui_renderer_antialias());

                        /* render surface */
                        cairo_paint(cr);

                        /* reset transformation matrix */
                        cairo_identity_matrix(cr);
                }

                /* Draw chain of this hardware */
                /* s =
                 * chain_get_surface(led_chain_get_privdata(led_hardware_get_chain(h)));
                 * cairo_set_source_surface(cr, s, 0, 0); cairo_paint(cr); */

        }

        cairo_destroy(cr);

        return NFT_SUCCESS;
}