]> git.tdb.fi Git - libs/gui.git/commitdiff
Exception changes
authorMikko Rasa <tdb@tdb.fi>
Thu, 25 Aug 2011 09:16:25 +0000 (12:16 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 25 Aug 2011 09:18:44 +0000 (12:18 +0300)
source/gbase/display.cpp
source/gbase/display.h
source/gbase/drawcontext.cpp
source/gbase/glcontext.cpp
source/gbase/glcontext.h
source/gbase/image.cpp
source/gbase/image.h
source/gbase/window.cpp
source/input/control.cpp
source/input/hub.cpp
source/input/keys.cpp

index 6ae2906f42cbf903bbe4e2c20b90e4a2e5cb6e1e..da036a1baa9a4174049f40920fd0575e42fc0ceb 100644 (file)
@@ -5,7 +5,6 @@
 #include <X11/extensions/xf86vmode.h>
 #endif
 #endif
-#include <msp/core/except.h>
 #include <msp/strings/format.h>
 #include <msp/strings/lexicalcast.h>
 #include "display.h"
@@ -48,6 +47,11 @@ int x_error_handler(Display *display, XErrorEvent *event)
 namespace Msp {
 namespace Graphics {
 
+unsupported_video_mode::unsupported_video_mode(const VideoMode &mode):
+       runtime_error(format("%dx%d", mode.width, mode.height))
+{ }
+
+
 Display::Display(const string &disp_name):
        priv(new Private)
 {
@@ -77,7 +81,7 @@ Display::Display(const string &disp_name):
        else
                priv->display = XOpenDisplay(disp_name.c_str());
        if(!priv->display)
-               throw Exception("Couldn't open X display");
+               throw runtime_error("XOpenDisplay");
 
        XSetErrorHandler(x_error_handler);
 
@@ -141,7 +145,9 @@ void Display::set_mode(const VideoMode &mode)
                info.dmDisplayFrequency = mode.rate;
        }
 
-       ChangeDisplaySettings(&info, CDS_FULLSCREEN);
+       LONG ret = ChangeDisplaySettings(&info, CDS_FULLSCREEN);
+       if(ret!=DISP_CHANGE_SUCCESSFUL)
+               throw unsupported_video_mode(mode);
 #elif defined(WITH_XF86VIDMODE)
        int screen = DefaultScreen(priv->display);
 
@@ -163,10 +169,10 @@ void Display::set_mode(const VideoMode &mode)
                }
        }
 
-       throw InvalidParameterValue("Requested mode not supported");
+       throw unsupported_video_mode(mode);
 #else
        (void)mode;
-       throw Exception("Video mode switching not supported");
+       throw runtime_error("no xf86vidmode support");
 #endif
 }
 
@@ -229,7 +235,7 @@ void Display::check_error()
        if(error_flag)
        {
                error_flag = false;
-               throw Exception(error_msg);
+               throw runtime_error(error_msg);
        }
 }
 
index c058e4118c80faec75c9dd0046a4add0d576ca82..662e73fa67e6dc8742a49fec2994dbdb7caf89b0 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <list>
 #include <map>
+#include <stdexcept>
 #include <string>
 
 namespace Msp {
@@ -20,6 +21,15 @@ struct VideoMode
        VideoMode(unsigned w, unsigned h): width(w), height(h), rate(0) { }
 };
 
