1 #define _XOPEN_SOURCE 700
4 #include <caml/alloc.h>
5 #include <caml/memory.h>
6 #include <caml/custom.h>
7 #include <caml/signals.h>
8 #include <caml/mlvalues.h>
9 #include <caml/bigarray.h>
17 #include <sys/sysinfo.h>
22 static void failwith_fmt (const char *fmt
, ...) Noreturn
;
23 static void failwith_fmt (const char *fmt
, ...)
29 vsnprintf (buf
, sizeof (buf
), fmt
, ap
);
35 CAMLprim value
ml_waitalrm (value unit_v
)
42 sigaddset (&set
, SIGALRM
);
44 caml_enter_blocking_section ();
46 if (sigwait (&set
, &signr
)) {
47 failwith_fmt ("sigwait: %s", strerror (errno
));
50 caml_leave_blocking_section ();
52 CAMLreturn (Val_unit
);
55 CAMLprim value
ml_sysinfo (value unit_v
)
58 CAMLlocal2 (res_v
, loads_v
);
62 failwith_fmt ("sysinfo: %s", strerror (errno
));
65 loads_v
= caml_alloc_tuple (3);
66 Store_field (loads_v
, 0, caml_copy_int64 (si
.loads
[0]));
67 Store_field (loads_v
, 1, caml_copy_int64 (si
.loads
[1]));
68 Store_field (loads_v
, 2, caml_copy_int64 (si
.loads
[2]));
70 res_v
= caml_alloc_tuple (9);
71 Store_field (res_v
, 0, caml_copy_int64 (si
.uptime
));
72 Store_field (res_v
, 1, loads_v
);
73 Store_field (res_v
, 2, caml_copy_int64 (si
.totalram
));
74 Store_field (res_v
, 3, caml_copy_int64 (si
.freeram
));
75 Store_field (res_v
, 4, caml_copy_int64 (si
.sharedram
));
76 Store_field (res_v
, 5, caml_copy_int64 (si
.bufferram
));
77 Store_field (res_v
, 6, caml_copy_int64 (si
.totalswap
));
78 Store_field (res_v
, 7, caml_copy_int64 (si
.freeswap
));
79 Store_field (res_v
, 8, caml_copy_int64 (si
.procs
));
84 CAMLprim value
ml_get_nprocs (value unit_v
)
89 nprocs
= get_nprocs ();
91 failwith_fmt ("get_nprocs: %s", strerror (errno
));
94 CAMLreturn (Val_int (nprocs
));
97 CAMLprim value
ml_idletimeofday (value fd_v
, value nprocs_v
)
99 CAMLparam2 (fd_v
, nprocs_v
);
102 int fd
= Int_val (fd_v
);
103 int nprocs
= Int_val (nprocs_v
);
104 size_t n
= nprocs
* sizeof (tv
);
111 failwith_fmt ("alloca: %s", strerror (errno
));
114 m
= read (fd
, buf
, n
);
116 failwith_fmt ("read [n=%zu, m=%zi]: %s", n
, m
, strerror (errno
));
119 res_v
= caml_alloc (nprocs
* Double_wosize
, Double_array_tag
);
120 for (i
= 0; i
< nprocs
; ++i
) {
121 double d
= buf
[i
].tv_sec
+ buf
[i
].tv_usec
* 1e-6;
123 Store_double_field (res_v
, i
, d
);
128 CAMLprim value
ml_get_hz (value unit_v
)
131 CAMLreturn (Val_int (sysconf (_SC_CLK_TCK
)));
134 CAMLprim value
ml_nice (value nice_v
)
137 int niceval
= Int_val (nice_v
);
139 if (!nice (niceval
)) {
140 failwith_fmt ("nice %d: %s", niceval
, strerror (errno
));
143 CAMLreturn (Val_unit
);
148 #include <X11/Xlib.h>
149 #include <X11/Xatom.h>
160 CAMLprim value
ml_seticon (value data_v
)
163 static struct X11State static_state
;
164 struct X11State
*s
= &static_state
;
165 void *ptr
= String_val (data_v
);
167 unsigned char *data
= ptr
;
171 s
->dpy
= XOpenDisplay (NULL
);
176 /* "tiny bit" hackish */
177 s
->id
= glXGetCurrentDrawable ();
182 s
->property
= XInternAtom (s
->dpy
, "_NET_WM_ICON", False
);
183 if (s
->property
== None
){
188 printf ("id = %#x, property = %d\n",
189 (int) s
->id
, (int) s
->property
);
197 XChangeProperty (s
->dpy
, s
->id
, s
->property
, XA_CARDINAL
,
198 32, PropModeReplace
, data
, 32 * 32 + 2);
200 CAMLreturn (Val_unit
);
203 XCloseDisplay (s
->dpy
);
206 CAMLreturn (Val_unit
);