Skip to content

Commit

Permalink
StShared - avoid linking to GTK2 by default, use zenity for openfile …
Browse files Browse the repository at this point in the history
…dialog
  • Loading branch information
gkv311 committed Jan 2, 2025
1 parent f4077ea commit 56f0acb
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 32 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build_ubuntu_20.04.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ jobs:
sudo apt-get update
sudo apt-get install -y \
g++ ninja-build \
libgtk2.0-dev \
libopenal-dev \
libopenvr-dev \
libgl-dev \
libfreetype-dev \
libconfig++-dev libconfig-dev \
libxpm-dev \
libxrandr-dev libxpm-dev \
libavcodec-dev libavdevice-dev libavformat-dev libavutil-dev libswscale-dev
- name: Compile
run: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/build_ubuntu_22.04.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ jobs:
sudo apt-get update
sudo apt-get install -y \
g++ ninja-build \
libgtk2.0-dev \
libopenal-dev \
libopenvr-dev \
libgl-dev \
libfreetype-dev \
libconfig++-dev libconfig-dev \
libxpm-dev \
libxrandr-dev libxpm-dev \
libavcodec-dev libavdevice-dev libavformat-dev libavutil-dev libswscale-dev
- name: Compile
run: |
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/build_ubuntu_24.04.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ jobs:
sudo apt-get update
sudo apt-get install -y \
g++ ninja-build \
libgtk2.0-dev \
zenity \
libopenal-dev \
libopenvr-dev \
libgl-dev \
libfreetype-dev \
libconfig++-dev libconfig-dev \
libxpm-dev \
libxrandr-dev libxpm-dev \
libavcodec-dev libavdevice-dev libavformat-dev libavutil-dev libswscale-dev
- name: Compile
run: |
Expand Down
19 changes: 11 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -198,18 +198,21 @@ elseif (ANDROID)
#
else()
# Linux
set (USE_GTK ON)
set (USE_GTK OFF CACHE BOOL "Use GTK2 for openfile dialog.")
set (USE_XLIB ON)
set (USE_LIBCONFIG ON)

find_package (GTK2 REQUIRED gtk)
if (NOT GTK2_FOUND)
message (FATAL_ERROR "could not find GTK2 (libgtk2.0-dev)" )
endif()
if (NOT GTK2_INCLUDE_DIRS)
message (FATAL_ERROR "could not find GTK2 (GTK2_INCLUDE_DIRS)" )
if (USE_GTK)
find_package (GTK2 REQUIRED gtk)
if (NOT GTK2_FOUND)
message (FATAL_ERROR "could not find GTK2 (libgtk2.0-dev)" )
endif()
if (NOT GTK2_INCLUDE_DIRS)
message (FATAL_ERROR "could not find GTK2 (GTK2_INCLUDE_DIRS)" )
endif()
message (STATUS "GTK2_INCLUDE_DIRS=${GTK2_INCLUDE_DIRS}")
add_definitions(-DST_HAVE_GTK)
endif()
message (STATUS "GTK2_INCLUDE_DIRS=${GTK2_INCLUDE_DIRS}")

