Skip to content

Commit

Permalink
Add linux alert (#294)
Browse files Browse the repository at this point in the history
Very early WIP implementation of gtk dialog alert to fix #278 (please ignore the commented out code etc. just trying to get stuff working)

Progress so far:

No longer chokes on `#include <gtk/gtk.h>` - currently need to tweak the borrowed snippet from #278 as an example to create an alert doesn't work as is, I think

Things to note/note to self:
- [x] I have to rework the `discover.ml` so it can use the pkg_config functionality on linux but behave as normal on other systems

- [x] Fix GTK crash on opening dialog - things compile but the function to open a dialog causes a crash, note this also happens in a separate project when I try and create a dialog with the same function (might be specific to my machine or more likely a bug in the code)
  • Loading branch information
akinsho authored Feb 5, 2019
1 parent 8053caf commit ed83266
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 47 deletions.
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
# ESY__NPM_ROOT: /opt/hostedtoolcache/node/8.14.0/x64/lib/node_modules/esy

steps:
- script: sudo apt-get install -y libxrandr-dev libxinerama-dev libxcursor-dev libxi-dev libegl1-mesa-dev mesa-utils mesa-utils-extra ragel
- script: sudo apt-get install -y libxrandr-dev libxinerama-dev libxcursor-dev libxi-dev libegl1-mesa-dev mesa-utils mesa-utils-extra ragel libgtk-3-dev
- template: .ci/use-node.yml
- template: .ci/restore-build-cache.yml
- template: .ci/esy-build-steps.yml
Expand Down
3 changes: 3 additions & 0 deletions src/Native/ReveryGtk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extern "C" {
void revery_alert_gtk(void *pWin, const char *szMessage);
}
52 changes: 36 additions & 16 deletions src/Native/config/discover.ml
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
type os =
| Windows
| Mac
| Linux
| Unknown
module C = Configurator.V1

type os = Windows | Mac | Linux | Unknown

type config = {libs: string list; cflags: string list; flags: string list}

let get_mac_config () =
{cflags= ["-I"; "."; "-x"; "objective-c"]; libs= []; flags= []}

let get_linux_config c =
let default = {libs= []; cflags= []; flags= []} in
match C.Pkg_config.get c with
| None -> default
| Some pc -> (
match C.Pkg_config.query pc ~package:"gtk+-3.0" with
| None -> default
| Some conf -> {libs= conf.libs; cflags= conf.cflags; flags= []} )

let uname () =
let ic = Unix.open_process_in "uname" in
let uname = input_line ic in let () = close_in ic in uname
let uname = input_line ic in
let () = close_in ic in
uname

let get_os =
match Sys.os_type with
| "Win32" -> Windows
| _ ->
(match uname () with
| "Darwin" -> Mac
| "Linux" -> Linux
| _ -> Unknown)
let c_flags =
match get_os with | Mac -> ["-I"; "."; "-x"; "objective-c"] | _ -> []
let flags = []
;;Configurator.V1.Flags.write_sexp "c_flags.sexp" c_flags;
Configurator.V1.Flags.write_sexp "flags.sexp" flags
| _ -> (
match uname () with "Darwin" -> Mac | "Linux" -> Linux | _ -> Unknown )

let () =
C.main ~name:"discover" (fun c ->
let conf =
match get_os with
| Mac -> get_mac_config ()
| Linux -> get_linux_config c
| _ -> {libs= []; flags= []; cflags= []}
in
C.Flags.write_sexp "flags.sexp" conf.flags ;
C.Flags.write_sexp "c_flags.sexp" conf.cflags ;
C.Flags.write_sexp "c_library_flags.sexp" conf.libs )
60 changes: 32 additions & 28 deletions src/Native/dialog.cpp
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
#include <stdio.h>

#include <caml/mlvalues.h>
#include <caml/memory.h>
#include <caml/alloc.h>
#include <caml/callback.h>
#include <caml/memory.h>
#include <caml/mlvalues.h>

#ifdef WIN32
#include "ReveryWin32.h"
#include "ReveryWin32.h"
#elif __APPLE__
#include "ReveryCocoa.h"
#include "ReveryCocoa.h"
#else
#include "ReveryGtk.h"
#endif

extern "C" {
CAMLprim value
revery_alertSupported() {
#ifdef WIN32
return Val_true;
#elif __APPLE__
return Val_true;
#else
return Val_false;
#endif
}
CAMLprim value revery_alertSupported() {
#ifdef WIN32
return Val_true;
#elif __APPLE__
return Val_true;
#elif __linux__
return Val_true;
#else
return Val_false;
#endif
}

CAMLprim value
revery_alert(value vWindow, value vMessage) {
CAMLparam2(vWindow, vMessage);
const char *szMessage = String_val(vMessage);
void* pWin = (void *)vWindow;
CAMLprim value revery_alert(value vWindow, value vMessage) {
CAMLparam2(vWindow, vMessage);
const char *szMessage = String_val(vMessage);
void *pWin = (void *)vWindow;

#ifdef WIN32
revery_alert_win32(pWin, szMessage);
#elif __APPLE__
revery_alert_cocoa(pWin, szMessage);
#else
printf("WARNING - Not implemented: alert");
#endif
return Val_unit;
}
#ifdef WIN32
revery_alert_win32(pWin, szMessage);
#elif __APPLE__
revery_alert_cocoa(pWin, szMessage);
#elif __linux__
revery_alert_gtk(pWin, szMessage);
#else
printf("WARNING - Not implemented: alert");
#endif
return Val_unit;
}
}
33 changes: 33 additions & 0 deletions src/Native/dialog_gtk.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifdef __linux__
#include <gtk/gtk.h>

// The callback to g_signal_connect MUST be an `activate` function
static void activate(GtkApplication *app, const char *user_data) {
GtkWidget *dialog;

GtkDialogFlags flags = GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT;
dialog = gtk_message_dialog_new(NULL, flags, GTK_MESSAGE_INFO,
GTK_BUTTONS_CLOSE, user_data);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
}

void revery_alert_gtk(void *pWin, const char *szMessage) {
/*
* TODO:
* 1. figure out how to convert the pointer from an X11 window handle
* to a GTK window, see (for inspiration):
* https://gist.github.com/mmozeiko/2401933b1fa89e5d5bd238b33eab0465
*
* 2. Get reference to revery application, is there an existing
* gtk application reference when a glfw window is created that can be reused?
*/
GtkApplication *app;
app = gtk_application_new("org.gtk.revery", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), (gpointer)szMessage);
/* argv the final argument to run can be set to NULL in which case argc should
* be set to 0 */
g_application_run(G_APPLICATION(app), 0, NULL);
g_object_unref(app);
}
#endif
5 changes: 3 additions & 2 deletions src/Native/dune
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
(preprocess (pps lwt_ppx))
(library_flags (:include flags.sexp))
(js_of_ocaml (javascript_files dialog.js))
(c_names dialog_cocoa dialog_win32)
(c_names dialog_cocoa dialog_win32 dialog_gtk)
(cxx_names dialog)
(c_flags (:include c_flags.sexp))
(c_library_flags (:include c_library_flags.sexp))
(libraries reglfw))

(rule
(targets c_flags.sexp flags.sexp)
(targets flags.sexp c_flags.sexp c_library_flags.sexp)
(deps (:discover config/discover.exe))
(action (run %{discover})))

0 comments on commit ed83266

Please sign in to comment.