#define _GNU_SOURCE
#include <gtk/gtk.h>
#include <sys/time.h>
#include <stdio.h>      /* standard I/O                */
#include <stdlib.h>     /* for exit   - 1 occurrence   */
#include <unistd.h>
#include <string.h>

FILE *fp;
FILE* boincstrs;
char linestring[100];

void title() {
        printf("\tniceboinc is a product of the linuxsociety team\n");
        printf("\tThis is version 0.0.1 dated 04/15/2009\n");
        //printf("\t\n\n");
return;
}

struct GtkDialog
{
      GtkWindow window;    
      GtkWidget *table;
      GtkWidget *buffer;
      GtkWidget *view;
};

/*
static void callback1( GtkWidget *widget,
                      gpointer   data )
{
	title();
	system("memdir/lsmem");
        buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
    
        gchar *str;
    
        //fp=fopen("memdir/lsmem.log", "r");
        //fscanf(fp, "%s", str);
        //fclose(fp);

        gtk_text_buffer_set_text (buffer,str, -1);

        gtk_table_attach_defaults (GTK_TABLE (table), view, 1, 2, 0, 7);
    gtk_widget_show (view);
//system("results&");

}

static void callback2( GtkWidget *widget,
                      gpointer   data )
{
	title();
        system("aludir/lsalu");
	system("results&");                                  
}

static void callback3( GtkWidget *widget,
                      gpointer   data )
{
	title();
        system("fpudir/lsfpu");                                  
        system("results&");
}

static void callback4( GtkWidget *widget,
                      gpointer   data )
{
	title();
        system("compiledir/lscompile");                                  
        system("results&");
}

static void callback5( GtkWidget *widget,
                      gpointer   data )
{
	title();
        system("compressiondir/lscompress");                                  
        system("results&");
}

static void callback6( GtkWidget *widget,
                      gpointer   data )
{
	title();
        system("mediadir/lsmedia");                                  
        system("results&");
}

static void callback7( GtkWidget *widget,
                      gpointer   data )
{
	title();
        system("gfxdir/lsgfx");                                  
        system("results&");
}

static void callback8( GtkWidget *widget,
                      gpointer   data )
{
	title();
        system("memdir/lsmem");
        system("aludir/lsalu");
        system("fpudir/lsfpu");
        system("compiledir/lscompile");
        system("compressiondir/lscompress");
        system("mediadir/lsmedia");
        system("gfxdir/lsgfx");
        system("results&");
                                  
}

*/

static void callback1( GtkWidget *widget,
                      gpointer   data )  
{
        system("killall -9 nice-19.run&");
        system("killall -9 nice+19.run&");
        system("killall -9 nice-custom.run&");
	system("nohup ./nice-19.run&");
	//PID2=$(ps aux | grep astro | awk '{ print $2 }')
	//for PID in $PID2; do renice -19 "$PID" &> /dev/null ; done

/*        fp=fopen("boincstrs.txt", "r");
        //fscanf(fp, "%s", str);
        //fclose(fp);  


        if( ( boincstrs = fopen( "boincstrs.txt", "r" ) ) == NULL ) {
                fprintf( stderr, "Error opening boincstrs.txt\n" );
                exit( 1 );
        }

        while( fgets(linestring, sizeof(linestring), boincstrs) != NULL)
        {

		//system("for PID in $PID2; do renice -19 "$PID" &> /dev/null ; done");
		printf("%s", linestring);
		

	}
	fclose(fp);
*/        //fclose(out);
}


static void callback2( GtkWidget *widget,
                      gpointer   data )
{
	system("killall -9 nice-19.run&");
	system("killall -9 nice+19.run&");
        system("killall -9 nice-custom.run&");
        system("nohup ./nice+19.run&");
}

static void callback3( GtkWidget *widget,
                      gpointer   data )
{
        system("killall -9 nice-19.run&");
        system("killall -9 nice+19.run&");
        system("killall -9 nice-custom.run&");
        system("nohup ./nice-custom.run&");
}


static void callback4( GtkWidget *widget,
                      gpointer   data )
{
        system("killall -9 nice-19.run&");
        system("killall -9 nice+19.run&");
	system("killall -9 nice-custom.run&");
        //system("nice+19.run&");
}



static gboolean delete_event( GtkWidget *widget,
                              GdkEvent  *event,
                              gpointer   data )
{
    gtk_main_quit ();
    return FALSE;
}




