xfersend 22.2 KB
Newer Older
Neil Turton's avatar
Neil Turton committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright 1996 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/************************************************************************/
/*  Acorn Computers Ltd, 1992.                                         */
/*                                                                      */
/* This file forms part of an unsupported source release of RISC_OSLib. */
/*                                                                      */
/* It may be freely used to create executable images for saleable       */
/* products but cannot be sold in source form or as an object library   */
/* without the prior written consent of Acorn Computers Ltd.            */
/*                                                                      */
/* If this file is re-distributed (even if modified) it should retain   */
/* this copyright notice.                                               */
/*                                                                      */
/************************************************************************/

/* Title: c.xfersend
Neil Turton's avatar
Neil Turton committed
30
 * Purpose: generalised data transfer to a concurrent wimp program.
31 32
 * History: IDJ: 07-Feb-92: prepared for source release
 *
Neil Turton's avatar
Neil Turton committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
 */

#define BOOL int
#define TRUE 1
#define FALSE 0

#define USE_DRAGASPRITE 1

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "trace.h"
#include "swis.h"
#include "os.h"
#include "bbc.h"
#include "wimp.h"
#include "wimpt.h"
#include "win.h"
#include "dbox.h"
#include "xfersend.h"
#include "fileicon.h"
#include "werr.h"
#include "menu.h"
#include "event.h"
#include "msgs.h"
#include "dragasprit.h"
60
#include "VerIntern/messages.h"
Neil Turton's avatar
Neil Turton committed
61

62
static int xfersend__rcvbufsize;
Neil Turton's avatar
Neil Turton committed
63 64 65 66 67 68 69 70 71 72 73 74
static int xfersend__msgid = 0;           /* my_ref of last DataSave message */
static xfersend_saveproc xfersend__saveproc;
static xfersend_sendproc xfersend__sendproc;
static xfersend_printproc xfersend__printproc;
static int xfersend__filetype;
static void *xfersend__savehandle;
static int xfersend__estsize = 0;
static wimp_t xfersend__receiver;
static BOOL xfersend__fileissafe;
static char *xfersend__filename;  /*[256]*/

#ifdef SHARED_C_LIBRARY
75 76
static wimp_mousestr xfersend__mousestr = {0};
static wimp_msgstr xfersend__msg = {0};
Neil Turton's avatar
Neil Turton committed
77 78 79 80 81 82 83 84 85
#else
static wimp_mousestr xfersend__mousestr;
static wimp_msgstr xfersend__msg;
#endif

static BOOL xfersend__close = FALSE;
static wimp_w xfersend__w;

void xfersend_close_on_xfer (BOOL do_we_close, wimp_w w)
86
{
87 88
  xfersend__close = do_we_close;
  xfersend__w = w;
Neil Turton's avatar
Neil Turton committed
89 90 91
}

static void xfersend__winclose (void)
92
{
93 94 95
  wimp_eventdata e;
  e.o.w = xfersend__w;
  wimpt_noerr (wimp_sendwmessage (wimp_ECLOSE, (wimp_msgstr*) &e, e.o.w, -1));
Neil Turton's avatar
Neil Turton committed
96 97 98 99 100
}

#if USE_DRAGASPRITE
static BOOL xfersend__using_dragasprite (void)
{
101
  int r1, r2;
Neil Turton's avatar
Neil Turton committed
102

103 104 105
  r1 = 0x1c;
  os_byte (161, &r1, &r2);   /* read CMOS flag */
  return r2 & 0x02;
Neil Turton's avatar
Neil Turton committed
106 107 108
}
#endif

109 110 111 112 113
static int xfersend__suggest_leaf (wimp_msgdatasave *datasave)
{
  int i, tail, namelen;
  char name[256];

Robert Sprowson's avatar
Robert Sprowson committed
114 115
  strncpy (name, xfersend__filename, sizeof (name));
  name[sizeof (name) - 1] = '\0';
116 117 118 119 120 121 122
  namelen = tail = strlen (name); /* point at the zero */
  while (tail > 0 && name[tail-1] != '.' && name[tail-1] != ':')
    tail--;

  for (i = 0; tail <= namelen && i < (sizeof (datasave->leaf) - 1); i++)
    datasave->leaf[i] = name[tail++];

Robert Sprowson's avatar
Robert Sprowson committed
123
  datasave->leaf[i] = '\0';       /* force termination */
124 125
  tracef1 ("suggest leaf '%s'.\n", (int) datasave->leaf);

Robert Sprowson's avatar
Robert Sprowson committed
126
  return i /* name */ + 1 /* terminator */;
127 128
}

