1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
* main.cc
* Copyright (C) E. Mark Anderson 2010 <kd0bpv@gmail.com>
*
* bTouch is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* bTouch is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* I need to make sure I'm not importing anything I'm not using.
* Else I'll be distributing a bloated program. Anyone mind double
* checking these for me and telling me which ones, if any are unneeded?
*/
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
using namespace std;
void WriteLog(char sMessage[50], int iSeverity)
{
/* iSeverity is an int that represents how severe a message is.
* 4 is general messages, 3 is important messages, 2 is warnings,
* 1 is fatal errors, 0 would be the unlikely case that it screws up
* royally and causes the kernel to panic -- or worse.
* Would I get appreciable memory conservation if I make it a short int?
*/
ofstream ofLog;
ofLog.open("/home/mark/btouch.log", ios::app);
if (ofLog.is_open())
{
ofLog << iSeverity << ": " << sMessage << endl;
ofLog.close();
} else { cout << "Could not open stream" << endl; }
// Is this proper? or should I open it once at the start of main()
// then close it once at the end of main?
}
bool TouchpadState() {
/*
* Function to query the system for the touchpad's state.
* Returns the following:
* 0 || false == Touchpad is on
* 1 || true == Touchpad is off
* anything else == error
*/
FILE * fp;
// Open a pipe and run the system query.
fp = popen("synclient -l | grep -i touchpadoff", "r");
// Don't assume the pipe is open, could cause problems if we do.
if (fp == NULL) {
WriteLog("ERROR: popen(): Could not open stream!", 1);
return NULL;
}
char sStatus[50];
fgets(sStatus, 50, fp); // Grab the returned data from the pipe.
pclose(fp); // And close it.
bool bStatus;
// Parse the data for what we need and use it to set a proper
// boolean.
if (sStatus[30] == '0') {bStatus = 0;}
if (sStatus[30] == '1') {bStatus = 1;}
// Return the boolean to main() so it can be used
// to determin the proper actions.
return bStatus;
}
void TouchpadOn() {
WriteLog("Touchpad was OFF. Turning ON.", 3);
// execute system call to enable touchpad
system("synclient TouchpadOff=0");
// Double check it worked.
bool bState = TouchpadState();
switch (bState) // Should now be == false
{
case 0:
WriteLog("Successfuly toggled ON.", 4);
break;
case 1:
WriteLog("ERROR: TOUCHPAD IS STILL OFF!", 2);
break;
default:
WriteLog("ERROR: TOUCHPAD STATE UNKNOWN!", 1);
break;
}
}
void TouchpadOff() {
WriteLog("Touchpad was ON. Turning OFF.", 3);
// execute system call to disable touchpad
system("synclient TouchpadOff=1");
// Double check it worked.
bool bState = TouchpadState();
switch (bState) // Should now be == true
{
case 1:
WriteLog("Successfully toggled OFF.", 4);
break;
case 0:
WriteLog("ERROR: TOUCHPAD IS STILL ON!", 2);
break;
default:
WriteLog("ERROR: TOUCHPAD STATE UNKNOWN!", 1);
break;
}
}
int main() // If I modify this to allow arguments, is there a way to then make it global?
{
WriteLog("NEW EXECUTION", 4);
bool bState = TouchpadState(); // Get the touchpad's state.
switch (bState) // Find out which actions need to be taken.
{
case 1:
TouchpadOn(); // If it's off, turn it on.
break;
case 0:
TouchpadOff(); // If it's on, turn it off.
break;
default: // If neither are true, log an error.
WriteLog("ERROR: Unable to determine state", 1);
break;
}
// Any errors that occured should have been handled by us, therefore
// we should terminate with success.
WriteLog("Execution complete\n", 4);
return 0;
}
|