system-controller: passthrough support for layer requests
[profile/ivi/murphy.git] / packaging.in / murphy.lua
1 with_system_controller = false
2 with_amb = false
3 verbose = 0
4
5 m = murphy.get()
6
7 -- try loading the various logging plugins
8 m:try_load_plugin('systemd')
9 m:try_load_plugin('dlog')
10
11 -- load the console plugin
12 m:try_load_plugin('console')
13
14 m:try_load_plugin('console.disabled', 'webconsole', {
15                   address = 'wsck:127.0.0.1:3000/murphy',
16                   httpdir = '/usr/share/murphy/webconsole' });
17
18 -- load the dbus plugin
19 if m:plugin_exists('dbus') then
20     m:load_plugin('dbus')
21 end
22
23 -- load the native resource plugin
24 if m:plugin_exists('resource-native') then
25     m:load_plugin('resource-native')
26     m:info("native resource plugin loaded")
27 else
28     m:info("No native resource plugin found...")
29 end
30
31 -- load the dbus resource plugin
32 m:try_load_plugin('resource-dbus', {
33     dbus_bus = "system",
34     dbus_service = "org.Murphy",
35     dbus_track = true,
36     default_zone = "driver",
37     default_class = "implicit"
38 })
39
40 -- load the domain control plugin
41 if m:plugin_exists('domain-control') then
42     m:load_plugin('domain-control')
43 else
44     m:info("No domain-control plugin found...")
45 end
46
47 if m:plugin_exists('glib') then
48     m:load_plugin('glib')
49 else
50     m:info("No glib plugin found...")
51 end
52
53 -- load the AMB plugin
54 if m:plugin_exists('amb') then
55     m:try_load_plugin('amb')
56
57     if builtin.method.amb_initiate and
58        builtin.method.amb_update
59     then
60         with_amb = true
61     end
62 else
63     m:info("No amb plugin found...")
64 end
65
66 -- load the ASM resource plugin
67 if m:plugin_exists('resource-asm') then
68     m:try_load_plugin('resource-asm', {
69         zone = "driver",
70         share_mmplayer = "player:AVP,mandatory,exclusive,strict",
71         ignored_argv0 = "WebProcess"
72     })
73 else
74     m:info("No audio session manager plugin found...")
75 end
76
77 if m:plugin_exists('system-controller') then
78     with_system_controller = true
79 elseif m:plugin_exists('ivi-resource-manager') then
80     m:load_plugin('ivi-resource-manager')
81     with_system_controller = false
82 end
83
84 -- define application classes
85 application_class {
86     name     = "interrupt",
87     priority = 99,
88     modal    = true ,
89     share    = false,
90     order    = "fifo"
91 }
92
93 application_class {
94     name     = "emergency",
95     priority = 80,
96     modal    = false,
97     share    = false,
98     order    = "fifo"
99 }
100 application_class {
101     name     = "system",
102     priority = 52,
103     modal    = false,
104     share    = true,
105     order    = "lifo"
106 }
107 application_class {
108     name     = "alert",
109     priority = 51,
110     modal    = false,
111     share    = false,
112     order    = "fifo"
113 }
114
115 application_class {
116     name     = "navigator",
117     priority = 50,
118     modal    = false,
119     share    = true,
120     order    = "fifo"
121 }
122
123 application_class {
124     name     = "phone",
125     priority = 6 ,
126     modal    = false,
127     share    = true ,
128     order    = "lifo"
129 }
130 application_class {
131     name     = "camera",
132     priority = 5,
133     modal    = false,
134     share    = false,
135     order    = "lifo"
136 }
137
138 application_class { name="event"    , priority=4 , modal=false, share=true , order="fifo" }
139 application_class { name="game"     , priority=3 , modal=false, share=false, order="lifo" }
140 --# doesn't need to be created here, ivi-resource-manager creates it if loaded
141 --#application_class { name="basic"    , priority=2 , modal=false, share=false, order="lifo" }
142 application_class { name="player"   , priority=1 , modal=false, share=true , order="lifo" }
143 application_class { name="implicit" , priority=0 , modal=false, share=false, order="lifo" }
144
145 -- define zone attributes
146 zone.attributes {
147     type = {mdb.string, "common", "rw"},
148     location = {mdb.string, "anywhere", "rw"}
149 }
150
151 -- define zones
152 zone {
153      name = "driver",
154      attributes = {
155          type = "common",
156          location = "front-left"
157      }
158 }
159
160 zone {
161      name = "passanger1",
162      attributes = {
163          type = "private",
164          location = "front-right"
165      }
166 }
167
168 zone {
169      name = "passanger2",
170      attributes = {
171          type = "private",
172          location = "back-left"
173      }
174 }
175
176 zone {
177      name = "passanger3",
178      attributes = {
179          type = "private",
180          location = "back-right"
181      }
182 }
183
184 zone {
185      name = "passanger4",
186      attributes = {
187          type = "private",
188          location = "back-left"
189      }
190 }
191
192
193 -- define resource classes
194 if not m:plugin_exists('ivi-resource-manager') and
195    not with_system_controller
196 then
197    resource.class {
198         name = "audio_playback",
199         shareable = true,
200         attributes = {
201             role = { mdb.string, "music", "rw" },
202             pid = { mdb.string, "<unknown>", "rw" },
203             policy = { mdb.string, "relaxed", "rw" }
204         }
205    }
206 end
207
208 resource.class {
209      name = "audio_recording",
210      shareable = true,
211      attributes = {
212          role   = { mdb.string, "music"    , "rw" },
213          pid    = { mdb.string, "<unknown>", "rw" },
214          policy = { mdb.string, "relaxed"  , "rw" }
215      }
216 }
217
218 resource.class {
219      name = "video_playback",
220      shareable = false,
221 }
222
223 resource.class {
224      name = "video_recording",
225      shareable = false
226 }
227
228 resource.class {
229      name = "speech_recognition",
230      shareable = true
231 }
232
233 resource.class {
234      name = "speech_synthesis",
235      shareable = true
236 }
237
238 if not m:plugin_exists('ivi-resource-manager') and
239    not with_system_controller
240 then
241     resource.method.veto = {
242         function(zone, rset, grant, owners, req_set)
243             return true
244          end
245     }
246 end
247
248 -- test for creating selections
249 mdb.select {
250            name = "audio_owner",
251            table = "audio_playback_owner",
252            columns = {"application_class"},
253            condition = "zone_name = 'driver'"
254 }
255
256 mdb.select {
257            name = "vehicle_speed",
258            table = "amb_vehicle_speed",
259            columns = {"value"},
260            condition = "key = 'VehicleSpeed'"
261 }
262
263 element.lua {
264    name    = "speed2volume",
265    inputs  = { speed = mdb.select.vehicle_speed, param = 9 },
266    outputs = {  mdb.table { name = "speedvol",
267                             index = {"zone", "device"},
268                             columns = {{"zone", mdb.string, 16},
269                                        {"device", mdb.string, 16},
270                                        {"value", mdb.floating}},
271                             create = true
272                            }
273              },
274    oldvolume = 0.0,
275    update  = function(self)
276                 speed = self.inputs.speed.single_value
277                 if (speed) then
278                     volume = (speed - 144.0) / 7.0
279                 else
280                     volume = 0.0
281                 end
282                 diff = volume - self.oldvolume
283                 if (diff*diff > self.inputs.param) then
284                     print("*** element "..self.name.." update "..volume)
285                     self.oldvolume = volume
286                     mdb.table.speedvol:replace({zone = "driver", device = "speakers", value = volume})
287                 end
288              end
289 }
290
291 mdb.select {
292     name = "amb_state",
293     table = "amb_state",
294     columns = { "state" },
295     condition = "id = 0"
296 }
297
298 -- Night mode processing chain
299
300 mdb.select {
301     name = "exterior_brightness",
302     table = "amb_exterior_brightness",
303     columns = { "value" },
304     condition = "key = 'ExteriorBrightness'"
305 }
306
307 element.lua {
308     name    = "nightmode",
309     inputs  = { brightness = mdb.select.exterior_brightness },
310     oldmode = -1;
311     outputs = {
312         mdb.table {
313             name = "amb_nightmode",
314             index = { "id" },
315             create = true,
316             columns = {
317                 { "id", mdb.unsigned },
318                 { "night_mode", mdb.unsigned }
319             }
320         }
321     },
322     update = function(self)
323         -- This is a trivial function to calculate night mode. Later, we will
324         -- need a better threshold value and hysteresis to prevent oscillation.
325
326         brightness = self.inputs.brightness.single_value
327
328         if not brightness then
329             return
330         end
331
332         print("*** element "..self.name.." update brightness: "..brightness)
333
334         if brightness > 300 then
335             mode = 0
336         else
337             mode = 1
338         end
339
340         print("*** resulting mode: ".. mode)
341
342         if not (mode == self.oldmode) then
343             mdb.table.amb_nightmode:replace({ id = 0, night_mode = mode })
344         end
345
346         self.oldmode = mode
347     end
348 }
349
350 mdb.select {
351     name = "select_night_mode",
352     table = "amb_nightmode",
353     columns = { "night_mode" },
354     condition = "id = 0"
355 }
356
357 if with_amb then
358     sink.lua {
359         name = "night_mode",
360         inputs = { NightMode = mdb.select.select_night_mode,
361                    amb_state = mdb.select.amb_state },
362         property = "NightMode",
363         type = "b",
364         initiate = builtin.method.amb_initiate,
365         update = builtin.method.amb_update
366     }
367 end
368
369 -- Night mode general handlers
370
371 sink.lua {
372     name = "nightmode_homescreen",
373     inputs = { owner = mdb.select.select_night_mode },
374     initiate = function(self)
375         -- data = mdb.select.select_night_mode.single_value
376         return true
377     end,
378     update = function(self)
379         data = mdb.select.select_night_mode.single_value
380
381         if verbose > 1 then
382             print("Night mode updated: " .. tostring(data))
383         end
384
385         if sc then
386             -- tell homescreen that night mode was updated
387             sc:send_message(homescreen, m:JSON({command=0x60001,arg=m:JSON({stateid=2,state=data})}))
388         end
389         return true
390     end
391 }
392
393 -- Driving mode processing chain
394
395 element.lua {
396     name    = "drivingmode",
397     inputs  = { speed = mdb.select.vehicle_speed },
398     oldmode = -1;
399     outputs = {
400         mdb.table {
401             name = "amb_drivingmode",
402             index = { "id" },
403             create = true,
404             columns = {
405                 { "id", mdb.unsigned },
406                 { "driving_mode", mdb.unsigned }
407             }
408         }
409     },
410     update = function(self)
411
412         speed = self.inputs.speed.single_value
413
414         if not speed then
415             return
416         end
417
418         if speed == 0 then
419             mode = 0
420         else
421             mode = 1
422         end
423
424         if not (mode == self.oldmode) then
425             mdb.table.amb_drivingmode:replace({ id = 0, driving_mode = mode })
426         end
427
428         self.oldmode = mode
429     end
430 }
431
432 mdb.select {
433     name = "select_driving_mode",
434     table = "amb_drivingmode",
435     columns = { "driving_mode" },
436     condition = "id = 0"
437 }
438
439 if with_amb then
440     sink.lua {
441         name = "driving_mode",
442         inputs = { DrivingMode = mdb.select.select_driving_mode,
443                    amb_state = mdb.select.amb_state },
444         property = "DrivingMode",
445         type = "u",
446         initiate = builtin.method.amb_initiate,
447         update = builtin.method.amb_update
448     }
449 end
450
451 -- turn signals (left, right)
452
453 mdb.select {
454     name = "winker",
455     table = "amb_turn_signal",
456     columns = { "value" },
457     condition = "key = 'TurnSignal'"
458 }
459
460 -- define three categories
461
462 mdb.select {
463     name = "undefined_applications",
464     table = "aul_applications",
465     columns = { "appid" },
466     condition = "category = '<undefined>'"
467 }
468
469 mdb.select {
470     name = "basic_applications",
471     table = "aul_applications",
472     columns = { "appid" },
473     condition = "category = 'basic'"
474 }
475
476 mdb.select {
477     name = "entertainment_applications",
478     table = "aul_applications",
479     columns = { "appid" },
480     condition = "category = 'entertainment'"
481 }
482
483 function ft(t)
484     -- filter the object garbage out of the tables
485     ret = {}
486
487     for k,v in pairs(t) do
488         if k ~= "userdata" and k ~= "new" then
489             ret[k] = v
490         end
491     end
492
493     return ret
494 end
495
496 function getApplication(appid)
497     local conf = nil
498
499     -- find the correct local application definition
500
501     for k,v in pairs(ft(application)) do
502         if appid == v.appid then
503             conf = v
504             break
505         end
506     end
507
508     return conf
509 end
510
511 function regulateApplications(t, regulation)
512     for k,v in pairs(ft(t)) do
513
514         -- iterate through the undefined and entertainment apps, see if
515         -- they have been overruled in local config
516
517         local conf = getApplication(v.appid)
518
519         if conf and conf.resource_class ~= "player" then
520             -- override, don't disable
521             resmgr:disable_screen_by_appid("*", "*", v.appid, false, false)
522         else
523             resmgr:disable_screen_by_appid("*", "*", v.appid, regulation == 1, false)
524         end
525     end
526     resource.method.recalc("driver")
527 end
528
529 -- regulation (on), use "select_driving_mode"
530
531 sink.lua {
532     name = "driving_regulation",
533     inputs = { owner = mdb.select.select_driving_mode },
534     initiate = function(self)
535         -- local data = mdb.select.select_driving_mode.single_value
536         return true
537     end,
538     update = function(self)
539         local data = mdb.select.select_driving_mode.single_value
540
541         if verbose > 1 then
542             print("Driving mode updated: " .. tostring(data))
543         end
544
545         if not sc then
546             return true
547         end
548
549         -- tell homescreen that driving mode was updated
550         sc:send_message(homescreen, m:JSON({command=0x60001,arg=m:JSON({stateid=1,state=data})}))
551
552         --[[
553         -- bulk handle the applications that need requisites
554         r = requisite { driving = true }
555         resmgr:disable_screen_by_requisite("*", "*", r, data == 1, true)
556         --]]
557
558         regulateApplications(ft(mdb.select.entertainment_applications), data)
559         regulateApplications(ft(mdb.select.undefined_applications), data)
560
561         return true
562     end
563 }
564 --[[
565 sink.lua {
566     name = "regulated_app_change",
567     inputs = { undef = mdb.select.undefined_applications,
568                entertainment = mdb.select.entertainment_applications },
569     initiate = function(self)
570         return true
571     end,
572     update = function(self)
573         local data = mdb.select.select_driving_mode.single_value
574
575         if not sc then
576             return
577         end
578
579         if verbose > 1 then
580             print("regulated application list was changed")
581         end
582
583         regulateApplications(ft(mdb.select.entertainment_applications), data)
584         regulateApplications(ft(mdb.select.undefined_applications), data)
585
586         return true
587     end
588 }
589 --]]
590
591 -- shift position (parking, reverse, other)
592
593 mdb.select {
594     name = "gear_position",
595     table = "amb_gear_position",
596     columns = { "value" },
597     condition = "key = 'GearPosition'"
598 }
599
600 -- cameras (back, front, left, right)
601
602 element.lua {
603     name    = "camera_state",
604     inputs  = { winker = mdb.select.winker, gear = mdb.select.gear_position },
605     oldmode = -1;
606     outputs = {
607         mdb.table {
608             name = "target_camera_state",
609             index = { "id" },
610             create = true,
611             columns = {
612                 { "id", mdb.unsigned },
613                 { "front_camera", mdb.unsigned },
614                 { "back_camera", mdb.unsigned },
615                 { "right_camera", mdb.unsigned },
616                 { "left_camera", mdb.unsigned }
617             }
618         }
619     },
620     update = function(self)
621
622         front_camera = 0
623         back_camera = 0
624         right_camera = 0
625         left_camera = 0
626
627         if self.inputs.gear == 128 then
628             back_camera = 1
629         elseif self.inputs.winker == 1 then
630             right_camera = 1
631         elseif self.inputs.winker == 2 then
632             left_camera = 1
633         end
634
635         mdb.table.target_camera_state:replace({ id = 0, front_camera = front_camera, back_camera = back_camera, right_camera = right_camera, left_camera = left_camera })
636
637     end
638 }
639
640 -- system controller test setup
641
642 if not with_system_controller then
643    -- ok, we should have 'audio_playback' defined by now
644    m:try_load_plugin('telephony')
645    return
646 end
647
648 m:load_plugin('system-controller')
649
650 window_manager_operation_names = {
651     [1] = "create",
652     [2] = "destroy"
653 }
654
655 function window_manager_operation_name(oper)
656     local name = window_manager_operation_names[oper]
657     if name then return name end
658     return "<unknown " .. tostring(oper) .. ">"
659 end
660
661 window_operation_names = {
662     [1] = "create",
663     [2] = "destroy",
664     [3] = "name_change",
665     [4] = "visible",
666     [5] = "configure",
667     [6] = "active"
668 }
669
670 function window_operation_name(oper)
671     local name = window_operation_names[oper]
672     if name then return name end
673     return "<unknown " .. tostring(oper) .. ">"
674 end
675
676 layer_operation_names = {
677     [1] = "create",
678     [2] = "destroy",
679     [3] = "visible"
680 }
681
682 function layer_operation_name(oper)
683     local name = layer_operation_names[oper]
684     if name then return name end
685     return "<unknown " .. tostring(oper) .. ">"
686 end
687
688 input_manager_operation_names = {
689     [1] = "create",
690     [2] = "destroy",
691     [3] = "ready"
692 }
693
694 function input_manager_operation_name(oper)
695     local name = input_manager_operation_names[oper]
696     if name then return name end
697     return "<unknown " .. tostring(oper) .. ">"
698 end
699
700 input_operation_names = {
701     [1] = "create",
702     [2] = "destroy",
703     [3] = "update"
704 }
705
706 function input_operation_name(oper)
707     local name = input_operation_names[oper]
708     if name then return name end
709     return "<unknown " .. tostring(oper) .. ">"
710 end
711
712 code_operation_names = {
713     [1] = "create",
714     [2] = "destroy",
715     [3] = "state_change"
716 }
717
718 function code_operation_name(oper)
719     local name = code_operation_names[oper]
720     if name then return name end
721     return "<unknown " .. tostring(oper) .. ">"
722 end
723
724 command_names = {
725     [0x00001] = "send_appid",
726     [0x10001] = "create",
727     [0x10002] = "destroy",
728     [0x10003] = "show",
729     [0x10004] = "hide",
730     [0x10005] = "move",
731     [0x10006] = "change_active",
732     [0x10007] = "change_layer",
733     [0x10008] = "change_attr",
734     [0x10009] = "name",
735     [0x10011] = "map_thumb",
736     [0x10012] = "unmap_thumb",
737     [0x10020] = "show layer",
738     [0x10021] = "hide_layer",
739     [0x10022] = "change_layer_attr",
740     [0x20001] = "add_input",
741     [0x20002] = "del_input",
742     [0x20003] = "send_input",
743     [0x40001] = "acquire_res",
744     [0x40002] = "release_res",
745     [0x40003] = "deprive_res",
746     [0x40004] = "waiting_res",
747     [0x40005] = "revert_res",
748     [0x40011] = "create_res",
749     [0x40012] = "destroy_res",
750     [0x50001] = "set_region",
751     [0x50002] = "unset_region",
752     [0x60001] = "change_state"
753 }
754
755 function command_name(command)
756     local name = command_names[command]
757     if name then return name end
758     return "<unknown " .. tostring(command) .. ">"
759 end
760
761 input_layer = {
762    [3] = true, -- input
763    [4] = true, -- touch
764    [5] = true  -- cursor
765 }
766
767 resmgr = resource_manager {
768   screen_event_handler = function(self, ev)
769                              local event = ev.event
770                              local surface = ev.surface
771
772                              if event == "init" then
773                                  if verbose > 0 then
774                                      print("*** init screen resource allocation -- disable all 'player'")
775                                  end
776                                  resmgr:disable_audio_by_appid("*", "player", "*", true, false)
777                              elseif event == "preallocate" then
778                                  if verbose > 0 then
779                                      print("*** preallocate screen resource "..
780                                            "for '" .. ev.appid .. "' -- enable 'player', if any")
781                                  end
782                                  resmgr:disable_audio_by_appid("*", "player", ev.appid, false, false)
783                              elseif event == "grant" then
784                                  if verbose > 0 then
785                                     print("*** make visible surface "..surface)
786                                  end
787                                  local a = animation({})
788                                  local r = m:JSON({surface = surface,
789                                                    visible = 1,
790                                                    raise   = 1})
791                                  wmgr:window_request(r,a,0)
792                              elseif event == "revoke" then
793                                  if verbose > 0 then
794                                     print("*** hide surface "..surface)
795                                  end
796                                  local a = animation({})
797                                  local r = m:JSON({surface = ev.surface,
798                                                    visible = 0})
799                                  wmgr:window_request(r,a,0)
800
801                              elseif event == "create" then
802
803                                 if verbose > 0 then
804                                     print("*** screen resource event: " ..
805                                           tostring(ev))
806                                 end
807
808                                 local regulation = mdb.select.select_driving_mode.single_value
809
810                                 if regulation == 1 then
811
812                                     local blacklisted = false
813
814                                     -- applications which have their category set to "entertainment"
815                                     -- or "undefined" are blacklisted, meaning they should be regulated
816
817                                     for i,v in pairs(ft(mdb.select.undefined_applications)) do
818                                         if v.appid == ev.appid then
819                                             if verbose > 0 then
820                                                 print(ev.appid .. " was blacklisted (undefined)")
821                                             end
822                                             blacklisted = true
823                                             break
824                                         end
825                                     end
826
827                                     if not blacklisted then
828                                         for i,v in pairs(ft(mdb.select.entertainment_applications)) do
829                                             if v.appid == ev.appid then
830                                                 if verbose > 0 then
831                                                     print(ev.appid .. " was blacklisted (entertainment)")
832                                                 end
833                                                 blacklisted = true
834                                                 break
835                                             end
836                                         end
837                                     end
838
839                                     -- our local application config, which takes precedence
840                                     local conf = getApplication(ev.appid)
841
842                                     -- disable only non-whitelisted applications
843                                     if not conf or conf.resource_class == "player" then
844                                         if blacklisted then
845                                             if verbose > 0 then
846                                                 print("disabling screen for " .. ev.appid)
847                                             end
848                                             resmgr:disable_screen_by_appid("*", "*", ev.appid, true, true)
849                                         end
850                                     end
851                                 end
852
853                              elseif event == "destroy" then
854                                if verbose > 0 then
855                                     print("*** screen resource event: " ..
856                                           tostring(ev))
857                                  end
858                              else
859                                  if verbose > 0 then
860                                     print("*** screen resource event: " ..
861                                           tostring(ev))
862                                  end
863                              end
864                          end,
865   audio_event_handler = function(self, ev)
866                              local event = ev.event
867                              local appid = ev.appid
868                              local audioid = ev.audioid
869
870                              if event == "grant" then
871                                  if verbose > 0 then
872                                     print("*** grant audio to "..appid..
873                                           " ("..audioid..") in '" ..
874                                           ev.zone .. "' zone")
875                                  end
876                              elseif event == "revoke" then
877                                  if verbose > 0 then
878                                     print("*** revoke audio from "..appid..
879                                           " ("..audioid..") in '" ..
880                                           ev.zone .. "' zone")
881                                  end
882                              else
883                                  if verbose > 0 then
884                                     print("*** audio resource event: " ..
885                                           tostring(ev))
886                                  end
887                              end
888                         end
889 }
890
891 resclnt = resource_client {}
892
893 wmgr = window_manager {
894   geometry = function(self, w,h, v)
895                   if type(v) == "function" then
896                       return v(w,h)
897                   end
898                   return v
899              end,
900
901   application = function(self, appid)
902                      if appid then
903                          local app = application_lookup(appid)
904                          if not app then
905                              app = application_lookup("default")
906                          end
907                          return app
908                      end
909                      return { privileges = {screen="none", audio="none"} }
910                 end,
911
912   output_order = { 1, 0 },
913
914   outputs = { { name  = "Mid",
915                 id    = 0,
916                 zone  = "driver",
917                 areas = { Full = {
918                               id     = 20,
919                               pos_x  = 0,
920                               pos_y  = 0,
921                               width  = function(w,h) return w end,
922                               height = function(w,h) return h end
923                           },
924                           Left = {
925                               id     = 21,
926                               pos_x  = 0,
927                               pos_y  = 0,
928                               width  = 320,
929                               height = function(w,h) return h end
930                           },
931                           Right = {
932                               id     = 22,
933                               pos_x  = function(w,h) return w-320 end,
934                               pos_y  = 0,
935                               width  = 320,
936                               height = function(w,h) return h end
937                           }
938                         }
939                },
940                { name  = "Center",
941                  id    = 1,
942                  zone  = "driver",
943                  areas = { Status = {
944                               id     = 0,
945                               pos_x  = 0,
946                               pos_y  = 0,
947                               width  = function(w,h) return w end,
948                               height = 64
949                            },
950                            Full = {
951                               id     = 1,
952                               pos_x  = 0,
953                               pos_y  = 64,
954                               width  = function(w,h) return w end,
955                               height = function(w,h) return h-64-128 end
956                            },
957                            Upper = {
958                               id     = 2,
959                               pos_x  = 0,
960                               pos_y  = 64,
961                               width  = function(w,h) return w end,
962                               height = function(w,h) return (h-64-128)/2 end
963                            },
964                            Lower = {
965                               id     = 3,
966                               pos_x  = 0,
967                               pos_y  = function(w,h) return (h-64-128)/2+64 end,
968                               width  = function(w,h) return w end,
969                               height = function(w,h) return (h-64-128)/2 end
970                            },
971                            UpperLeft = {
972                               id     = 4,
973                               pos_x  = 0,
974                               pos_y  = 64,
975                               width  = function(w,h) return w/2 end,
976                               height = function(w,h) return (h-64-128)/2 end
977                            },
978                            UpperRight = {
979                               id     = 5,
980                               pos_x  = function(w,h) return w/2 end,
981                               pos_y  = 64,
982                               width  = function(w,h) return w/2 end,
983                               height = function(w,h) return (h-64-128)/2 end
984                            },
985                            LowerLeft = {
986                               id     = 6,
987                               pos_x  = 0,
988                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
989                               width  = function(w,h) return w/2 end,
990                               height = function(w,h) return (h-64-128)/2 end
991                            },
992                            LowerRight = {
993                               id     = 7,
994                               pos_x  = function(w,h) return w/2 end,
995                               pos_y  = function(w,h) return (h-64-128/2)+64 end,
996                               width  = function(w,h) return w/2 end,
997                               height = function(w,h) return (h-64-128)/2 end
998                            },
999                            SysApp = {
1000                               id     = 8,
1001                               pos_x  = 0,
1002                               pos_y  = 64,
1003                               width  = function(w,h) return w end,
1004                               height = function(w,h) return h-64-128 end
1005                            },
1006                            ["SysApp.Left"] = {
1007                               id     = 9,
1008                               pos_x  = 0,
1009                               pos_y  = 64,
1010                               width  = function(w,h) return w/2-181 end,
1011                               height = function(w,h) return h-64-128 end
1012                            },
1013                            ["SysApp.Right"] = {
1014                               id     = 10,
1015                               pos_x  = function(w,h) return w/2+181 end,
1016                               pos_y  = 64,
1017                               width  = function(w,h) return w/2-181 end,
1018                               height = function(w,h) return h-64-128 end
1019                            },
1020                         }
1021               }
1022   },
1023   layers = { {      0, "Background"   , 1 },
1024              {      1, "Application"  , 2 },
1025              {      2, "Softkeyboard" , 4 },
1026              {      3, "HomeScreen"   , 2 },
1027              {      4, "ControlBar"   , 2 },
1028              {      5, "InterruptApp" , 2 },
1029              {      6, "OnScreen"     , 2 },
1030              {    101, "Input"        , 3 },
1031              {    102, "Cursor"       , 5 },
1032              {    103, "Startup"      , 6 },
1033              { 0x1000, "Background"   , 1 },
1034              { 0x2000, "Normal"       , 2 },
1035              { 0x3000, "Fullscreen"   , 2 },
1036              { 0x4000, "InputPanel"   , 3 },
1037              { 0xA000, "Touch"        , 4 },
1038              { 0xB000, "Cursor"       , 5 },
1039              { 0xC000, "Startup"      , 6 }
1040   },
1041
1042
1043   manager_update = function(self, oper)
1044                        if verbose > 0 then
1045                            print("### <== WINDOW MANAGER UPDATE:" ..
1046                                  window_manager_operation_name(oper))
1047                        end
1048                        if oper == 1 then
1049                            local wumask = window_mask { raise   = true,
1050                                                         visible = true,
1051                                                         active  = true }
1052                            local wrmask = window_mask { active  = true }
1053                            local lumask = layer_mask  { visible = true }
1054                            local lrmask = layer_mask  { visible = true }
1055                            local req = m:JSON({
1056                                passthrough_window_update  = wumask:tointeger(),
1057                                passthrough_window_request = wrmask:tointeger(),
1058                                passthrough_layer_update   = lumask:tointeger(),
1059                                passthrough_layer_request  = lrmask:tointeger()
1060                            })
1061                            self:manager_request(req)
1062                        end
1063                    end,
1064
1065   window_update = function(self, oper, win, mask)
1066                       if verbose > 0 then
1067                           print("### <== WINDOW UPDATE oper:" ..
1068                                 window_operation_name(oper) ..
1069                                 " mask: " .. tostring(mask))
1070                           if verbose > 1 then
1071                               print(win)
1072                           end
1073                       end
1074
1075                       local arg = m:JSON({ surface = win.surface,
1076                                            winname = win.name,
1077                       })
1078                       local command = 0
1079
1080                       if oper == 1 then -- create
1081                            local layertype = win.layertype
1082                            if layertype and input_layer[layertype] then
1083                                if verbose > 0 then
1084                                    print("ignoring input panel creation")
1085                                end
1086                                return
1087                            end
1088                            command     = 0x10001
1089                       elseif oper == 2 then -- destroy
1090                            command     = 0x10002
1091                       elseif oper == 3 then  -- namechange
1092                            command     = 0x10009
1093                       elseif oper == 4 or oper == 5 then --visible or configure
1094                            command     = 0x10008
1095                            arg.zone    = win.area
1096                            arg.node    = win.node
1097                            arg.layer   = win.layer
1098                            arg.pos_x   = win.pos_x
1099                            arg.pos_y   = win.pos_y
1100                            arg.width   = win.width
1101                            arg.height  = win.height
1102                            arg.raise   = win.raise
1103                            arg.visible = win.visible
1104                            arg.active  = win.active
1105                       elseif oper == 6 then -- active
1106                            command     = 0x10006
1107                            arg.active  = win.active
1108                       else
1109                            if verbose > 0 then
1110                                print("### nothing to do")
1111                            end
1112                            return
1113                       end
1114
1115                       local msg = m:JSON({ command = command,
1116                                            appid   = win.appid,
1117                                            pid     = win.pid,
1118                                            arg     = arg
1119                       })
1120                       if verbose > 0 then
1121                           print("### <== sending " ..
1122                                 command_name(msg.command) ..
1123                                 " window message to '" .. win.name .. "'")
1124                           if verbose > 1 then
1125                               print(msg)
1126                           end
1127                       end
1128                       sc:send_message(homescreen, msg)
1129
1130                       if oper == 1 then -- create
1131                           local i = input_layer[win.layertype]
1132                           local p = self:application(win.appid)
1133                           local s = p.privileges.screen
1134
1135                           if s == "system" then
1136                               local a = animation({})
1137                               local r = m:JSON({surface = win.surface,
1138                                                 visible = 1,
1139                                                 raise   = 1})
1140                               self:window_request(r,a,0)
1141                           else
1142                               if i then
1143                                   if verbose > 0 then
1144                                       print("do not make resource for " ..
1145                                             "input window")
1146                                   end
1147                               else
1148                                   resclnt:resource_set_create("screen",
1149                                                                "driver",
1150                                                                win.appid,
1151                                                                win.surface)
1152                               end
1153                           end
1154                       elseif oper == 2 then -- destroy
1155                           resclnt:resource_set_destroy("screen", win.surface)
1156                       elseif oper == 6 then -- active
1157                           if win.active then
1158                               local i = input_layer[win.layertype]
1159                               local p = self:application(win.appid)
1160                               local s = p.privileges.screen
1161                               local surface = win.surface
1162                               if not i and s ~= "system" then
1163                                  resclnt:resource_set_acquire("screen",surface)
1164                                  resmgr:window_raise(win.appid, surface, 1)
1165                               end
1166                           end
1167                       end
1168                   end,
1169
1170   layer_update = function(self, oper, layer, mask)
1171                       if verbose > 0 then
1172                           print("### LAYER UPDATE:" ..
1173                                 layer_operation_name(oper) ..
1174                                 " mask: " .. tostring(mask))
1175                           if verbose > 1 then
1176                               print(layer)
1177                           end
1178                       end
1179                       if oper == 3 then -- visible
1180                          local command = 0x10022
1181                          local msg = m:JSON({
1182                                          command = command,
1183                                          appid = "",
1184                                          arg = m:JSON({layer = layer.id,
1185                                                        visible = layer.visible
1186                                          })
1187                                 })
1188                          if verbose > 0 then
1189                             print("### <== sending "..command_name(command)..
1190                                   " layer message")
1191                             if verbose > 1 then
1192                                 print(msg)
1193                             end
1194                          end
1195                          sc:send_message(homescreen, msg)
1196                       else
1197                            if verbose > 0 then
1198                                print("### nothing to do")
1199                            end
1200                       end
1201                  end,
1202
1203   output_update = function(self, oper, out, mask)
1204                       local idx = out.index
1205                       local defidx = self.output_order[idx+1]
1206                       if verbose > 0 then
1207                           print("### OUTPUT UPDATE:" .. oper ..
1208                                 " mask: "..tostring(mask))
1209                       end
1210                       if not defidx then
1211                           return
1212                       end
1213                       print(out)
1214                       local outdef = self.outputs[defidx+1]
1215                       if (oper == 1) then -- create
1216                           if outdef then
1217                               self:output_request(m:JSON({index = idx,
1218                                                           id    = outdef.id,
1219                                                           name  = outdef.name
1220                                                           }))
1221                           end
1222                       elseif (oper == 5) then -- done
1223                           local ads = outdef.areas
1224                           local on = outdef.name
1225                           if ads then
1226                               for name,ad in pairs(ads) do
1227                                   local can = wmgr:canonical_name(on.."."..name)
1228                                   local a = m:JSON({name   = name,
1229                                                     output = out.index})
1230                                   for fld,val in pairs(ad) do
1231                                       a[fld] = self:geometry(out.width,
1232                                                              out.height,
1233                                                              val)
1234                                    end
1235                                    self:area_create(a)
1236                                    resmgr:area_create(area[can], outdef.zone)
1237                               end
1238                           end
1239                       end
1240                   end
1241 }
1242
1243
1244 imgr = input_manager {
1245   inputs = {{ name = "G27 Racing Wheel",
1246               id = 0,
1247               switch = { [2] = {appid="org.tizen.ico.app-soundsample"      },
1248                          [3] = {appid="org.tizen.ico.homescreen", keycode=1},
1249                          [4] = {appid="org.tizen.ico.app-soundsample"      },
1250                          [5] = {appid="org.tizen.ico.homescreen", keycode=2}
1251              }}
1252   },
1253
1254   manager_update = function(self, oper)
1255                        if verbose > 0 then
1256                            print("### <== INPUT MANAGER UPDATE:" ..
1257                                  input_manager_operation_name(oper))
1258                        end
1259                    end,
1260
1261   input_update = function(self, oper, inp, mask)
1262                      if verbose > 0 then
1263                          print("### INPUT UPDATE:" ..
1264                                 input_operation_name(oper) ..
1265                                 " mask: " .. tostring(mask))
1266                           if verbose > 1 then
1267                               print(inp)
1268                           end
1269                       end
1270                  end,
1271   code_update  = function(self, oper, code, mask)
1272                      if verbose > 0 then
1273                          print("### CODE UPDATE: mask: " .. tostring(mask))
1274                          if verbose > 1 then
1275                              print(code)
1276                          end
1277                      end
1278                      local msg = m:JSON({ command = 1,
1279                                           appid = "org.tizen.ico.homescreen",
1280                                           arg = m:JSON({ device = code.device,
1281                                                          time = code.time,
1282                                                          input = code.input,
1283                                                          code = code.id,
1284                                                          state = code.state
1285                                            })
1286                      })
1287                      if verbose > 0 then
1288                          print("### <== sending " ..
1289                                command_name(msg.command) ..
1290                                " input message")
1291                          if verbose > 1 then
1292                              print(msg)
1293                          end
1294                      end
1295                      sc:send_message(homescreen, msg)
1296                  end
1297 }
1298
1299 sc = m:get_system_controller()
1300
1301 -- resource sets
1302 sets = {}
1303
1304 -- user manager
1305 um = m:UserManager()
1306
1307 connected = false
1308 homescreen = ""
1309
1310 -- these shoud be before wmgr:connect() is called
1311 if verbose > 0 then
1312    print("====== creating applications ======")
1313 end
1314 application {
1315     appid          = "default",
1316     area           = "Center.Full",
1317     privileges     = { screen = "none", audio = "none" },
1318     resource_class = "player",
1319     screen_priority = 0
1320 }
1321
1322 application {
1323     appid           = "weston",
1324     area            = "Center.Full",
1325     privileges      = { screen = "system", audio = "none" },
1326     resource_class  = "implicit",
1327     screen_priority = 30
1328 }
1329
1330 application {
1331     appid           = "org.tizen.ico.homescreen",
1332     area            = "Center.Full",
1333     privileges      = { screen = "system", audio = "system" },
1334     resource_class  = "player",
1335     screen_priority = 20
1336 }
1337
1338 application {
1339     appid           = "org.tizen.ico.statusbar",
1340     area            = "Center.Status",
1341     privileges      = { screen = "system", audio = "none" },
1342     resource_class  = "player",
1343     screen_priority = 20
1344 }
1345
1346 application {
1347     appid           = "org.tizen.ico.onscreen",
1348     area            = "Center.Full",
1349     privileges      = { screen = "system", audio = "system" },
1350     resource_class  = "player",
1351     screen_priority = 20
1352 }
1353
1354 application {
1355     appid           = "org.tizen.ico.login",
1356     area            = "Center.Full",
1357     privileges      = { screen = "system", audio = "system" },
1358     resource_class  = "player",
1359     screen_priority = 20
1360 }
1361
1362 application {
1363     appid           = "org.tizen.ico.camera_left",
1364     area            = "Center.SysApp.Left",
1365     privileges      = { screen = "system", audio = "none" },
1366     requisites      = { screen = "blinker_left", audio = "none" },
1367     resource_class  = "player",
1368     screen_priority = 30
1369 }
1370
1371 application {
1372     appid           = "org.tizen.ico.camera_right",
1373     area            = "Center.SysApp.Right",
1374     privileges      = { screen = "system", audio = "none" },
1375     requisites      = { screen = "blinker_right", audio = "none" },
1376     resource_class  = "player",
1377     screen_priority = 30
1378 }
1379
1380 application {
1381     appid           = "net.zmap.navi",
1382     area            = "Center.Full",
1383     privileges      = { screen = "none", audio = "none" },
1384     resource_class  = "navigator",
1385     screen_priority = 30
1386 }
1387
1388 application {
1389     appid           = "GV3ySIINq7.GhostCluster",
1390     area            = "Mid.Full",
1391     privileges      = { screen = "none", audio = "none" },
1392     resource_class  = "system",
1393     screen_priority = 30
1394 }
1395
1396 if sc then
1397     sc.client_handler = function (self, cid, msg)
1398         local command = msg.command
1399         if verbose > 0 then
1400             print('### ==> client handler:')
1401             if verbose > 1 then
1402                 print(msg)
1403             end
1404         end
1405
1406         -- known commands: 1 for SEND_APPID, synthetic command 0xFFFF for
1407         -- disconnection
1408
1409         if command == 0xFFFF then
1410             if verbose > 1 then
1411                 print('client ' .. cid .. ' disconnected')
1412             end
1413             if msg.appid == homescreen then
1414                 connected = false
1415                 homescreen = ""
1416             end
1417             return
1418         end
1419
1420         -- handle the connection
1421
1422         if not connected then
1423             print('Setting homescreen='..msg.appid)
1424             homescreen = msg.appid
1425             print('Trying to connect to wayland...')
1426             connected = wmgr:connect()
1427         end
1428         if connected and command then
1429             if command == 1 then -- send_appid
1430                 local driving_mode = mdb.select.select_driving_mode.single_value
1431                 local night_mode = mdb.select.select_night_mode.single_value
1432
1433                 if not driving_mode then driving_mode = 0 end
1434                 if not night_mode then night_mode = 0 end
1435
1436                 local reply = m:JSON({ command = 0x60001,
1437                                        arg     = m:JSON({ stateid = 1,
1438                                                           state   = driving_mode})
1439                                      })
1440                  if verbose > 0 then
1441                      print("### <== sending " ..
1442                            command_name(command) .. " message")
1443                      if verbose > 1 then
1444                          print(reply)
1445                      end
1446                  end
1447                  sc:send_message(homescreen, reply)
1448
1449                  reply = m:JSON({ command = 0x60001,
1450                                   arg     = m:JSON({ stateid = 2,
1451                                                      state   = night_mode})
1452                                 })
1453                  if verbose > 0 then
1454                      print("### <== sending " ..
1455                            command_name(command) .. " message")
1456                      if verbose > 1 then
1457                          print(reply)
1458                      end
1459                  end
1460                  sc:send_message(homescreen, reply)
1461             end
1462         end
1463     end
1464
1465     sc.generic_handler = function (self, cid, msg)
1466         if verbose > 0 then
1467             print('### ==> generic handler:')
1468             if verbose > 1 then
1469                print(msg)
1470             end
1471         end
1472     end
1473
1474     sc.window_handler = function (self, cid, msg)
1475         if verbose > 0 then
1476             print('### ==> received ' ..
1477                    command_name(msg.command) .. ' message')
1478             if verbose > 1 then
1479                 print(tostring(msg))
1480             end
1481         end
1482
1483         local a = animation({})
1484         local nores = false
1485         if msg.command == 0x10003 then       -- ico SHOW command
1486             local raise_mask = 0x01000000
1487             local lower_mask = 0x02000000
1488             local nores_mask = 0x40000000
1489             local time_mask  = 0x00ffffff
1490             msg.arg.visible = 1
1491             if msg.arg then
1492                 local time = 200
1493                 if  msg.arg.anim_time then
1494                     local t = msg.arg.anim_time
1495                     time = m:AND(t, time_mask)
1496                     nores = not m:AND(t, nores_mask)
1497                     if m:AND(t, raise_mask) then
1498                         msg.arg.raise = 1
1499                     elseif m:AND(t, lower_mask) then
1500                         msg.arg.raise = 0
1501                     end
1502                 end
1503                 if msg.arg.anim_name then
1504                     a.show = { msg.arg.anim_name, time }
1505                     print('time: ' .. tostring(a.show[2]))
1506                 end
1507             end
1508             if not nores then
1509                 local p = wmgr:application(msg.appid)
1510                 local s = p.privileges.screen
1511                 if s == "system" then
1512                     nores = true
1513                     if not msg.arg.raise then
1514                         msg.arg.raise = 1
1515                     end
1516                 end
1517             end
1518             if verbose > 2 then
1519                 print('### ==> SHOW')
1520                 print(tostring(msg.arg))
1521             end
1522             if nores then
1523                 wmgr:window_request(msg.arg, a, 0)
1524             else
1525                 local surface = msg.arg.surface
1526                 resclnt:resource_set_acquire("screen", surface)
1527                 resmgr:window_raise(msg.appid, surface, 1)
1528             end
1529         elseif msg.command == 0x10004 then   -- ico HIDE command
1530             local raise_mask = 0x01000000
1531             local lower_mask = 0x02000000
1532             local nores_mask = 0x40000000
1533             local time_mask  = 0x00ffffff
1534             msg.arg.visible = 0
1535             if msg.arg then
1536                 local time = 200
1537                 if msg.arg.anim_time then
1538                     local t = msg.arg.anim_time
1539                     time = m:AND(t, time_mask)
1540                     nores = not m:AND(t, nores_mask)
1541                 end
1542                 if msg.arg.anim_name then
1543                     a.hide = { msg.arg.anim_name, time }
1544                     print('hide animation time: ' .. tostring(a.hide[2]))
1545                 end
1546             end
1547             if not nores then
1548                 local p = wmgr:application(msg.appid)
1549                 local s = p.privileges.screen
1550                 if s == "system" then
1551                     nores = true
1552                     msg.arg.raise = 0
1553                 end
1554             end
1555             if verbose > 2 then
1556                 print('### ==> HIDE REQUEST')
1557                 print(tostring(msg.arg))
1558             end
1559             if nores then
1560                 wmgr:window_request(msg.arg, a, 0)
1561             else
1562                 resmgr:window_raise(msg.appid, msg.arg.surface, -1)
1563             end
1564         elseif msg.command == 0x10005 then   -- ico MOVE
1565             if verbose > 2 then
1566                 print('### ==> MOVE REQUEST')
1567                 print(tostring(msg.arg))
1568             end
1569             if msg.arg.zone then
1570                 msg.arg.area = msg.arg.zone
1571             end
1572             wmgr:window_request(msg.arg, a, 0)
1573             -- TODO: handle if area changed
1574         elseif msg.command == 0x10006 then   -- ico ACTIVE
1575             if not msg.arg.active then
1576                 msg.arg.active = 3 -- pointer + keyboard
1577             end
1578             if verbose > 2 then
1579                 print('### ==> ACTIVE REQUEST')
1580                 print(tostring(msg.arg))
1581             end
1582             wmgr:window_request(msg.arg, a, 0)
1583         elseif msg.command == 0x10007 then   -- ico CHANGE_LAYER
1584             if verbose > 2 then
1585                 print('### ==> CHANGE_LAYER REQUEST')
1586                 print(tostring(msg.arg))
1587             end
1588             --[[
1589             if msg.arg.layer ~= 4 or msg.arg.layer ~= 5 then
1590                 print("do not change layer for other than cursor or touch")
1591                 return
1592             end
1593             --]]
1594             wmgr:window_request(msg.arg, a, 0)
1595         elseif msg.command == 0x10011 then   -- ico MAP_THUMB
1596             local framerate = msg.arg.framerate
1597             if not framerate or framerate < 0 then
1598                 framerate = 0
1599             end
1600             msg.arg.map = 1
1601             if verbose > 2 then
1602                 print('### ==> MAP_THUMB REQUEST')
1603                 print(msg.arg)
1604                 print('framerate: '..framerate)
1605             end
1606             wmgr:window_request(msg.arg, a, framerate)
1607         elseif msg.command == 0x10012 then   -- ico UNMAP_THUMB
1608             msg.arg.map = 0
1609             if verbose > 2 then
1610                 print('### ==> UNMAP_THUMB REQUEST')
1611                 print(msg.arg)
1612             end
1613             wmgr:window_request(msg.arg, a, 0)
1614         elseif msg.command == 0x10020 then   -- ico SHOW_LAYER command
1615             msg.arg.visible = 1
1616             if verbose > 2 then
1617                 print('### ==> SHOW_LAYER REQUEST')
1618                 print(msg.arg)
1619             end
1620             wmgr:layer_request(msg.arg)
1621         elseif msg.command == 0x10021 then   -- ico HIDE_LAYER command
1622             msg.arg.visible = 0
1623             if verbose > 2 then
1624                 print('### ==> HIDE_LAYER REQUEST')
1625                 print(msg.arg)
1626             end
1627             wmgr:layer_request(msg.arg)
1628         end
1629     end
1630
1631     sc.input_handler = function (self, cid, msg)
1632         if verbose > 0 then
1633             print('### ==> input handler: ' .. command_name(msg.command))
1634             if verbose > 1 then
1635                 print(msg)
1636             end
1637         end
1638         if msg.command == 0x20001 then -- add_input
1639             msg.arg.appid = msg.appid
1640             if verbose > 2 then
1641                 print('### ==> ADD_INPUT REQUEST')
1642                 print(tostring(msg.arg))
1643             end
1644             imgr:input_request(msg.arg)
1645         elseif msg.command == 0x20002 then -- del_input
1646             msg.arg.appid = ''
1647             if verbose > 2 then
1648                 print('### ==> DEL_INPUT REQUEST')
1649                 print(tostring(msg.arg))
1650             end
1651             imgr:input_request(msg.arg)
1652         elseif msg.command == 0x20003 then -- send_input
1653         end
1654     end
1655
1656     sc.user_handler = function (self, cid, msg)
1657         if verbose > 0 then
1658             print('### ==> user handler: ' .. command_name(msg.command))
1659             if verbose > 1 then
1660                 print(msg)
1661             end
1662         end
1663
1664         if not um then
1665             print("User Manager not initialized")
1666             return
1667         end
1668
1669         if msg.command == 0x00030001 then -- MSG_CMD_CHANGE_USER
1670             print("command CHANGE_USER")
1671             if not msg.arg then
1672                 print("invalid message")
1673                 return
1674             end
1675
1676             username = msg.arg.user
1677             passwd = msg.arg.pass
1678
1679             if not username then
1680                 username = ""
1681             end
1682
1683             if not passwd then
1684                 passwd = ""
1685             end
1686
1687             success = um:changeUser(username, passwd)
1688
1689             if not success then
1690                 reply = m.JSON({
1691                     appid = msg.appid,
1692                     command = cmd
1693                 })
1694                 if sc:send_message(msg.appid, reply) then
1695                     print('*** sent authentication failed message')
1696                 else
1697                     print('*** failed to send authentication failed message')
1698                 end
1699             end
1700
1701         elseif msg.command == 0x00030002 then -- MSG_CMD_GET_USERLIST
1702             print("command GET_USERLIST")
1703             if not msg.appid then
1704                 print("invalid message")
1705                 return
1706             end
1707
1708             users, currentUser = um:getUserList()
1709
1710             if not users then
1711                 print("failed to get user list")
1712                 return
1713             end
1714
1715             nUsers = 0
1716
1717             for i,v in pairs(users) do
1718                 nUsers = nUsers + 1
1719             end
1720
1721             if not currentUser then
1722                 currentUser = ""
1723             end
1724
1725             if verbose > 1 then
1726                 print("current user: " .. currentUser)
1727                 print("user list:")
1728                 for i,v in pairs(users) do
1729                     print(v)
1730                 end
1731             end
1732
1733             reply = m.JSON({
1734                 appid = msg.appid,
1735                 command = cmd,
1736                 arg = m.JSON({
1737                     user_num = nUsers,
1738                     user_list = users,
1739                     user_login = currentUser
1740                 })
1741             })
1742
1743             if verbose > 1 then
1744                 print("### <== GetUserList reply: " .. tostring(reply))
1745             end
1746
1747             if sc:send_message(msg.appid, reply) then
1748                 print('*** reply OK')
1749             else
1750                 print('*** reply FAILED')
1751             end
1752
1753         elseif msg.command == 0x00030003 then -- MSG_CMD_GET_LASTINFO
1754             print("command GET_LASTINFO")
1755             if not msg.appid then
1756                 print("invalid message")
1757                 return
1758             end
1759
1760             lastInfo = um:getLastInfo(msg.appid)
1761
1762             if not lastInfo then
1763                 print("failed to get last info for app" .. msg.appid)
1764                 return
1765             end
1766
1767             reply = m.JSON({
1768                 appid = msg.appid,
1769                 command = cmd,
1770                 arg = m.JSON({
1771                     lastinfo = lastinfo
1772                 })
1773             })
1774
1775             if sc:send_message(msg.appid, reply) then
1776                 print('*** reply OK')
1777             else
1778                 print('*** reply FAILED')
1779             end
1780
1781         elseif msg.command == 0x00030004 then -- MSG_CMD_SET_LASTINFO
1782             print("command SET_LASTINFO")
1783             if not msg.arg or not msg.appid then
1784                 print("invalid message")
1785                 return
1786             end
1787
1788             lastInfo = um:setLastInfo(msg.appid, msg.arg.lastinfo)
1789         end
1790     end
1791
1792     sc.resource_handler = function (self, cid, msg)
1793         if verbose > 0 then
1794             print('### ==> resource handler: ' .. command_name(msg.command))
1795             if verbose > 1 then
1796                 print(msg)
1797             end
1798         end
1799
1800         createResourceSet = function (ctl, client, msg)
1801             cb = function(rset, data)
1802                 print("> resource callback")
1803
1804                 -- type is either basic (0) or interrupt (1)
1805                 requestType = 0
1806                 if msg.res.type then
1807                     requestType = msg.res.type
1808                 end
1809
1810                 if rset.acquired then
1811                     cmd = 0x00040001 -- acquire
1812                 else
1813                     cmd = 0x00040002 -- release
1814                 end
1815
1816                 reply = m.JSON({
1817                         appid = data.client,
1818                         command = cmd,
1819                         res = {
1820                             type = requestType
1821                         }
1822                     })
1823
1824                 if rset.resources.audio_playback then
1825                     reply.res.sound = {
1826                         zone = "driver",
1827                         name = msg.appid,
1828                         adjust = 0,
1829                         -- id = "0"
1830                     }
1831                 end
1832
1833                 if rset.resources.display then
1834                     reply.res.window = {
1835                         zone = "driver",
1836                         name = msg.appid,
1837                         -- id = "0"
1838                     }
1839                 end
1840
1841                 if rset.resources.input then
1842                     reply.res.input = {
1843                         name = msg.appid,
1844                         event = 0
1845                     }
1846                 end
1847                 print("sending message to client: " .. data.client)
1848
1849                 if sc:send_message(data.client, reply) then
1850                     print('*** reply OK')
1851                 else
1852                     print('*** reply FAILED')
1853                 end
1854             end
1855
1856             rset = m:ResourceSet({
1857                     application_class = "player",
1858                     zone = "driver", -- msg.zone ("full")
1859                     callback = cb
1860                 })
1861
1862             rset.data = {
1863                 cid = cid,
1864                 ctl = ctl
1865             }
1866
1867             if msg.res.sound then
1868                 rset:addResource({
1869                         resource_name = "audio_playback"
1870                     })
1871                 rset.resources.audio_playback.attributes.pid = tostring(msg.pid)
1872                 rset.resources.audio_playback.attributes.appid = msg.appid
1873                 print("sound name: " .. msg.res.sound.name)
1874                 print("sound zone:" .. msg.res.sound.zone)
1875                 print("sound adjust: " .. tostring(msg.res.sound.adjust))
1876                 if msg.res.sound.id then
1877                     print("sound id: " .. msg.res.sound.id)
1878                 end
1879             end
1880
1881             if msg.res.input then
1882                 rset:addResource({
1883                         resource_name = "input"
1884                     })
1885                 rset.resources.input.attributes.pid = tostring(msg.pid)
1886                 rset.resources.input.attributes.appid = msg.appid
1887                 print("input name: " .. msg.res.sound.name)
1888                 print("input event:" .. tostring(msg.res.input.event))
1889             end
1890
1891             if msg.res.window then
1892                 rset:addResource({
1893                         resource_name = "display"
1894                     })
1895                 rset.resources.display.attributes.pid = tostring(msg.pid)
1896                 rset.resources.display.attributes.appid = msg.appid
1897                 print("display name: " .. msg.res.display.name)
1898                 print("display zone:" .. msg.res.display.zone)
1899                 if msg.res.display.id then
1900                     print("display id: " .. msg.res.display.id)
1901                 end
1902             end
1903
1904             return rset
1905         end
1906
1907         -- parse the message
1908
1909         -- fields common to all messages:
1910         --      msg.command
1911         --      msg.appid
1912         --      msg.pid
1913
1914         if msg.command == 0x00040011 then -- MSG_CMD_CREATE_RES
1915             print("command CREATE")
1916
1917             if not sets.cid then
1918                 sets.cid = createResourceSet(self, cid, msg)
1919             end
1920
1921         elseif msg.command == 0x00040012 then -- MSG_CMD_DESTORY_RES
1922             print("command DESTROY")
1923
1924             if sets.cid then
1925                 sets.cid:release()
1926             end
1927
1928             sets.cid = nil -- garbage collecting
1929
1930         elseif msg.command == 0x00040001 then -- MSG_CMD_ACQUIRE_RES
1931             print("command ACQUIRE")
1932
1933             if not sets.cid then
1934                 sets.cid = createResourceSet(self, cid, msg)
1935             end
1936
1937             sets.cid:acquire()
1938
1939         elseif msg.command == 0x00040002 then -- MSG_CMD_RELEASE_RES
1940             print("command RELEASE")
1941
1942             if sets.cid then
1943                 sets.cid:release()
1944             end
1945
1946         elseif msg.command == 0x00040003 then -- MSG_CMD_DEPRIVE_RES
1947             print("command DEPRIVE")
1948
1949         elseif msg.command == 0x00040004 then -- MSG_CMD_WAITING_RES
1950             print("command WAITING")
1951
1952         elseif msg.command == 0x00040005 then -- MSG_CMD_REVERT_RES
1953             print("command REVERT")
1954         end
1955     end
1956
1957     sc.inputdev_handler = function (self, cid, msg)
1958         if verbose > 0 then
1959             print('*** inputdev handler: ' .. command_name(msg.command))
1960             if verbose > 1 then
1961                 print(msg)
1962             end
1963         end
1964     end
1965 end
1966
1967 -- we should have 'audio_playback' defined by now
1968 m:try_load_plugin('telephony')