Neil Turton's avatar
Neil Turton committed
129
static BOOL xfersend__unknowns (wimp_eventstr *e, void *handle)
130 131
{
  tracef1 ("xfersend raw event %i.\n", e->e);
Neil Turton's avatar
Neil Turton committed
132 133 134
  handle = handle;

  switch (e->e)
135 136
  {
    case wimp_EUSERDRAG:
Neil Turton's avatar
Neil Turton committed
137 138
    { /* finish my drag */
      tracef0 ("drag event received.\n");
139 140 141 142
#if USE_DRAGASPRITE
      if (xfersend__using_dragasprite ())
        dragasprite_stop ();
#endif
Neil Turton's avatar
Neil Turton committed
143 144
      wimp_get_point_info (&xfersend__mousestr);
      if (xfersend__mousestr.w != -1)
145 146
      {
        wimp_msgstr msg;
147
        int         leaflen;
Neil Turton's avatar
Neil Turton committed
148 149

        tracef1 ("drag to window %i: offer data.\n", xfersend__mousestr.w);
150 151 152 153 154
        leaflen = (xfersend__suggest_leaf(&msg.data.datasave) + 3) & ~3;
        msg.hdr.size = sizeof (wimp_msghdr)
                     + sizeof (wimp_msgdatasave)
                     - sizeof (msg.data.datasave.leaf)
                     + leaflen;
Neil Turton's avatar
Neil Turton committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
        msg.hdr.task = xfersend__mousestr.w;
        msg.hdr.your_ref = 0;
        msg.hdr.action = wimp_MDATASAVE;
        msg.data.datasave.w = xfersend__mousestr.w;
        msg.data.datasave.i = xfersend__mousestr.i;
        msg.data.datasave.x = xfersend__mousestr.x;
        msg.data.datasave.y = xfersend__mousestr.y;
        msg.data.datasave.type = xfersend__filetype;
        msg.data.datasave.estsize = xfersend__estsize;

        wimpt_noerr (wimp_sendwmessage (wimp_ESEND, &msg, xfersend__mousestr.w,
            xfersend__mousestr.i));
        xfersend__msgid = msg.hdr.my_ref; /* filled in by wimp. */
        /* We still get unknown events, so we'll get the reply sometime. */
      }
      else
        /* do nothing */
        tracef0 ("drag to no window: has no effect.\n");

      return TRUE;
    }
    break;

    case wimp_ESEND:
    case wimp_ESENDWANTACK:
      tracef3 ("xfersend msg %x received: %i %i.\n",
          e->data.msg.hdr.action,e->data.msg.hdr.your_ref,xfersend__msgid);

      if (e->data.msg.hdr.your_ref == xfersend__msgid)
        switch (e->data.msg.hdr.action)
185 186
        {
          case wimp_MRAMFETCH:
187
            if (xfersend__sendproc != NULL)
188 189
            {
              xfersend__fileissafe = FALSE;
Neil Turton's avatar
Neil Turton committed
190 191 192 193 194 195 196 197 198 199 200 201

              /*He wants to do an in-core transfer, and we can do this. Note
                that this can't be other than the first response, as others
                are grabbed by sendbuf*/
              tracef0 ("ram transfer starting.\n");

              /* Prepare the reply record. */
              xfersend__msg = e->data.msg;
              xfersend__msg.hdr.your_ref = xfersend__msg.hdr.my_ref;
              xfersend__msg.hdr.action = wimp_MRAMTRANSMIT;
              xfersend__msg.data.ramtransmit.addr = e->data.msg.data.ramfetch.addr;
              xfersend__msg.data.ramtransmit.nbyteswritten = 0; /* so far. */
202
              xfersend__rcvbufsize = e->data.msg.data.ramfetch.nbytes;
Neil Turton's avatar
Neil Turton committed
203 204 205 206 207

              xfersend__receiver = e->data.msg.hdr.task;
              /*the copy in xfersend__msg.hdr.task is overwritten by the Wimp
                message sending */

208
              if (xfersend__sendproc (xfersend__savehandle, &xfersend__rcvbufsize))
209 210
              {
                /* See sendbuf for all the real work for this case... */
Neil Turton's avatar
Neil Turton committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239

                tracef0 ("The send succeeded; send final RAMTRANSMIT.\n");

                /*We may have transferred some data but not yet told the
                  other end about it. xfersend__msg contains a final RAMTRANSMIT,
                  which does not quite fill his buffer (or we'd have sent it
                  already) thus signalling to him that the transfer is over.*/

                wimpt_noerr (wimp_sendmessage (wimp_ESEND, &xfersend__msg,
                    xfersend__receiver));
              }
              else
                tracef0 ("the send failed.\n");

              if (xfersend__close) xfersend__winclose ();
              return TRUE;
            }
          break;

          case wimp_MPrintFile: /* was dropped on a printer application*/
            /*From RISC O S 3.00, we always bounce this message. Printers will
              respond with a Message_DataSaveAck (if it wants a copy of the
              file), or Message_PrintTypeOdd (if it wants the file to be printed
              now).*/
            return TRUE;

          case wimp_MPrintTypeOdd: /*was dropped on a printer application with
              queue empty - print immediately*/
            if (xfersend__printproc != NULL)
240 241
            {
              wimp_t xfersend__printer;
Neil Turton's avatar
Neil Turton committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

              win_remove_unknown_event_processor (&xfersend__unknowns, 0);

              tracef0 ("immediate print request acceptable\n");
              xfersend__fileissafe = FALSE;

              /*Print the file now.*/
              (void) (*xfersend__printproc) (e->data.msg.data.print.name,
                  xfersend__savehandle);
                  /*reports errors, if any*/

              xfersend__printer = e->data.msg.hdr.task;
              xfersend__msg = e->data.msg;
              xfersend__msg.hdr.your_ref = xfersend__msg.hdr.my_ref;
              xfersend__msg.hdr.action = wimp_MPrintTypeKnown;
              wimpt_noerr (wimp_sendmessage (wimp_ESEND, &xfersend__msg,
                  xfersend__printer));
              if (xfersend__close) xfersend__winclose ();
            }
            /*If we weren't given a printproc, Printers' message bounces and
              it tries to copy the data away for printing via Message_
              DataSaveAck.*/

265
            /*In either case, the message has been handled successfully */
Neil Turton's avatar
Neil Turton committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
            return TRUE;
          break;

          case wimp_MPrintError:
            if (e->data.msg.hdr.size > 24)
              werr(FALSE, &e->data.msg.data.chars[4]);
            win_remove_unknown_event_processor (&xfersend__unknowns, 0);
            if (xfersend__close) xfersend__winclose ();
            return TRUE;
          break;

          case wimp_MDATASAVEOK:
            tracef4 ("datasaveok %i %i %i %i.\n",
                e->data.msg.hdr.size,
                e->data.msg.hdr.task,
                e->data.msg.hdr.your_ref,
                e->data.msg.hdr.my_ref);
            tracef4 ("datasaveok %x %x %x %x.\n",
                e->data.msg.data.words[0],
                e->data.msg.data.words[1],
                e->data.msg.data.words[2],
                e->data.msg.data.words[3]);
            tracef1 ("it's the datasaveok, to file '%s'.\n",
                (int) &e->data.msg.data.datasaveok.name[0]);

            win_remove_unknown_event_processor (&xfersend__unknowns, 0);

            tracef1 ("save to filename '%s'.\n",
                (int) &e->data.msg.data.datasaveok.name[0]);

            xfersend__fileissafe = e->data.msg.data.datasaveok.estsize >= 0;

            if (xfersend__saveproc != NULL &&
                xfersend__saveproc (&e->data.msg.data.datasaveok.name[0],
                xfersend__savehandle))
            { tracef0 ("the save succeeded: send dataload\n");

              xfersend__msg = e->data.msg;
                  /* sets hdr.size, data.w,i,x,y, size, name */
              xfersend__msg.hdr.your_ref = e->data.msg.hdr.my_ref;
              xfersend__msg.hdr.action = wimp_MDATALOAD;
              xfersend__msg.data.dataload.type = xfersend__filetype;
              wimpt_noerr (wimp_sendmessage (wimp_ESENDWANTACK, &xfersend__msg,
                  e->data.msg.hdr.task));
            }
            else
              /* he has already reported the error: nothing more to do. */
              tracef0 ("save was not successful.\n");

            if (xfersend__close) xfersend__winclose ();
            return xfersend__saveproc != NULL;
          break;
        }
    break;

    case wimp_EACK:
      /*If this is our Message_PrintSave being bounced, then we have to print the
323
         file now (according to the revised protocol 3 Apr '92). */
Neil Turton's avatar
Neil Turton committed
324 325
      if (e->data.msg.hdr.action == wimp_MPrintSave &&
          e->data.msg.hdr.my_ref == xfersend__msgid)
326 327
      {
        tracef0 ("no printer manager - printing direct\n");
Neil Turton's avatar
Neil Turton committed
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362

        if (xfersend__printproc != NULL)
          (void) (*xfersend__printproc) (e->data.msg.data.print.name,
              xfersend__savehandle);
            /*This reports errors itself.*/
        else
          tracef0 ("no printing function supplied\n");

        return TRUE;
      }
    break;
  }
  return FALSE;
}

