How to calculate system idle time in Ubuntu on Wayland? - c++

I've implemented the below code to calculate system idle time in ubuntu on xorg which is working fine:-
#include <X11/extensions/scrnsaver.h>
#include <stdio.h>
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xinerama.h>
#include <thread>
#include <chrono>
#include <iostream>
using namespace std;
uint getSystemIdleTime()
{
Display *dpy = XOpenDisplay(nullptr);
if (!dpy) {
return(0);
}
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
XScreenSaverInfo *info = XScreenSaverAllocInfo();
XScreenSaverQueryInfo(dpy, DefaultRootWindow(dpy), info);
uint idletime= info->idle / 1000;
XFree(info);
XCloseDisplay(dpy);
return idletime;
}
int main()
{
int ans = getSystemIdleTime();
cout << ans << endl;
}
but I'm unable to find any method/API to calculate system idle in ubuntu on Wayland.

Related

command spawnl() not executed

I have a code that is executable without error messages but it seems like it denies to run the code
I call by the "spawnl" command. This is my code and I receive "error=-1". I tried may different ways to solve the situation but I always receive the "-1" for an answer. I use Dev C++ compiler with 32bit release. My problem is to call the other program, sending the name of the file.
#include <stdlib.h>
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <conio.h>
#include <process.h>
#include <ctype.h>
#include <dir.h>
#include <windows.h>
#include <dos.h>
#include <iostream>
#include <stddef.h>
void translate_to_ascii_files(),
save_mesh_data(),
make_no(),
give_names();
int load_patches(char *);
FILE *memco ;
int outnod[400] ;
float r_vector[21][21][3],cx[500],cy[500],cz[500] ;
int NOP1,NOP2; /* Number Of Points */
int patches;
char nams[26][26];
int num_of_files;
int kk,nv,nh,exnod,totnod ;
int no[4][400];
char filename[26];
void *buf;
COORD coord= {0,0};
HANDLE hConsole;
using namespace std; // std::cout, std::cin
void gotoxy(int x,int y) {
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
int main(int argc,char *argv[]) {
char com_nam[26];
int counter1,counter2,i;
int error;
system("cls");
give_names();
printf("give the final filename:");scanf("%s",filename);
for(patches=1;patches<=num_of_files;patches++) {
strcpy(com_nam,nams[patches]);
load_patches(com_nam); /* load patches for meshing*/
i=1;
for(counter1=0;counter1<=NOP1;counter1++) {
for(counter2=0;counter2<=NOP2;counter2++) {
cx[i]=r_vector[counter1][counter2][0];
cy[i]=r_vector[counter1][counter2][1];
cz[i]=r_vector[counter1][counter2][2];
i=i+1;
}
}
nh=NOP1+1;nv=NOP2+1;
make_no();
save_mesh_data();
if(argc ==1){
error=spawnl(P_WAIT,"c:\\cpprog\\unitsrf.exe","",filename,NULL);
if (error ==0) {
else {printf ("error=%d\n",error); system("PAUSE");
} }
else{gotoxy(2,11);printf("error=%s\n\n",argv[1]);
system("PAUSE");
}
} translate_to_ascii_files();
}

gRPC keeps returning error code 12 from server

I need to implement a distributed database and I implemented based on gRPC example. The following is my code snippet.
sundial_sync_server.h
#ifndef SUNDIAL_GRPC_SYNC_SERVER_H
#define SUNDIAL_GRPC_SYNC_SERVER_H
#endif //SUNDIAL_GRPC_SYNC_SERVER_H
#include "sundial_grpc.grpc.pb.h"
#include "sundial_grpc.pb.h"
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
using grpc::Channel;
using grpc::ServerContext;
using grpc::Status;
using sundial_rpc::SundialRequest;
using sundial_rpc::SundialResponse;
using sundial_rpc::Sundial_GRPC_SYNC;
#ifndef ABC
#define ABC
class SundialServiceImp final : public Sundial_GRPC_SYNC::Service
{
public:
Status contactRemote(::grpc::ServerContext* context, const ::sundial_rpc::SundialRequest* request, ::sundial_rpc::SundialResponse* response) override;
void run();
};
#endif
sundial_sync_server.cpp
#include "sundial_grpc.grpc.pb.h"
#include "sundial_grpc.pb.h"
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#include "txn.h"
#include "global.h"
#include "manager.h"
#include "stats.h"
#include "helper.h"
#include "grpc_sync_server.h"
#include "txn_table.h"
#include <grpcpp/grpcpp.h>
#include <grpcpp/health_check_service_interface.h>
#include <grpcpp/ext/proto_server_reflection_plugin.h>
using grpc::ServerContext;
using grpc::Status;
using sundial_rpc::SundialRequest;
using sundial_rpc::SundialResponse;
using sundial_rpc::Sundial_GRPC_SYNC;
using grpc::Server;
using grpc::ServerBuilder;
Status SundialServiceImp::contactRemote(::grpc::ServerContext* context, const ::sundial_rpc::SundialRequest* request, ::sundial_rpc::SundialResponse* response){
if (request->request_type() == SundialRequest::SYS_REQ) {
glob_manager->receive_sync_request();
response->set_response_type( SundialResponse::SYS_RESP );
return Status::OK;
}
uint64_t txn_id = request->txn_id();
TxnManager * txn_man = txn_table->get_txn(txn_id);
// If no TxnManager exists for the requesting transaction, create one.
if (txn_man == NULL) {
//printf("adding txnID=%ld into txn_table\n", txn_id);
assert( request->request_type() == SundialRequest::READ_REQ );
txn_man = new TxnManager();
txn_man->set_txn_id( txn_id );
txn_table->add_txn( txn_man );
}
// the transaction handles the RPC call
txn_man->process_remote_request(request, response);
// if the sub-transaction is no longer required, remove from txn_table
if (response->response_type() == SundialResponse::RESP_ABORT
|| response->response_type() == SundialResponse::PREPARED_OK_RO
|| response->response_type() == SundialResponse::PREPARED_ABORT
|| response->response_type() == SundialResponse::ACK) {
txn_table->remove_txn( txn_man );
delete txn_man;
}
return Status::OK;
}
void SundialServiceImp::run(){
std::istringstream in(ifconfig_string);
string line;
uint32_t num_nodes = 0;
string port;
while (getline (in, line)) {
if (line[0] == '#')
continue;
else {
if (num_nodes == g_node_id) {
port = line.substr(0, line.length());
break;
}
num_nodes ++;
}
}
port.append(sync_port);
grpc::EnableDefaultHealthCheckService(true);
grpc::reflection::InitProtoReflectionServerBuilderPlugin();
ServerBuilder builder;
builder.AddListeningPort(port, grpc::InsecureServerCredentials());
builder.RegisterService(this);
std::unique_ptr<Server> server(builder.BuildAndStart());
std::cout << "Server listening on " << port << std::endl;
server->Wait();
}
`sundial_sync_client.h'
#endif //SUNDIAL_GRPC_CLIENT_H
#include "sundial_grpc.grpc.pb.h"
#include "sundial_grpc.pb.h"
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using sundial_rpc::SundialRequest;
using sundial_rpc::SundialResponse;
using sundial_rpc::Sundial_GRPC_SYNC;
//toDo: now assume we only have 2 nodes
#ifndef SSC
#define SSC
class Sundial_Sync_Client{
public:
Sundial_Sync_Client(std::shared_ptr<Channel>* channel);
Status contactRemote(uint64_t node_id,SundialRequest& request, SundialResponse* response);
private:
std::unique_ptr<Sundial_GRPC_SYNC::Stub> stub_;
};
#endif
sundial_sync_client.cpp
#include "sundial_grpc.grpc.pb.h"
#include "sundial_grpc.pb.h"
#include "grpc_sync_client.h"
#include <iostream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#include "txn.h"
#include "global.h"
#include "helper.h"
#include "manager.h"
#include "stats.h"
using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;
using sundial_rpc::SundialRequest;
using sundial_rpc::SundialResponse;
using sundial_rpc::Sundial_GRPC_SYNC;
//toDo: add more nodes to it
Sundial_Sync_Client::Sundial_Sync_Client(std::shared_ptr<Channel>* channel){
for(int i=0; i<g_num_nodes;i++){
if(i==g_node_id)
continue;
//stub_[i]=Sundial_GRPC_SYNC::NewStub(channel[i]);
stub_=Sundial_GRPC_SYNC::NewStub(channel[i]);
}
}
Status
Sundial_Sync_Client::contactRemote(uint64_t node_id, SundialRequest& request, SundialResponse* response){
//toDo: choose the right stub with node id
ClientContext context;
printf("Client sends request\n");
//Status status = stub_[node_id]->contactRemote(&context, request, &response);
Status status = stub_->contactRemote(&context, request, response);
if (status.ok()) {
//printf("status ok\n");
glob_stats->_stats[GET_THD_ID]->_resp_msg_count[ response->response_type() ] ++;
glob_stats->_stats[GET_THD_ID]->_resp_msg_size[ response->response_type() ] += response->SpaceUsedLong();
return status;
} else {
std::cout << status.error_code() << ": " << status.error_message()
<< std::endl;
return status;
}
}
It runs pretty smoothly at the setup. I did get the ip address and let servers run at the right address. However, when I make the client send request, the status it returns always has error code 12 without error message.
Error code 12 is "UNIMPLEMENTED". Check your proto and make sure your server is implementing the correct RPC method (correct upper/lower case for method name and things like that)

Some problems with the functions GetCursorPos() and SetCursorPos() in C++

I am a new user of C++ and do not know all about types of variables.
I have this code but it doesn't work normally. For normal i mean - after starting cursor must be random moves for -25 to 25 pixel of screen.
Sorry if i provided few information. Ask me i can send what you want. And sorry for my bad English.
#include <iostream>
#include "MyForm1.h"
#include <windows.h>
#include <cstdlib>
#include <winuser.h>
#include <playsoundapi.h>
using namespace System;
using namespace System::Windows::Forms;
using namespace std;
// Cursor random moving here :3
int shakecursor() {
POINT p;
int __cdecl GetCursorPos(int &p);
cout << p.x << p.y << endl;
int x_p1;
int y_p1;
x_p1 = rand() % 0 -25;
y_p1 = rand() % 0 -25;
int x_p = p.x + x_p1;
int y_p = p.y + y_p1;
int __cdecl SetCursorPos(int x_p1, int y_p1);
Sleep(10);
return 0;
}
[STAThreadAttribute]
int main(cli::array<System::String ^> ^args) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
My3yPaB::MyForm mainForm;
Application::Run(%mainForm);
bool shaking = true;
while (shaking = true) {
shakecursor();
}
}```
These
int __cdecl GetCursorPos(int &p);
int __cdecl SetCursorPos(int x_p1, int y_p1);
are not function calls. they are function declarations.
Instead it seems you mean
GetCursorPos( p );
and
SetCursorPos( x_p, y_p );
So i fixed this problem and program doesn't do anything here code:
#include <iostream>
#include "MyForm1.h"
#include <windows.h>
#include <cstdlib>
#include <winuser.h>
#include <Mmsystem.h>
#include <playsoundapi.h>
#pragma comment (lib, "User32.lib")
using namespace System;
using namespace System::Windows::Forms;
using namespace std;
int shakecursor() {
POINT p;
GetCursorPos(&p);
cout << p.x << p.y << endl;
int x_p1;
int y_p1;
x_p1 = rand() % 51 - 25;
y_p1 = rand() % 51 - 25;
int x_p = p.x + x_p1;
int y_p = p.y + y_p1;
SetCursorPos(x_p, y_p);
Sleep(10);
return 0;
}
[STAThreadAttribute]
int main(cli::array<System::String ^> ^args) {
//PlaySound(L"start.mp3", NULL, NULL);
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
My3yPaB::MyForm mainForm;
Application::Run(%mainForm);
bool shaking = true;
while (shaking = true) {
shakecursor();
}
}```

Ambiguous pointer; <SiHCollection> is ambiguous

below I've posted my code which is meant to store hits collections in HCE (hit collection of events).
The code compiles successfully but on running the program, the following error is printed to the terminal seven times:
< SiHCollection> is ambiguous.
I have a feeling it is because I am using namespace std although I don't know how to amend the code. Any thoughts?
#include "SiSD.h"
#include "SiHit.h"
#include "G4HCofThisEvent.hh"
#include "G4Step.hh"
#include "G4ThreeVector.hh"
#include "G4SDManager.hh"
#include "G4ios.hh"
#include "G4UnitsTable.hh"
#include <fstream>
#include <iostream>
#include <sstream>
using namespace std;
extern ofstream outfile;
SiSD::SiSD(G4String name)
:G4VSensitiveDetector(name)
{
collectionName.insert("SiHCollection");
}
SiSD::~SiSD(){ }
void SiSD::Initialize(G4HCofThisEvent* HCE)
{
SiHCollection = new SiHitsCollection(SensitiveDetectorName,
collectionName[0]);
static G4int HCID = -1;
if(HCID<0)
{
HCID = G4SDManager::GetSDMpointer()->GetCollectionID(collectionName[0]);
}
HCE->AddHitsCollection(HCID, SiHCollection);
}
G4bool SiSD::ProcessHits(G4Step* aStep, G4TouchableHistory*)
{
if(aStep->GetTrack()->GetTrackID() > 0) {
G4double edep = aStep->GetTotalEnergyDeposit();
if(edep==0) return false;
SiHit* aHit = new SiHit();
aHit->SetEdep(edep);
SiHCollection->insert(aHit);
return true;
} else return false;
}

Loading an image from a folder using OpenCV 3.0 in Windows

I am using Visual Studio 2010, with OpenCV 3.0. I'm trying to load some images from a folder but I am having problems.
Firstly I did not have the file dirent.h, so I downloaded it in order to get the DIR* and "dirent*" structures to access to the files. All seems to be well, but now when I get to the line
string fileName = in_file->d_name;
I have found that I don't access to the name of the file.
Anyone have any thoughts on this?
This is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <opencv2/core/dirent.h>
#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
#include <conio.h>
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>
#pragma comment(lib, "User32.lib")
#include <errno.h>
#include <iostream>
#include <time.h>
#include <limits.h>
#include <fstream>
#include <sys/stat.h>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/ml.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace cv::ml;
using namespace std;
int patchWidth = 15;
int patchHeight = 15;
int main(int, char**)
{
string imagesPath = "Images";
string resultsPath = "Patches";
DIR* FD;
struct dirent* in_file;
if (NULL == (FD = opendir (imagesPath.c_str())))
{
fprintf(stderr, "Error : Failed to open input directory\n");
return 0;
}
while ((in_file = readdir(FD)))
{
/* On linux/Unix we don't want current and parent directories
* If you're on Windows machine remove this two lines
*/
// if (!strcmp (in_file->d_name, "."))
// continue;
// if (!strcmp (in_file->d_name, ".."))
// continue;
/* Open directory entry file for common operation */
/* TODO : change permissions to meet your need! */
string fileName = in_file->d_name;
string pathFile = imagesPath;
pathFile.append("/");
pathFile.append(fileName);
//pathFile.append(".jpg");
Mat img = imread(pathFile.c_str());
Thanks in advance.
for a much simpler solution, just use cv::glob :
String imagesPath = "Images/*.png"; // it has filters, too !
vector<String> fn;
glob(path, fn, true); // recursive, if you want
for (size_t i=0; i<fn.size(); i++)
{
Mat img = imread(fn[i]);
...
}