+
+class unsupported_video_mode: public std::runtime_error
+{
+public:
+       unsupported_video_mode(const VideoMode &);
+       virtual ~unsupported_video_mode() throw () { }
+};
+
+
 class Display
 {
 public:
index 0f8d44b25539196f69f428ac8497c6fce9a57eb6..03965671306f5f7224f920e9a414abb4a06f126a 100644 (file)
@@ -1,3 +1,4 @@
+#include <stdexcept>
 #ifndef WIN32
 #include <sys/ipc.h>
 #include <sys/shm.h>
@@ -5,12 +6,13 @@
 #include <X11/extensions/XShm.h>
 #include <X11/Xutil.h>
 #endif
-#include <msp/core/except.h>
 #include "display.h"
 #include "drawcontext.h"
 #include "window.h"
 #include "display_priv.h"
 
+using namespace std;
+
 namespace Msp {
 namespace Graphics {
 
@@ -28,7 +30,7 @@ DrawContext::DrawContext(Window &w):
        window(w)
 {
 #ifdef WIN32
-       throw Exception("DrawContext not supported on win32 yet");
+       throw runtime_error("no DrawContext support on windows");
 #else
        priv = new Private;
 
@@ -43,7 +45,7 @@ DrawContext::DrawContext(Window &w):
        {
                priv->image = XShmCreateImage(dpy, wa.visual, wa.depth, ZPixmap, 0, &priv->shminfo, wa.width, wa.height);
                if(!priv->image)
-                       throw Exception("Could not create shared memory XImage");
+                       throw runtime_error("XShmCreateImage");
 
                priv->shminfo.shmid = shmget(IPC_PRIVATE, priv->image->bytes_per_line*priv->image->height, IPC_CREAT|0666);
                priv->shminfo.shmaddr=priv->image->data = reinterpret_cast<char *>(shmat(priv->shminfo.shmid, 0, 0));
@@ -58,7 +60,7 @@ DrawContext::DrawContext(Window &w):
        {
                priv->image = XCreateImage(dpy, wa.visual, wa.depth, ZPixmap, 0, 0, wa.width, wa.height, 8, 0);
                if(!priv->image)
-                       throw Exception("Could not create XImage");
+                       throw runtime_error("XCreateImage");
                priv->image->data = new char[priv->image->bytes_per_line*priv->image->height];
        }
 #endif
index 22dca9010462d922ad800f19447609e5a1735024..b71383063c05d6edb036959e8034349897bff228 100644 (file)
@@ -9,7 +9,7 @@
 #endif
 #endif
 #include <msp/core/application.h>
-#include <msp/core/except.h>
+#include <msp/strings/format.h>
 #include "display.h"
 #include "glcontext.h"
 #include "window.h"
@@ -26,6 +26,12 @@ GLOptions::GLOptions():
 { }
 
 
+unsupported_gl_mode::unsupported_gl_mode(const GLOptions &opts):
+       runtime_error(format("{ .alpha=%s, .stencil=%s, .doublebuffer=%s, .multisample=%d }",
+               opts.alpha, opts.stencil, opts.doublebuffer, opts.multisample))
+{ }
+
+
 #ifdef WITH_OPENGL
 #ifdef WIN32
 typedef HGLRC Context;
@@ -71,7 +77,7 @@ GLContext::GLContext(Window &wnd, const GLOptions &opts):
 
        int pf_index = ChoosePixelFormat(dc, &pfd);
        if(!pf_index)
-               throw Exception("Couldn't find a suitable pixel format");
+               throw unsupported_gl_mode(opts);
        SetPixelFormat(dc, pf_index, &pfd);
 
        priv->context = wglCreateContext(dc);
@@ -112,7 +118,7 @@ GLContext::GLContext(Window &wnd, const GLOptions &opts):
 
        XVisualInfo *vi = glXChooseVisual(dpy, DefaultScreen(dpy), &attribs.front());
        if(!vi)
-               throw Exception("Couldn't find a suitable GLX visual");
+               throw unsupported_gl_mode(opts);
        priv->context = glXCreateContext(dpy, vi, 0, true);
 
        XSetWindowAttributes attr;
@@ -128,7 +134,7 @@ GLContext::GLContext(Window &wnd, const GLOptions &opts):
 #else
        (void)wnd;
        (void)opts;
-       throw Exception("OpenGL support not compiled in");
+       throw runtime_error("no OpenGL support");
 #endif
 
        window.signal_resize.connect(sigc::mem_fun(this, &GLContext::window_resized));
index a719b85d82ee134174e444bd68e33a21797a75c9..dfac328d9846f550d680cb5695d5ec9694856d28 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef MSP_GBASE_GLCONTEXT_H_
 #define MSP_GBASE_GLCONTEXT_H_
 
+#include <stdexcept>
+
 namespace Msp {
 namespace Graphics {
 
@@ -17,6 +19,15 @@ struct GLOptions
        GLOptions();
 };
 
+
+class unsupported_gl_mode: public std::runtime_error
+{
+public:
+       unsupported_gl_mode(const GLOptions &);
+       virtual ~unsupported_gl_mode() throw () { }
+};
+
+
 class GLContext
 {
 private:
index 24b760e0aaedb33de123b495bcdb9ac52e79910b..3360b72e7610d0cdf68eb8d9e75a6993682e1a91 100644 (file)
@@ -6,7 +6,6 @@
 #include <msp/io/file.h>
 #include <msp/io/memory.h>
 #endif
-#include <msp/core/except.h>
 #include "image.h"
 
 using namespace std;
@@ -64,7 +63,7 @@ void load_png(IO::Base &in, Image::Private &priv)
                info = png_create_info_struct(png);
 
                if(setjmp(png_jmpbuf(png)))
-                       throw Exception("Error loading PNG image");
+                       throw bad_image_data("PNG error");
 
                png_set_read_fn(png, &in, read);
                png_read_info(png, info);
@@ -76,7 +75,7 @@ void load_png(IO::Base &in, Image::Private &priv)
                priv.width = width;
                priv.height = height;
                if(depth!=8)
-                       throw Exception("Only 8-bit PNG images are supported");
+                       throw unsupported_image_format("depth!=8");
                switch(color)
                {
                case PNG_COLOR_TYPE_PALETTE:    priv.fmt = COLOR_INDEX; break;
@@ -84,7 +83,7 @@ void load_png(IO::Base &in, Image::Private &priv)
                case PNG_COLOR_TYPE_GRAY_ALPHA: priv.fmt = LUMINANCE_ALPHA; break;
                case PNG_COLOR_TYPE_RGB:        priv.fmt = RGB; break;
                case PNG_COLOR_TYPE_RGB_ALPHA:  priv.fmt = RGBA; break;
-               default: throw Exception("Unknown color type");
+               default: throw unsupported_image_format("unknown color type");
                }
 
                unsigned nchans = png_get_channels(png, info);
@@ -133,7 +132,7 @@ Image::Image():
        priv(new Private)
 {
 #if !defined(WITH_DEVIL) && !defined(WITH_LIBPNG)
-       throw Exception("Image needs either DevIL or libpng support");
+       throw runtime_error("no image support");
 #endif
 }
 
@@ -164,9 +163,9 @@ void Image::load_file(const string &fn)
                ensure_devil_image(priv->id);
                ilBindImage(priv->id);
                if(!ilLoadImage(const_cast<char *>(fn.c_str())))
-                       throw Exception("Error loading image "+fn);
+                       throw bad_image_data("IL error");
 #else
-               throw Exception("Not a PNG image and DevIL support not compiled in");
+               throw unsupported_image_format("DevIL needed for non-PNG images");
 #endif
        }
        (void)fn;
@@ -187,9 +186,9 @@ void Image::load_memory(const void *data, unsigned size)
                ensure_devil_image(priv->id);
                ilBindImage(priv->id);
                if(!ilLoadL(IL_TYPE_UNKNOWN, const_cast<void *>(data), size))
-                       throw Exception("Error loading image from memory");
+                       throw bad_image_data("IL error");
 #else
-               throw Exception("Not a PNG image and DevIL support not compiled in");
+               throw unsupported_image_format("DevIL needed for non-PNG images");
 #endif
        }
        (void)data;
@@ -215,7 +214,8 @@ PixelFormat Image::get_format() const
                case IL_RGBA: return RGBA;
                case IL_BGR: return BGR;
                case IL_BGRA: return BGRA;
-               default: throw InvalidParameterValue("Unknown pixel format in image");
+               // XXX bad, should throw when loading
+               default: throw invalid_argument("unknown pixel format in image");
                }
        }
 #endif
index 60dd895b370864892f10ca65e2faa9d5219b8dc7..aec1b0dc15ffb1be598e9d67b0beb38279cc17c9 100644 (file)
@@ -7,6 +7,21 @@
 namespace Msp {
 namespace Graphics {
 
+class unsupported_image_format: public std::runtime_error
+{
+public:
+       unsupported_image_format(const std::string &w): std::runtime_error(w) { }
+       virtual ~unsupported_image_format() throw() { }
+};
+
+class bad_image_data: public std::runtime_error
+{
+public:
+       bad_image_data(const std::string &w): std::runtime_error(w) { }
+       virtual ~bad_image_data() throw() { }
+};
+
+
 class Image
 {
 public:
index fbcbd519f99dbe01a49f70490b4f6de259d6c87b..77959ded2ddd3de77627846028f5a7946ae9cd8b 100644 (file)
@@ -9,7 +9,6 @@
 #include <windowsx.h>
 #endif
 #include <msp/core/application.h>
-#include <msp/core/except.h>
 #include "display.h"
 #include "window.h"
 #include "display_priv.h"
@@ -104,7 +103,7 @@ void Window::init()
                wndcl.hIconSm = 0;
 
                if(!RegisterClassEx(&wndcl))
-                       throw Exception("Couldn't register window class");
+                       throw system_error("RegisterClassEx");
 
                wndclass_created = true;
        }
@@ -129,7 +128,7 @@ void Window::init()
                reinterpret_cast<HINSTANCE>(Application::get_data()),
                this);
        if(!priv->window)
-               throw Exception("CreateWindowEx failed");
+               throw system_error("CreateWindowEx");
 
 #else
        ::Display *dpy = display.get_private().display;
index 565d5c3242a35fc968e1efc60443f8f131c66b4a..eee0f1523e98256604ca4e0213669134a555c4d1 100644 (file)
@@ -1,4 +1,3 @@
-#include <msp/core/except.h>
 #include <msp/strings/lexicalcast.h>
 #include "control.h"
 #include "device.h"
@@ -90,8 +89,6 @@ void Control::connect_signals()
        case AXIS_NEG:
                src.dev->signal_axis_motion.connect(sigc::mem_fun(this, &Control::axis_motion));
                break;
-       default:
-               throw Exception("Invalid source in Control");
        }
 }
 
index 6a78882c06273f209830253202acc79c8aa96f84..cbf9f78a689e083d79057945ba84eb218aba3685 100644 (file)
@@ -1,7 +1,9 @@
+#include <stdexcept>
 #include <sigc++/bind.h>
-#include <msp/core/except.h>
 #include "hub.h"
 
+using namespace std;
+
 namespace Msp {
 namespace Input {
 
@@ -24,7 +26,7 @@ std::string Hub::get_button_name(unsigned btn) const
 {
        unsigned dev_index = btn>>12;
        if(dev_index>=devices.size())
-               throw InvalidParameterValue("Button does not exist");
+               throw invalid_argument("Hub::get_button_name");
 
        const Device &dev = *devices[dev_index];
        return dev.get_name()+": "+dev.get_button_name(btn&0xFFF);
@@ -34,7 +36,7 @@ std::string Hub::get_axis_name(unsigned axis) const
 {
        unsigned dev_index = axis>>12;
        if(dev_index>=devices.size())
-               throw InvalidParameterValue("Axis does not exist");
+               throw invalid_argument("Hub::get_axis_name");
 
        const Device &dev = *devices[dev_index];
        return dev.get_name()+": "+dev.get_axis_name(axis&0xFFF);
index 082e02299b9c829f4458024147d8fe3d16b0f099..508c06fb9d24b4a38bdd216b1231924d9a0f4e7b 100644 (file)
@@ -1,11 +1,11 @@
 #include <map>
+#include <stdexcept>
 #ifdef WIN32
 #include <windows.h>
 #else
 #include <X11/X.h>
 #include <X11/keysym.h>
 #endif
-#include <msp/core/except.h>
 #include "keys.h"
 
 using namespace std;
@@ -135,7 +135,7 @@ unsigned key_from_sys(unsigned code)
 unsigned key_to_sys(unsigned key)
 {
        if(key>=N_KEYS_)
-               throw InvalidParameterValue("Key out of range");
+               throw invalid_argument("key_to_sys");
        return keymap[key];
 }