<commit_before>#lang racket/base
(require racket/class
         ffi/unsafe
         "utils.rkt"
         "types.rkt"
         "../../lock.rkt"
         "../common/queue.rkt"
         "../common/freeze.rkt"
         "clipboard.rkt"
         "const.rkt"
	 "w32.rkt"
         "unique.rkt")

(provide (protect-out gtk-start-event-pump
                      try-to-sync-refresh
                      set-widget-hook!
                      x11-display)
         ;; from common/queue:
         current-eventspace
         queue-event
         yield)


;; ------------------------------------------------------------
;; Gtk initialization

(define-gtk gtk_init_check (_fun (_ptr io _int) (_ptr io _gcpointer) -> _gboolean))

(define x11-display
  (let* ([argc-ptr (scheme_register_process_global "PLT_X11_ARGUMENT_COUNT" #f)]
         [argc (or (and argc-ptr (cast argc-ptr _pointer _long)) 0)]
         [argv (and (positive? argc)
                    (scheme_register_process_global "PLT_X11_ARGUMENTS" #f))]
         [display (getenv "DISPLAY")])
    ;; Convert X11 arguments, if any, to Gtk form:
    (let-values ([(args single-instance?)
                  (if (zero? argc)
                      (values null #f)
                      (let loop ([i 1][si? #f])
                        (if (= i argc) 
                            (values null si?)
                            (let ([s (ptr-ref argv _bytes i)])
                              (cond
                               [(bytes=? s #"-display")
                                (let-values ([(args si?) (loop (+ i 2) si?)]
                                             [(d) (ptr-ref argv _bytes (add1 i))])
                                  (set! display (bytes->string/utf-8 d #\?))
                                  (values (list* #"--display" d args)
                                          si?))]
                               [(bytes=? s #"-synchronous")
                                (let-values ([(args si?) (loop (+ i 1) si?)])
                                  (values (cons #"--sync" args)
                                          si?))]
                               [(bytes=? s #"-singleInstance")
                                (loop (add1 i) #t)]
                               [(or (bytes=? s #"-iconic")
                                    (bytes=? s #"-rv")
                                    (bytes=? s #"+rv")
                                    (bytes=? s #"-reverse"))
                                ;; ignored with 0 arguments
                                (loop (add1 i) #t)]
                               [else
                                ;; all other ignored flags have a single argument
                                (loop (+ i 2) #t)])))))])
      (let-values ([(new-argc new-argv)
                    (if (null? args)
                        (values 0 #f)
                        (values (add1 (length args))
                                (cast (cons (ptr-ref argv _bytes 0)
                                            args)
                                      (_list i _bytes)
                                      _pointer)))])
        (unless (gtk_init_check new-argc new-argv)
          (error (format
                  "Gtk initialization failed for display ~s"
                  (or display ":0"))))
        (when single-instance?
          (do-single-instance))
        display))))

;; ------------------------------------------------------------
;; Gtk event pump

(define-gtk gtk_events_pending (_fun -> _gboolean))
(define-gtk gtk_main_iteration_do (_fun _gboolean -> _gboolean))

(define _GMainContext (_cpointer 'GMainContext))
(define _GdkEvent (_cpointer 'GdkEvent))

(define-cstruct _GPollFD ([fd _int]
                          [events _short]
                          [revents _short]))

(define-glib g_main_context_default (_fun -> _GMainContext))
(define-glib g_main_context_query (_fun _GMainContext
					_int
					_pointer
					_pointer ;; GPollFD array
					_int<commit_msg>gtk: set app name on startup<commit_after>#lang racket/base
(require racket/class
         ffi/unsafe
         "utils.rkt"
         "types.rkt"
         "../../lock.rkt"
         "../common/queue.rkt"
         "../common/freeze.rkt"
         "clipboard.rkt"
         "const.rkt"
	 "w32.rkt"
         "unique.rkt")

(provide (protect-out gtk-start-event-pump
                      try-to-sync-refresh
                      set-widget-hook!
                      x11-display)
         ;; from common/queue:
         current-eventspace
         queue-event
         yield)


;; ------------------------------------------------------------
;; Gtk initialization

(define-gtk gtk_init_check (_fun (_ptr io _int) (_ptr io _gcpointer) -> _gboolean))

(define x11-display
  (let* ([argc-ptr (scheme_register_process_global "PLT_X11_ARGUMENT_COUNT" #f)]
         [argc (or (and argc-ptr (cast argc-ptr _pointer _long)) 0)]
         [argv (and (positive? argc)
                    (scheme_register_process_global "PLT_X11_ARGUMENTS" #f))]
         [display (getenv "DISPLAY")])
    ;; Convert X11 arguments, if any, to Gtk form:
    (let-values ([(args single-instance?)
                  (if (zero? argc)
                      (values null #f)
                      (let loop ([i 1][si? #f])
                        (if (= i argc) 
                            (values null si?)
                            (let ([s (ptr-ref argv _bytes i)])
                              (cond
                               [(bytes=? s #"-display")
                                (let-values ([(args si?) (loop (+ i 2) si?)]
                                             [(d) (ptr-ref argv _bytes (add1 i))])
                                  (set! display (bytes->string/utf-8 d #\?))
                                  (values (list* #"--display" d args)
                                          si?))]
                               [(bytes=? s #"-synchronous")
                                (let-values ([(args si?) (loop (+ i 1) si?)])
                                  (values (cons #"--sync" args)
                                          si?))]
                               [(bytes=? s #"-singleInstance")
                                (loop (add1 i) #t)]
                               [(or (bytes=? s #"-iconic")
                                    (bytes=? s #"-rv")
                                    (bytes=? s #"+rv")
                                    (bytes=? s #"-reverse"))
                                ;; ignored with 0 arguments
                                (loop (add1 i) #t)]
                               [else
                                ;; all other ignored flags have a single argument
                                (loop (+ i 2) #t)])))))])
      (let-values ([(new-argc new-argv)
                    (values (add1 (length args))
                            (cast (cons (path->bytes (find-system-path 'run-file))
                                        args)
                                  (_list i _bytes)
                                  _pointer))])
        (unless (gtk_init_check new-argc new-argv)
          (error (format
                  "Gtk initialization failed for display ~s"
                  (or display ":0"))))
        (when single-instance?
          (do-single-instance))
        display))))

;; ------------------------------------------------------------
;; Gtk event pump

(define-gtk gtk_events_pending (_fun -> _gboolean))
(define-gtk gtk_main_iteration_do (_fun _gboolean -> _gboolean))

(define _GMainContext (_cpointer 'GMainContext))
(define _GdkEvent (_cpointer 'GdkEvent))

(define-cstruct _GPollFD ([fd _int]
                          [events _short]
                          [revents _short]))

(define-glib g_main_context_default (_fun -> _GMainContext))
(define-glib g_main_context_query (_fun _GMainContext
					_int
					_pointer
					_pointer ;; GPollFD array
					_int
{'fraction_non_alphanumeric': 0.14519788253087976, 'fraction_numerical': 0.006301991429291656, 'mean_word_length': 1.0078441295546559, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 35, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7376180', 'n_tokens_mistral': 2208, 'n_tokens_neox': 2033, 'n_words': 527}