Example #1
0
/** print list of installed plugins + information they provide */
static void _print_plugin_help()
{

        /* save current loglevel */
        NftLoglevel ll_current = nft_log_level_get();
        nft_log_level_set(L_INFO);

        int i;
        for(i = 0; i < led_hardware_plugin_total_count(); i++)
        {
                const char *name;
                if(!(name = led_hardware_plugin_get_family_by_n(i)))
                        continue;

                printf("======================================\n\n");

                LedHardware *h;
                if(!(h = led_hardware_new("tmp01", name)))
                        continue;

                printf("\tID Example: %s\n",
                       led_hardware_plugin_get_id_example(h));


                led_hardware_destroy(h);

        }

        /* restore logolevel */
        nft_log_level_set(ll_current);
}
Example #2
0
/** loglevel changed */
G_MODULE_EXPORT void on_log_combobox_changed(
        GtkComboBox * w,
        gpointer u)
{
        NftLoglevel l = (NftLoglevel) gtk_combo_box_get_active(w) + 1;

        if(!nft_log_level_set(l))
                g_warning("Failed to set loglevel: \"%s\" (%d)",
                          nft_log_level_to_string(l), (gint) l);
}
Example #3
0
/** configure from preferences */
static NftResult _this_from_prefs(
        NftPrefs * prefs,
        void **newObj,
        NftPrefsNode * node,
        void *userptr)
{
		/* window geometry */
		gint x = 0, y = 0, width = 0, height = 0;
        nft_prefs_node_prop_int_get(node, "x", &x);
        nft_prefs_node_prop_int_get(node, "y", &y);
        nft_prefs_node_prop_int_get(node, "width", &width);
        nft_prefs_node_prop_int_get(node, "height", &height);

        if(width > 0 && height > 0)
                gtk_window_resize(GTK_WINDOW(UI("window")), width, height);
        if(x > 0 && y > 0)
                gtk_window_move(GTK_WINDOW(UI("window")), x, y);
		
		/* log visible? */
		bool boolean = false;
		nft_prefs_node_prop_boolean_get(node, "window-visible", &boolean);
		gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(niftyconf_ui("item_log_win")), boolean);
		ui_log_show(boolean);

		/* log level */
		char *loglevel;
		if((loglevel = nft_prefs_node_prop_string_get(node, "loglevel")))
		{
				nft_log_level_set(nft_log_level_from_string(loglevel));
				nft_prefs_free(loglevel);
		}

		/* log flags */
		nft_prefs_node_prop_boolean_get(node, "show-file", &boolean);
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(UI("checkbutton_file")), boolean);
		nft_prefs_node_prop_boolean_get(node, "show-line", &boolean);
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(UI("checkbutton_line")), boolean);
		nft_prefs_node_prop_boolean_get(node, "show-function", &boolean);
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(UI("checkbutton_function")), boolean);
		
		return NFT_SUCCESS;
}
Example #4
0
int main(int argc, char *argv[])
{
        /* current configuration */
        LedPrefs *p = NULL;
    	/** current setup */
    	LedSetup *s = NULL;
        /** list of LED hardware adapters */
        LedHardware *hw = NULL;
        /* input pixel-frame buffer */
        LedFrame *frame = NULL;
        /** width of map */
        LedFrameCord width;
        /** height of map */
        LedFrameCord height;



        /* check libniftyled binary version compatibility */
        NFT_LED_CHECK_VERSION

        /* set default loglevel to INFO */
        nft_log_level_set(L_INFO);

        /* initialize exit handlers */
#if WIN32
        int signals[] = { SIGINT, SIGABRT };
#else
        int signals[] = { SIGHUP, SIGINT, SIGQUIT, SIGABRT };
#endif
        unsigned int i;
        for(i=0; i<sizeof(signals)/sizeof(int); i++)
        {
            if(signal(signals[i], _exit_signal_handler) == SIG_ERR)
            {
                NFT_LOG_PERROR("signal()");
                return -1;
            }
        }



        /* default result of main() function */
        int res = -1;

        /* default fps */
        _c.fps = 25;

        /* default endianess */
        _c.is_big_endian = FALSE;

#if HAVE_IMAGEMAGICK == 1
        /* default handle-input-as-raw */
        _c.raw = FALSE;
#endif

        /* default looping */
        _c.do_loop = FALSE;

        /* set "running" flag */
        _c.running = TRUE;

        /* default pixel-format */
        strncpy(_c.pixelformat, "RGB u8", sizeof(_c.pixelformat));

        /* default prefs-filename */
        if(!led_prefs_default_filename(_c.prefsfile, sizeof(_c.prefsfile), ".ledcat.xml"))
                return -1;


	/* parse commandline arguments */
	if(!_parse_args(argc, argv))
		return -1;



	/* print welcome msg */
	NFT_LOG(L_INFO, "%s %s (c) D.Hiepler 2006-2012", PACKAGE_NAME, ledcat_version_long());
	NFT_LOG(L_VERBOSE, "Loglevel: %s", nft_log_level_to_string(nft_log_level_get()));


#if HAVE_IMAGEMAGICK == 1
        /* initialize imagemagick */
        if(!_c.raw)
        {
                if(!im_init(&_c))
                {
                        NFT_LOG(L_ERROR, "Failed to initialize ImageMagick");
                        return -1;
                }
        }
#endif

    	/* initialize preferences context */
    	if(!(p = led_prefs_init()))
    		return -1;

	/* parse prefs-file */
    	LedPrefsNode *pnode;
    	if(!(pnode = led_prefs_node_from_file(_c.prefsfile)))
    	{
		NFT_LOG(L_ERROR, "Failed to open configfile \"%s\"",
		        		_c.prefsfile);
		goto m_deinit;
	}

        /* create setup from prefs-node */
    	if(!(s = led_prefs_setup_from_node(p, pnode)))
    	{
		NFT_LOG(L_ERROR, "No valid setup found in preferences file.");
		led_prefs_node_free(pnode);
		goto m_deinit;
	}

    	/* free preferences node */
    	led_prefs_node_free(pnode);

        /* determine width of input-frames */
        if(!_c.width)
                /* width of mapped chain */
                width = led_setup_get_width(s);
        else
                /* use value from cmdline arguments */
                width = _c.width;


        /* determine height of input-frames */
        if(!_c.height)
                /* height of mapped chain */
                height = led_setup_get_height(s);
        else
                height = _c.height;


        /* validate dimensions */
        if(width <= 0 || height <= 0)
        {
                NFT_LOG(L_ERROR, "Dimensions %dx%d not possible in this universe. Exiting.", width, height);
                goto m_deinit;
        }


	/* allocate frame (where our pixelbuffer resides) */
	NFT_LOG(L_INFO, "Allocating frame: %dx%d (%s)",
	        width, height, _c.pixelformat);
        LedPixelFormat *format = led_pixel_format_from_string(_c.pixelformat);
	if(!(frame = led_frame_new(width, height, format)))
		goto m_deinit;


        /* get first toplevel hardware */
        if(!(hw = led_setup_get_hardware(s)))
                goto m_deinit;

        /* initialize hardware */
	/*if(!(led_hardware_init(hw, ledcount, (LedGreyscaleFormat) format)))
	{
		NFT_LOG(L_ERROR, "failed to initialize hardware.");
                goto m_deinit;
	}*/

        /* initialize pixel->led mapping */
        if(!led_hardware_list_refresh_mapping(hw))
                goto m_deinit;

        /* precalc memory offsets for actual mapping */
        LedHardware *ch;
        for(ch = hw; ch; ch = led_hardware_list_get_next(ch))
        {
                if(!led_chain_map_from_frame(led_hardware_get_chain(ch), frame))
                        goto m_deinit;
        }

        /* set correct gain to hardware */
        if(!led_hardware_list_refresh_gain(hw))
                goto m_deinit;

#if HAVE_IMAGEMAGICK == 1
        /* determine format that ImageMagick should provide */
        if(!_c.raw)
        {
                if(!(im_format(&_c, format)))
                {
                        NFT_LOG(L_ERROR, "Failed to determine valid ImageMagick format");
                        goto m_deinit;
                }
        }
#endif

	/* do we have at least one filename? */
	if(!_c.files[0])
	{
		NFT_LOG(L_ERROR, "No input file(s) given");
		goto m_deinit;
	}


        /* initially sample time for frame-timing */
        if(!led_fps_sample())
                goto m_deinit;


#if ! WIN32
        /* initialize alarm-signal handler for periodical fps output */
        {
                struct sigaction sa;
                struct itimerval timer;

                memset(&sa, 0, sizeof(sa));
                sa.sa_handler = &_alarm_signal_handler;
                sigaction(SIGALRM, &sa, NULL);
                timer.it_value.tv_sec = 1;
                timer.it_value.tv_usec = 0;
                timer.it_interval.tv_sec = 1;
                timer.it_interval.tv_usec = 0;
                setitimer(ITIMER_REAL, &timer, NULL);
        }
#endif

        /* get data-buffer of frame to write our pixels to */
        char *buf;
        if(!(buf = led_frame_get_buffer(frame)))
        {
                NFT_LOG(L_ERROR, "Frame has NULL buffer");
                goto m_deinit;
        }


	/* walk all files (supplied as commandline arguments) and output them */
	int filecount;
	for(filecount = 0; _c.files[filecount]; filecount++)
	{

		NFT_LOG(L_VERBOSE, "Getting pixels from \"%s\"", _c.files[filecount]);

		/* open file */
		if(_c.files[filecount][0] == '-' && strlen(_c.files[filecount]) == 1)
		{
			_c.fd = STDIN_FILENO;
		}
		else
		{
			if((_c.fd = open(_c.files[filecount], O_RDONLY)) < 0)
			{
				NFT_LOG(L_ERROR, "Failed to open \"%s\": %s",
				        _c.files[filecount], strerror(errno));
				continue;
			}
		}

#if HAVE_IMAGEMAGICK == 1
                /** initialize stream for ImageMagick */
                if(!_c.raw)
                {
                        if(!(im_open_stream(&_c)))
                                continue;
                }
#endif



		/* output file frame-by-frame */
		while(_c.running)
		{

#if HAVE_IMAGEMAGICK == 1
                        /* use imagemagick to load file if we're not in "raw-mode" */
                        if(!_c.raw)
                        {
                                /* load frame to buffer using ImageMagick */
                                if(!im_read_frame(&_c, width, height, buf))
                                        break;

                        }
                        else
                        {
#endif
                                /* read raw frame */
                                if(raw_read_frame(&_c.running, buf, _c.fd,
                                               led_pixel_format_get_buffer_size(
                                                        led_frame_get_format(frame),
                                                        led_frame_get_width(frame)*led_frame_get_height(frame))) == 0)
                                        continue;

#if HAVE_IMAGEMAGICK == 1
                        }
#endif

                        /* set endianess (flag will be changed when conversion occurs) */
                        led_frame_set_big_endian(frame, _c.is_big_endian);

			/* print frame for debugging */
			//nft_frame_buffer_print(frame);

			/* fill chain of every hardware from frame */
                        LedHardware *h;
                        for(h = hw; h; h = led_hardware_list_get_next(h))
                        {
                                if(!led_chain_fill_from_frame(led_hardware_get_chain(h), frame))
                                {
                                        NFT_LOG(L_ERROR, "Error while mapping frame");
                                        break;
                                }
                        }

			/* send frame to hardware(s) */
			NFT_LOG(L_DEBUG, "Sending frame");
                        led_hardware_list_send(hw);

			/* delay in respect to fps */
			if(!led_fps_delay(_c.fps))
				break;

			/* latch hardware */
			NFT_LOG(L_DEBUG, "Showing frame");
                        led_hardware_list_show(hw);


			/* save time when frame is displayed */
			if(!led_fps_sample())
				break;


			/* clear frame */
			//nft_frame_clear_buffer(frame);

		}


#if HAVE_IMAGEMAGICK == 1
		if(!_c.raw)
                	im_close_stream(&_c);
#else
                close(_c.fd);
#endif

                /* loop endlessly? */
                if((_c.running) && (!_c.files[filecount+1]) && _c.do_loop)
                {
                        /* start over by resetting the for-loop */
                        filecount = -1;
                }
	}

        /* all ok */
        res = 0;

m_deinit:
    	/* free setup */
    	led_setup_destroy(s);

	/* free frame */
	led_frame_destroy(frame);

        /* destroy config */
        led_prefs_deinit(p);

#if HAVE_IMAGEMAGICK == 1
        /* deinitialize ImageMagick */
        im_deinit(&_c);
#endif

        return res;
}
Example #5
0
/** parse commandline arguments */
static NftResult _parse_args(int argc, char *argv[])
{
	int index, argument;

	static struct option loptions[] =
	{
		{"help", 0, 0, 'h'},
                {"plugin-help", 0, 0, 'p'},
		{"loglevel", required_argument, 0, 'l'},
		{"config", required_argument, 0, 'c'},
                {"dimensions", required_argument, 0, 'd'},
                {"fps", required_argument, 0, 'F'},
                {"format", required_argument, 0, 'f'},
                {"big-endian", no_argument, 0, 'b'},
                {"loop", no_argument, 0, 'L'},
#if HAVE_IMAGEMAGICK == 1
                {"raw", no_argument, 0, 'r'},
#endif
		{0,0,0,0}
	};

#if HAVE_IMAGEMAGICK == 1
	const char arglist[] = "hpl:c:d:F:f:bLr";
#else
	const char arglist[] = "hpl:c:d:F:f:bL";
#endif
	while((argument = getopt_long(argc, argv, arglist, loptions, &index)) >= 0)
	{

		switch(argument)
		{
			/* --help */
			case 'h':
			{
				_print_help(argv[0]);
				return NFT_FAILURE;
			}

                        /* --plugin-help */
                        case 'p':
                        {
                                _print_plugin_help();
                                return NFT_FAILURE;
                        }

			/* --config */
			case 'c':
			{
				/* save filename for later */
				strncpy(_c.prefsfile, optarg, sizeof(_c.prefsfile));
				break;
			}

                        /** --loop */
                        case 'L':
                        {
                                _c.do_loop = TRUE;
                                break;
                        }

                        /** --dimensions */
                        case 'd':
                        {
                                if(sscanf(optarg, "%dx%d", (int*) &_c.width, (int*) &_c.height) != 2)
				{
					NFT_LOG(L_ERROR, "Invalid dimension \"%s\" (Use something like 320x400)", optarg);
					return NFT_FAILURE;
				}
                                break;
                        }

                        /** --fps */
                        case 'F':
                        {
                                if(sscanf(optarg, "%d", (int*) &_c.fps) != 1)
				{
					NFT_LOG(L_ERROR, "Invalid framerate \"%s\" (Use an integer)", optarg);
					return NFT_FAILURE;
				}
				break;
                        }

			/** --loglevel */
			case 'l':
			{
				if(!nft_log_level_set(nft_log_level_from_string(optarg)))
                                {
                                        _print_loglevels();
                                        return NFT_FAILURE;
                                }

				break;
			}

                        /** --format */
                        case 'f':
                        {
                                strncpy(_c.pixelformat, optarg, sizeof(_c.pixelformat));
                                break;
                        }

#if HAVE_IMAGEMAGICK == 1
                        /** --raw */
                        case 'r':
                        {
                                _c.raw = TRUE;
                                break;
                        }
#endif

                        /** --big-endian */
                        case 'b':
                        {
                                _c.is_big_endian = TRUE;
                                break;
                        }

			/* invalid argument */
			case '?':
			{
				NFT_LOG(L_ERROR, "argument %d is invalid", index);
				_print_help(argv[0]);
				return NFT_FAILURE;
			}

			/* unhandled arguments */
			default:
			{
				NFT_LOG(L_ERROR, "argument %d is invalid", index);
				break;
			}
		}
	}


	_c.files = &argv[optind];


	return NFT_SUCCESS;
}
Example #6
0
int main(int argc, char *argv[])
{
        nft_log_level_set(L_DEBUG);

        /* register new dummy hardware called "nlo01" */
        LedHardware *h;
        if(!(h = led_hardware_new("nlo01", "dummy")))
        {
                NFT_LOG(L_ERROR, "Hardware creation FAILED");
                return -1;
        }

        /* initialize any hardware that can be found (id="*"), define 12
         * connected LEDs (4 BGR pixels). Greyscale data should be provided in
         * unsigned 8-bit format */
        if(!led_hardware_init(h, "*", 12, "BGR u8"))
        {
                NFT_LOG(L_ERROR, "failed to initialize hardware");
                return -1;
        }


                /**********************************************************************
		 * niftyled basically provides 2 ways to send data to LED hardware:
		 * 
		 * 1.  set "raw" greyscale value of a LED in a chain connected to a
		 *     correctly initialized LED hardware
		 * 
		 * 1.1 use the API to set each greyscale value
		 * 
		 * 1.2 directly write to the RAW buffer
		 *
		 *
		 * 2. send a complete pixelframe and have niftyled translate that to 
		 *    mapped LEDs connected to one or more LED hardware devices 
		 *    according to the current setup configuration (where each LED
		 *    has been assigned a X/Y coordinate and a "Component" that refers 
		 *    to a component of a pixel at this position in the pixelframe)
		 */


                /****** METHOD 1.1 *******/
        /* get chain of this hardware */
        LedChain *c = led_hardware_get_chain(h);

        /* set all LEDs to half brightness */
        LedCount l;
        for(l = 0; l < led_chain_get_ledcount(c); l++)
        {
                led_chain_set_greyscale(c, l, 128);
        }

        /* send data to hardware */
        led_hardware_send(h);

        /* show previously sent hardware (latch to LEDs) */
        led_hardware_show(h);



                /****** METHOD 1.2 *******/
        /* ...or access raw pixelbuffer */
        uint8_t *pixels = led_chain_get_buffer(c);

        /* walk all pixels to turn on all LEDs */
        int i;
        for(i = 0; i < 16; i++)
                pixels[i] = 0xff;

        /* send data to hardware */
        led_hardware_send(h);

        /* show previously sent hardware (latch to LEDs) */
        led_hardware_show(h);


                /****** METHOD 2 - s. ledcat sources *******/


        /* deinitialize hardware */
        led_hardware_deinit(h);

        return 0;

}
Example #7
0
int main(int argc, char *argv[])
{
    int result = EXIT_FAILURE;

    /* do preliminary version checks */
    if(!NFT_PREFS_CHECK_VERSION)
    		return EXIT_FAILURE;

	/* set loglevel */
	nft_log_level_set(L_MAX);
	
	/* initialize libniftyprefs */
	NftPrefs *prefs;
	if(!(prefs = nft_prefs_init(1)))
	{
			NFT_LOG(L_ERROR, "initialize prefs");
			goto _deinit;
	}

	/* register "people" class to niftyprefs */
	if(!
	   (nft_prefs_class_register
		(prefs, PEOPLE_NAME, &_people_from_prefs, NULL)))
	{
			NFT_LOG(L_ERROR, "failed to register class");
			goto _deinit;
	}

	/* register "person" class to niftyprefs */
	if(!
	   (nft_prefs_class_register
		(prefs, PERSON_NAME, &_person_from_prefs, NULL)))
	{
			NFT_LOG(L_ERROR, "failed to register class");
			goto _deinit;
	}

	/* register updater for v0 "person" nodes */
	if(!nft_prefs_updater_register(prefs, _update_person, PERSON_NAME, 0, NULL))
	{
			NFT_LOG(L_ERROR, "failed to register updater");
			goto _deinit;
	}


	                               
	/* parse file to prefs node */
	NftPrefsNode *node;
	if(!(node = nft_prefs_node_from_file(prefs, "test-prefs.xml")))
	{
			NFT_LOG(L_ERROR,
					"failed to parse prefs file \"test-prefs.xml\"");
			goto _deinit;
	}

	/* dump updated node to file */
	if(!nft_prefs_node_to_file(prefs, node, "test-prefs_updated.xml", true))
	{
			NFT_LOG(L_ERROR, 
			        "failed to save prefs file \"test-prefs_updated.xml\"");
			goto _deinit;
	}
	
	/* create object from node */
	struct People *people;
	if(!(people = nft_prefs_obj_from_node(prefs, node, NULL)))
	{
			NFT_LOG(L_ERROR, "failed to create object from prefs node");
			goto _deinit;
	}

	/* free node */
	nft_prefs_node_free(node);

	/* process all persons */
	size_t n;
	for(n = 0; n < people->people_count; n++)
	{
			/* print info */
			printf("\tperson(name=\"%s\", email=\"%s@%s\", age=\"%d\", vitality=\"%s\")\n", 
			       	people->people[n]->name, 
			       	people->people[n]->email_user, 
			       	people->people[n]->email_host, 
			       	people->people[n]->age, 
			       	people->people[n]->alive ? "alive" : "dead");
	}

	/* we should get what we put in */
	if((strcmp(people->people[0]->name, "Bob") != 0) ||
	   (strcmp(people->people[0]->email_user, "bob") != 0) ||
	   (strcmp(people->people[0]->email_host, "example.com") != 0) ||
	   (people->people[0]->age != 30) ||
	   (people->people[0]->alive != true) ||
	   (strcmp(people->people[1]->name, "Alice") != 0) ||
	   (strcmp(people->people[1]->email_user, "alice") != 0) ||
	   (strcmp(people->people[1]->email_host, "example.com") != 0) ||
	   (people->people[1]->age != 30) ||
	   (people->people[1]->alive != false))
	{
			NFT_LOG(L_ERROR,
					"Updated input doesn't match output!");
			goto _deinit;
	}


	/* all good */
	result = EXIT_SUCCESS;

_deinit:
	nft_prefs_deinit(prefs);

	return result;
}
Example #8
0
int main(int argc, char *argv[])
{
        /** return value of main() */
        int res = 1;
        /* current configuration */
        LedPrefs *prefs = NULL;
	/* current setup */
	LedSetup *setup = NULL;
        /** framebuffer for captured image */
        LedFrame *frame = NULL;



        /* check binary version compatibility */
        NFT_LED_CHECK_VERSION


        /* set default loglevel to INFO */
	nft_log_level_set(L_INFO);


	/* initialize exit handlers */
	int signals[] = { SIGHUP, SIGINT, SIGQUIT, SIGABRT };
	unsigned int i;
	for(i=0; i<sizeof(signals)/sizeof(int); i++)
	{
		if(signal(signals[i], _exit_signal_handler) == SIG_ERR)
		{
			NFT_LOG_PERROR("signal()");
			goto _m_exit;
		}
	}



        /* default fps */
        _c.fps = 25;

        /* default mechanism */
        _c.method = METHOD_MIN+1;

        /* default config-filename */
        if(!led_prefs_default_filename(_c.prefsfile, sizeof(_c.prefsfile), ".ledcap.xml"))
                goto _m_exit;

        /* parse cmdline-arguments */
        if(!_parse_args(argc, argv))
                goto _m_exit;


	/* print welcome msg */
	NFT_LOG(L_INFO, "%s %s (c) D.Hiepler 2006-2012", PACKAGE_NAME, ledcap_version_long());
	NFT_LOG(L_VERBOSE, "Loglevel: %s", nft_log_level_to_string(nft_log_level_get()));



	/* initialize preferences context */
    	if(!(prefs = led_prefs_init()))
    		return -1;

	/* parse prefs-file */
    	LedPrefsNode *pnode;
    	if(!(pnode = led_prefs_node_from_file(_c.prefsfile)))
    	{
		NFT_LOG(L_ERROR, "Failed to open configfile \"%s\"",
		        		_c.prefsfile);
		goto _m_exit;
	}

        /* create setup from prefs-node */
    	if(!(setup = led_prefs_setup_from_node(prefs, pnode)))
    	{
		NFT_LOG(L_ERROR, "No valid setup found in preferences file.");
		led_prefs_node_free(pnode);
		goto _m_exit;
	}

    	/* free preferences node */
    	led_prefs_node_free(pnode);




        /* determine width of input-frames */
        LedFrameCord width, height;
        if((width = led_setup_get_width(setup)) > _c.width)
        {
                NFT_LOG(L_WARNING, "LED-Setup width (%d) > our width (%d). Using setup-value",
                        width,_c.width);
                /* use dimensions of mapped chain */
                _c.width = width;
        }

        /* determine height of input-frames */
        if((height = led_setup_get_height(setup)) > _c.height)
        {
                NFT_LOG(L_WARNING, "LED-Setup height (%d) > our height (%d). Using setup-value.",
                        height, _c.height);
                /* use dimensions of mapped chain */
                _c.height = height;
        }


        if(_c.width < 0)
        {
                NFT_LOG(L_ERROR, "width (%d) < 0", _c.width);
                goto _m_exit;
        }

        if(_c.height < 0)
        {
                NFT_LOG(L_ERROR, "height (%d) < 0", _c.height);
                goto _m_exit;
        }

        /* sanitize x-offset @todo check for maximum */
        if(_c.x < 0)
        {
                NFT_LOG(L_ERROR, "Invalid x coordinate: %d, using 0", _c.x);
                _c.x = 0;
        }

        /* sanitize y-offset @todo check for maximum */
        if(_c.y < 0)
        {
                NFT_LOG(L_ERROR, "Invalid y coordinate: %d, using 0", _c.y);
                _c.y = 0;
        }


        /* initialize capture mechanism (only imlib for now) */
        if(!capture_init(_c.method))
                goto _m_exit;

        /* allocate framebuffer */
        NFT_LOG(L_INFO, "Allocating frame: %dx%d (%s)",
	        _c.width, _c.height, capture_format());
        if(!(frame = led_frame_new(_c.width, _c.height, led_pixel_format_from_string(capture_format()))))
                goto _m_exit;

        /* respect endianess */
        led_frame_set_big_endian(frame, capture_is_big_endian());

        /* get first hardware */
        LedHardware *hw;
        if(!(hw = led_setup_get_hardware(setup)))
                goto _m_exit;

        /* initialize pixel->led mapping */
        if(!led_hardware_list_refresh_mapping(hw))
                goto _m_exit;

        /* precalc memory offsets for actual mapping */
        if(!led_chain_map_from_frame(led_hardware_get_chain(hw), frame))
                goto _m_exit;

        /* set saved gain to all registered hardware instances */
        if(!led_hardware_list_refresh_gain(hw))
                goto _m_exit;


	/* print some debug-info */
        led_frame_print(frame, L_VERBOSE);
        led_hardware_print(hw, L_VERBOSE);


        /* initially sample time for frametiming */
        if(!led_fps_sample())
                goto _m_exit;


        /* output some useful info */
        NFT_LOG(L_INFO, "Capturing %dx%d pixels at position x/y: %d/%d", _c.width, _c.height, _c.x, _c.y);

        /* loop until _c.running is set to FALSE */
        _c.running = TRUE;
        while(_c.running)
        {
                /* capture frame */
                if(!(capture_frame(frame, _c.x, _c.y)))
                        break;

                /* print frame for debugging */
                //led_frame_buffer_print(frame);

                /* map from frame */
                LedHardware *h;
                for(h = hw; h; h = led_hardware_list_get_next(h))
                {
                        if(!led_chain_fill_from_frame(led_hardware_get_chain(h), frame))
                        {
                                NFT_LOG(L_ERROR, "Error while mapping frame");
                                break;
                        }
                }


                /* send frame to hardware(s) */
                led_hardware_list_send(hw);

                /* delay in respect to fps */
                if(!led_fps_delay(_c.fps))
                        break;

                /* show frame */
                led_hardware_list_show(hw);

                /* save time when frame is displayed */
                if(!led_fps_sample())
                        break;
        }



        /* mark success */
        res = 0;

_m_exit:
        /* deinitialize capture mechanism */
        capture_deinit();

	/* free frame */
        led_frame_destroy(frame);

	/* destroy config */
        led_setup_destroy(setup);

	/* destroy config */
        led_prefs_deinit(prefs);


        return res;
}
Example #9
0
/** parse commandline arguments */
static NftResult _parse_args(int argc, char *argv[])
{
	int index, argument;

	static struct option loptions[] =
	{
		{"help", 0, 0, 'h'},
                {"plugin-help", 0, 0, 'p'},
		{"loglevel", required_argument, 0, 'l'},
		{"config", required_argument, 0, 'c'},
                {"x", required_argument, 0, 'x'},
                {"y", required_argument, 0, 'y'},
                {"dimensions", required_argument, 0, 'd'},
                {"fps", required_argument, 0, 'f'},
                {"mechanism", required_argument, 0, 'm'},
		{0,0,0,0}
	};

	while((argument = getopt_long(argc, argv, "hpl:c:x:y:d:f:m:", loptions, &index)) >= 0)
	{

		switch(argument)
		{
			/** --help */
			case 'h':
			{
				_print_help(argv[0]);
				return NFT_FAILURE;
			}

                        /* --plugin-help */
                        case 'p':
                        {
                                _print_plugin_help();
                                return NFT_FAILURE;
                        }

                        /* --mechanism */
                        case 'm':
                        {
                                _c.method = capture_method_from_string(optarg);
                                break;
                        }

			/** --config */
			case 'c':
			{
				/* save filename for later */
				strncpy(_c.prefsfile, optarg, sizeof(_c.prefsfile));
				break;
			}

                        /** --x */
                        case 'x':
                        {
                                if(sscanf(optarg, "%d", (int*) &_c.x) != 1)
				{
					NFT_LOG(L_ERROR, "Invalid x-coordinate \"%s\" (Use an integer)", optarg);
					return NFT_FAILURE;
				}
				break;
                        }

                        /** --y */
                        case 'y':
                        {
                                if(sscanf(optarg, "%d", (int*) &_c.y) != 1)
				{
					NFT_LOG(L_ERROR, "Invalid y-coordinate \"%s\" (Use an integer)", optarg);
					return NFT_FAILURE;
				}
				break;
                        }

                        /** --dimensions */
                        case 'd':
                        {
                                if(sscanf(optarg, "%dx%d", (int*) &_c.width, (int*) &_c.height) != 2)
				{
					NFT_LOG(L_ERROR, "Invalid dimension \"%s\" (Use something like 320x400)", optarg);
					return NFT_FAILURE;
				}
                                break;
                        }

                        /** --fps */
                        case 'f':
                        {
                                if(sscanf(optarg, "%d", (int*) &_c.fps) != 1)
				{
					NFT_LOG(L_ERROR, "Invalid framerate \"%s\" (Use an integer)", optarg);
					return NFT_FAILURE;
				}
				break;
                        }

			/** --loglevel */
			case 'l':
			{
				if(!nft_log_level_set(nft_log_level_from_string(optarg)))
                                {
                                        _print_loglevels();
                                        return NFT_FAILURE;
                                }
				break;
			}

			/* invalid argument */
			case '?':
			{
				NFT_LOG(L_ERROR, "argument %d is invalid", index);
				_print_help(argv[0]);
				return NFT_FAILURE;
			}

			/* unhandled arguments */
			default:
			{
				NFT_LOG(L_ERROR, "argument %d is invalid", index);
				break;
			}
		}
	}


	return NFT_SUCCESS;
}