home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / XAP / XFM / XFM-1.3 / XFM-1 / xfm-1.3 / xfm / FmMain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-16  |  13.3 KB  |  370 lines

  1. /*---------------------------------------------------------------------------
  2.  Module FmMain
  3.  
  4.  (c) S.Marlow 1990-92
  5.  (c) A.Graef 1994
  6.  
  7.  main module for file manager    
  8. ---------------------------------------------------------------------------*/
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <signal.h>
  13. #include <fcntl.h>
  14. #include <sys/wait.h>
  15.  
  16. #include <X11/Intrinsic.h>
  17. #include <X11/StringDefs.h>
  18. #include <X11/Xaw/Cardinals.h>
  19. #include <X11/Shell.h>
  20.  
  21. #include "Am.h"
  22. #include "Fm.h"
  23.  
  24. #define XtRDisplayType "DisplayType"
  25. #define XtRSortType "SortType"
  26.  
  27. /*---------------------------------------------------------------------------
  28.   Public variables
  29. ---------------------------------------------------------------------------*/
  30.  
  31. /* program name */
  32. char *progname;
  33.  
  34. /* information about the user */
  35. UserInfo user;
  36.  
  37. /* application resource values */
  38. Resources resources;
  39.  
  40. /* application context */
  41. XtAppContext app_context;
  42.  
  43. /* Update semaphor */
  44. int freeze = False;
  45.  
  46. /*---------------------------------------------------------------------------
  47.   Command line options
  48. ---------------------------------------------------------------------------*/
  49.  
  50. static XrmOptionDescRec options[] = {
  51.   { "-appmgr", ".appmgr", XrmoptionNoArg, "True" },
  52.   { "-filemgr", ".filemgr", XrmoptionNoArg, "True" }
  53. };
  54.  
  55. /*---------------------------------------------------------------------------
  56.   Application Resources
  57. ---------------------------------------------------------------------------*/
  58.  
  59. static XtResource resource_list[] = {
  60.   { "appmgr", "Appmgr", XtRBoolean, sizeof(Boolean),
  61.       XtOffsetOf(Resources, appmgr), XtRImmediate, (XtPointer) False },
  62.   { "filemgr", "Filemgr", XtRBoolean, sizeof(Boolean),
  63.       XtOffsetOf(Resources, filemgr), XtRImmediate, (XtPointer) False },
  64.   { "initGeometry", "InitGeometry", XtRString, sizeof(String),
  65.       XtOffsetOf(Resources, init_geometry), XtRString, NULL },
  66.   { "iconFont", XtCFont, XtRFontStruct, sizeof(XFontStruct *), 
  67.       XtOffsetOf(Resources, icon_font), XtRString, XtDefaultFont },
  68.   { "buttonFont", XtCFont, XtRFontStruct, sizeof(XFontStruct *), 
  69.       XtOffsetOf(Resources, button_font), XtRString, XtDefaultFont },
  70.   { "menuFont", XtCFont, XtRFontStruct, sizeof(XFontStruct *), 
  71.       XtOffsetOf(Resources, menu_font), XtRString, XtDefaultFont },
  72.   { "labelFont", XtCFont, XtRFontStruct, sizeof(XFontStruct *), 
  73.       XtOffsetOf(Resources, label_font), XtRString, XtDefaultFont },
  74.   { "statusFont", XtCFont, XtRFontStruct, sizeof(XFontStruct *), 
  75.       XtOffsetOf(Resources, status_font), XtRString, XtDefaultFont },
  76.   { "boldFont", XtCFont, XtRFontStruct, sizeof(XFontStruct *), 
  77.       XtOffsetOf(Resources, bold_font), XtRString, XtDefaultFont },
  78.   { "cellFont", XtCFont, XtRFontStruct, sizeof(XFontStruct *), 
  79.       XtOffsetOf(Resources, cell_font), XtRString, XtDefaultFont },
  80.   { "appIconWidth", "Width", XtRInt, sizeof(int),
  81.       XtOffsetOf(Resources, app_icon_width), XtRImmediate, (XtPointer) 48 },
  82.   { "appIconHeight", "Height", XtRInt, sizeof(int),
  83.       XtOffsetOf(Resources, app_icon_height), XtRImmediate, (XtPointer) 40 },
  84.   { "fileIconWidth", "Width", XtRInt, sizeof(int),
  85.       XtOffsetOf(Resources, file_icon_width), XtRImmediate, (XtPointer) 48 },
  86.   { "fileIconHeight", "Height", XtRInt, sizeof(int),
  87.       XtOffsetOf(Resources, file_icon_height), XtRImmediate, (XtPointer) 40 },
  88.   { "treeIconWidth", "Width", XtRInt, sizeof(int),
  89.       XtOffsetOf(Resources, tree_icon_width), XtRImmediate, (XtPointer) 48 },
  90.   { "treeIconHeight", "Height", XtRInt, sizeof(int),
  91.       XtOffsetOf(Resources, tree_icon_height), XtRImmediate, (XtPointer) 32 },
  92.   { "bitmapPath", "Path", XtRString, sizeof(String),
  93.       XtOffsetOf(Resources, bitmap_path), XtRString, NULL },
  94.   { "pixmapPath", "Path", XtRString, sizeof(String),
  95.       XtOffsetOf(Resources, pixmap_path), XtRString, NULL },
  96.   { "applicationDataFile", "ConfigFile",  XtRString, sizeof(String),
  97.       XtOffsetOf(Resources, app_file_r), XtRString, "~/.xfm-apps" },
  98.   { "configFile", "ConfigFile",  XtRString, sizeof(String),
  99.       XtOffsetOf(Resources, cfg_file_r), XtRString, "~/.xfmrc" },
  100.   { "devFile", "ConfigFile",  XtRString, sizeof(String),
  101.       XtOffsetOf(Resources, dev_file_r), XtRString, "~/.xfmdev" },
  102.   { "autoSave", "AutoSave", XtRBoolean, sizeof(Boolean),
  103.       XtOffsetOf(Resources, auto_save), XtRImmediate, (XtPointer) True },
  104.   { "confirmDeletes", "Confirm", XtRBoolean, sizeof(Boolean),
  105.       XtOffsetOf(Resources, confirm_deletes), XtRImmediate, (XtPointer) True },
  106.   { "confirmDeleteFolder", "Confirm", XtRBoolean, sizeof(Boolean),
  107.       XtOffsetOf(Resources, confirm_delete_folder), XtRImmediate,
  108.       (XtPointer) True },
  109.   { "confirmMoves", "Confirm", XtRBoolean, sizeof(Boolean),
  110.       XtOffsetOf(Resources, confirm_moves), XtRImmediate, (XtPointer) True },
  111.   { "confirmCopies", "Confirm", XtRBoolean, sizeof(Boolean),
  112.       XtOffsetOf(Resources, confirm_copies), XtRImmediate, (XtPointer) True },
  113.   { "confirmOverwrite", "Confirm", XtRBoolean, sizeof(Boolean),
  114.       XtOffsetOf(Resources, confirm_overwrite), XtRImmediate,
  115.       (XtPointer) True },
  116.   { "confirmQuit", "Confirm", XtRBoolean, sizeof(Boolean),
  117.       XtOffsetOf(Resources, confirm_quit), XtRImmediate, (XtPointer) True },
  118.   { "echoActions", "Echo", XtRBoolean, sizeof(Boolean),
  119.       XtOffsetOf(Resources, echo_actions), XtRImmediate, (XtPointer) False },
  120.   { "showOwner", "ShowOwner", XtRBoolean, sizeof(Boolean),
  121.       XtOffsetOf(Resources, show_owner), XtRImmediate, (XtPointer) True },
  122.   { "showDate", "ShowDate", XtRBoolean, sizeof(Boolean),
  123.       XtOffsetOf(Resources, show_date), XtRImmediate, (XtPointer) True },
  124.   { "showPermissions", "ShowPermissions", XtRBoolean, sizeof(Boolean),
  125.       XtOffsetOf(Resources, show_perms), XtRImmediate, (XtPointer) True },
  126.   { "showLength", "ShowLength", XtRBoolean, sizeof(Boolean),
  127.       XtOffsetOf(Resources, show_length), XtRImmediate, (XtPointer) True },
  128.   { "defaultDisplayType", "DefaultDisplayType", XtRDisplayType, 
  129.       sizeof(DisplayType), XtOffsetOf(Resources, default_display_type),
  130.       XtRImmediate, (XtPointer) Icons },
  131.   { "initialDisplayType", "InitialDisplayType", XtRDisplayType, 
  132.       sizeof(DisplayType), XtOffsetOf(Resources, initial_display_type),
  133.       XtRImmediate, (XtPointer) Tree },
  134.   { "defaultSortType", "DefaultSortType", XtRSortType, 
  135.       sizeof(SortType), XtOffsetOf(Resources, default_sort_type),
  136.       XtRImmediate, (XtPointer) SortByName },
  137.   { "doubleClickTime", "DoubleClickTime", XtRInt, sizeof(int),
  138.       XtOffsetOf(Resources, double_click_time), XtRImmediate,
  139.       (XtPointer) 500 },
  140.   { "updateInterval", "UpdateInterval", XtRInt, sizeof(int),
  141.       XtOffsetOf(Resources, update_interval), XtRImmediate,
  142.       (XtPointer) 10000 },
  143.   { "defaultEditor", "DefaultEditor", XtRString, sizeof(String),
  144.       XtOffsetOf(Resources, default_editor), XtRString, NULL },
  145. };
  146.  
  147. /*---------------------------------------------------------------------------
  148.  Fallback resources
  149. ---------------------------------------------------------------------------*/
  150.  
  151. static String fallback_resources[] = {
  152.   "*Command.cursor : hand2",
  153.   "*MenuButton.cursor : hand2",
  154.   "*viewport.forceBars: true",
  155.   "*popup form*bitmap.borderWidth : 0",
  156.   "*popup form*label.borderWidth : 0",
  157.   "*button box.orientation : horizontal",
  158.   "*button box.borderWidth: 0",
  159.   "*viewport.borderWidth: 0",
  160.   "*viewport.icon box*Label.borderWidth : 0",
  161.   "*viewport.icon box.Command.borderWidth : 0",
  162.   "*viewport.icon box.Form.borderWidth : 0",
  163.   "*viewport.icon box*Toggle.borderWidth : 1",
  164.   "*chmod*Label.borderWidth : 0",
  165.   "*info*Label.borderWidth : 0",
  166.   "*error*Label.borderWidth : 0",
  167.   "*confirm*Label.borderWidth : 0",
  168.   "*Text*translations : #override \\n\
  169.     <Key>Return: no-op() \\n\
  170.     <Key>Linefeed : no-op() \\n\
  171.     Ctrl<Key>J : no-op() \\n",
  172. NULL,
  173. };
  174.  
  175. /*---------------------------------------------------------------------------
  176.   Widget argument lists
  177. ---------------------------------------------------------------------------*/
  178.  
  179. static Arg shell_args[] = {
  180.   { XtNtitle, (XtArgVal) "Applications" }
  181. };
  182.  
  183. /*-----------------------------------------------------------------------------
  184.   Signal handler - clears up Zombie processes
  185.   I'll probably extend this in the future to do something useful.
  186. -----------------------------------------------------------------------------*/
  187. static void sigcldHandler()
  188. {
  189.   waitpid(-1,NULL,WNOHANG);
  190. }
  191.  
  192. static struct sigaction sigcld, sigterm;
  193.  
  194. /*---------------------------------------------------------------------------
  195.   Resource converter functions
  196. ---------------------------------------------------------------------------*/
  197.  
  198. static void CvtStringToDisplayType(XrmValue *args, Cardinal *n_args,
  199.                    XrmValue *fromVal, XrmValue *toVal)
  200. {
  201.   static DisplayType d;
  202.  
  203.   if (!strcmp(fromVal->addr, "Tree"))
  204.     d = Tree;
  205.   else if (!strcmp(fromVal->addr, "Icons"))
  206.     d = Icons;
  207.   else if (!strcmp(fromVal->addr, "Text"))
  208.     d = Text;
  209.   else {
  210.     XtStringConversionWarning(fromVal->addr, XtRDisplayType);
  211.     return;
  212.   }
  213.   
  214.   toVal->addr = (caddr_t) &d;
  215.   toVal->size = sizeof(DisplayType);
  216. }
  217.  
  218. /*---------------------------------------------------------------------------*/
  219.  
  220. static void CvtStringToSortType(XrmValue *args, Cardinal *n_args,
  221.                 XrmValue *fromVal, XrmValue *toVal)
  222. {
  223.   static SortType d;
  224.  
  225.   if (!strcmp(fromVal->addr, "SortByName"))
  226.     d = SortByName;
  227.   else if (!strcmp(fromVal->addr, "SortBySize"))
  228.     d = SortBySize;
  229.   else if (!strcmp(fromVal->addr, "SortByDate"))
  230.     d = SortByMTime;
  231.   else {
  232.     XtStringConversionWarning(fromVal->addr, XtRSortType);
  233.     return;
  234.   }
  235.   
  236.   toVal->addr = (caddr_t) &d;
  237.   toVal->size = sizeof(SortType);
  238. }
  239.  
  240. /*---------------------------------------------------------------------------
  241.   Main function
  242. ---------------------------------------------------------------------------*/
  243.  
  244. void main(int argc, char *argv[])
  245. {
  246.   char *s;
  247.  
  248.   progname = argv[0];
  249.  
  250.   /* get some information about the user */
  251.   user.uid = getuid();
  252.   user.gid = getgid();
  253.  
  254.   if (s = getenv("HOME"))
  255.     strcpy(user.home,s);
  256.   else
  257.     getwd(user.home);
  258.  
  259.   if (s = getenv("SHELL"))
  260.     strcpy(user.shell,s);
  261.   else
  262.     strcpy(user.shell,"/bin/sh");
  263.  
  264.   user.umask = umask(0);
  265.   umask(user.umask);
  266.   user.umask = 0777777 ^ user.umask;
  267.  
  268.   /* initialise the application and create the application shell */
  269.  
  270.   aw.shell = XtAppInitialize(&app_context, "Xfm", options, XtNumber(options),
  271.                  &argc, argv, fallback_resources, shell_args,
  272.                  XtNumber(shell_args) );
  273.  
  274.   /* make sure we can close-on-exec the display connection */
  275.   if (fcntl(ConnectionNumber(XtDisplay(aw.shell)), F_SETFD, 1) == -1)
  276.     abortXfm("Couldn't mark display connection as close-on-exec");
  277.  
  278.   /* register resource converters */
  279.   XtAppAddConverter(app_context, XtRString, XtRDisplayType, 
  280.             CvtStringToDisplayType, NULL, ZERO);
  281.   XtAppAddConverter(app_context, XtRString, XtRSortType, 
  282.             CvtStringToSortType, NULL, ZERO);
  283.  
  284.   /* get the application resources */
  285.   XtGetApplicationResources(aw.shell, &resources, resource_list,
  286.                 XtNumber(resource_list), NULL, ZERO);
  287.  
  288.   /* default is to launch both application and file manager */
  289.   if (!resources.appmgr && !resources.filemgr)
  290.     resources.appmgr = resources.filemgr = True;
  291.  
  292.   /* set the multi-click time */
  293.   XtSetMultiClickTime(XtDisplay(aw.shell), resources.double_click_time);
  294.  
  295.   /* initialise the utilities module */
  296.   initUtils();
  297.  
  298.   /* set up signal handlers */
  299.   sigcld.sa_handler = sigcldHandler;
  300.   sigemptyset(&sigcld.sa_mask);
  301.   sigcld.sa_flags = 0;
  302.   sigaction(SIGCHLD,&sigcld,NULL);
  303.   sigterm.sa_handler = quit;
  304.   sigemptyset(&sigterm.sa_mask);
  305.   sigterm.sa_flags = 0;
  306.   sigaction(SIGTERM,&sigterm,NULL);
  307.  
  308.   /* initialise the communications module */
  309.   initComms();
  310.  
  311.   /* create all the bitmaps & cursors needed */
  312.   readBitmaps();
  313.   
  314.   /* Set the icon for the application manager */
  315.   XtVaSetValues(aw.shell, XtNiconPixmap, bm[APPMGR_BM], NULL);
  316.   XtVaSetValues(aw.shell, XtNiconMask, bm[APPMGRMSK_BM], NULL);
  317.  
  318.   /* create the main popup shells */
  319.   createMainPopups();
  320.  
  321.   /* initialise the applications module & create the window*/
  322.   if (resources.appmgr) {
  323.     strcpy(resources.app_file, resources.app_file_r);
  324.     fnexpand(resources.app_file);
  325.     readApplicationData(resources.app_file);
  326.     createApplicationWindow();
  327.     createApplicationDisplay();
  328.     XtRealizeWidget(aw.shell);
  329.     XSetWMProtocols(XtDisplay(aw.shell), XtWindow(aw.shell),
  330.             &wm_delete_window, 1);
  331.     XtAddEventHandler(aw.shell, (EventMask)0L, True,
  332.               (XtEventHandler)clientMessageHandler, (XtPointer)NULL);
  333.   }
  334.  
  335.   /* initialise the file windows module & create a file window */
  336.   strcpy(resources.cfg_file, resources.cfg_file_r);
  337.   fnexpand(resources.cfg_file);
  338.   strcpy(resources.dev_file, resources.dev_file_r);
  339.   fnexpand(resources.dev_file);
  340.   initFileWindows();
  341.   if (resources.filemgr)
  342.     newFileWindow(user.home,resources.initial_display_type,False);
  343.   resources.init_geometry = NULL;
  344.  
  345.   /* start up window refresh timer */
  346.   if (resources.update_interval > 0)
  347.     XtAppAddTimeOut(app_context, resources.update_interval, timeoutCb, NULL);
  348.  
  349.   /* collect & process events */
  350.   XtAppMainLoop(app_context);
  351. }
  352.  
  353. /*---------------------------------------------------------------------------*/
  354.  
  355. void quit()
  356. {
  357.   int d;
  358.  
  359.   for (d = 0; d < n_devices; d++)
  360.     if (devs[d].mounted) {
  361.       devs[d].mounted = 1;
  362.       umountDev(d);
  363.     }
  364.   if (resources.appmgr && resources.auto_save && aw.modified)
  365.     writeApplicationData(resources.app_file);
  366.  
  367.   XtDestroyApplicationContext(app_context);
  368.   exit(0);
  369. }
  370.