int main(int argc, char* argv[])
{
  int               i;
  double            msec;
  double            decTime;
  double            encTime;
  Ipp8u*            buf;
  IM_TYPE           fmtIn;
  IM_TYPE           fmtOut;
  ExcStatus         res;
  CIppImage         image;
  CommandLine       cmdline;
  CFormatDetector   detector;
  BaseStream::TSize cnt;
  BaseStream::TSize size;
  bool              help_flag;
  bool              is_raw = false;

  CStdFileInput   fi;
  CStdFileOutput  fo;
  CMemBuffInput   mi;
  CMemBuffOutput  mo;

  CmdOptions cmdOptions;
  cmdOptions.loops = 1;
  ippStaticInit();

  copyright_();

  // Common params
  cmdline.AddKey("i","input file name", cmdOptions.src, 1, (StringA)"uic_test_image.jpg", IT_UNKNOWN);
  cmdline.AddKey("o","output file name (out.jp2 - default)", cmdOptions.dst, 1, (StringA)"out.jp2", IT_UNKNOWN);
  cmdline.AddKey("r","treat input image as raw (0 - default)", is_raw, 0, 0, IT_UNKNOWN);
  cmdline.AddKey("t","advanced timing, 0 - disable (default); 1 - enable", cmdOptions.timing, 1, 0, IT_UNKNOWN);
  cmdline.AddKey("m","number of loops for advanced timing (1 - default)", cmdOptions.loops, 1, 1, IT_UNKNOWN);
  cmdline.AddKey("d", "print problem diagnostics messages", cmdOptions.verbose_mode, 0, 0, IT_UNKNOWN);
  cmdline.AddKey("n","number of threads", cmdOptions.nthreads, 1, 0, IT_UNKNOWN);
  cmdline.AddKey("h","help key", help_flag, 0, 0, IT_UNKNOWN);

  cmdline.AddKey("W","width resolution", cmdOptions.res_width, 1, 0, IT_RAW);
  cmdline.AddKey("H","height resolution", cmdOptions.res_height, 1, 0, IT_RAW);
  cmdline.AddKey("D","data offset", cmdOptions.data_offset, 1, 0, IT_RAW);
  cmdline.AddKey("C","color format: gray, rgb (default), rgba, cmyk", cmdOptions.color_format, 1, (StringA)"rgb", IT_RAW);
  cmdline.AddKey("P","pixel format: 8u (default), 16u, 16s, 32u, 32s, 32f", cmdOptions.pixel_format, 1, (StringA)"8u", IT_RAW);

  cmdline.AddKey("q","quality [1...100]", cmdOptions.jpg_quality, 1, 100, IT_JPEG);
  cmdline.AddKey("s","sampling, 0 - 444 (default); 1 - 422; 2 - 411", cmdOptions.sampling, 1, 0, IT_JPEG);
  cmdline.AddKey("j","jpeg encoder mode: b - BASELINE (8-bit lossy); e - EXTENDED (12-bit lossy); p - PROGRESSIVE  (8-bit progressive); l - LOSSLESS (2-16 bit lossless)", cmdOptions.jmode, 1, (StringA)"b", IT_JPEG);

  cmdline.AddKey("q","quality [1...100]", cmdOptions.jpg_quality, 1, 100, IT_JPEG2000);
  cmdline.AddKey("l","encoding mode, 0 - lossy encode (default); 1 - lossless encoding", cmdOptions.lossless, 1, 0, IT_JPEG2000);

  cmdline.AddKey("f","activate all filters", cmdOptions.png_filter[4], 0, 0, IT_PNG);
  cmdline.AddKey("fs","add sub filter", cmdOptions.png_filter[0], 0, 0, IT_PNG);
  cmdline.AddKey("fu","add up filter", cmdOptions.png_filter[1], 0, 0, IT_PNG);
  cmdline.AddKey("fa","add average filter", cmdOptions.png_filter[2], 0, 0, IT_PNG);
  cmdline.AddKey("fp","add Paeth filter", cmdOptions.png_filter[3], 0, 0, IT_PNG);

  cmdline.AddKey("q","quality [1...255] (1 - lossless, default; less is better)", cmdOptions.jxr_quality, 1, 1, IT_JPEGXR);
  cmdline.AddKey("Q","planar alpha quality [1...255] (1 - lossless, default; less is better)", cmdOptions.jxr_aquality, 1, 1, IT_JPEGXR);
//  cmdline.AddKey("s","sampling, 0 - 444 (default); 1 - 422; 2 - 420", cmdOptions.sampling, 1, 0, IT_JPEGXR);
//  cmdline.AddKey("f","bitstream order, 0 - spatial (default); 1 - frequency", cmdOptions.bitstream, 1, 0, IT_JPEGXR);
  cmdline.AddKey("l","overlap mode, 0 - no overlap (default); 1 -  overlap one; 2 - overlap two", cmdOptions.overlap, 1, 0, IT_JPEGXR);
  cmdline.AddKey("a","alpha mode, 0 - interleaved alpha (default); 1 - planar alpha", cmdOptions.alpha_mode, 1, 0, IT_JPEGXR);
  cmdline.AddKey("F","trim flexbits [0...15] (0 - all flexbits, default; 15 - no flexbits)", cmdOptions.trim, 1, 0, IT_JPEGXR);
  cmdline.AddKey("b","bands mode, 0 - all bands (default); 1 - no flexbits; 2 - no highpass; 3 - dc only", cmdOptions.bands, 1, 0, IT_JPEGXR);
  cmdline.AddKey("c","encode CMYK images directly, without color conversion", cmdOptions.cmyk_direct, 0, 0, IT_JPEGXR);
  cmdline.AddKey("v","color conversion bits shift value for 16-bit and 32-bit images [0...32]", cmdOptions.bits_shift, 1, -1, IT_JPEGXR);
  cmdline.AddKey("U","amount of tiles per rows and columns (1 1 - default)", cmdOptions.tiles_amount[0], 2, 1, IT_JPEGXR);
  cmdline.AddKey("X","maximum tiles size in macroblocks (0 0 - default; 1 = 16px; override U argument)", cmdOptions.tiles_sizes[0], 2, 0, IT_JPEGXR);
#ifdef USE_TBB
  cmdline.AddKey("u","multithread mode, 0 - pipeline threading (threads limited to 3); 1 - tile threading (threads limited to number of tiles); 2 - mixed threading (default; threads limited to number of tiles + 3)", cmdOptions.jxr_thread_mode, 1, 2, IT_JPEGXR);
#endif

  if(argc == 1)
  {
    cmdline.HelpMessage(format_str);
    return -1;
  }

  if(!cmdline.Parse(argv, argc, IT_UNKNOWN, IT_UNKNOWN))
  {
    cmdline.HelpMessage(format_str);
    printf("Invalid argument!\n");
    return -1;
  }

  if(help_flag)
    cmdline.HelpMessage(format_str);

  fmtOut = ImageFormatFromExtension((const char*)cmdOptions.dst);
  if(IT_UNKNOWN == fmtOut || IT_DICOM == fmtOut)
  {
    printf("Unsupported output image format!\n");
    return -1;
  }

  if(!BaseStream::IsOk(fi.Open(cmdOptions.src)))
  {
    printf("Can not open input file!\n");
    return 1;
  }

  if(!BaseStream::IsOk(fo.Open(cmdOptions.dst)))
  {
    printf("Can not open output file!\n");
    return 1;
  }

  if(is_raw)
    fmtIn = IT_RAW;
  else
    fmtIn = detector.ImageFormat(fi);
  if(IT_UNKNOWN == fmtIn)
  {
    printf("Unsupported input image format!\n");
    return -1;
  }

  if(!cmdline.Parse(argv, argc, fmtIn, fmtOut))
  {
    if(!help_flag)
      cmdline.HelpMessage(format_str);
    printf("Invalid argument!\n");
    return -1;
  }

  CheckParam(cmdOptions.timing, 0, 1);
  CheckParam(cmdOptions.loops,  1, IPP_MAX_16U);

  if(!cmdOptions.timing)
  {
    res = DecodeImage(fi, image, cmdOptions, fmtIn, NULL);
    if(!IsOk(res))
    {
      printf("Error in DecodeImage finction!\n");
      return 1;
    }
  }
  else
  {
    msec = 0.0;

    fi.Size(size);

    buf = (Ipp8u*)ippMalloc((int)size);
    if(0 == buf)
      return 1;

    res = fi.Read(buf, size, cnt);
     if(!IsOk(res))
      return 1;
#if !defined(__WIN32)
      printf("WARNING: initialization of CTimer may take long!\n");
#endif

    for(i = 0; i < cmdOptions.loops; i++)
    {
      mi.Open(buf, (int)size);

      res = DecodeImage(mi, image, cmdOptions, fmtIn, &decTime);
      if(!IsOk(res))
        return 1;

      msec += decTime;

      mi.Close();
    }

    ippFree(buf);

    decTime = msec / cmdOptions.loops;
  }

  printf("image: %s, %dx%dx%d, %d-bits %s, color: %s, sampling: %s\n",
    (const char*)cmdOptions.src,
    image.Width(),
    image.Height(),
    image.NChannels(),
    image.Precision(),
    pxformat_str[image.Format()],
    color_str[image.Color()],
    sampling_str[image.Sampling()]);

  if(cmdOptions.timing) printf("decode time: %.2f msec\n", decTime);


  if(!cmdOptions.timing)
  {
    res = EncodeImage(image, fo, cmdOptions, fmtOut, NULL);
    if(!IsOk(res))
    {
      printf("Error in EncodeImage finction!\n");
      return 1;
    }
  }
  else
  {
    BaseStream::TPosition pos = 0;

    msec = 0.0;
    size = image.Step()*image.Height()*2;

    buf = (Ipp8u*)ippMalloc((int)size);
    if(0 == buf)
      return 1;

    for(i = 0; i < cmdOptions.loops; i++)
    {
      mo.Open(buf, (int)size);

      res = EncodeImage(image, mo, cmdOptions, fmtOut, &encTime);
      if(!IsOk(res))
        return 1;

      msec += encTime;

      mo.Position(pos);

      mo.Close();
    }

    fo.Write(buf, pos, cnt);

    ippFree(buf);

    encTime = msec / cmdOptions.loops;
  }

  if(cmdOptions.timing) printf("encode time: %.2f msec\n", encTime);

  return 0;
} // main()
Exemple #2
0
sint32 ui_HandleKeypress(WPARAM wParam, LPARAM lParam)
{
    WORLD_DIRECTION	d;
    BOOL			move = FALSE;
#ifdef _PLAYTEST
    int i;
#endif
	BOOL isMyTurn = !g_network.IsActive() || g_network.IsMyTurn();
	Unit city;

	
	if ( g_isKMScreen) {
		switch(wParam) {
			case VK_ESCAPE:
			case VK_LEFT + 256:
			case VK_RIGHT + 256:
			case VK_UP + 256:
			case VK_DOWN + 256:
			case 29: 
			case 28: 
				
				return TRUE;
			default:
				
				km_screen_remapKey( wParam, lParam );
				return TRUE;
		}
	}
	
#ifdef _PLAYTEST
	if(commandMode) {
		switch(wParam) {
			case '\r' + 128: wParam = '\r'; break;
			case '\t' + 128: wParam = '\t'; break;
			case 8 + 128: wParam = 8; break;
		}
		commandMode = g_commandLine.AddKey(static_cast<char>(wParam));
		return TRUE;
	}
#endif






	
	if (wParam == VK_ESCAPE) {
		
		extern OptionsWindow *g_optionsWindow;
		
		if(g_c3ui->TopWindow() && g_c3ui->TopWindow() == DipWizard::GetWindow()) {
			
			
		} else if(g_keyboardHandlers.GetTail()) {
			g_keyboardHandlers.GetTail()->kh_Close();
		} else if (g_civApp->IsGameLoaded()) {
			if(g_currentMessageWindow && 
			   g_currentMessageWindow->GetMessage() &&
			   (g_theMessagePool->IsValid(*g_currentMessageWindow->GetMessage()))) {
				g_currentMessageWindow->GetMessage()->Minimize();
			} else if(g_modalMessage) {
				
				Message *msg = g_modalMessage->GetMessage();
				if(msg && g_theMessagePool->IsValid(*msg)) {
					Assert(msg->IsAlertBox());
					MessageData *data = msg->AccessData();
					if(data->GetNumButtons() <= 2 && data->GetNumButtons() > 0) {
						data->GetButton(0)->Callback();
					}
				}	
			} else if (g_optionsWindow && g_c3ui->GetWindow(g_optionsWindow->Id())) {
				
				optionsscreen_removeMyWindow(AUI_BUTTON_ACTION_EXECUTE);
			} else if(g_c3ui->TopWindow() && g_c3ui->TopWindow()->HandleKey(wParam)) {
				
			} else if(g_battleViewWindow) {
				battleview_ExitButtonActionCallback( NULL, AUI_BUTTON_ACTION_EXECUTE, 0, NULL);
			} else {
				
				optionsscreen_Initialize();
				optionsscreen_displayMyWindow(1);
			}
		}
		return TRUE;
	}

	
	aui_Window *topWindow = g_c3ui->TopWindow();
	if(topWindow && (!g_controlPanel || topWindow != g_controlPanel->GetWindow()) && topWindow != g_statusWindow) {
		if(topWindow->HandleKey(wParam))
			return 0;
	}

	
	
	if (!theKeyMap) return 0;

	
	if (g_slicEngine && g_slicEngine->RunKeyboardTrigger(static_cast<char>(wParam))) 
	{
		return 0;
	}

	if (!g_civApp->IsGameLoaded()) {
		return TRUE;
	}

	KEY_FUNCTION	kf = theKeyMap->get_function(wParam); 
	if (kf != KEY_FUNCTION_NOOP) 
	{
		g_selected_item->RegisterUIClick();
	}

	if (topWindow->IsStronglyModal() /*&& keypress_IsGameFunction(kf)*/) // I should not be able to open other windows if I have have open strongly modal windows like the DipWizzard
	{
		return 0;
	}

#if 0
	MapPoint point;
	g_tiledMap->GetMouseTilePos(point);
#endif

	switch (kf) {
#ifdef _PLAYTEST
	case KEY_FUNCTION_ENTER_COMMAND: 
		segmentlist_Display();
		break;
	case KEY_FUNCTION_ENTER_COMMAND_ALTERNATE:
		commandMode = TRUE; 
		move = FALSE; 
		break;
#endif
    case KEY_FUNCTION_NOOP: return FALSE; 
    case KEY_FUNCTION_MOVE_NORTHWEST: d = NORTHWEST; move = TRUE; break;
    case KEY_FUNCTION_MOVE_NORTH: d = NORTH; move = TRUE; break; 
    case KEY_FUNCTION_MOVE_NORTHEAST: d = NORTHEAST; move = TRUE; break; 
    case KEY_FUNCTION_MOVE_WEST: d = WEST; move = TRUE; break; 
    case KEY_FUNCTION_MOVE_EAST: d = EAST; move = TRUE; break; 
    case KEY_FUNCTION_MOVE_SOUTHWEST: d = SOUTHWEST; move = TRUE; break; 
    case KEY_FUNCTION_MOVE_SOUTH: d = SOUTH; move = TRUE; break; 
    case KEY_FUNCTION_MOVE_SOUTHEAST: d = SOUTHEAST; move = TRUE; break;

	case KEY_FUNCTION_ENTRENCH:
		if(isMyTurn) g_selected_item->Entrench();
		break;









	case KEY_FUNCTION_UNLOAD_TRANS:
	{
		sint32 order;
		if(!g_theOrderDB->GetNamedItem("ORDER_UNLOAD", order))
			return 0;

		Army a;
		if(g_selected_item->GetSelectedArmy(a))
			g_controlPanel->BeginOrderDelivery(g_theOrderDB->Access(order));
	}
	break;
	case KEY_FUNCTION_SETTLE:
	{
		g_selected_item->Settle();
		move = FALSE;
		break;
	}
	case KEY_FUNCTION_SPACE_LAUNCH:
		if(isMyTurn) {
			sint32 index;
			if(g_theOrderDB->GetNamedItem("ORDER_SPACE_LAUNCH", index)) {
				g_controlPanel->BeginOrderDelivery(g_theOrderDB->Access(index));
			}
			
			return TRUE;
		}
		break;
	case KEY_FUNCTION_DESCEND:		
		g_selected_item->Descend();
		break;
	case KEY_FUNCTION_PILLAGE:
	{
		sint32 order;
		if(!g_theOrderDB->GetNamedItem("ORDER_PILLAGE", order))
			return 0;

		Army a;
		if(g_selected_item->GetSelectedArmy(a) && a.CanPillage()) {
			g_controlPanel->BeginOrderDelivery(g_theOrderDB->Access(order));
		} else {
			g_selected_item->Pillage();
			return 0;
		}
	}
		break;
	case KEY_FUNCTION_BOMBARD:
	{
		sint32 order;
		if(!g_theOrderDB->GetNamedItem("ORDER_BOMBARD", order))
			return 0;

		Army a;
		if(g_selected_item->GetSelectedArmy(a) && a.CanBombard()) {
			g_controlPanel->BeginOrderDelivery(g_theOrderDB->Access(order));
		} else {
			return 0;
		}
	}
		




		break;
	case KEY_FUNCTION_EXPEL:		
	{
		sint32 order;
		if(!g_theOrderDB->GetNamedItem("ORDER_EXPEL", order))
			return 0;

		Army a;
		if(g_selected_item->GetSelectedArmy(a) && a.CanExpel()) {
			g_controlPanel->BeginOrderDelivery(g_theOrderDB->Access(order));
		} else {
			return 0;
		}
	}
			



		break;
	case KEY_FUNCTION_SLEEP:
		if(isMyTurn) {
			move = FALSE;

			g_selected_item->Sleep();
		}
		break;
    case KEY_FUNCTION_NEXT_ITEM: 
        g_selected_item->NextItem(); 
		move = FALSE;
        break;

	case KEY_FUNCTION_PARADROP:
		if(isMyTurn) {
			MapPoint point;
			g_tiledMap->GetMouseTilePos(point);
			g_selected_item->Paradrop(point);
			return TRUE;
		}
		break;

	case KEY_FUNCTION_INVESTIGATE_CITY:
        {
            MapPoint    point;
            g_tiledMap->GetMouseTilePos(point);
		    g_selected_item->InvestigateCity(point);
        }
		break;

	case KEY_FUNCTION_PLANT_NUKE:				
	{
		sint32 order;
		if(!g_theOrderDB->GetNamedItem("ORDER_PLANT_NUKE", order))
			return 0;

		Army a;
		double chance, escape_chance;
		if(g_selected_item->GetSelectedArmy(a) && a.CanPlantNuke(chance, escape_chance)) {
			g_controlPanel->BeginOrderDelivery(g_theOrderDB->Access(order));
		} else {
			return 0;
		}
	}
	
		break;
	case KEY_FUNCTION_BIOINFECT:				
	{
		sint32 order;
		if(!g_theOrderDB->GetNamedItem("ORDER_BIO_INFECT", order))
			return 0;

		Army a;
		double chance;
		if(g_selected_item->GetSelectedArmy(a) && a.CanBioInfect(chance)) {
			g_controlPanel->BeginOrderDelivery(g_theOrderDB->Access(order));
		} else {
			return 0;
		}
	}
	
		break;
	case KEY_FUNCTION_NANOTERROR:				
	{
		sint32 order;
		if(!g_theOrderDB->GetNamedItem("ORDER_NANO_INFECT", order))
			return 0;

		Army a;
		double chance;
		if(g_selected_item->GetSelectedArmy(a) && a.CanNanoInfect(chance)) {
			g_controlPanel->BeginOrderDelivery(g_theOrderDB->Access(order));
		} else {
			return 0;
		}
	}
	
		break;
	case KEY_FUNCTION_CREATE_PARK:				
	{
		sint32 order;
		if(!g_theOrderDB->GetNamedItem("ORDER_CREATE_PARK", order))
			return 0;

		Army a;
		if(g_selected_item->GetSelectedArmy(a) && a.AccessData()->CanCreatePark()) {
			g_controlPanel->BeginOrderDelivery(g_theOrderDB->Access(order));
		} else {
			return 0;
		}
	}
	
		break;
	case KEY_FUNCTION_REFORM:
	{
		sint32 order;
		if(!g_theOrderDB->GetNamedItem("ORDER_REFORM", order))
			return 0;

		Army a;
		if(g_selected_item->GetSelectedArmy(a) && a.AccessData()->CanReformCity()) {
			g_controlPanel->BeginOrderDelivery(g_theOrderDB->Access(order));
		} else {
			return 0;
		}
	}
	
		break;

	case KEY_FUNCTION_OPEN_WORK_VIEW:
		ArmyManagerWindow::Toggle();
		break;
	case KEY_FUNCTION_OPEN_CITY_VIEW:
		if(!g_modalWindow) {
			close_AllScreens();
			open_CityView();
		}
		break;
	case KEY_FUNCTION_OPEN_CITY_STATUS:
		if ( !g_modalWindow ) {
			close_AllScreens();
			open_CityStatus();
		}
		break;
	case KEY_FUNCTION_OPEN_CIV_STATUS:
		if ( !g_modalWindow ) {
			close_AllScreens();
			open_CivStatus();
		}
		break;
	case KEY_FUNCTION_OPEN_SCIENCE_STATUS:
		if ( !g_modalWindow ) {
			close_AllScreens();
			open_ScienceStatus();
		}
		break;
	case KEY_FUNCTION_OPEN_UNIT_STATUS:
		if ( !g_modalWindow ) {
			close_AllScreens();
			open_UnitStatus();
		}
		break;
	case KEY_FUNCTION_OPEN_TRADE_STATUS:
		if ( !g_modalWindow ) {
			close_AllScreens();
			TradeManager::Display();
			TradeManager::SetMode(TRADE_MANAGER_MARKET);
			
		}
		break;
	case KEY_FUNCTION_OPEN_DIPLOMACY:
		if ( !g_modalWindow ) {
			close_AllScreens();
			open_Diplomacy();
		}
		break;
	case KEY_FUNCTION_OPEN_INFO_SCREEN:
		if ( !g_modalWindow ) {
			InfoWindow::SelectScoreTab();
			InfoWindow::Open();
		}
		break;
	case KEY_FUNCTION_OPEN_GREAT_LIBRARY:
		if ( !g_modalWindow ) {
			close_AllScreens();
			open_GreatLibrary();
		}
		break;
	case KEY_FUNCTION_OPEN_OPTIONS_SCREEN:
		if ( !g_modalWindow ) {
			close_AllScreens();
			open_OptionsScreen(1);
		}
		break;

	case KEY_FUNCTION_OPEN_SCENARIO_EDITOR:
		if(!g_modalWindow
		&& !g_turn->IsEmail()
		){
			close_AllScreens();
			optionsscreen_mapeditorPress(NULL, AUI_BUTTON_ACTION_EXECUTE, 0, NULL);
		}
		break;
		
		
		
    case KEY_FUNCTION_ZOOM_IN1:
        if (g_tiledMap) {
			g_tiledMap->ZoomIn();
		}
		break;

    case KEY_FUNCTION_ZOOM_OUT1:
        if (g_tiledMap) {
			g_tiledMap->ZoomOut();
        }

		break;

	case KEY_FUNCTION_CENTER_MAP:
	{
		PLAYER_INDEX s_player; 
		ID s_item; 
		SELECT_TYPE s_state; 
		g_selected_item->GetTopCurItem(s_player, s_item, s_state);

        MapPoint pos;
		switch(s_state) {
			case SELECT_TYPE_LOCAL_ARMY:
			{	
				Army army(s_item);
				army.GetPos(pos);
				g_radarMap->CenterMap(pos);
				g_tiledMap->Refresh();
				g_tiledMap->InvalidateMap();
				break;
			}
			case SELECT_TYPE_LOCAL_CITY:
			{	
				Unit unit(s_item);
				unit.GetPos(pos);
				g_radarMap->CenterMap(pos);
				g_tiledMap->Refresh();
				g_tiledMap->InvalidateMap();

				break;
			}
		}
	}
	break;

	case KEY_FUNCTION_SAVE_GAME:
		if(g_network.IsActive()) {
			if(g_network.IsHost()) {
				
				
				g_isScenario = FALSE;
				loadsavescreen_displayMyWindow(LSS_SAVE_MP);
			}
		} else {
			
			
			g_isScenario = FALSE;

			loadsavescreen_displayMyWindow(LSS_SAVE_GAME);
		}
		break;
	case KEY_FUNCTION_LOAD_GAME:
		if(!g_network.IsActive()) {
			loadsavescreen_displayMyWindow(LSS_LOAD_GAME);
		}
		break;						
	case KEY_FUNCTION_REMAP_KEYBOARD:	
		open_KeyMappingScreen();
		break;
	
    case KEY_FUNCTION_KEYBOARD_SELECT_UNIT:
         g_selected_item->KeyboardSelectFirstUnit(); 
		 move = FALSE;
		 break; 

#ifdef _PLAYTEST
    case KEY_FUNCTION_RAND_TEST:
       g_is_rand_test=TRUE; 
         return TRUE; 
		 break;
#endif
        
#if 0
	case KEY_FUNCTION_ENDSLICE:
		if(g_network.IsActive()) {
			if(g_network.IsMyTurn() ||
			   (g_network.IsHost() && 
				g_player[g_selected_item->GetCurPlayer()]->IsRobot())) {
				g_turn->EndThisSliceBeginNewSlice();
			}
		} else {
			g_turn->EndThisSliceBeginNewSlice();
		}
		move = FALSE;
		break;
#endif
    case KEY_FUNCTION_ENDTURN: 
#ifdef _PLAYTEST
        if (g_selected_item->GetCurPlayer() != g_selected_item->GetVisiblePlayer()) 
           break; 

		if(g_network.IsActive()) {
			g_turn->NetworkEndTurn();
		} else {
            g_selected_item->Deselect(g_selected_item->GetCurPlayer());

			

			
			NewTurnCount::StartNextPlayer(true);

 			
          	
            
            
			

			
			
            
            

			g_tiledMap->InvalidateMix();
			g_tiledMap->InvalidateMap();
			g_tiledMap->Refresh();
			g_radarMap->Update();
			g_turn->InformMessages();
		}
		move = FALSE;
        break;
#endif
    
    case KEY_FUNCTION_NEXT_ROUND:   

		
		
		if(g_modalMessage) {
			Message *msg = g_modalMessage->GetMessage();
			if(g_theMessagePool->IsValid(*msg)) {
				Assert(msg->IsAlertBox());
				MessageData *data = msg->AccessData();
				if(data->GetNumButtons() <= 2 && data->GetNumButtons() > 0) {
					data->GetButton(0)->Callback();
				}
			}	
		} else if(g_utilityTextMessage) {
			g_utilityTextMessage->m_callback(FALSE);
			g_utilityTextMessage->RemoveWindow();
		} else {
			
			
			if(g_selected_item->GetVisiblePlayer() == g_selected_item->GetCurPlayer()) {
				DPRINTF(k_DBG_GAMESTATE, ("Keypress end turn, %d\n", g_selected_item->GetCurPlayer()));
				g_selected_item->RegisterManualEndTurn();
				g_director->AddEndTurn(); 
			}
			else
			{
				
				
				
			}
		}
        break; 

    case KEY_FUNCTION_SAVE_WORLD :
		if (g_civApp->IsGameLoaded() && !g_network.IsClient()) {
			g_civApp->AutoSave(g_selected_item->GetVisiblePlayer(), true);

			
			
			

			
		}
		move = FALSE ;
		break ;
        
    case KEY_FUNCTION_LOAD_WORLD :
		if (g_civApp->IsGameLoaded() && !g_network.IsActive()) {
			{
				g_civApp->PostLoadQuickSaveAction(g_selected_item->GetVisiblePlayer());
				
				
				
				
				
			}
			move = FALSE ;
		}
		break ;

    case KEY_FUNCTION_QUIT:
		optionwarningscreen_displayMyWindow(OWS_QUIT);


        return 1;
        break;

#ifdef _PLAYTEST
    case KEY_FUNCTION_GAMESTATE_DEBUG:
        g_theWorld->GamestateDebug();
        for (i=0; i<2; i++) { 
            g_player[i]->GamestateDebug(); 
        }
        return TRUE; 
		break;
#endif
	
    case KEY_FUNCTION_GROUP_ARMY:
	{
		Army a;
		if(g_selected_item->GetSelectedArmy(a)) {
			if(g_network.IsClient()) 
            {
				CellUnitList units;
				Cell *cell = g_theWorld->GetCell(a->RetPos());
				for (sint32 i = 0; i < cell->GetNumUnits(); i++) {
					if(cell->AccessUnit(i).GetArmy().m_id != a.m_id) {
						units.Insert(cell->AccessUnit(i));
					}
				}
				if(units.Num() > 0) {
					g_network.SendGroupRequest(units, a);
				}
			} else {
				g_gevManager->AddEvent(GEV_INSERT_Tail, GEV_GroupOrder,
									   GEA_Army, a.m_id,
									   GEA_End);
			}
		}	

		return TRUE;
	}
	break;
	case KEY_FUNCTION_UNGROUP_ARMY:	
	{	
		Army a;
		if(g_selected_item->GetSelectedArmy(a)) {
			if(g_network.IsClient()) {
				g_network.SendUngroupRequest(a, *a.AccessData());
			} else {
				g_gevManager->AddEvent(GEV_INSERT_Tail, GEV_UngroupOrder,
									   GEA_Army, a.m_id,
									   GEA_End);
			}
		}
		

		return TRUE; 
	}
	break;
	case KEY_FUNCTION_PROCESS_UNIT_ORDERS:
		if(isMyTurn) {
			g_gevManager->Pause();
			for 
            (
                sint32 i = 0; 
                i < g_player[g_selected_item->GetVisiblePlayer()]->m_all_armies->Num(); 
                i++
            ) 
            {
				g_gevManager->AddEvent(GEV_INSERT_Tail, GEV_BeginTurnExecute,
									   GEA_Army, g_player[g_selected_item->GetVisiblePlayer()]->m_all_armies->Access(i),
									   GEA_End);
			}
			g_gevManager->Resume();
			
		}
		break;
	case KEY_FUNCTION_EXECUTE_ORDERS:
		if(isMyTurn)
		{
			PLAYER_INDEX s_player; 
			ID s_item; 
			SELECT_TYPE s_state; 
			g_selected_item->GetTopCurItem(s_player, s_item, s_state);
			Army army(s_item);
			if (s_state == SELECT_TYPE_LOCAL_ARMY && army.IsValid())
            {
				army.ExecuteOrders();
			}
		}
		break;

#if defined(CTP1_HAS_RISEN_FROM_THE_GRAVE)
	case KEY_FUNCTION_TOGGLE_SPACE:
	{
		if(!g_network.IsActive()) {
			extern sint32 g_fog_toggle;
			g_fog_toggle = !g_fog_toggle;
			void WhackScreen();
			WhackScreen();
		}
		break;
	}
#endif

	case KEY_FUNCTION_TOGGLE_CITY_NAMES:
		g_theProfileDB->SetShowCityNames(!g_theProfileDB->GetShowCityNames());
		break;

	case KEY_FUNCTION_TOGGLE_TRADE_ROUTES:
		g_theProfileDB->SetShowTradeRoutes(!g_theProfileDB->GetShowTradeRoutes());
		break;
//#if 0
//	case KEY_FUNCTION_TOGGLE_ARMY_NAMES: //emod
//		g_theProfileDB->SetShowArmyNames(!g_theProfileDB->GetShowArmyNames());
//		break;
//#endif

#ifdef _DEBUG
#if 0
	case KEY_FUNCTION_CRC:
		if(g_debugOwner != k_DEBUG_OWNER_CRC) {
            if (!g_dataCheck) { 
                g_dataCheck = new DataCheck();
            }
			g_debugOwner = k_DEBUG_OWNER_CRC;
			g_dataCheck->BeginTurn();
			g_dataCheck->SetDisplay(1);
		} else {
			g_debugOwner = k_DEBUG_OWNER_NONE;
		}
		break;	
#endif
#endif
	case KEY_FUNCTION_HELP_MODE_TOGGLE:
		break;

	case KEY_FUNCTION_CHAT_KEY:
		if (g_chatBox) 
        {
			g_chatBox->SetActive(!g_chatBox->IsActive());
		}
		break;

	case KEY_FUNCTION_UNIT_CITY_TOGGLE:
		g_selected_item->UnitCityToggle();
		break;
	case KEY_FUNCTION_END_UNIT_TURN:
		g_selected_item->EndUnitTurn();
		break;

	case KEY_FUNCTION_NETWORK_PLAYERS_SCREEN:
		if ( !g_networkPlayersScreen ) {
			g_networkPlayersScreen = new c3_UtilityPlayerListPopup((c3_UtilityPlayerListCallback *)network_PlayerListCallback);
		}
		g_networkPlayersScreen->DisplayWindow();
		if ( g_network.IsHost() ) {
			g_networkPlayersScreen->EnableButtons();
		}
		else if ( g_network.IsClient() ) {		
			g_networkPlayersScreen->DisableButtons();
		}
		
		break;
	case KEY_FUNCTION_CIV_TAB:
		if(g_controlPanel) {
			g_controlPanel->SetTab(CP_TAB_CIV);
		}
		break;
	case KEY_FUNCTION_MSG_TAB:
		if(g_controlPanel) {
			g_controlPanel->SetTab(CP_TAB_MSGLOG);
		}
		break;
	case KEY_FUNCTION_CITY_TAB:
		if(g_controlPanel) {
			g_controlPanel->SetTab(CP_TAB_CITY);
		}
		break;
	case KEY_FUNCTION_UNIT_TAB:
		if(g_controlPanel) {
			g_controlPanel->SetTab(CP_TAB_UNIT);
		}
		break;
	case KEY_FUNCTION_TILE_TAB:
		if(g_controlPanel) {
			g_controlPanel->SetTab(CP_TAB_TILEIMP);
		}
		break;
		
	case KEY_FUNCTION_CONTROL_NEXT:
#if 0
		if(g_controlPanel) {

			if(g_cp_productionTab && 
			   (g_cp_productionTab->GetMode() == PRODTAB_MODE_CHANGE ||
				g_cp_productionTab->GetMode() == PRODTAB_MODE_QUEUE)) {
				c3_ListBox *plist = g_cp_productionTab->GetPurchaseList();
				if(plist && plist->NumItems() > 0) {
					sint32 curIndex = plist->GetSelectedItemIndex();
					if(curIndex >= plist->NumItems() - 1) {
						curIndex = -1;
					}
					plist->SelectItem(curIndex + 1);
					plist->GetVerticalRanger()->SetValue(0, curIndex + 1 - 2);
				}
			}
		}
#endif
		break;
	case KEY_FUNCTION_CONTROL_PREV:
#if 0
		if(g_controlPanel) {

			if(g_cp_productionTab && 
			   (g_cp_productionTab->GetMode() == PRODTAB_MODE_CHANGE ||
				g_cp_productionTab->GetMode() == PRODTAB_MODE_QUEUE)) {
				c3_ListBox *plist = g_cp_productionTab->GetPurchaseList();
				if(plist && plist->NumItems() > 0) {
					sint32 curIndex = plist->GetSelectedItemIndex();
					if(curIndex <= 0) {
						curIndex = plist->NumItems();
					}
					plist->SelectItem(curIndex - 1);
					plist->GetVerticalRanger()->SetValue(0, curIndex - 1 - 2);
				}
			}
		}
#endif
		break;
	case KEY_FUNCTION_CLOSE:		
		if(g_currentMessageWindow) {
			if(g_theMessagePool->IsValid(*g_currentMessageWindow->GetMessage())) {
				g_currentMessageWindow->GetMessage()->Kill();
			}
		} else if(g_modalMessage) {
			Message *msg = g_modalMessage->GetMessage();
			if(g_theMessagePool->IsValid(*msg)) {
				Assert(msg->IsAlertBox());
				MessageData *data = msg->AccessData();
				if(data->GetNumButtons() <= 2 && data->GetNumButtons() > 0) {
					data->GetButton(0)->Callback();
				}
			}	
		}

		break;
	case KEY_FUNCTION_YES:
	{
		if(g_modalMessage) {
			Message *msg = g_modalMessage->GetMessage();
			if(g_theMessagePool->IsValid(*msg)) {
				Assert(msg->IsAlertBox());
				MessageData *data = msg->AccessData();
				if(data->GetNumButtons() == 2) {
					data->GetButton(1)->Callback();
				}
			}
		} else if(g_utilityTextMessage) {
			g_utilityTextMessage->m_callback(TRUE);
			g_utilityTextMessage->RemoveWindow();
		}
		break;
	}
	case KEY_FUNCTION_NO:
	{
		if(g_modalMessage) {
			Message *msg = g_modalMessage->GetMessage();
			if(g_theMessagePool->IsValid(*msg)) {
				Assert(msg->IsAlertBox());
				MessageData *data = msg->AccessData();
				if(data->GetNumButtons() == 2) {
					data->GetButton(0)->Callback();
				}
			}
		} else if(g_utilityTextMessage) {
			g_utilityTextMessage->m_callback(FALSE);
			g_utilityTextMessage->RemoveWindow();
		}
		break;
	}
	case KEY_FUNCTION_CLEAR_QUEUE:
		if(g_controlPanel 

			) {
			Unit city;
			if(g_selected_item->GetSelectedCity(city)) {
				city.GetData()->GetCityData()->GetBuildQueue()->Clear();
#if 0
				if(g_cp_productionTab) {
					g_cp_productionTab->LoadCityData(city);
					g_cp_productionTab->FillQueueList(city);
				}
#endif
			}
		}
		break;
	case KEY_FUNCTION_OPEN_MESSAGE: 
		{
			
			
			extern sint32 g_modalWindow;
			if (g_modalWindow == 0) {
				if(g_currentMessageWindow) {
					if(g_currentMessageWindow->GetMinimizeAction()) {
						g_currentMessageWindow->GetMinimizeAction()->Execute(NULL,
																		   AUI_BUTTON_ACTION_EXECUTE,
																		   0);
					}
				} else if(g_selected_item) {
					sint32 visPlayer = g_selected_item->GetVisiblePlayer();
					
					if(g_player[visPlayer] && g_player[visPlayer]->m_messages->Num() > 0) {
						sint32 m;
						for(m = 0; m < g_player[visPlayer]->m_messages->Num(); m++) {
							if(!g_player[visPlayer]->m_messages->Access(m).IsRead()) {
								g_player[visPlayer]->m_messages->Access(m).Show();
								break;
							}
						}
					}
				}
			}
		}
		break;
	case KEY_FUNCTION_EXECUTE_EYEPOINT:
		{
			
			if(g_currentMessageWindow) {
				if(g_theMessagePool->IsValid(*g_currentMessageWindow->GetMessage())) {
					g_currentMessageWindow->GetMessage()->AccessData()->EyePointCallback(0);
				}
			}
			break;
		}
	case KEY_FUNCTION_MOVE_ORDER:
		g_controlPanel->BeginOrderDelivery(g_selected_item->GetMoveOrder());
		break;

	case KEY_FUNCTION_TOGGLE_RADAR:
		radarwindow_Toggle();
		break;

	case KEY_FUNCTION_TOGGLE_CONTROL_PANEL:
		g_controlPanel->Toggle();
		break;

	case KEY_FUNCTION_TOGGLE_ALL:
		if(!g_c3ui->GetWindow(g_controlPanel->GetWindow()->Id())) {
			g_c3ui->AddWindow(g_controlPanel->GetWindow());
			radarwindow_Show();
		} else {
			g_c3ui->RemoveWindow(g_controlPanel->GetWindow()->Id());
			radarwindow_Hide();
		}
		break;

	
	case KEY_FUNCTION_TRADE_SUMMARY:    
		if (!g_modalWindow) {
			close_AllScreens();
			TradeManager::Display();
			TradeManager::SetMode(TRADE_MANAGER_SUMMARY);
		}
		break;

	case KEY_FUNCTION_GAIA:
		if(!g_modalWindow) {
			close_AllScreens();
			ScienceVictoryDialog::Open();
		}
		break;

	case KEY_FUNCTION_BUILD_QUEUE:
	{
		if(!g_modalWindow && g_player[g_selected_item->GetVisiblePlayer()]) {
			Unit city;
			if(g_selected_item->GetSelectedCity(city)) {
				city = g_theWorld->GetCity(g_selected_item->GetCurSelectPos());
			} else if(g_player[g_selected_item->GetVisiblePlayer()]->GetNumCities()) {
				city = g_player[g_selected_item->GetVisiblePlayer()]->m_all_cities->Access(0);
			}
				if(city.IsValid()) {
					close_AllScreens();
					EditQueue::Display(CityWindow::GetCityData(city));
				}
		} 
		break;
	}		
	case KEY_FUNCTION_CITY_MANAGEMENT:
		if(!g_modalWindow && g_player[g_selected_item->GetVisiblePlayer()]) {
			close_AllScreens();
			Unit city;
			if(g_selected_item->GetSelectedCity(city)) {
				CityWindow::Display(city.GetData()->GetCityData());
			} else if(g_player[g_selected_item->GetVisiblePlayer()]->GetNumCities()) {
				city = g_player[g_selected_item->GetVisiblePlayer()]->m_all_cities->Access(0);
				g_selected_item->SetSelectCity(city);
				CityWindow::Display(city.GetData()->GetCityData());
			}
		}
		break;

	case KEY_FUNCTION_NEW_PROPOSAL:
		if(!g_modalWindow) {
			if(DipWizard::CanInitiateRightNow()) {
				DipWizard::Display();
			}
		}
		break;

	case KEY_FUNCTION_TIMELINE:
		if(!g_modalWindow) {
			InfoWindow::SelectWonderTab();
			InfoWindow::Open();
		}
		break;

	case KEY_FUNCTION_RANK:
		if(!g_modalWindow) {
			InfoWindow::SelectRankingTab();
			InfoWindow::Open();
		}
		break;
	
	case KEY_FUNCTION_RESTART:
		//Added by Martin Gühmann to disable also the restart key in network
		//games, hot seat games and email games.
		if(!g_modalWindow 
		&& !g_theProfileDB->IsScenario() 
		&& !g_isScenario
		&& !g_network.IsActive()
		&& !g_turn->IsHotSeat() 
		&& !g_turn->IsEmail()
		) {
			optionwarningscreen_displayMyWindow(OWS_RESTART) ;
		}
		break;

	case KEY_FUNCTION_NEW_GAME:
		if(!g_modalWindow) {

			optionwarningscreen_displayMyWindow(OWS_QUITTOSHELL);	
		}
		break;

	case KEY_FUNCTION_SOUND_OPTIONS:
		if(!g_modalWindow) {
			soundscreen_displayMyWindow();
		}
		break;
		
	// MUSIC added by ahenobarb
	case KEY_FUNCTION_MUSIC_OPTIONS:
		if(!g_modalWindow) {
			musicscreen_displayMyWindow();
		}
		break;
		
	case KEY_FUNCTION_GRAPHICS_OPTIONS:
		if(!g_modalWindow) {
			graphicsscreen_displayMyWindow();
		}
		break;

	case KEY_FUNCTION_GAMEPLAY_OPTIONS:
		if(!g_modalWindow) {
			gameplayoptions_displayMyWindow();
		}
		break;
	case KEY_FUNCTION_ADVANCED_OPTIONS:
		if(!g_modalWindow) {
			ProfileEdit::Display();
		}
		break;
	default :
        Assert(FALSE); 
    }
    
    if (move && isMyTurn) {
		
		PLAYER_INDEX s_player; 
		ID s_item; 
		SELECT_TYPE s_state; 
        
		g_selected_item->GetTopCurItem(s_player, s_item, s_state);
		
		switch(s_state) { 
			case SELECT_TYPE_LOCAL_ARMY:
			{
				Army army(s_item);
				MapPoint pos;
				army.GetPos(pos);
				MapPoint newpos;

				if(pos.GetNeighborPosition(d, newpos)) {
					g_gevManager->AddEvent(GEV_INSERT_Tail, GEV_MoveToOrder,
										   GEA_Army, army.m_id,
										   GEA_Direction, d,
										   GEA_End);
					
					g_selected_item->DidKeyboardMove();
				}
				break;
			}
				
			default: 
				break; 
		}
	}

	return TRUE;
}