#include "xlib.h"

s48_ref_t scx_List_Extensions (s48_call_t call, s48_ref_t d) {
  s48_ref_t ret;
  int n, i;
  char **p;
  
  //Disable_Interrupts;
  p = XListExtensions (scx_extract_display(call, d), &n);
  //Enable_Interrupts;
  ret = s48_make_vector_2(call, n, s48_false_2(call));
  for (i = 0; i < n; i++) {
    s48_vector_set_2(call, ret, i, s48_enter_string(call, p[i]));
  }
  XFreeExtensionList(p);
  return ret;
}

s48_ref_t scx_Query_Extension (s48_call_t call, s48_ref_t d, s48_ref_t name) {
  int opcode, event, error;
  s48_ref_t ret;

  if (!XQueryExtension (scx_extract_display(call, d), 
			s48_extract_string(call, name), 
			&opcode, &event, &error))
    return s48_false_2(call);

  ret = s48_make_vector_2(call, 3, s48_false_2(call));

  s48_vector_set_2(call, ret, 0, opcode ? 
       s48_enter_long_2 (call, opcode) : s48_false_2(call));
  s48_vector_set_2(call, ret, 1, event ? 
       s48_enter_long_2 (call, event) : s48_false_2(call));
  s48_vector_set_2(call, ret, 2, error ? 
       s48_enter_long_2 (call, error) : s48_false_2(call));

  return ret;
}

void scx_init_extension () {
  S48_EXPORT_FUNCTION(scx_List_Extensions);
  S48_EXPORT_FUNCTION(scx_Query_Extension);
}
