
// Copyright 2007 Regents of the University of California

// SETI_BOINC 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 2, or (at your option) any later
// version.

// SETI_BOINC 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 SETI_BOINC; see the file COPYING.  If not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

// In addition, as a special exception, the Regents of the University of
// California give permission to link the code of this program with libraries
// that provide specific optimized fast Fourier transform (FFT) functions and
// distribute a linked executable.  You must obey the GNU General Public 
// License in all respects for all of the code used other than the FFT library
// itself.  Any modification required to support these libraries must be
// distributed in source code form.  If you modify this file, you may extend 
// this exception to your version of the file, but you are not obligated to 
// do so. If you do not wish to do so, delete this exception statement from 
// your version.

#include <errno.h>
#include "astropulse.h"
#include "ap_gfx_main.h"
#include "boinc_api.h"
#include "graphics2.h"

REDUCED_ARRAY_GEN rarray;
AP_GDATA* ap_gdata;
bool nographics_flag = false;

void update_shmem() {
}

void ap_graphics_init(APP_INIT_DATA& app_init_data) {
   // Checking for blank statefile
   printf("In ap_gfx_main.cpp: in ap_graphics_init(): Starting client.\n");
   fprintf(stderr, "In ap_gfx_main.cpp: in ap_graphics_init(): Starting client.\n");
   AP_SHMEM* &ap_shmem=Astropulse::client.ap_shmem;
/*  This stuff will need to be redefined */
#if 0
    if ((app_init_data.host_info.m_nbytes != 0)  &&
        (app_init_data.host_info.m_nbytes <= (double)(48*1024*1024))
    ) {
        fprintf(stderr,"Low memory machine... Disabling graphics.\n");
        fprintf(stderr,"%f <= %f\n",app_init_data.host_info.m_nbytes,(double)48*1024*1024);
        nographics_flag = true;
        return;
    }
#endif
    // ap_shmem is defined extern in ap_gfx_main.h
    ap_shmem = (AP_SHMEM *)(boinc_graphics_make_shmem("astropulse", sizeof(AP_SHMEM)));
    if (!ap_shmem) {
        fprintf(stderr, "boinc_graphics_make_shmem failed: %d\n", errno);
        exit(ERR_SHMEM_NAME);
    }

    // initialize ap_shmem with placement new.
	// VC++ doesn't support placement new in debug mode!?!?!  What the hell?
#if !defined(_DEBUG) || !defined(_MSC_VER)
    ap_shmem = new (ap_shmem) AP_SHMEM();
#else
    // VC++ doesn't support placement new in debug mode!?!?!  What the hell?
	// Fortunately AP_SHMEM doesn't contain any self-referential pointers.
    AP_SHMEM *tmp=new AP_SHMEM();
	memcpy(ap_shmem,tmp,sizeof(AP_SHMEM));
	delete tmp;
#endif
    ap_gdata = &(ap_shmem->ap_gdata);
    boinc_register_timer_callback(update_shmem);
}