static int sendbuf__state;

static BOOL sendbuf__unknowns (wimp_eventstr *e, void *h)
{
 h = h;

 tracef4 ("sendbuf__unknowns %d %d %d %d\n",
          e->data.msg.hdr.my_ref, e->data.msg.hdr.your_ref,
          xfersend__msg.hdr.your_ref, xfersend__msg.hdr.my_ref);

 if ( (e->e == wimp_ESENDWANTACK || e->e == wimp_ESEND) &&
     e->data.msg.hdr.your_ref == xfersend__msg.hdr.my_ref &&
     e->data.msg.hdr.action == wimp_MRAMFETCH)
 {
  /* Prepare xfersend__msg as the next RAMTRANSMIT. Most of
  the fields are already set up. */

  xfersend__msg.data.ramtransmit.addr = e->data.msg.data.ramfetch.addr;
  xfersend__msg.data.ramtransmit.nbyteswritten = 0;
  xfersend__msg.hdr.your_ref = e->data.msg.hdr.my_ref;
363
  xfersend__rcvbufsize = e->data.msg.data.ramfetch.nbytes;
Neil Turton's avatar
Neil Turton committed
364 365

  tracef2 ("RAMFETCH received: continue with buffer at %x, size %d\n",
366
          (int) xfersend__msg.data.ramtransmit.addr, xfersend__rcvbufsize);
Neil Turton's avatar
Neil Turton committed
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403

  sendbuf__state = 1;
  return TRUE;      /* We've had another RAMFETCH: off we go again */
 }

 if (e->e == wimp_EACK &&
    e->data.msg.hdr.my_ref == xfersend__msg.hdr.my_ref)
 {
  sendbuf__state = 2;
  tracef0 ("xfersend RAMTRANSMIT bounced; set failed state\n");
  return TRUE;/* our message bounced back; give up */
 }

 return FALSE;    /* we don't want it */
}


