Skip to content

Commit

Permalink
Merge pull request #32 from ogaml/segfault_fix
Browse files Browse the repository at this point in the history
Definitely merging this one, I just want this bug to die.
  • Loading branch information
VLanvin committed Mar 5, 2017
2 parents 0ea1047 + 5539389 commit 5e74597
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 96 deletions.
17 changes: 12 additions & 5 deletions src/core/x11/stubs/atoms_stubs.c
Expand Up @@ -32,7 +32,8 @@ CAMLprim value
caml_xintern_atom(value disp, value nm, value exists)
{
CAMLparam3(disp, nm, exists);
Atom tmp = XInternAtom((Display*) disp, String_val(nm), Bool_val(exists));
Display* dpy = Display_val(disp);
Atom tmp = XInternAtom(dpy, String_val(nm), Bool_val(exists));
if(tmp == None)
CAMLreturn(Val_int(0));
else
Expand All @@ -46,12 +47,14 @@ CAMLprim value
caml_xset_wm_protocols(value disp, value win, value atoms, value size)
{
CAMLparam4(disp, win, atoms, size);
Display* dpy = Display_val(disp);
Window w = Window_val(win);
Atom tmp[Int_val(size)];
int i = 0;
for(i = 0; i < Int_val(size); i++) {
tmp[i] = (Atom) Field(atoms, i);
}
XSetWMProtocols((Display*) disp, (Window) win, tmp, Int_val(size));
XSetWMProtocols(dpy, w, tmp, Int_val(size));
CAMLreturn(Val_unit);
}

Expand All @@ -62,13 +65,15 @@ CAMLprim value
caml_xchange_property(value disp, value win, value prop, value atoms, value length)
{
CAMLparam5(disp, win, prop, atoms, length);
Display* dpy = Display_val(disp);
Window w = Window_val(win);
Atom tmp[Int_val(length)+1];
int i = 0;
for(i = 0; i < Int_val(length); i++) {
tmp[i] = (Atom) Field(atoms, i);
}
tmp[Int_val(length)] = None;
XChangeProperty((Display*) disp, (Window) win, (Atom) prop, XA_ATOM, 32, PropModeReplace, (unsigned char*) tmp, Int_val(length));
XChangeProperty(dpy, w, (Atom) prop, XA_ATOM, 32, PropModeReplace, (unsigned char*) tmp, Int_val(length));
CAMLreturn(Val_unit);
}

Expand All @@ -79,6 +84,8 @@ CAMLprim value
caml_xsend_event(value disp, value win, value prop, value atoms, value length)
{
CAMLparam5(disp, win, prop, atoms, length);
Display* dpy = Display_val(disp);
Window w = Window_val(win);
XEvent xev;
int i = 0;
for(i = 0; i < Int_val(length); i++) {
Expand All @@ -88,9 +95,9 @@ caml_xsend_event(value disp, value win, value prop, value atoms, value length)
xev.xclient.type = ClientMessage;
xev.xclient.serial = 0;
xev.xclient.send_event = True;
xev.xclient.window = (Window) win;
xev.xclient.window = w;
xev.xclient.message_type = (Atom) prop;
xev.xclient.format = 32;
XSendEvent((Display*) disp, DefaultRootWindow((Display*) disp), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
XSendEvent(dpy, DefaultRootWindow(dpy), False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
CAMLreturn(Val_unit);
}
25 changes: 14 additions & 11 deletions src/core/x11/stubs/display_stubs.c
Expand Up @@ -10,16 +10,14 @@ CAMLprim value
caml_xopen_display(value name)
{
CAMLparam1(name);
if(current_display != NULL)
CAMLreturn((value)current_display);
if(current_display != NULL) {}
else if(name == Val_none) {
current_display = XOpenDisplay(NULL);
CAMLreturn((value)current_display);
}
else {
current_display = XOpenDisplay(String_val(Some_val(name)));
CAMLreturn((value)current_display);
}
CAMLreturn(Val_Display(current_display));
}


Expand All @@ -30,8 +28,9 @@ caml_xscreen_size(value disp, value screen)
{
CAMLparam2(disp, screen);

int w = XDisplayWidth ((Display*) disp, Int_val(screen));
int h = XDisplayHeight((Display*) disp, Int_val(screen));
Display* dpy = Display_val(disp);
int w = XDisplayWidth (dpy, Int_val(screen));
int h = XDisplayHeight(dpy, Int_val(screen));

CAMLreturn(Int_pair(w,h));
}
Expand All @@ -44,8 +43,9 @@ caml_xscreen_sizemm(value disp, value screen)
{
CAMLparam2(disp, screen);

int w = XDisplayWidthMM ((Display*) disp, Int_val(screen));
int h = XDisplayHeightMM ((Display*) disp, Int_val(screen));
Display* dpy = Display_val(disp);
int w = XDisplayWidthMM (dpy, Int_val(screen));
int h = XDisplayHeightMM (dpy, Int_val(screen));

CAMLreturn(Int_pair(w,h));
}
Expand All @@ -57,7 +57,8 @@ CAMLprim value
caml_xscreen_count(value disp)
{
CAMLparam1(disp);
CAMLreturn(Val_int(XScreenCount((Display*) disp)));
Display* dpy = Display_val(disp);
CAMLreturn(Val_int(XScreenCount(dpy)));
}


Expand All @@ -67,7 +68,8 @@ CAMLprim value
caml_xdefault_screen(value disp)
{
CAMLparam1(disp);
CAMLreturn(Val_int(XDefaultScreen((Display*) disp)));
Display* dpy = Display_val(disp);
CAMLreturn(Val_int(XDefaultScreen(dpy)));
}


Expand All @@ -77,7 +79,8 @@ CAMLprim value
caml_xflush(value disp)
{
CAMLparam1(disp);
XFlush((Display*) disp);
Display* dpy = Display_val(disp);
XFlush(dpy);
CAMLreturn(Val_unit);
}

24 changes: 15 additions & 9 deletions src/core/x11/stubs/event_stubs.c
Expand Up @@ -11,14 +11,16 @@ caml_xselect_input(value disp, value win, value masks)
{
CAMLparam3(disp, win, masks);
CAMLlocal2(hd, tl);
Display* dpy = Display_val(disp);
Window w = Window_val(win);
int mask = 0;
tl = masks;
while(tl != Val_emptylist) {
hd = Field(tl,0);
tl = Field(tl,1);
mask |= (1L << (Int_val(hd)));
}
XSelectInput((Display*) disp, (Window) win, mask);
XSelectInput(dpy, w, mask);
CAMLreturn(Val_unit);
}

Expand All @@ -36,15 +38,19 @@ CAMLprim value
caml_xnext_event(value disp, value win)
{
CAMLparam1(disp);
CAMLlocal1(evt);
CAMLlocal2(res, ev);
Display* dpy = Display_val(disp);
Window w = Window_val(win);
XEvent event;
if(XCheckIfEvent((Display*) disp, &event, &checkEvent, (XPointer)win) == True) {
evt = caml_alloc_custom(&empty_custom_opts, sizeof(XEvent), 0, 1);
memcpy(Data_custom_val(evt), &event, sizeof(XEvent));
CAMLreturn(Val_some(evt));
if(XCheckIfEvent(dpy, &event, &checkEvent, (XPointer)w) == True) {
XEvent_alloc(ev);
XEvent_copy(ev, &event);
res = Val_some(ev);
}
else
CAMLreturn(Val_int(0));
else {
res = Val_int(0);
}
CAMLreturn(res);
}


Expand Down Expand Up @@ -197,7 +203,7 @@ caml_event_type(value evt)
{
CAMLparam1(evt);
CAMLlocal1(result);
result = extract_event((XEvent*)Data_custom_val(evt));
result = extract_event(&XEvent_val(evt));
CAMLreturn(result);
}

Expand Down
41 changes: 26 additions & 15 deletions src/core/x11/stubs/glx_stubs.c
Expand Up @@ -12,10 +12,12 @@ CAMLprim value
caml_glx_choose_visual(value disp, value scr, value attributes, value len)
{
CAMLparam4(disp, scr, attributes, len);
CAMLlocal2(hd,tl);
CAMLlocal3(hd,tl,res);

int attrs[Int_val(len)+1];
int i = 0;
int fbcount;
Display* dpy = Display_val(disp);

tl = attributes;

Expand Down Expand Up @@ -56,12 +58,14 @@ caml_glx_choose_visual(value disp, value scr, value attributes, value len)
}

attrs[i] = None;

int fbcount;
GLXFBConfig* fbc = glXChooseFBConfig((Display*)disp, Int_val(scr), attrs, &fbcount);
XVisualInfo* vis = glXGetVisualFromFBConfig((Display*)disp, fbc[0]);

CAMLreturn((value) vis);
GLXFBConfig* fbc = glXChooseFBConfig(dpy, Int_val(scr), attrs, &fbcount);
XVisualInfo* vis = glXGetVisualFromFBConfig(dpy, fbc[0]);

XVisualInfo_alloc(res);
XVisualInfo_copy(res, vis);

CAMLreturn(res);
}


Expand All @@ -72,13 +76,13 @@ caml_glx_create_context(value disp, value vi)
{
CAMLparam2(disp, vi);

// a GLXContext is already a pointer
GLXContext tmp = glXCreateContext(
(Display*) disp,
(XVisualInfo*) vi,
NULL, True);
Display* dpy = Display_val(disp);
XVisualInfo* xvi = XVisualInfo_val(vi);

GLXContext tmp = glXCreateContext(dpy, xvi, NULL, True);

CAMLreturn((value)tmp);
// a GLXContext is a pointer
CAMLreturn(Val_GLXContext(tmp));
}


Expand All @@ -88,7 +92,9 @@ CAMLprim value
caml_glx_swap_buffers(value disp, value win)
{
CAMLparam2(disp, win);
glXSwapBuffers((Display*)disp, (Window)win);
Display* dpy = Display_val(disp);
Window w = Window_val(win);
glXSwapBuffers(dpy, w);
CAMLreturn(Val_unit);
}

Expand All @@ -99,7 +105,10 @@ CAMLprim value
caml_glx_make_current(value disp, value win, value ctx)
{
CAMLparam3(disp, win, ctx);
glXMakeCurrent((Display*)disp, (Window)win, (GLXContext)ctx);
Display* dpy = Display_val(disp);
Window w = Window_val(win);
GLXContext glc = GLXContext_val(ctx);
glXMakeCurrent(dpy, w, glc);
CAMLreturn(Val_unit);
}

Expand All @@ -110,7 +119,9 @@ CAMLprim value
caml_glx_destroy_context(value disp, value ctx)
{
CAMLparam2(disp, ctx);
glXDestroyContext((Display*)disp, (GLXContext)ctx);
Display* dpy = Display_val(disp);
GLXContext glc = GLXContext_val(ctx);
glXDestroyContext(dpy, glc);
CAMLreturn(Val_unit);
}

Expand Down
10 changes: 6 additions & 4 deletions src/core/x11/stubs/keyboard_stubs.c
Expand Up @@ -5,13 +5,15 @@
#include "utils.h"

CAMLprim value
caml_is_key_down(value display, value code)
caml_is_key_down(value disp, value code)
{
CAMLparam2(display, code);
CAMLparam2(disp, code);

Display* dpy = Display_val(disp);

xcb_generic_error_t* error = NULL;

xcb_connection_t* conn = XGetXCBConnection((Display*) display);
xcb_connection_t* conn = XGetXCBConnection(dpy);

xcb_query_keymap_reply_t* keymap =
xcb_query_keymap_reply(conn,
Expand All @@ -27,7 +29,7 @@ caml_is_key_down(value display, value code)
int val = Int_val(Field(code,0));
char str[2] = {val, '\0'};
int ks = XStringToKeysym(str);
sym = XKeysymToKeycode((Display*)display, ks);
sym = XKeysymToKeycode(dpy, ks);
}

CAMLreturn(Val_bool((keymap->keys[sym / 8] & (1 << (sym % 8))) != 0));
Expand Down
14 changes: 10 additions & 4 deletions src/core/x11/stubs/mouse_stubs.c
Expand Up @@ -8,8 +8,10 @@ CAMLprim value
caml_xwarp_pointer(value disp, value win, value x, value y)
{
CAMLparam4(disp, win, x, y);
XWarpPointer((Display*)disp, None, (Window)win, 0, 0, 0, 0, Int_val(x), Int_val(y));
XFlush((Display*)disp);
Display* dpy = Display_val(disp);
Window w = Window_val(win);
XWarpPointer(dpy, None, w, 0, 0, 0, 0, Int_val(x), Int_val(y));
XFlush(dpy);
CAMLreturn(Val_unit);
}

Expand All @@ -20,10 +22,12 @@ CAMLprim value
caml_xquery_pointer_position(value disp, value win)
{
CAMLparam2(disp, win);
Display* dpy = Display_val(disp);
Window w = Window_val(win);
Window rr, cr;
int rx, ry, wx, wy;
unsigned int mask;
XQueryPointer((Display*)disp, (Window)win, &rr, &cr, &rx, &ry, &wx, &wy, &mask);
XQueryPointer(dpy, w, &rr, &cr, &rx, &ry, &wx, &wy, &mask);
CAMLreturn(Int_pair(wx, wy));
}

Expand All @@ -35,10 +39,12 @@ caml_xquery_button_down(value disp, value win, value but)
{
CAMLparam3(disp, win, but);
CAMLlocal1(res); res = Val_false;
Display* dpy = Display_val(disp);
Window w = Window_val(win);
Window rr, cr;
int rx, ry, wx, wy;
unsigned int mask;
XQueryPointer((Display*)disp, (Window)win, &rr, &cr, &rx, &ry, &wx, &wy, &mask);
XQueryPointer(dpy, w, &rr, &cr, &rx, &ry, &wx, &wy, &mask);
if(Int_val(but) >= 1 && Int_val(but) <= 5) {
if((1 << (Int_val(but) + 7)) & mask)
res = Val_true;
Expand Down
32 changes: 25 additions & 7 deletions src/core/x11/stubs/utils.c
Expand Up @@ -2,15 +2,33 @@

#include "utils.h"

struct custom_operations empty_custom_opts = {
.identifier = "obj_st handling",
.finalize = custom_finalize_default,
.compare = custom_compare_default,
.hash = custom_hash_default,
.serialize = custom_serialize_default,
.deserialize = custom_deserialize_default
static struct custom_operations XVisualInfo_custom_ops = {
identifier : "XVisualInfo handling",
finalize : custom_finalize_default,
compare : custom_compare_default,
hash : custom_hash_default,
serialize : custom_serialize_default,
deserialize : custom_deserialize_default
};

static struct custom_operations XEvent_custom_ops = {
identifier : "XEvent handling",
finalize : custom_finalize_default,
compare : custom_compare_default,
hash : custom_hash_default,
serialize : custom_serialize_default,
deserialize : custom_deserialize_default
};

static struct custom_operations Window_custom_ops = {
identifier : "Window handling",
finalize : custom_finalize_default,
compare : custom_compare_default,
hash : custom_hash_default,
serialize : custom_serialize_default,
deserialize : custom_deserialize_default
};

value Val_some(value v)
{
CAMLparam1(v);
Expand Down

0 comments on commit 5e74597

Please sign in to comment.