OSDN Git Service

first
[psychlops/cpp.git] / psychlops / platform / win32 / psychlops_app_state_Win32.cpp
1 /*
2  *  psychlops_app_state_Win32.h
3  *  Psychlops Standard Library (Universal)
4  *
5  *  Last Modified 2007/06/22 by Kenchi HOSOKAWA
6  *  (C) 2006 Kenchi HOSOKAWA, Kazushi MARUYA and Takao SATO
7  */
8
9 #include <windows.h>\r
10 #include <iostream>
11
12 #include "psychlops_app_state_Win32.h"
13 #include "../../core/ApplicationInterfaces/psychlops_app_info.h"
14
15 namespace Psychlops {
16
17 \r
18         int AppState::createSynchronousProcess(std::string command)\r
19         {\r
20                 const char *lpArg = command.c_str();\r
21                 PROCESS_INFORMATION pi;\r
22                 STARTUPINFO si;\r
23                 ZeroMemory(&si,sizeof(si));\r
24                 si.cb=sizeof(si);\r
25 \r
26                 ZeroMemory(&pi,sizeof(pi));\r
27                 if(!CreateProcess(NULL,(LPTSTR)lpArg,NULL,NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,&si, &pi))\r
28                 {\r
29                         std::cout << "createSynchronousProcess:CreateProcess" << std::endl;\r
30                 }\r
31 \r
32                 CloseHandle(pi.hThread);\r
33                 WaitForSingleObject(pi.hProcess,INFINITE);\r
34                 CloseHandle(pi.hProcess);\r
35 \r
36                 return 0;\r
37         }
38
39         APIApplicationProperties APIApplicationProperties::startupinfo;
40
41         APIApplicationProperties::APIApplicationProperties(){
42                 initialize();
43         }
44         APIApplicationProperties::APIApplicationProperties(HINSTANCE _hInstance, HINSTANCE _hPrevInstance, LPSTR _lpCmdLine, int _iCmdShow)
45         : hInstance_(_hInstance), hPrevInstance_(_hPrevInstance), lpCmdLine_(_lpCmdLine), iCmdShow_(_iCmdShow)  {
46                 initialize();
47         }
48         APIApplicationProperties::~APIApplicationProperties() {
49         }
50
51         void APIApplicationProperties::set(HINSTANCE _hInstance, HINSTANCE _hPrevInstance, LPSTR _lpCmdLine, int _iCmdShow) {
52                 hInstance_ = _hInstance;
53                 hPrevInstance_ = _hPrevInstance;
54                 lpCmdLine_ = _lpCmdLine;
55                 iCmdShow_ = _iCmdShow;
56                 initialize();
57         }
58         DWORD APIApplicationProperties::getPrimaryThreadID() {
59                 return primaryThreadID;
60         }
61
62         void APIApplicationProperties::initialize() {
63                 primaryThreadID = GetCurrentThreadId();
64                 pClassName = "PsychlopsWindow";
65                 pAppName = "Psychlops Application";
66
67                 thread_state_[AppState::SLEEP] = IDLE_PRIORITY_CLASS;
68                 thread_state_[AppState::IDLE] = IDLE_PRIORITY_CLASS;
69                 thread_state_[AppState::LOW] = BELOW_NORMAL_PRIORITY_CLASS;
70                 thread_state_[AppState::NORMAL] = NORMAL_PRIORITY_CLASS;
71                 thread_state_[AppState::HIGH] = HIGH_PRIORITY_CLASS;
72                 SetPriorityClass(GetCurrentProcess(), thread_state_[AppState::NORMAL]);
73         }
74         LRESULT CALLBACK APIApplicationProperties::proc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
75                 switch (message) {
76                         case WM_QUIT:
77                                 return DefWindowProc( hWnd, message, wParam, lParam );
78                                 //ExitThread(TRUE);
79                         case WM_CLOSE:
80                         case WM_DESTROY:
81                                 PostQuitMessage(0);
82                                 return 0;
83                         default:
84                                 return DefWindowProc( hWnd, message, wParam, lParam );
85                 }
86         }
87         void APIApplicationProperties::setThreadPriority( unsigned int priority ) {
88                 if(priority>=0 && priority<5) {
89                         SetPriorityClass(GetCurrentProcess(), startupinfo.thread_state_[priority]);
90                 }
91         }
92         int APIApplicationProperties::getThreadPriority() {
93                 DWORD threadState = GetPriorityClass(GetCurrentProcess());
94                 switch(threadState) {
95                 case IDLE_PRIORITY_CLASS:
96                         return AppState::IDLE;
97                 case BELOW_NORMAL_PRIORITY_CLASS:
98                         return AppState::LOW;
99                 default:
100                 case NORMAL_PRIORITY_CLASS:
101                         return AppState::NORMAL;
102                 case HIGH_PRIORITY_CLASS:
103                         return AppState::HIGH;
104                 }
105         }
106
107         void APIApplicationProperties::analyzeAPIError() {
108                 LPVOID lpMsgBuf;
109                 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
110                 MessageBox(NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION);
111                 LocalFree(lpMsgBuf);
112         }
113
114
115
116         void AppState::alert(std::string message) {
117                 unsigned char str[255];
118                 MessageBox( NULL, message.c_str(), "Psychlops Notification", MB_OK );
119         }
120
121 }       /*      <- namespace Psycholops         */