BOOL xfersend_sendbuf (char *buffer, int size)
{

/* Called by his sendproc when sending things in memory. The
reply record is in xfersend__msg. */

 tracef2 ("xfersend_sendbuf %i %i\n", (int) buffer, size);

 /* Make the data transfer */
 tracef3 ("transfer block of %d from %x to %x\n", size, (int) buffer,
          (int) (xfersend__msg.data.ramtransmit.addr +
            xfersend__msg.data.ramtransmit.nbyteswritten));

 wimpt_noerr (wimp_transferblock (wimpt_task (), buffer, xfersend__receiver,
      xfersend__msg.data.ramtransmit.addr +
      xfersend__msg.data.ramtransmit.nbyteswritten,
      size));

 /* record bytes to be sent to the other end */
 xfersend__msg.data.ramtransmit.nbyteswritten += size;
404
 xfersend__rcvbufsize -= size;
Neil Turton's avatar
Neil Turton committed
405 406 407

 /* if size != 0, there are still bytes to send. */

408
  if (xfersend__rcvbufsize > 0) return TRUE;
Neil Turton's avatar
Neil Turton committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434

 tracef1 ("xfersend message has put %d into buffer\n",size);
 /* Tell him that you've done it */
 wimpt_noerr (wimp_sendmessage (wimp_ESENDWANTACK,
      &xfersend__msg,
      xfersend__receiver));

  /* Get his reply. Poll and despatch events until get nack or message */

 sendbuf__state = 0;

 win_add_unknown_event_processor (sendbuf__unknowns, 0);
 do event_process (); while (sendbuf__state == 0);
 win_remove_unknown_event_processor (sendbuf__unknowns, 0);

  /* This exit happens in the cases where the buffers at each end
      are of identical size. So, return for another call to sendbuf, or
      so that the sendbuf procedure can return. */

 return sendbuf__state != 2;  /* OK unless state = broken */
}

