Skip to content

Commit

Permalink
Remove X11Exception class
Browse files Browse the repository at this point in the history
- Replace X11Exception with std::runtime_error
- Add `default` case to the open_display switch
- Add missing return code to the exception handler
  • Loading branch information
sergei-mironov committed Jul 22, 2019
1 parent c37b407 commit c4bf5a2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 66 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
build
.*
result
43 changes: 18 additions & 25 deletions src/XKbSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
* You should have received a copy of the GNU General Public License
* along with Xkb-switch. If not, see <http://www.gnu.org/licenses/>.
*/
#include <X11/XKBlib.h>

#include <iostream>
#include <algorithm>
#include "XKeyboard.h"
#include "X11Exception.h"
#include <X11/XKBlib.h>
#include <sstream>

#include "XKeyboard.h"
#include "XKbSwitch.hpp"

using namespace std;
using namespace kb;

Expand Down Expand Up @@ -105,7 +106,7 @@ int main( int argc, char* argv[] )
return 1;
}
else {
throw string("Invalid argument: " + arg);
THROW_MSG("Invalid argument: '" << arg << "'. Check --help.");
}
}

Expand Down Expand Up @@ -154,25 +155,17 @@ int main( int argc, char* argv[] )
cout << syms.at(xkb.get_group()) << endl;
}

if(m_list) {
for(int i=0; i<syms.size(); i++) {
cout << syms[i] << endl;
}
}
return 0;
}
catch(X11Exception &err) {
cerr << "xkb-switch: " << err.what() << endl;
cerr << "xkb-switch: layouts: " << print_layouts(syms) << endl;
return 2;
}
catch(std::string & err) {
cerr << "xkb-switch: " << err << endl;
cerr << "xkb-switch: layouts: " << print_layouts(syms) << endl;
return 2;
}
catch(std::exception & err) {
cerr << "xkb-switch: " << err.what() << endl;
cerr << "xkb-switch: layouts: " << print_layouts(syms) << endl;
}
if(m_list) {
for(int i=0; i<syms.size(); i++) {
cout << syms[i] << endl;
}
}
return 0;
}
catch(std::exception & err) {
cerr << "xkb-switch: " << err.what() << endl;
// TODO: don't print syms if they are not yet collected
cerr << "xkb-switch: layouts: " << print_layouts(syms) << endl;
return 2;
}
}
34 changes: 14 additions & 20 deletions src/X11Exception.h → src/XKbSwitch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,26 @@
* along with Xkb-switch. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef X11EXCEPTION
#define X11EXCEPTION
#ifndef XKBSWITCH_HPP
#define XKBSWITCH_HPP

#include <exception>
#include <stdexcept>

#define THROW_MSG(msg) do{ \
std::ostringstream oss; \
oss << __FILE__ << ":" << __LINE__ << ":" << msg; \
throw std::runtime_error(oss.str()); \
} while(0)

#define CHECK_MSG(x,msg) do{ \
if(!(x)) { \
std::ostringstream oss; \
oss << __FILE__ << ":" << __LINE__ << ": Condition " << #x << " failed. " << msg; \
throw std::runtime_error(oss.str()); \
} \
} while(0)
if(!(x)) { \
std::ostringstream oss; \
oss << __FILE__ << ":" << __LINE__ << ": Condition " << #x << " failed. " << msg; \
throw std::runtime_error(oss.str()); \
} \
} while(0)

#define CHECK(x) CHECK_MSG(x,"")

class X11Exception : public std::exception
{
public:
X11Exception() : _reason("unknown") {}
X11Exception(const std::string& what) : _reason(what) {}
virtual ~X11Exception() throw () {};
virtual const char* what() const throw () { return _reason.c_str(); }

private:
std::string _reason;
};

#endif
34 changes: 14 additions & 20 deletions src/XKeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@
* any later version.
*/

#include "XKeyboard.h"
#include "X11Exception.h"
#include <algorithm>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <X11/XKBlib.h>
#include <X11/extensions/XKBrules.h>

#include <iostream>
#include <string>
#include <sstream>

#include <X11/XKBlib.h>
#include <X11/extensions/XKBrules.h>

#include "XKeyboard.h"
#include "XKbSwitch.hpp"

namespace kb {

XKeyboard::XKeyboard()
Expand All @@ -45,25 +47,17 @@ void XKeyboard::open_display()
&minor, &reasonReturn);
free(displayName);
switch (reasonReturn) {
case XkbOD_BadLibraryVersion:
throw X11Exception("Bad XKB library version.");
break;
case XkbOD_ConnectionRefused:
throw X11Exception("Connection to X server refused.");
break;
case XkbOD_BadServerVersion:
throw X11Exception("Bad X11 server version.");
break;
case XkbOD_NonXkbServer:
throw X11Exception("XKB not present.");
break;
case XkbOD_Success:
break;
case XkbOD_Success: break;
case XkbOD_BadLibraryVersion: THROW_MSG("Bad XKB library version.");
case XkbOD_ConnectionRefused: THROW_MSG("Connection to X server refused.");
case XkbOD_BadServerVersion: THROW_MSG("Bad X11 server version.");
case XkbOD_NonXkbServer: THROW_MSG("XKB not present.");
default: THROW_MSG("XKB refused to open the display with reason '" << reasonReturn << "'.");
}

_kbdDescPtr = XkbAllocKeyboard();
if (_kbdDescPtr == NULL) {
throw X11Exception("Failed to get keyboard description.");
THROW_MSG("Failed to get keyboard description.");
}

_kbdDescPtr->dpy = _display;
Expand All @@ -78,7 +72,7 @@ XKeyboard::~XKeyboard()
XkbFreeKeyboard(_kbdDescPtr, 0, True);

if (_display!=NULL) {
XCloseDisplay(_display);
XCloseDisplay(_display);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/XKeyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class XKeyboard
XKeyboard();
~XKeyboard();

// Opens display (or throw X11Exception)
// Opens display (or throw std::runtime_error)
void open_display(void);

// Gets the current layout
Expand Down

0 comments on commit c4bf5a2

Please sign in to comment.