find_package (X11 REQUIRED)
if (NOT X11_FOUND)
Expand Down
94 changes: 92 additions & 2 deletions StShared/StFileNode2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <windows.h>
#elif defined(__ANDROID__)
//
#elif defined(__linux__)
#elif defined(ST_HAVE_GTK)
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
Expand Down Expand Up @@ -136,7 +136,7 @@ bool StFileNode::openFileDialog(StString& theFilePath,
#elif defined(__ANDROID__)
//bool ST_NOT_IMPLEMENTED_FOR_ANDROID = true;
return false;
#elif defined(__linux__)
#elif defined(ST_HAVE_GTK)
if(!StMessageBox::initGlobals()) {
return false;
}
Expand Down Expand Up @@ -167,6 +167,96 @@ bool StFileNode::openFileDialog(StString& theFilePath,
gdk_flush(); // we need this call!
gdk_threads_leave();
return isFileSelected;
#elif(__linux__)
// use Zenity
static const char ST_ZENITY[] = "/usr/bin/zenity";

StString aCmd = StString(ST_ZENITY) + " --file-selection --modal";
if (theToSave) {
aCmd += " --save --confirm-overwrite";
}

if (!theInfo.Title.isEmpty()) {
aCmd += StString(" --title=\"") + theInfo.Title + "\"";
}

if (!theInfo.Folder.isEmpty()) {
StString aFolder = theInfo.Folder;
if (!aFolder.isEndsWith('/')) {
aFolder += "/";
}
aCmd += StString(" --filename=\"") + aFolder + "\"";
}

StString aFilterString, anAllSupportedExt, anExtraSupportedExt;
for(size_t aMimeId = 0; aMimeId < theInfo.Filter.size(); ++aMimeId) {
const StMIME& aMime = theInfo.Filter[aMimeId];
if(aMimeId > 0) {
anAllSupportedExt += StString(' ');
}
anAllSupportedExt += StString("*.") + aMime.getExtension();
}

// add 'All supported'
if(!anAllSupportedExt.isEmpty() && theInfo.Filter.size() > 1) {
aCmd += " --file-filter=\"";
aCmd += !theInfo.FilterTitle.isEmpty() ? theInfo.FilterTitle : StString("All supported");
aCmd += StString(" | ") + anAllSupportedExt + "\"";
}

// add 'Extra supported'
for(size_t aMimeId = 0; aMimeId < theInfo.ExtraFilter.size(); ++aMimeId) {
const StMIME& aMime = theInfo.ExtraFilter[aMimeId];
if(aMimeId > 0) {
anExtraSupportedExt += StString(' ');
}
anExtraSupportedExt += StString("*.") + aMime.getExtension();
}
if(!anExtraSupportedExt.isEmpty() && theInfo.ExtraFilter.size() > 1) {
aCmd += " --file-filter=\"";
aCmd += !theInfo.ExtraFilterTitle.isEmpty() ? theInfo.ExtraFilterTitle : StString("Extra supported");
aCmd += StString(" | ") + anExtraSupportedExt + "\"";
}

// add per-type filters
for(size_t aMimeId = 0; aMimeId < theInfo.Filter.size(); ++aMimeId) {
const StMIME& aMime = theInfo.Filter[aMimeId];
if((aMimeId > 0) && (aMime.getDescription() == theInfo.Filter[aMimeId - 1].getDescription())) {
// append extension to previous MIME (prevent duplication)
aCmd = aCmd.subString(0, aCmd.getLength() - 1); // backstep
aCmd += StString(" *.") + aMime.getExtension() + "\"";
} else {
aCmd += StString(" --file-filter=\"") + aMime.getDescription() + StString(" | *.") + aMime.getExtension() + "\"";
}
}

// add 'Any File'
aCmd += " --file-filter=\"All Files (*) | *\"";

//ST_DEBUG_LOG(aCmd);
FILE* aPipe = popen(aCmd.toCString(), "r");
if (aPipe == NULL) {
ST_DEBUG_LOG(ST_ZENITY + " is not found!");
return false;
}

char aBuffer[4096] = {};
if (fgets(aBuffer, sizeof(aBuffer), aPipe) == NULL) {
ST_DEBUG_LOG(ST_ZENITY + " calling failure");
}
int aRes = pclose(aPipe);
if (aRes != 0) {
return false;
}

theFilePath = aBuffer;
if (theFilePath.isEndsWith('\n')) {
if (theFilePath.getLength() == 1) {
return false;
}
theFilePath = theFilePath.subString(0, theFilePath.getLength() - 1);
}
return true;
#endif
}

Expand Down
97 changes: 92 additions & 5 deletions StShared/StLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

#if defined(__ANDROID__)
#include <android/log.h>
#elif defined(__linux__)
static const char ST_ZENITY[] = "/usr/bin/zenity";
#endif

// we do not use st::cerr here to avoid
Expand Down Expand Up @@ -217,7 +219,7 @@ void StLogger::write(const StString& theMessage,
#include <windows.h>
#elif defined(__ANDROID__)
//
#elif defined(__linux__)
#elif defined(ST_HAVE_GTK)
#include <gtk/gtk.h>
#include <X11/Xlib.h>
namespace {
Expand Down Expand Up @@ -258,8 +260,12 @@ void StMessageBox::setCallback(msgBoxFunc_t theFunc) {

#elif defined(__linux__)
bool StMessageBox::initGlobals() {
#ifdef ST_HAVE_GTK
static const bool isInitOK = stGtkInitForce();
return isInitOK;
#else
return true;
#endif
}
#endif

Expand All @@ -273,7 +279,7 @@ void StMessageBox::Info(const StString& theMessage) {
} else {
StMessageBox::InfoConsole(theMessage);
}
#elif defined(__linux__)
#elif defined(ST_HAVE_GTK)
if(initGlobals()) {
gdk_threads_enter();
GtkWidget* dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "%s", theMessage.toCString());
Expand All @@ -282,6 +288,26 @@ void StMessageBox::Info(const StString& theMessage) {
gdk_flush(); // we need this call!
gdk_threads_leave();
}
#elif defined(__linux__)
// use Zenity
/*StArrayList<StString> anArgs(4);
anArgs.add("--info").add("--no-markup").add("--no-wrap").add(StString() + "--text=" + theMessage + "");
if(!StProcess::execProcess(ST_ZENITY, anArgs)) { ST_DEBUG_LOG(ST_ZENITY + " is not found!"); }*/

const StString aMsg = theMessage.replace(stCString("\""), stCString("\\\""));
const StString aCmd = StString(ST_ZENITY) + " --info --no-markup --no-wrap --text=\"" + aMsg + "\"";

FILE* aPipe = popen(aCmd.toCString(), "r");
if (aPipe == NULL) {
ST_DEBUG_LOG(ST_ZENITY + " is not found!");
return;
}

char aBuffer[4096] = {};
if (fgets(aBuffer, sizeof(aBuffer), aPipe) == NULL) {
//
}
pclose(aPipe);
#endif
}

Expand All @@ -295,7 +321,7 @@ void StMessageBox::Warn(const StString& theMessage) {
} else {
StMessageBox::WarnConsole(theMessage);
}
#elif defined(__linux__)
#elif defined(ST_HAVE_GTK)
if(initGlobals()) {
gdk_threads_enter();
GtkWidget* dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, "%s", theMessage.toCString());
Expand All @@ -304,6 +330,26 @@ void StMessageBox::Warn(const StString& theMessage) {
gdk_flush(); // we need this call!
gdk_threads_leave();
}
#elif defined(__linux__)
// use Zenity
/*StArrayList<StString> anArgs(4);
anArgs.add("--warning").add("--no-markup").add("--no-wrap").add(StString() + "--text=" + theMessage + "");
if(!StProcess::execProcess(ST_ZENITY, anArgs)) { ST_DEBUG_LOG(ST_ZENITY + " is not found!"); }*/

const StString aMsg = theMessage.replace(stCString("\""), stCString("\\\""));
const StString aCmd = StString(ST_ZENITY) + " --warning --no-markup --no-wrap --text=\"" + aMsg + "\"";

FILE* aPipe = popen(aCmd.toCString(), "r");
if (aPipe == NULL) {
ST_DEBUG_LOG(ST_ZENITY + " is not found!");
return;
}

char aBuffer[4096] = {};
if (fgets(aBuffer, sizeof(aBuffer), aPipe) == NULL) {
//
}
pclose(aPipe);
#endif
}

Expand All @@ -317,7 +363,7 @@ void StMessageBox::Error(const StString& theMessage) {
} else {
StMessageBox::ErrorConsole(theMessage);
}
#elif defined(__linux__)
#elif defined(ST_HAVE_GTK)
if(initGlobals()) {
gdk_threads_enter();
GtkWidget* dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", theMessage.toCString());
Expand All @@ -326,6 +372,26 @@ void StMessageBox::Error(const StString& theMessage) {
gdk_flush(); // we need this call!
gdk_threads_leave();
}
#elif defined(__linux__)
// use Zenity
/*StArrayList<StString> anArgs(4);
anArgs.add("--error").add("--no-markup").add("--no-wrap").add(StString() + "--text=" + theMessage + "");
if(!StProcess::execProcess(ST_ZENITY, anArgs)) { ST_DEBUG_LOG(ST_ZENITY + " is not found!"); }*/

const StString aMsg = theMessage.replace(stCString("\""), stCString("\\\""));
const StString aCmd = StString(ST_ZENITY) + " --error --no-markup --no-wrap --text=\"" + aMsg + "\"";

FILE* aPipe = popen(aCmd.toCString(), "r");
if (aPipe == NULL) {
ST_DEBUG_LOG(ST_ZENITY + " is not found!");
return;
}

char aBuffer[4096] = {};
if (fgets(aBuffer, sizeof(aBuffer), aPipe) == NULL) {
//
}
pclose(aPipe);
#endif
}

Expand All @@ -338,7 +404,7 @@ bool StMessageBox::Question(const StString& theMessage) {
} else {
return StMessageBox::QuestionConsole(theMessage);
}
#elif defined(__linux__)
#elif defined(ST_HAVE_GTK)
if(initGlobals()) {
gdk_threads_enter();
GtkWidget* aDialog = gtk_message_dialog_new(NULL, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO, "%s", theMessage.toCString());
Expand All @@ -349,6 +415,27 @@ bool StMessageBox::Question(const StString& theMessage) {
return anAnswer == GTK_RESPONSE_YES;
}
return false;
#elif defined(__linux__)
// use Zenity
/*StArrayList<StString> anArgs(4);
anArgs.add("--question").add("--no-markup").add("--no-wrap").add(StString() + "--text=" + theMessage + "");
if(!StProcess::execProcess(ST_ZENITY, anArgs)) { ST_DEBUG_LOG(ST_ZENITY + " is not found!"); }*/

const StString aMsg = theMessage.replace(stCString("\""), stCString("\\\""));
const StString aCmd = StString(ST_ZENITY) + " --question --no-markup --no-wrap --text=\"" + aMsg + "\"";

FILE* aPipe = popen(aCmd.toCString(), "r");
if (aPipe == NULL) {
ST_DEBUG_LOG(ST_ZENITY + " is not found!");
return false;
}

char aBuffer[4096] = {};
if (fgets(aBuffer, sizeof(aBuffer), aPipe) == NULL) {
//
}
int aRes = pclose(aPipe);
return aRes == 0;
#endif
}

Expand Down
4 changes: 2 additions & 2 deletions debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ Source: sview
Section: contrib/video
Priority: extra
Maintainer: Kirill Gavrilov <[email protected]>
Build-Depends: debhelper (>= 7.0.50~), g++ (>= 4.0), cmake, ninja-build, libopenal-dev, libgl-dev, libgtk2.0-dev, libxpm-dev, libavcodec-dev, libavdevice-dev, libavformat-dev, libavutil-dev, libswscale-dev, libconfig++-dev, libfreetype-dev, libxpm-dev, libopenvr-dev
Build-Depends: debhelper (>= 7.0.50~), g++ (>= 4.0), cmake, ninja-build, libopenal-dev, libgl-dev, libxrandr-dev, libxpm-dev, libavcodec-dev, libavdevice-dev, libavformat-dev, libavutil-dev, libswscale-dev, libconfig++-dev, libfreetype-dev, libxpm-dev, libopenvr-dev
Standards-Version: 3.9.1
Homepage: http://www.sview.ru

Package: sview
Architecture: any
Depends: ${shlibs:Depends}, ${misc:Depends}, xdg-utils
Depends: ${shlibs:Depends}, ${misc:Depends}, xdg-utils, zenity
Description: stereoscopic media player sView
.
sView is a stereoscopic Image Viewer and Movie Player.
Expand Down
Loading

0 comments on commit 56f0acb

Please sign in to comment.