BOOL xfersend (int filetype, char *filename, int estsize,
   xfersend_saveproc saver, xfersend_sendproc sender, xfersend_printproc printer,
   wimp_eventstr *e, void *handle)

435 436
{
   wimp_wstate wstate;
Neil Turton's avatar
Neil Turton committed
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
   wimp_icon icon;
   wimp_w w = e->data.but.m.w;
   wimp_mousestr mouse_str;
   int
      x_limit = bbc_vduvar (bbc_XWindLimit) << bbc_vduvar (bbc_XEigFactor),
      y_limit = bbc_vduvar (bbc_YWindLimit) << bbc_vduvar (bbc_YEigFactor),
      x0, y0, x1, y1;
   int screen_x0;
   int screen_y0;

   xfersend__saveproc = saver;
   xfersend__sendproc = sender;
   xfersend__printproc = printer;
   xfersend__filetype = filetype;
   xfersend__estsize = estsize;
   xfersend__savehandle = handle;
453 454
   if (xfersend__filename == NULL) xfersend__filename = malloc (256);
   if (filename == NULL)
Neil Turton's avatar
Neil Turton committed
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
      strcpy (xfersend__filename, msgs_lookup (MSGS_xfersend2));
   else
      strncpy (xfersend__filename,filename,256);

   tracef0 ("Initiate a drag.\n");

   /*Find screen origin*/
   wimp_get_wind_state (w, &wstate);
   screen_x0 = wstate.o.box.x0 - wstate.o.x;
   screen_y0 = wstate.o.box.y1 - wstate.o.y;

   /*Get initial icon position*/
   wimp_get_icon_info (w, e->data.but.m.i, &icon);
   x0 = icon.box.x0 += screen_x0;
   y0 = icon.box.y0 += screen_y0;
   x1 = icon.box.x1 += screen_x0;
   y1 = icon.box.y1 += screen_y0;

#if USE_DRAGASPRITE
   if (xfersend__using_dragasprite ())
475
   {
476 477 478 479
      char *name;
      sprite_area *area;
      BOOL semi = TRUE;

Neil Turton's avatar
Neil Turton committed
480
      if (icon.flags & wimp_ITEXT)
481 482 483 484 485 486 487 488 489 490 491 492 493
      {  /* Indirect, sprite, text. Pick up the sprite name from
            the validation string */
         area = wimp_spritearea;
         name = icon.data.indirecttext.validstring;
         while (*name)
         {
           if (((*name == 'S') || (*name == 's')) && semi)
           {
              name++; break;
           }
           semi = (*name == ';');
           name++;
         }
Neil Turton's avatar
Neil Turton committed
494 495
      }
      else
496 497 498
      {  /* Indirect, sprite, no text. As DragASprite always needs a
            sprite name, the 'nameisname' field is assumed non 0 */
         area = icon.data.indirectsprite.spritearea;
Neil Turton's avatar
Neil Turton committed
499 500 501 502 503 504 505 506 507
         name = icon.data.indirectsprite.name;
      }

      wimpt_complain (dragasprite_start (dragasprite_HJUSTIFY_CENTRE |
          dragasprite_VJUSTIFY_CENTRE | dragasprite_BOUNDTO_SCREEN |
          dragasprite_BOUND_POINTER | dragasprite_DROPSHADOW_PRESENT,
          area, name, &icon.box, NULL));
   }
   else   /* not using dragasprite */
508 509
   {
      /*Get pointer position to allow icon to be dragged partially off-screen */
Neil Turton's avatar
Neil Turton committed
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
      int mouse_x,mouse_y;
      wimp_dragstr dr;
      wimp_get_point_info (&mouse_str);
      mouse_x = mouse_str.x;
      mouse_y = mouse_str.y;

      /*Set up drag*/
      dr.window    = w; /*not relevant*/
      dr.type      = wimp_USER_FIXED;
      dr.box.x0    = x0;
      dr.box.y0    = y0;
      dr.box.x1    = x1;
      dr.box.y1    = y1;
      dr.parent.x0 = x0 - mouse_x; /*Expanded parent by box overlap*/
      dr.parent.y0 = y0 - mouse_y;
      dr.parent.x1 = x1 - mouse_x + x_limit;
      dr.parent.y1 = y1 - mouse_y + y_limit;
      wimp_drag_box (&dr);
   }
#else
530
   /*Get pointer position to allow icon to be dragged partially off-screen. */
Neil Turton's avatar
Neil Turton committed
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
   wimp_get_point_info (&mouse_str);
   mouse_x = mouse_str.x;
   mouse_y = mouse_str.y;

   /*Set up drag*/
   dr.window    = w; /*not relevant*/
   dr.type      = wimp_USER_FIXED;
   dr.box.x0    = x0;
   dr.box.y0    = y0;
   dr.box.x1    = x1;
   dr.box.y1    = y1;
   dr.parent.x0 = x0 - mouse_x; /*Expanded parent by box overlap*/
   dr.parent.y0 = y0 - mouse_y;
   dr.parent.x1 = x1 - mouse_x + x_limit;
   dr.parent.y1 = y1 - mouse_y + y_limit;
   wimp_drag_box (&dr);
#endif

   win_add_unknown_event_processor (&xfersend__unknowns, NULL);
   return TRUE;
}