/*
gboolean resize_image(GtkWidget *widget, GdkEvent *event, GtkWidget *window)
{
	//GdkPixbuf *pixbuf =	gtk_image_get_pixbuf(GTK_IMAGE(widget));
	if (pixbuf == NULL)
	{
		g_printerr("Failed to resize image\n");
		return 1;
	}
	
	printf("Width: %i\nHeight%i\n", widget->allocation.width, widget->allocation.height);
	
	pixbuf = gdk_pixbuf_scale_simple(pixbuf, widget->allocation.width, widget->allocation.height, GDK_INTERP_BILINEAR);
	
	gtk_image_set_from_pixbuf(GTK_IMAGE(widget), pixbuf);
	
	return FALSE;
}

*/




int main( int   argc,
          char *argv[] )
{
    GtkWidget *window;
    GtkWidget *table;
    GtkWidget *button;
    //GdkPixbuf *pixbuf;
    //GtkWidget *picture;


    gtk_init (&argc, &argv);
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title (GTK_WINDOW (window), "niceboinc v0.0.1");

    g_signal_connect (G_OBJECT (window), "delete_event",
		      G_CALLBACK (delete_event), NULL);

    gtk_container_set_border_width (GTK_CONTAINER (window), 10);

    table = gtk_table_new (2, 5, FALSE);


    gtk_container_add (GTK_CONTAINER (window), table);

    button = gtk_button_new_with_label ("\t\tSet nice -19\t\t");
    g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (callback1), (gpointer) "button 1");
	gtk_table_attach_defaults (GTK_TABLE (table), button, 0, 1, 0, 1);
    gtk_widget_show (button);

    button = gtk_button_new_with_label ("\t\tSet nice 19\t\t");
    g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (callback2), (gpointer) "button 2");
	gtk_table_attach_defaults (GTK_TABLE (table), button, 0, 1, 1, 2);
    gtk_widget_show (button);

    button = gtk_button_new_with_label ("\t\tSet nice Custom\t\t");
    g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (callback3), (gpointer) "button 3");
	gtk_table_attach_defaults (GTK_TABLE (table), button, 0, 1, 2, 3);
    gtk_widget_show (button);

    button = gtk_button_new_with_label ("\t\tStop all scripts\t\t");
    g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (callback4), (gpointer) "button 4");
	gtk_table_attach_defaults (GTK_TABLE (table), button, 0, 1, 3, 4);
    gtk_widget_show (button);
/*
    button = gtk_button_new_with_label ("Run Compression Test");
    g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (callback5), (gpointer) "button 5");
	gtk_table_attach_defaults (GTK_TABLE (table), button, 0, 1, 4, 5);
    gtk_widget_show (button);

    button = gtk_button_new_with_label ("Run Media Test");
    g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (callback6), (gpointer) "button 6");
	gtk_table_attach_defaults (GTK_TABLE (table), button, 0, 1, 5, 6);
    gtk_widget_show (button);

    button = gtk_button_new_with_label ("Run Graphics Test");
    g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (callback7), (gpointer) "button 7");
	gtk_table_attach_defaults (GTK_TABLE (table), button, 0, 1, 6, 7);
    gtk_widget_show (button);

    button = gtk_button_new_with_label ("Run All Tests");
    g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (callback8), (gpointer) "button 8");
	gtk_table_attach_defaults (GTK_TABLE (table), button, 0, 1, 7, 8);
    gtk_widget_show (button);

*/
 /* Add a "close" button to the bottom of the dialog */
    button = gtk_button_new_with_label ("close");
    g_signal_connect_swapped (G_OBJECT (button), "clicked", G_CALLBACK (delete_event), G_OBJECT (window));
    gtk_table_attach_defaults (GTK_TABLE (table), button, 0, 1, 8, 9);
    gtk_widget_show (button);


//pixbuf = gdk_pixbuf_new_from_file_at_size ("test.png", -1, -1, NULL);

//button = gtk_image_new_from_file("test.png");

//pixbuf = gdk_pixbuf_new_from_file_at_scale("test.png", -1, -1, TRUE, NULL);
//button=gtk_image_new_from_pixbuf(pixbuf);

//button = gtk_image_new_from_file ("test.png");

//g_signal_connect(button, "expose-event", G_CALLBACK(resize_image), (gpointer)table);
    //g_signal_connect_swapped (G_OBJECT (button), "clicked", G_CALLBACK (delete_event), G_OBJECT (window));
    //gtk_table_attach_defaults (GTK_TABLE (table), button, 0, 1, 8, 9);

//gtk_table_attach (GTK_TABLE (table), button, 0, 1, 8, 9, 1, 1, 1, 1);
    gtk_widget_show (button);

//gtk_container_add(GTK_CONTAINER(table), GTK_WIDGET(button));
gtk_container_add(GTK_CONTAINER(window), button);


    gtk_widget_show (table);

    gtk_widget_show (window);
    /* Rest in gtk_main and wait for the fun to begin! */
    gtk_main ();

    return 0;
}
