<commit_before>%
% Authors:
%   <NAME> (2000)
%
% Copyright:
%   <NAME> (2000)
%
% Last change:
%   $Date$
%   $Revision$
%
% This file is part of Mozart, an implementation
% of Oz 3:
%   http://www.mozart-oz.org
%
% See the file "LICENSE" or
%   http://www.mozart-oz.org/LICENSE.html
% for information on usage and redistribution
% of this file, and for a DISCLAIMER OF ALL
% WARRANTIES.
%

functor

import
   GtkNative at 'GTK.so{native}'
   System

export
   Dispatcher

   % Registry stuff
   GetObject
   RegisterNativeObject
   UnregisterNativeObject
   RegisterObject

   % Non-autogenerated GTK classes
   Object

   % Misc
   Exit
   Main
   MainQuit

   \insert 'gtkexports.oz'

define

% -----------------------------------------------------------------------------
% Object Registry
% -----------------------------------------------------------------------------

   % stores GTK object --> OZ object corrospondence
   ObjectRegistry = {Dictionary.new $}

   % store a GTK object --> Oz object corrospondence
   proc {RegisterObject Object}
      NativeObj = {Object getNative($)}
   in
      {Dictionary.put ObjectRegistry {ForeignPointer.toInt NativeObj} Object}
   end

   % store a GTK object --> Oz object corrospondence
   proc {RegisterNativeObject Object NativeObj}
      {Dictionary.put ObjectRegistry {ForeignPointer.toInt NativeObj} Object}
   end

   proc {UnregisterNativeObject NativeObj}
      {Dictionary.remove ObjectRegistry {ForeignPointer.toInt NativeObj}}
   end

   % Get the corrosponding Oz object from a GTK object
   proc {GetObject MyForeignPointer ?MyObject}
      if MyForeignPointer == 0 then
         MyObject = nil
      else
         {Dictionary.get
          ObjectRegistry
          {ForeignPointer.toInt MyForeignPointer}
          MyObject}
      end
   end

% -----------------------------------------------------------------------------
% Object
% -----------------------------------------------------------------------------

   class Object
      attr nativeObject

      % For building Oz object from native objects
      % Only for internal use!
      meth newWrapper(NativeObj)
         nativeObject <- NativeObj
      end
      meth getNative($) % get native GTK object from an Oz object
         @nativeObject
      end

      meth ref
         {GtkNative.ref @nativeObject}
      end
      meth unref
         {GtkNative.unref @nativeObject}
      end

% Signal (made part of Object)

      meth signalConnect(Name Handler ?Id)
         % TODO: support user data (maybe superfluous)
         {Dispatcher registerHandler(Handler Id)}
         {GtkNative.signalConnect @nativeObject Name Id _}
      end
      meth signalDisconnect(Id)
         {Dispatcher unregisterSignal(Id)}
         {GtkNative.signalDisconnect @nativeObject Id}
      end
      meth signalHandlerBlock(HandlerId)
         {GtkNative.signalBlock @nativeObject HandlerId}
      end
      meth signalHandlerUnblock(HandlerId)
         {GtkNative.signalUnblock @nativeObject HandlerId}
      end
      meth signalEmitByName(Name)
         {GtkNative.signalEmitByName @nativeObject Name}
      end
   end