BOOL xfersend_print (int type, char *filename, int estsize,
   xfersend_saveproc saver, xfersend_sendproc sender, xfersend_printproc printer,
   void *handle)

557 558
{
   xfersend__saveproc = saver;
Neil Turton's avatar
Neil Turton committed
559 560 561 562 563 564 565 566 567
   xfersend__sendproc = sender;
   xfersend__printproc = printer;
   xfersend__filetype = type;
   xfersend__estsize = estsize;
   xfersend__savehandle = handle;
   if (xfersend__filename == NULL &&
        (xfersend__filename = malloc (256)) == NULL)
      return FALSE;

568
   sprintf (xfersend__filename, "%.256s", filename != NULL ? filename:
Neil Turton's avatar
Neil Turton committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
       msgs_lookup (MSGS_xfersend2));

   /*Broadcast a PrintSave message. The printer will reply with PrintFile
     (which we ignore in RISC O S 3.00)), then DataSaveAck which is caught by
      xfersend__unknowns ().*/
   xfersend__msg.hdr.size = sizeof (wimp_msghdr) +
         sizeof (wimp_msgdatasave);
   xfersend__msg.hdr.your_ref = 0;
   xfersend__msg.hdr.action = wimp_MPrintSave;
   xfersend__msg.data.datasave.estsize = estsize; /*this is PrintSave not
         DataSave, but it matches (apart from w, i, x, y not used)*/
   xfersend__msg.data.datasave.type = type;
   sprintf (xfersend__msg.data.datasave.leaf, "%.*s",
         sizeof xfersend__msg.data.datasave.leaf - 1, xfersend__filename);

   tracef1 ("sending Message_PrintSave \"%s\"\n",
         xfersend__msg.data.datasave.leaf);
   wimpt_noerr (wimp_sendmessage (wimp_ESENDWANTACK, &xfersend__msg, NULL));

   tracef1 ("message id is %d\n", xfersend__msg.hdr.my_ref);
   xfersend__msgid = xfersend__msg.hdr.my_ref; /*Filled in by WIMP.*/

   tracef0 ("adding unknown event processor\n");
   win_add_unknown_event_processor (&xfersend__unknowns, NULL);

   return TRUE;
}

