4coder/platform_linux/linux_error_box.cpp

191 lines
5.4 KiB
C++
Raw Normal View History

2017-07-18 23:41:28 +00:00
/*
* Mr. 4th Dimention - Allen Webster
* (Mostly by insofaras)
*
* 18.07.2017
*
* Linux fatal error message box.
*
*/
// TOP
// HACK(allen): // NOTE(inso): this was a quick hack, might need some cleanup.
internal void
2017-11-10 21:13:02 +00:00
system_error_box(char *msg, b32 shutdown = true){
2020-02-02 15:50:18 +00:00
fprintf(stderr, "Fatal Error: %s\n", msg);
//LOGF("Fatal Error: %s\n", msg);
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
Display *dpy = XOpenDisplay(0);
if (!dpy){
exit(1);
}
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
const int num_cols = 50;
int win_w = (num_cols + 10) * 9;
int win_h = 140;
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
{
const char *start_p = msg, *space_p = NULL;
for(const char* p = msg; *p; ++p){
if (*p == ' ') space_p = p;
if (*p == '\n' || p - start_p > num_cols){
win_h += 18;
start_p = space_p ? space_p + 1 : p;
space_p = NULL;
}
}
}
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
Window w = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy), 0, 0, win_w, win_h, 0, 0, 0x227A3B);
XStoreName(dpy, w, "4coder Error");
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
XSizeHints* sh = XAllocSizeHints();
sh->flags = PMinSize;
sh->min_width = win_w;
sh->min_height = win_h;
XSetWMNormalHints(dpy, w, sh);
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
Atom type = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
XChangeProperty(dpy, w, XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False), XA_ATOM, 32, PropModeReplace, (unsigned char*) &type, 1);
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
Atom WM_DELETE_WINDOW = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
XSetWMProtocols(dpy, w, &WM_DELETE_WINDOW, 1);
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
linux_set_icon(dpy, w);
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
XMapRaised(dpy, w);
XSync(dpy, False);
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
XSelectInput(dpy, w, ExposureMask | StructureNotifyMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask | KeyPressMask);
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
XFontStruct* font = XLoadQueryFont(dpy, "-*-fixed-bold-*-*-*-*-140-*-*-*-*-iso8859-1");
if (!font){
exit(1);
}
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
XGCValues gcv;
gcv.foreground = WhitePixel(dpy, 0);
gcv.line_width = 2;
gcv.font = font->fid;
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
GC gc1 = XCreateGC(dpy, w, GCForeground | GCFont | GCLineWidth, &gcv);
gcv.foreground = BlackPixel(dpy, 0);
GC gc2 = XCreateGC(dpy, w, GCForeground | GCFont | GCLineWidth, &gcv);
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
int button_trigger = 0;
int button_hi = 0;
2020-02-02 20:20:27 +00:00
int redraw = 1;
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
XEvent ev;
while (1){
XNextEvent(dpy, &ev);
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
if (ev.type == Expose) redraw = 1;
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
if (ev.type == ConfigureNotify){
redraw = 1;
win_w = ev.xconfigure.width;
win_h = ev.xconfigure.height;
}
2020-02-02 15:50:18 +00:00
XRectangle button_rect = { (short)(win_w/2-40), (short)(win_h*0.8f), 80, 20 };
2017-07-18 23:41:28 +00:00
if (ev.type == MotionNotify){
int new_hi = (ev.xmotion.x > button_rect.x &&
ev.xmotion.y > button_rect.y &&
ev.xmotion.x < button_rect.x + button_rect.width &&
ev.xmotion.y < button_rect.y + button_rect.height);
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
if (new_hi != button_hi){
button_hi = new_hi;
redraw = 1;
}
}
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
if (ev.type == KeyPress){
KeySym sym = XLookupKeysym(&ev.xkey, 0);
if (sym == XK_Escape || sym == XK_Return){
exit(1);
}
}
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
if (ev.type == ButtonPress && ev.xbutton.button == Button1){
if (button_hi) button_trigger = 1;
redraw = 1;
}
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
if (ev.type == ButtonRelease && ev.xbutton.button == Button1){
if (button_trigger){
if (button_hi){
exit(1);
} else {
button_trigger = 0;
}
}
redraw = 1;
}
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
if (ev.type == ClientMessage && ev.xclient.window == w && (Atom)ev.xclient.data.l[0] == WM_DELETE_WINDOW){
exit(1);
}
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
#define DRAW_STR(x, y, str, len) \
XDrawString(dpy, w, gc2, (x)+1, (y)+1, (str), (len)); \
XDrawString(dpy, w, gc1, (x) , (y) , (str), (len))
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
if (redraw){
2020-02-02 20:20:27 +00:00
redraw = 0;
2017-07-18 23:41:28 +00:00
XClearWindow(dpy, w);
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
const char* line_start = msg;
const char* last_space = NULL;
int y = 30;
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
{
const char title[] = "4coder - Fatal Error";
int width = XTextWidth(font, title, sizeof(title)-1);
int x = (win_w/2) - (width/2);
DRAW_STR(x, y, title, sizeof(title)-1);
}
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
y += 36;
int width = XTextWidth(font, "x", 1) * num_cols;
int x = (win_w/2) - (width/2);
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
for(const char* p = line_start; *p; ++p){
if (*p == ' ') last_space = p;
if (p - line_start > num_cols || *p == '\n' || !p[1]){
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
const char* new_line_start = last_space + 1;
if (!last_space || *p == '\n' || !p[1]){
new_line_start = last_space = (p + !p[1]);
}
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
DRAW_STR(x, y, line_start, last_space - line_start);
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
line_start = new_line_start;
last_space = NULL;
y += 18;
}
}
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
XDrawRectangles(dpy, w, gc1, &button_rect, 1);
if (button_hi || button_trigger){
XDrawRectangle(dpy, w, gc2, button_rect.x+1, button_rect.y+1, button_rect.width-2, button_rect.height-2);
}
2020-02-02 15:50:18 +00:00
2017-07-18 23:41:28 +00:00
DRAW_STR(button_rect.x + 20, button_rect.y + 15, "Drat!", 5);
}
}
#undef DRAW_STR
2020-02-02 15:50:18 +00:00
2017-11-10 21:13:02 +00:00
if (shutdown){
exit(1);
}
2017-07-18 23:41:28 +00:00
}
// BOTTOM