/*
    lsadl is a frontend/wrapper to the adl_sdk using gtk
    Copyright (C) 2011  Sterling Pickens

    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 3 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, see <http://www.gnu.org/licenses/>.
*/

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

//#include <dlfcn.h>

#include "config.h"
#include "Globals.h"
#ifdef HAVE_PTHREAD_H
#include <pthread.h>
#elif HAVE_PTHREADS_H
#include <pthreads.h>
#endif

//#include "adl_api.h"

#include "ls_gtk.h"
#include "ls_pth.h"


extern int No_Exit;
extern pthread_t Gtk_thread_ptr;
extern pthread_t Refresh_thread_ptr;
extern pthread_mutex_t Mutex1;

//void Usage(void){
//	printf("guicalc\n");
//    printf("\t[optional]\n");
//	printf("\tadapter\t= adapter int 0 to last unique adapter\n");
//	printf("\t\tdefaults to first adapter\n");
//}

int main(int argc, char **argv){
	printf("\t%s\n\thttp:/", PACKAGE_STRING);
	printf("/%s\n\t%s\n\n", PACKAGE_URL, PACKAGE_BUGREPORT);
	printf("Warning: Use at your own risk!\n");


	int num1 = 0;
	int ret_val = 0;
	No_Exit = 1;


	if(Perf_Alloc() == FALSE){
		printf("Perf_Alloc: memory allocation error!\n");
		printf("Exiting!\n");
		free(lpAdapterInfo);
		LSADL_Main_Control_Destroy();
		dlclose(so_handle);
		exit(1);
	}
#ifdef HAVE_GTK_G_THREAD_INIT
	g_thread_init(NULL);
#endif
	gdk_threads_init();
	gdk_threads_enter();
	gtk_init(&argc, &argv);

	if(pthread_create(&Gtk_thread_ptr, NULL, GTK_Func, NULL) != 0){
		printf("Error: pthread_create failed to launch Gtk thread !\n");
		printf("Exiting!\n");
		free(lpAdapterInfo);
		LSADL_Main_Control_Destroy();
		dlclose(so_handle);
		exit(1);
	}
	if(pthread_create(&Refresh_thread_ptr, NULL, Refresh_Func, NULL) != 0){
		printf("Error: pthread_create failed to launch Refresh thread !\n");
		printf("Exiting!\n");
		//fixme kill other thread
		free(lpAdapterInfo);
		LSADL_Main_Control_Destroy();
		dlclose(so_handle);
		exit(1);
	}
	if(pthread_join(Gtk_thread_ptr, NULL) != 0){
		printf("Error: pthread_join failed to join Gtk thread!\n");
		printf("\tExiting!\n");
		free(lpAdapterInfo);
		LSADL_Main_Control_Destroy();
		dlclose(so_handle);
		exit(1);
	}
	gdk_threads_leave();
	if(pthread_join(Refresh_thread_ptr, NULL) != 0){
		printf("Error: pthread_join failed to join Refresh thread!\n");
		printf("\tExiting!\n");
		free(lpAdapterInfo);
		LSADL_Main_Control_Destroy();
		dlclose(so_handle);
		exit(1);
	}
	if(Perf_Dealloc() == FALSE){
		printf("Perf_Delloc: memory allocation error!\n");
	}

	LSADL_Main_Control_Destroy();
	free(lpAdapterInfo);
	dlclose(so_handle);
	return 0;
}