597
BOOL xfersend_pipe(int filetype, char *filename, int estsize,
Neil Turton's avatar
Neil Turton committed
598 599 600
                     xfersend_saveproc saver,
                     xfersend_sendproc sender,
                     xfersend_printproc printer,
601
                     wimp_eventstr *e, void *handle)
Neil Turton's avatar
Neil Turton committed
602 603 604 605 606 607 608
{
      xfersend__saveproc = saver;
      xfersend__sendproc = sender;
      xfersend__printproc = printer;
      xfersend__filetype = filetype;
      xfersend__estsize = estsize;
      xfersend__savehandle = handle;
609 610
      if (xfersend__filename == NULL) xfersend__filename = malloc (256);
      if (filename == NULL)
Neil Turton's avatar
Neil Turton committed
611 612 613 614
        strcpy (xfersend__filename, msgs_lookup (MSGS_xfersend2));
      else
        strncpy (xfersend__filename,filename,256);

615
      {
Neil Turton's avatar
Neil Turton committed
616
        wimp_msgstr msg;
617 618 619 620 621 622 623 624
        int         leaflen;

        leaflen = (xfersend__suggest_leaf(&msg.data.datasave) + 3) & ~3;
        msg.hdr.size = sizeof (wimp_msghdr)
                     + sizeof (wimp_msgdatasave)
                     - sizeof (msg.data.datasave.leaf)
                     + leaflen;
        msg.hdr.your_ref = e->data.msg.hdr.my_ref;
Neil Turton's avatar
Neil Turton committed
625
        msg.hdr.action = wimp_MDATASAVE;
626 627 628 629
        msg.data.datasave.w = e->data.msg.data.datarequest.w;
        msg.data.datasave.i = (wimp_i)e->data.msg.data.datarequest.h;
        msg.data.datasave.x = e->data.msg.data.datarequest.x;
        msg.data.datasave.y = e->data.msg.data.datarequest.y;
Neil Turton's avatar
Neil Turton committed
630 631
        msg.data.datasave.type = xfersend__filetype;
        msg.data.datasave.estsize = xfersend__estsize;
632 633

        wimpt_noerr (wimp_sendmessage (wimp_ESEND, &msg, e->data.msg.hdr.task));
Neil Turton's avatar
Neil Turton committed
634 635 636 637 638 639 640
        xfersend__msgid = msg.hdr.my_ref; /* filled in by wimp. */
      }

      win_add_unknown_event_processor (&xfersend__unknowns, 0);
      return TRUE;
}

641 642 643
BOOL xfersend_file_is_safe(void)
{
  return xfersend__fileissafe;
Neil Turton's avatar
Neil Turton committed
644 645
}

646 647 648
void xfersend_set_fileissafe(BOOL value)
{
  xfersend__fileissafe = value;
Neil Turton's avatar
Neil Turton committed
649 650
}

651
void xfersend_clear_unknowns(void)
Neil Turton's avatar
Neil Turton committed
652
{
653 654
  win_remove_unknown_event_processor (sendbuf__unknowns, 0);
  win_remove_unknown_event_processor (&xfersend__unknowns, 0);
Neil Turton's avatar
Neil Turton committed
655 656
}

657
int xfersend_read_last_ref(void)
Neil Turton's avatar
Neil Turton committed
658 659 660 661 662
{
  return xfersend__msgid;        /* my_ref of last DataSave message */
}

/* end xfersend.c */