% -----------------------------------------------------------------------------
% Dispatcher
% -----------------------------------------------------------------------------

   local
      PollingIntervall = 50
   in
      class DispatcherClass
         attr
            id_counter : 0
            registry % A dictionary with id <--> handler corrospondences
            port
            stream
            fillerThread
         meth init
            proc {FillStream}
               {GtkNative.handlePendingEvents} % Send all pending GTK events to the Oz port
               {Time.delay PollingIntervall}
               {FillStream}
            end
         in
            registry <- {Dictionary.new}
            port     <- {Port.new @stream}
            {System.show 'M1'}
            {GtkNative.initializeSignalPort @port} % Tell the 'C side' about the signal port
            {System.show 'M2'}
            thread
               fillerThread <- {Thread.this $}
               {FillStream}
            end
            {System.show 'leaving SignalDispatcher init'}
         end
         meth GetUniqueID($)
            id_counter <- @id_counter + 1
            @id_counter
         end
         meth registerHandler(Handler ?Id)
         {self GetUniqueID(Id)}
            {Dictionary.put @registry Id Handler}
         end
         meth unregisterHandler(Id)
            {Dictionary.remove @registry Id}
         end
         meth dispatch
            Handler
            Event
            Tail
         in
            @stream = Event|Tail
            {Dictionary.get @registry Event Handler}
            % TODO: suspend marshaller with sending a new variable to a second port
            {Handler}
            % TODO: terminate marshaller with bounding this variable
            stream <- Tail
            DispatcherClass,dispatch
         end
         meth exit
            {Thread.terminate {Thread.this $}}
         end
      end
   end

% -----------------------------------------------------------------------------
% The abandoned and homeless
% -----------------------------------------------------------------------------

   proc {Main}
      {GtkNative.main}
   end

   proc {MainQuit}
      {Dispatcher exit}
      {GtkNative.mainQuit}
   end

   proc {Exit}
      {GtkNative.exit 0}
      {Dispatcher exit}
   end

% -----------------------------------------------------------------------------
% Autogenerated stuff
% -----------------------------------------------------------------------------

   \insert 'gtkclasses.oz'

% -----------------------------------------------------------------------------
% Finale
% -----------------------------------------------------------------------------

   % Start the dispatcher
   {System.show 'I am just a stupid debugging message ...'}
   Dispatcher = {New DispatcherClass init}
   {System.show 'I am an other stupid debugging message ...'}
   thread {Dispatcher dispatch} end
   {System.show 'Dispatcher successfully started and running ...'}

end % functor<commit_msg>Removed some silly debug messages<commit_after>%
% Authors:
%   <NAME> (2000)
%
% Copyright:
%   <NAME> (2000)
%
% Last change:
%   $Date$
%   $Revision$
%
% This file is part of Mozart, an implementation
% of Oz 3:
%   http://www.mozart-oz.org
%
% See the file "LICENSE" or
%   http://www.mozart-oz.org/LICENSE.html
% for information on usage and redistribution
% of this file, and for a DISCLAIMER OF ALL
% WARRANTIES.
%

functor

import
   GtkNative at 'GTK.so{native}'
   System

export
   Dispatcher

   % Registry stuff
   GetObject
   RegisterNativeObject
   UnregisterNativeObject
   RegisterObject

   % Non-autogenerated GTK classes
   Object

   % Misc
   Exit
   Main
   MainQuit

   \insert 'gtkexports.oz'

define

% -----------------------------------------------------------------------------
% Object Registry
% -----------------------------------------------------------------------------

   % stores GTK object --> OZ object corrospondence
   ObjectRegistry = {Dictionary.new $}

   % store a GTK object --> Oz object corrospondence
   proc {RegisterObject Object}
      NativeObj = {Object getNative($)}
   in
      {Dictionary.put ObjectRegistry {ForeignPointer.toInt NativeObj} Object}
   end

   % store a GTK object --> Oz object corrospondence
   proc {RegisterNativeObject Object NativeObj}
      {Dictionary.put ObjectRegistry {ForeignPointer.toInt NativeObj} Object}
   end

   proc {UnregisterNativeObject NativeObj}
      {Dictionary.remove ObjectRegistry {ForeignPointer.toInt NativeObj}}
   end

   % Get the corrosponding Oz object from a GTK object
   proc {GetObject MyForeignPointer ?MyObject}
      if MyForeignPointer == 0 then
         MyObject = nil
      else
         {Dictionary.get
          ObjectRegistry
          {ForeignPointer.toInt MyForeignPointer}
          MyObject}
      end
   end

