Skip to content

Commit

Permalink
Merge pull request #142 from yast/init_ui
Browse files Browse the repository at this point in the history
 Init the UI without y2base (bsc#922023)
  • Loading branch information
mvidner committed May 18, 2015
2 parents b5035d2 + a99ffa9 commit 7e0aed6
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 17 deletions.
7 changes: 7 additions & 0 deletions package/yast2-ruby-bindings.changes
@@ -1,3 +1,10 @@
-------------------------------------------------------------------
Mon May 18 08:34:37 UTC 2015 - mvidner@suse.com

- Initialize the YaST UI so that it can be called
when the main program is not y2base (bsc#922023).
- 3.1.32

-------------------------------------------------------------------
Wed Apr 1 15:38:04 UTC 2015 - ancor@suse.com

Expand Down
2 changes: 1 addition & 1 deletion package/yast2-ruby-bindings.spec
Expand Up @@ -17,7 +17,7 @@


Name: yast2-ruby-bindings
Version: 3.1.31
Version: 3.1.32
Url: https://github.com/yast/yast-ruby-bindings
Release: 0
BuildRoot: %{_tmppath}/%{name}-%{version}-build
Expand Down
15 changes: 0 additions & 15 deletions src/CMakeLists.txt
@@ -1,17 +1,2 @@
add_subdirectory(ruby)
add_subdirectory(binary)

# rdoc
set(rdoc_dir "${CMAKE_CURRENT_BINARY_DIR}/html")
ADD_CUSTOM_COMMAND (
OUTPUT ${rdoc_dir}
COMMAND ${CMAKE_COMMAND} -E echo_append "Creating rdoc documentation ..."
COMMAND rm -rf ${rdoc_dir}
COMMAND rdoc -o ${rdoc_dir} ruby binary/Yast.cc
COMMAND ${CMAKE_COMMAND} -E echo "Done."
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/src/
DEPENDS ${CMAKE_SOURCE_DIR}/src/binary/Yast.cc ${CMAKE_SOURCE_DIR}/src/ruby/*.rb
)
add_custom_target(rdoc ALL DEPENDS "${rdoc_dir}")
add_dependencies(rdoc yastx)
add_dependencies(rdoc scrx)
10 changes: 10 additions & 0 deletions src/binary/CMakeLists.txt
Expand Up @@ -76,8 +76,17 @@ set_target_properties( builtinx PROPERTIES INSTALL_RPATH "${YAST_PLUGIN_DIR}")
#
SET ( CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-rpath,'${YAST_PLUGIN_DIR}'" )

# Correct initialization depends on libraries being linked and providing
# global variables with certain names. (Y2PluginComponent: "g_y2cc*")
# Tell the linker not to optimize them away.
set_target_properties( yastx PROPERTIES LINK_FLAGS "-Wl,--no-as-needed")
set_target_properties( builtinx PROPERTIES LINK_FLAGS "-Wl,--no-as-needed")

target_link_libraries( yastx ${YAST_LIBRARY} )
target_link_libraries( yastx ${YAST_YCP_LIBRARY} )
target_link_libraries( yastx ${YAST_PLUGIN_SCR_LIBRARY} )
target_link_libraries( yastx ${YAST_PLUGIN_WFM_LIBRARY} )
target_link_libraries( yastx ${YAST_PLUGIN_UI_LIBRARY} )
#
# The WFM and SCR component can only be initialized statically
# (e.g. through Y2CCWFM), thus we must link against the plugin libs
Expand All @@ -89,6 +98,7 @@ target_link_libraries( builtinx ${YAST_LIBRARY} )
target_link_libraries( builtinx ${YAST_YCP_LIBRARY} )
target_link_libraries( builtinx ${YAST_PLUGIN_SCR_LIBRARY} )
target_link_libraries( builtinx ${YAST_PLUGIN_WFM_LIBRARY} )
target_link_libraries( builtinx ${YAST_PLUGIN_UI_LIBRARY} )
target_link_libraries( builtinx crypt )
find_library( OWCRYPT_LIBRARY owcrypt )
if ( OWCRYPT_LIBRARY )
Expand Down
100 changes: 99 additions & 1 deletion src/binary/Yast.cc
Expand Up @@ -32,6 +32,7 @@ as published by the Free Software Foundation; either version
#include <ycp/y2log.h>
#include <ycp/YExpression.h>
#include <ycp/YCPValue.h>
#include <ycp/YCPVoid.h>
#include <ycp/YCPCode.h>
#include <ycp/YCPByteblock.h>
#include <ycp/Import.h>
Expand Down Expand Up @@ -99,7 +100,7 @@ import_namespace( const char *name)
* Tries to import a YCP namespace
*
* call-seq:
* YCP::import("name")
* Yast.import("name")
*
*/

Expand Down Expand Up @@ -384,6 +385,97 @@ static VALUE code_call( int argc, VALUE *argv, VALUE self )
rb_raise(rb_eRuntimeError, "YCode is empty");
}

/*
* Document-method: ui_component
*
* YaST component serving the UI: "gtk", "ncurses", "qt",
* or the dummy one "UI"
*/
static VALUE ui_get_component()
{
string s;
YUIComponent *c = YUIComponent::uiComponent();
if (c)
{
s = c->requestedUIName();
}
return yrb_utf8_str_new(s);
}

/*
* Document-method: ui_component=
*
* When Ruby is embedded in YaST (y2base is the main program), the UI
* is determined by the time Ruby code gets run. If ruby is the main program,
* we need to load the UI frontend if we need one.
*
* Assign "ncurses" or "qt" before UI calls.
*
* #! /usr/bin/env ruby
* require "yast"
* include Yast
* include Yast::UIShortcuts
*
* if Yast.ui_component == ""
* Yast.ui_component = ARGV[0] || "ncurses"
* end
*
* Builtins.y2milestone("UI component: %1", Yast.ui_component)
* Yast.import "UI"
*
* UI.OpenDialog(PushButton("This is a button"))
* UI.UserInput
* UI.CloseDialog
*/
static VALUE ui_set_component(VALUE self, VALUE name)
{
YUIComponent *c = YUIComponent::uiComponent();
if (c)
{
YUIComponent::setUseDummyUI(false);

string s = StringValuePtr(name);
c->setRequestedUIName(s);
}

return Qnil;
}

static void init_ui()
{
const char *ui_name = "UI";

Y2Component *c = YUIComponent::uiComponent();
if (c == 0)
{
y2debug ("UI component not created yet, creating %s", ui_name);

c = Y2ComponentBroker::createServer(ui_name); // just dummy ui if none is defined
if (c == 0)
{
y2error("can't create UI component");
return;
}

c->setServerOptions(0, NULL);
}
else
{
y2debug("UI component already present: %s", c->name ().c_str ());
}
}

static VALUE ui_finalizer()
{
YUIComponent *c = YUIComponent::uiComponent();
if (c)
{
// Shut down the component.
c->result(YCPVoid());
}
return Qnil;
}

} //extern C

extern "C"
Expand All @@ -398,6 +490,7 @@ extern "C"
Init_yastx()
{
YCPPathSearch::initialize();
init_ui();

/*
* module YCP
Expand All @@ -416,6 +509,11 @@ extern "C"
rb_define_method( rb_mYast, "y2_logger", RUBY_METHOD_FUNC(yast_y2_logger), -1);
rb_define_singleton_method( rb_mYast, "y2_logger", RUBY_METHOD_FUNC(yast_y2_logger), -1);

// UI initialization
rb_define_singleton_method( rb_mYast, "ui_component", RUBY_METHOD_FUNC(ui_get_component), 0);
rb_define_singleton_method( rb_mYast, "ui_component=", RUBY_METHOD_FUNC(ui_set_component), 1);
rb_define_singleton_method( rb_mYast, "ui_finalizer", RUBY_METHOD_FUNC(ui_finalizer), 0);

// Y2 references
rb_cYReference = rb_define_class_under(rb_mYast, "YReference", rb_cObject);
rb_define_method(rb_cYReference, "call", RUBY_METHOD_FUNC(ref_call), -1);
Expand Down
1 change: 1 addition & 0 deletions src/ruby/yast.rb
Expand Up @@ -41,6 +41,7 @@
require "yast/path"
require "yast/scr"
require "yast/term"
require "yast/ui"
require "yast/ui_shortcuts"
require "yast/wfm"

Expand Down
5 changes: 5 additions & 0 deletions src/ruby/yast/ui.rb
@@ -0,0 +1,5 @@
module Yast
# This is a counterpart to init_ui in the C code.
# It really belongs near it but I don't know how to code a proc in C.
ObjectSpace.define_finalizer(Yast, proc { Yast.ui_finalizer } )
end

0 comments on commit 7e0aed6

Please sign in to comment.