Scid  4.7.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
sc_info.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 1999-2004 Shane Hudson
3  * Copyright (c) 2018 Fulvio Benini
4  *
5  * This file is part of Scid (Shane's Chess Information Database).
6  *
7  * Scid is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation.
10  *
11  * Scid is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Scid. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifdef WIN32
21 #define NOMINMAX
22 #include <windows.h>
23 #undef NOMINMAX
24 #undef ERROR
25 #else
26 #include <sys/resource.h>
27 #include <sys/time.h>
28 #endif
29 
30 #include "ui.h"
31 
32 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33 // sc_info_priority:
34 // This gets or sets the priority class of a process.
36  const char** argv) {
37  const char* usage = "Usage: sc_info priority <pid> [normal|idle]";
38  if (argc < 3 || argc > 4)
39  UI_Result(ti, ERROR_BadArg, usage);
40 
41  int pid = strGetInteger(argv[2]);
42 
43  bool idlePriority = false;
44  if (argc == 4) {
45  switch (argv[3][0]) {
46  case 'i':
47  idlePriority = true;
48  break;
49  case 'n':
50  idlePriority = false;
51  break;
52  default:
53  return UI_Result(ti, ERROR_BadArg, usage);
54  }
55  }
56 
57 #ifdef WIN32
58  // Try to obtain a process handle for setting the priority class:
59  HANDLE hProcess = OpenProcess(
60  PROCESS_SET_INFORMATION | PROCESS_QUERY_INFORMATION, false, pid);
61  if (hProcess == NULL)
62  return UI_Result(ti, ERROR, "Unable to set process priority.");
63 
64  if (argc == 4)
65  SetPriorityClass(hProcess, idlePriority ? IDLE_PRIORITY_CLASS
66  : NORMAL_PRIORITY_CLASS);
67 
68  uint priorityClass = GetPriorityClass(hProcess);
69  CloseHandle(hProcess);
70 
71  return UI_Result(ti, OK, priorityClass == NORMAL_PRIORITY_CLASS ? 0 : 15);
72 
73 #else // #ifdef WIN32
74  if (argc == 4) {
75  // Try to assign a new priority:
76  if (setpriority(PRIO_PROCESS, pid, idlePriority ? 15 : 0) != 0)
77  return UI_Result(ti, ERROR, "Unable to set process priority.");
78  }
79  // Now return the process priority:
80  int priority = getpriority(PRIO_PROCESS, pid);
81  return UI_Result(ti, OK, priority);
82 #endif // #ifdef WIN32
83 }
const errorT OK
Definition: error.h:23
UI_res_t sc_info_priority(UI_extra_t, UI_handle_t ti, int argc, const char **argv)
Definition: sc_info.cpp:35
const errorT ERROR_BadArg
Definition: error.h:28
int UI_res_t
Definition: ui_tcltk.h:30
uint32_t uint
Definition: common.h:91
int strGetInteger(const char *str)
Definition: misc.h:185
Definition: errors.tcl:17
UI_res_t UI_Result(UI_handle_t ti, errorT res)
UI_Result() - pass the result of an operation from c++ to UI.
Definition: ui.h:140
Tcl_Interp * UI_handle_t
Definition: ui_tcltk.h:32
ClientData UI_extra_t
Definition: ui_tcltk.h:31