% -----------------------------------------------------------------------------
% Object
% -----------------------------------------------------------------------------

   class Object
      attr nativeObject

      % For building Oz object from native objects
      % Only for internal use!
      meth newWrapper(NativeObj)
         nativeObject <- NativeObj
      end
      meth getNative($) % get native GTK object from an Oz object
         @nativeObject
      end

      meth ref
         {GtkNative.ref @nativeObject}
      end
      meth unref
         {GtkNative.unref @nativeObject}
      end

% Signal (made part of Object)

      meth signalConnect(Name Handler ?Id)
         % TODO: support user data (maybe superfluous)
         {Dispatcher registerHandler(Handler Id)}
         {GtkNative.signalConnect @nativeObject Name Id _}
      end
      meth signalDisconnect(Id)
         {Dispatcher unregisterSignal(Id)}
         {GtkNative.signalDisconnect @nativeObject Id}
      end
      meth signalHandlerBlock(HandlerId)
         {GtkNative.signalBlock @nativeObject HandlerId}
      end
      meth signalHandlerUnblock(HandlerId)
         {GtkNative.signalUnblock @nativeObject HandlerId}
      end
      meth signalEmitByName(Name)
         {GtkNative.signalEmitByName @nativeObject Name}
      end
   end

% -----------------------------------------------------------------------------
% Dispatcher
% -----------------------------------------------------------------------------

   local
      PollingIntervall = 50
   in
      class DispatcherClass
         attr
            id_counter : 0
            registry % A dictionary with id <--> handler corrospondences
            port
            stream
            fillerThread
         meth init
            proc {FillStream}
               {GtkNative.handlePendingEvents} % Send all pending GTK events to the Oz port
               {Time.delay PollingIntervall}
               {FillStream}
            end
         in
            registry <- {Dictionary.new}
            port     <- {Port.new @stream}
            {GtkNative.initializeSignalPort @port} % Tell the 'C side' about the signal port
            thread
               fillerThread <- {Thread.this $}
               {FillStream}
            end
            {System.show 'leaving SignalDispatcher init'}
         end
         meth GetUniqueID($)
            id_counter <- @id_counter + 1
            @id_counter
         end
         meth registerHandler(Handler ?Id)
         {self GetUniqueID(Id)}
            {Dictionary.put @registry Id Handler}
         end
         meth unregisterHandler(Id)
            {Dictionary.remove @registry Id}
         end
         meth dispatch
            Handler
            Event
            Tail
         in
            @stream = Event|Tail
            {Dictionary.get @registry Event Handler}
            % TODO: suspend marshaller with sending a new variable to a second port
            {Handler}
            % TODO: terminate marshaller with bounding this variable
            stream <- Tail
            DispatcherClass,dispatch
         end
         meth exit
            {Thread.terminate {Thread.this $}}
         end
      end
   end

% -----------------------------------------------------------------------------
% The abandoned and homeless
% -----------------------------------------------------------------------------

   proc {Main}
      {GtkNative.main}
   end

   proc {MainQuit}
      {Dispatcher exit}
      {GtkNative.mainQuit}
   end

   proc {Exit}
      {GtkNative.exit 0}
      {Dispatcher exit}
   end

% -----------------------------------------------------------------------------
% Autogenerated stuff
% -----------------------------------------------------------------------------

   \insert 'gtkclasses.oz'

% -----------------------------------------------------------------------------
% Finale
% -----------------------------------------------------------------------------

   % Start the dispatcher
   Dispatcher = {New DispatcherClass init}
   thread {Dispatcher dispatch} end
   {System.show 'Dispatcher successfully started and running ...'}

end % functor
{'fraction_non_alphanumeric': 0.21177244327523842, 'fraction_numerical': 0.0026307135810588623, 'mean_word_length': 1.298318533912715, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5131403', 'n_tokens_mistral': 2964, 'n_tokens_neox': 2823, 'n_words': 863}