time_control.cpp

Go to the documentation of this file.
00001 // Name   : time_control
00002 // Author : Chris Koeritz
00003 /******************************************************************************
00004 * Copyright (c) 1994-$now By Author.  This program is free software; you can  *
00005 * redistribute it and/or modify it under the terms of the GNU General Public  *
00006 * License as published by the Free Software Foundation; either version 2 of   *
00007 * the License or (at your option) any later version.  This is online at:      *
00008 *     http://www.fsf.org/copyleft/gpl.html                                    *
00009 * Please send any updates to: fred@gruntose.com                               *
00010 \*****************************************************************************/
00011 
00012 #include "time_control.h"
00013 
00014 #include <application/windoze_helper.h>
00015 #include <basis/utf_conversion.h>
00016 
00017 #include <time.h>
00018 #if defined(__WIN32__) || defined(__UNIX__)
00019   #include <sys/timeb.h>
00020 #endif
00021 #ifdef __UNIX__
00022   #include <unistd.h>
00023 #endif
00024 
00025 using namespace basis;
00026 using namespace structures;
00027 
00028 namespace timely {
00029 
00030 void time_control::sleep_ms(basis::un_int msec)
00031 {
00032 #ifdef __UNIX__
00033   usleep(msec * 1000);
00034 #endif
00035 #ifdef __WIN32__
00036   Sleep(msec);
00037 #endif
00038 }
00039 
00040 bool time_control::set_time(const time_locus &new_time)
00041 {
00042 #ifdef __WIN32__
00043   SYSTEMTIME os_time;
00044   os_time.wYear = WORD(new_time.year);
00045   os_time.wMonth = new_time.month;
00046   os_time.wDayOfWeek = new_time.day_of_week;
00047   os_time.wDay = new_time.day_of_year;
00048   os_time.wHour = new_time.hour;
00049   os_time.wMinute = new_time.minute;
00050   os_time.wSecond = new_time.second;
00051   os_time.wMilliseconds = 0;  // currently unused.
00052 
00053   // get our process token for manipulation.
00054   HANDLE petoken;
00055   OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES
00056       | TOKEN_QUERY, &petoken);
00057   // get our 
00058 //something or other
00059 // identifier so we can adjust our privileges.
00060   LUID our_id;
00061   LookupPrivilegeValue(NULL, to_unicode_temp("SeSystemTimePrivilege"), &our_id);
00062   // make up a privilege structure for the adjustment.
00063   TOKEN_PRIVILEGES privs;
00064   privs.PrivilegeCount = 1;
00065   privs.Privileges[0].Luid = our_id;
00066   privs.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
00067   // enable system-time privileges.
00068   AdjustTokenPrivileges(petoken, false, &privs, sizeof(TOKEN_PRIVILEGES),
00069       NULL, NULL);
00070 
00071   SetLocalTime(&os_time);  // actually set the time.
00072 
00073   // disable the time adjustment privileges again.
00074   AdjustTokenPrivileges(petoken, true, &privs, sizeof(TOKEN_PRIVILEGES),
00075       NULL, NULL);
00076 
00077   // let all the main windows know that the time got adjusted.
00078 //do we need to do this ourselves?
00079   ::PostMessage(HWND_BROADCAST, WM_TIMECHANGE, 0, 0);
00080 
00081 //hmmm: make sure this seems right.
00082   CloseHandle(petoken);
00083 
00084   return true;
00085 #elif defined(__UNIX__)
00086 //no implem yet.
00087 
00088 //temp to shut up warnings
00089 time_locus ted = new_time;
00090 return ted.year ? 0:1;
00091 
00092 #else
00093   return false;
00094 #endif
00095 }
00096 
00097 } // namespace.
00098 
Generated on Sat Jan 28 04:22:33 2012 for hoople2 project by  doxygen 1.6.3